How do I get the label added_at in a PR for a repo? - github

I am trying to derive some metrics from the github, for which I have created few labels in my repositories and would want to track when that is added to the PR.
I am using the below API to get the PR details but I am able to see what are all the labels have been added to the PR but unable to get the time stamp of it , How do I get the label added_at time from the github APIs ?
API route - https://api.github.com/repos/<org>/<repo>/pulls?state=closed&page=%s&per_page=100
Label section of the API reponse :
{
"id":1234,
"node_id":"1234",
"url":"https://api.github.com/repos/<org>/<repo>/labels/enhancement",
"name":"enhancement",
"color":"a2eeef",
"default":true,
"description":"New feature or request"
}

Related

Get PR number from github templates

I create a new environtment for my backend for each commit, this environment have the PR number on the URL.
So I was hopping to create a template so that everytime someone creates a PR the text contains the URL for that enviroment, something like:
Im expecting to have a template pull_request_template.md like:
# How to test
Url: http://myurl-<PR-number>.myhost.com
And so everytime the PR
# How to test
Url: http://myurl-123.myhost.com
Where 123 is the PR number
As illustrated in here and in this discussion, you can have and use multiple template files (adding ?template=MY_PR_TEMPLATE.md), but not one file with variable content.
In your case, that wouldn't work.
What might work is a Pull Request Action, which would add a label and/or message, with said message being the URL computed automatically from the PR number.
That GitHub Action would help updating any PR with an additional information: the URL you want.

Get if pull request passed all required status checks using GitHub API

I need to check via GitHub API if a pull request passed all required status checks. I use GitHub Enterprise 2.8 at this moment.
I know that I can get all status checks for last commit (following statuses_url in pull request). However, I don't know which status checks are set up to be required in a given repository. This is my main problem.
I also need to aggregate these status checks, group them by context and take latest in each context. It's ok, but seems to be reimplementation of logic, that GitHub performs internally when decides if a pull request can be merged.
For my case, it would be ideal to have something like can_be_merged in pull request fields, which meaning is mergeable && all required status checks passed && approved, but as far as I know there is no such field.
Finally solved this! You actually need to get the information off the protected branch, not off the check itself. Here are some API details: https://developer.github.com/v3/repos/branches/#list-required-status-checks-contexts-of-protected-branch.
So the flow to solve this is:
Check if the base branch for the PR is protected, and if so;
Use the above endpoint to determine which checks are required;
Compare the checks on the latest PR commit to the required checks determined in step 2.
Based on #moustachio's answer, if you are using v4 graphql API, you may use
pullRequest(...) {
baseRef {
refUpdateRule {
requiredStatusCheckContexts
}
}
}
to get the same info without additional API request

Jenkins pipeline read commit status from webhook

I'm trying to find a way to have a Jenkins job be triggered by a PR comment matching a particular pattern, have the job check to see if the PR's latest commit status is PASSED, which would be set by a separate CI job that runs the moment the PR opens, and merge the code. The PR's comment would symbolize that the code has been properly peer reviewed, the commit status represents that all front and back end unit tests have already passed.
The only part of this that I'm really unsure about is retrieving the commit status from the webhook payload. Any advice?
TLDR
Call the statuses API to list all statuses created against that SHA or use the combined status API.
Finding the latest commit
If you don't have the SHA of the latest commit, then there are a few ways to get this from the API endpoints.
This will assume there is an application that listens for webhook events, specifically the IssuesCommentEvent, to parse the comment's body for the 'particular pattern'.
Get the URL to the Pull Request
A webhook event will contain the Pull Request url in the JSON body, e.g.:
{
"action": "created",
"issue": {
...
"pull_request": {
"url": "https://api.github.com/repos/sample/mysample/pulls/13",
...
}
}
}
}
Get the SHA or statuses endpoint
Use the pull request API endpoing url returned in the previous step. The JSON body will contain a statuses_url value, which will return each status created against that SHA or, or get the SHA and call any of the previously mentioned statuses endpoints.
{
"statuses_url": "https://api.github.com/repos/sample/mysamples/statuses/1985617647f17fe4fc85efeeaffef24581a12488",
...
"head":{
"sha": "1985617647f17fe4fc85efeeaffef24581a12488",
}
}

Is it possible to get all the pull requests made by a user on different repos using some API?

I am making an application where I need to get all the pull requests made by a particular user on various repositories.
I can get all the pull request on a particular repository but found no suitable API to get all the pull request by a user.
What kind of API call can I make to get those PR filtered by author?
The List Pull Request API has a head filter:
head string
Filter pulls by head user and branch name in the format of user:ref-name.
Example: github:new-script-format.
That wouldn't work in your case, as you don't know the branch names, only the author.
Use instead the search API for issues
GET /search/issues
It can filter by:
type: With this qualifier you can restrict the search to issues (issue) or pull request (pr) only.
author: Finds issues or pull requests created by a certain user.
Try using the following code
var d=2018-09-30T10%3A00%3A00%2B00%3A00 //start date
var f=2018-11-01T12%3A00%3A00%2B00%3A00 //end date
var t=iamshouvikmitra //username
$.getJSON("https://api.github.com/search/issues?q=-label:invalid+created:" + d + ".." + f + "+type:pr+is:public+author:" + t + "&per_page=300", function(e) {
}
Infact this is similar to the code that https://hacktoberfest.digitalocean.com Uses to count the number of pull request you have submitted during the month of october.

How can I get a list of all pull requests for a repo through the github API?

I want to obtain a list of all pull requests on a repo through the github API.
I've followed the instructions at http://developer.github.com/v3/pulls/ but when I query /repos/:owner/:repo/pulls it's consistently returning fewer pull requests than displayed on the website.
For example, when I query the torvalds/linux repo I get 9 open pull requests (there are 14 on the website). If I add ?state=closed I get a different set of 11 closed pull requests (the website shows around 20).
Does anyone know where this discrepancy arises, and if there's any way to get a complete list of pull requests for a repo through the API?
You can get all pull requests (closed, opened, merged) through the variable state.
Just set state=all in the GET query, like this->
https://api.github.com/repos/:owner/:repo/pulls?state=all
For more info: check the Parameters table at https://developer.github.com/v3/pulls/#list-pull-requests
Edit: As per Tomáš Votruba's comment:
the default value for, "per_page=30". The maximum is per_page=100. To get more than 100 results, you need to call it multiple itmes: "&page=1", "&page=2"...
PyGithub (https://github.com/PyGithub/PyGithub), a Python library to access the GitHub API v3, enables you to get paginated resources.
For example,
g = Github(login_or_token=$YOUR_TOKEN, per_page=100)
r = g.get_repo($REPO_NUMBER)
for pull in r.get_pulls('all'):
# You can access pulls
See the documentation (http://pygithub.readthedocs.io/en/latest/index.html).
With Github's new official CLI (command line interface):
gh pr list --repo OWNER/REPO
which would produce something like:
Showing 2 of 2 pull requests in OWNER/REPO
#62 Doing something that-weird-branch-name
#58 My PR title wasnt-inspired-branch
See additional details and options and installation instructions.
There is a way to get a complete list and you're doing it. What are you using to communicate with the API? I suspect you may not be doing something correctly. For example (there are only 13 open pull requests currently) using my API wrapper (github3.py) I get all of the open pull requests. An example of how to do it without my wrapper in python is:
import requests
r = requests.get('https://api.github.com/repos/torvalds/linux/pulls')
len(r.json()) == 13
and I can also get that result (vaguely) in cURL by counting the results myself: curl https://api.github.com/repos/torvalds/linux/pulls.
If you, however, run into a repository with more than 25 (or 30) pull requests that's an entirely different issue but most certainly it is not what you're encountering now.
If you want to retrieve all pull requests (commits, comments, issues etc) you have to use pagination.
https://developer.github.com/v3/#pagination
The GET request "pulls" will only return open pull-requests.
If you want to get all pull-requests either you do set the parameter state to all, or you use issues.
Extra information
If you need other data from Github, such as issues, then you can identify pull-requests from issues, and you can then retrieve each pull-request no matter if it is closed or open. It will also give you a couple of more attributes (mergeable, merged, merge-commit-sha, nr of commits etc)
If an issue is a pull-request, then it will contain that attribute. Otherwise, it is just an issue.
From the API: https://developer.github.com/v3/pulls/#labels-assignees-and-milestones
"Every pull request is an issue, but not every issue is a pull request. For this reason, “shared” actions for both features, like manipulating assignees, labels and milestones, are provided within the Issues API."
Edit I just found that issues behaves similar to pull-requests, so one would need to do retrieve all by setting the state parameter to all
You can also use GraphQL API v4 to request all pull requests for a repo. It requests all the pull requests by default if you don't specify the states field :
{
repository(name: "material-ui", owner: "mui-org") {
pullRequests(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) {
totalCount
nodes {
title
state
author {
login
}
createdAt
}
}
}
}
Try it in the explorer
The search API shoul help: https://help.github.com/enterprise/2.2/user/articles/searching-issues/
q = repo:org/name is:pr ...
GitHub provides a "Link" header which specifies the previous, next and last URL to fetch the values.Eg, Link Header response,
<https://api.github.com/repos/:owner/:repo/pulls?state=all&page=2>; rel="next", <https://api.github.com/repos/:owner/:repo/pulls?state=all&page=15>; rel="last"
rel="next" suggests the next set of values.
Here's a snippet of Python code that retrieves information of all pull requests from a specific GitHub repository and parses it into a nice DataFrame:
import pandas as pd
organization = 'pvlib'
repository = 'pvlib-python'
state = 'all' # other options include 'closed' or 'open'
page = 1 # initialize page number to 1 (first page)
dfs = [] # create empty list to hold individual dataframes
# Note it is necessary to loop as each request retrieves maximum 30 entries
while True:
url = f"https://api.github.com/repos/{organization}/{repository}/pulls?" \
f"state={state}&page={page}"
dfi = pd.read_json(url)
if dfi.empty:
break
dfs.append(dfi) # add dataframe to list of dataframes
page += 1 # Advance onto the next page
df = pd.concat(dfs, axis='rows', ignore_index=True)
# Create a new column with usernames
df['username'] = pd.json_normalize(df['user'])['login']