Get the pull request that introduced a commit - github

In github, when I click on a commit, I can see the pull request that introduced it.
You can see this commit was introduced by PR 15.
How can I get the associated pull request for a particular commit from the Github API?

the #15 indicates the issue/PR number. If you have a commit sha it is possible to find the associated PR# using the GitHub search API (v3).
For example, suppose you have a commit sha - 7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c. To find the PR# from this sha, your query using the GitHub APi can look something like this- https://api.github.com/search/issues?q=sha:7dd1bcf5f2f5eeed34cc2ec63053098fba302b6c. From JSON response, the field 'number' represents the PR# (in this case 16).
Ref. Search Issues

Related

Is there a way to filter pull requests where my review is stale/dismissed?

On GitHub, I would like to be able to have a list of Pull Requests that require my attention. This must include any pull request that is created by a member of my team that was not authored by me, but requires a review from me. So far, I have this:
is:pr is:open user:<ORGANIZATION> involves:coworker involves:coworker -author:#me -reviewed-by:#me
The problem is, when my review is dismissed or becomes stale, -reviewed-by:#me filters out that pull request because I've previously reviewed it, even though that review has been dismissed. If I've already approved it or if the pull request has changes requested by me that are not outdated, I do not want to see them in the list.
Is there an online query to be able to list these pull requests from github.com/pulls?
Taking inspiration of this use-case which illustrate the power of GitHub CLI gh, you could, for each of the currently listed PR, do a check (using the "List reviews for a pull request" API):
gh api repos/<your repo name>/pulls/<PR ID>/reviews --jq '.[] | [.id,.state] | join("=")' | grep -Po '\d+(?=\=DISMISSED)'
You can add the .user.login attribute to filter on your name and check if you have, for a given PR, reviews in "DISMISSED" state.
If yes, you know you can filter you that PR from your intial query.
This is not straightforward, as some processing is needed, but it can help.

How to format request for github api

I'm having trouble formatting my request for the github api.
I have tried looking at the documentation and doing:
https://api.github.com/repos/facebook/react/commits/master?since=2019-01-01&until=2019-12-30
But this only returns one result instead of all commits for the year. I am using the requests library in python, and have also tried it natively in the browser.
Any help appreciated, Thanks.
You are using the Single Commit API with GET /repos/:owner/:repo/commits/:ref
You need List commits API : GET /repos/:owner/:repo/commits :
https://api.github.com/repos/facebook/react/commits?since=2019-01-01&until=2019-12-30&sha=master
You can specify the branch with sha param. If you want the default branch, you can omit this field:
https://api.github.com/repos/facebook/react/commits?since=2019-01-01&until=2019-12-30

Are we able to use GitHub API to create a commit? especially v4?

Are we able to use GitHub API to create a commit?
Can we use API to upload a file/blob, like what we can do using Git command,
especially in API v4?
Presumably yes, using the GitHub Commits API (in the context of a GitHub application)
You can see the all sequence in "GitHub API Git Data":
Get the current commit object
Retrieve the tree it points to
Retrieve the content of the blob object that tree has for that particular file path
Change the content somehow and post a new blob object with that new content, getting a blob SHA back
Post a new tree object with that file path pointer replaced with your new blob SHA getting a tree SHA back
Create a new commit object with the current commit SHA as the parent and the new tree SHA, getting a commit SHA back
Update the reference of your branch to point to the new commit SHA
Each of those steps has its own GitHub API query.

How to get the associated changes, check-in comments and linked work items for a particular build/release in VSTS

I am using SendGrid email extension to trigger a custom email notification after the CI/CD process is complete in VSTS. This email task has HTML content in it which includes some content fetched using standard build/release variables (https://learn.microsoft.com/en-us/vsts/build-release/concepts/definitions/build/variables?tabs=batch).
How do I include associated code changes, check-in comments and linked work items for a particular release in the custom email? Are there any variables I can use? Any work around?
There aren’t the built-in variables that can get code changes, check-in comments and linked work items.
You can get them through REST API during build or release:
Get build changes and work items through Get Build Changes REST API (Build id variable: Build.BuildId during build or Release.Artifacts.{Artifact alias}.BuildId during release) Note: using Build.SourceVersion to get latest version)
Git: Get commit message through Get a batch of commits by a list of commit IDs REST API
TFVC: Get check-in comment through Get list of changesets by a list of IDS REST API
To get changed items, you can use get commit with changed items or Get list of changes in a changeset REST API.
No API to get detail code changes, but you may refer to this related issue: Lines of Code modified in each Commit in TFS rest api. How do i get?
Assuming TFS (which isn't specified in the question or tags), you could also call tf.exe directly to get some of that info. If you don't care about the output format, then the output of the following command produces a report of the Changeset details.
tf vc changeset <changeset id> /loginType:OAuth /login:.,<token> /noprompt
Where <changeset id> is the numeric Build.SourceVersion, and <token> is the System.AccessToken.
Comments and source code edits listing are included in the report.
Note: the agent job has to be given the "Allow scripts to access the OAuth token" permission (check box on the Agent Job properties).
See the updated link below for details on how to access the build variables. Same content as in the question, but new link. Both currently work.
https://learn.microsoft.com/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

Github : Automated tagging on Merging of Pull Requests

Is there a way to trigger an automatic, custom tagging of a repository every time a pull request is merged in github ? for example...
After merging "pull request 8", id like to automatically tag the repository at that state as "$Major_Version.$pr_id".
The result might be "0.1.8".
Is this possible?
Worst case, if someone hasn't written a thing to do that yet, is to use the post-receive webhooks https://help.github.com/articles/post-receive-hooks
You'd need an internet-visible web server to do this, and some code to recognize the merge (it will have two parents) and see if it mentions a pull-request.