Obtain TFS GIT Commit Details From TFS Work Item Artifact Link - azure-devops

Is it possible to leverage TFS or TS REST api to obtain details for a GIT commit by leveraging the work item commit "ArtifiactLink" url?

So you want to get detail commit information based on a work item artifacts link (while the artifact link type contains commit).
You can achieve that with two REST API, detail steps as below:
1. Get the work item with full expanded
GET https://{instance}/DefaultCollection/_apis/wit/workitems/{id}?api-version1.0&$expand=all
For TFS2015, the format looks like:
GET http://tfsServer:8080/tfs/DefaultCollection/_apis/wit/workitems?ids={id}&$expand=all&api-version=1.0
For VSTS, the format looks like:
GET https://account.visualstudio.com/DefaultCollection/_apis/wit/workitems?ids=7&$expand=all&api-version=1.0
2. Get commit(s) and related repo(s) linked in the above work item
Search in the response of the step1 REST API, get the part which rel is ArtifactLink and the url start with vstfs:///Git/Commit. The URL format is
vstfs:///Git/Commit/{project ID}%2F{repo ID}%2F{commit ID}
Such as part of the REST API response as:
{
"rel": "ArtifactLink",
"url": "vstfs:///Git/Commit/b959f22b-eeb7-40dc-b37e-986377eaa86f%2F4cfde261-fec3-451c-9d41-a400ba816110%2Fb3c3c5b8718f403402be770cb3b5912df7c64dd6",
"attributes": {
"authorizedDate": "2017-09-26T03:14:03.98Z",
"id": 92,
"resourceCreatedDate": "2017-09-26T03:14:03.98Z",
"resourceModifiedDate": "2017-09-26T03:14:03.98Z",
"revisedDate": "9999-01-01T00:00:00Z",
"name": "Fixed in Commit"
}
}
The project ID is b959f22b-eeb7-40dc-b37e-986377eaa86f, the repo ID is 2F4cfde261-fec3-451c-9d41-a400ba816110 and the commit ID is b3c3c5b8718f403402be770cb3b5912df7c64dd6.
3. Get commit(s) details
Use the project ID, repo ID and commit ID you get in step2 to get a single commit:
GET https://{instance}/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version={version}
For TFS 2015, the format looks like:
GET http://tfsServer:8080/tfs/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version=1.0
For VSTS, the format looks like:
GET https://account.visualstudio.com/DefaultCollection/{project ID}/_apis/git/repositories/{repo ID}/commits/{commit ID}?api-version=1.0

Related

Azure DevOps REST Api Tags call (Repos > Tags)

Is there a Repos Tags REST Api call to get the data from this page? (Repos > Tags)
It would be awesome if it also includes CREATE, PATCH and DELETE.
The tab it self use https://dev.azure.com/{organization}/_git/{repo}/tags?__rt=fps&__ver=2
which I interpret as a bad sign.
EDIT 1: Create a Tag
Create: the Create Tag button use: Annotated Tags
So what is missing in this REST Api call is a LIST to get the {objectId} of the elements.
EDIT 2: List & Delete Tags
List: To list all Tags objectId, I found out that you can use Refs - List
Delete: I think this call is complete undocumented. But you can comprehend that the TFS use the following payload to do this job:
var json = {
name: `refs/tags/${xName}`,
newObjectId: '0000000000000000000000000000000000000000',
oldObjectId: xObjectId
};
var payload = [json];
Post this payload to https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.1
EDIT 3: behavior on git client side
The only way I found to update the git tags on client side is here:
git tag -l | xargs git tag -d
git fetch --tags
1.To create Tags: Create/Get tag apis
Create tag:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/annotatedtags?api-version=6.0-preview.1
Get tag:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/annotatedtags/{objectId}?api-version=6.0-preview.1
2.To list Tags: We don't have "Annotated Tags-List", but we have Refs-tags which does similar job. (Hint from Mar Tin, thanks to him!)
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=tags/&api-version=6.0-preview.1
3.To delete the tag(Fetch it from F12):
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{RepoName}/refs?api-version=5.1
Request Body(Don't forget the [ and ],it's necessary when testing in PostMan):
[{
"name": "refs/tags/{TagName}",
"newObjectId": "0000000000000000000000000000000000000000",
"oldObjectId": "{OldObjectID}"
}]
Note: All the info about ObjectID can be fetched via Refs-tags api in tip2.
For your question about whether the tag is really deleted or is it
only moved to a sneaky archive.
You're see this when trying to delete the Tag via the Delete tag button in web portal:
And the corresponding request I fetched from Edge(F12) is:
So the tag will be permanently deleted, it's not just moved to a sneaky archive.

Copy pull request description to work item comments on Azure Devops

When PR is approved, there is message "Completing Pull Request 123 and the associated work items." added to associated work item's comments area.
Is there any way how to append PR description?
I have zapier webhook attached to comments and I wont to get PR message in another app.
When PR is approved, there is message "Completing Pull Request 123 and
the associated work items." added to associated work item's comments
area.
Based on this description, I guess you are attempting to append the PR description to work item comment while the Pull Request is completing, right?
Afraid to say that there's no such out-of-box feature can let you direct to use. But you can consider to run powershell scripts along with rest api in build pipeline to achieve such goal.
The logic of my suggestion is:
Step 1: Prepare environment.
Create one build pipeline, and make its trigger type as Continues Integration(CI). Only this, the pull request completing can trigger this pipeline processing, then do next job.
Step 2: Get the PR completing node id, then get corresponding Pull request ID by calling this PRs query api.
For the build which run by CI, there has one environment variable Build.SourceVersion can represents the merge node id which generated by Pull request complete.
POST https://dev.azure.com/{org}/{project name}/_apis/git/repositories/{repo name}/PullRequestQuery?api-version=6.0-preview.1
{
"queries": [
{
"type": 1,
"items": [
"$(Build.SourceVersion)" // Put the $(Build.SourceVersion) value here.
]
}
]
}
Then, in its response body, you will see there has one parameter pullRequestId which target to the Pull request this commit id associated with.
Step 3: Get detailed PR description and work item id by using the pull request id we get in step 2.
Get https://dev.azure.com/{org}/{project name}/_apis/git/repositories/{repo name}/pullrequests/{pull request id}?includeWorkItemRefs=true&api-version=5.1
Put the pull reqeust id we got from step 2 into this api, then you can see the description contents along with the work item id from its response body:
Step 4: Add this description contents to corresponding work item comment area.
POST https://dev.azure.com/{org}/{project name}/_apis/wit/workItems/{WorkItem Id}?api-version=5.1-preview.3
[
{
"op": "add",
"path": "/fields/System.History",
"Value": $(description) // put the description here
}
]
As I mentioned firstly, make sure this pipeline is triggered by CI. Then you will get the description contents be added into WIT comment once the Pull request is completing.

Using GitHub's API to get lines of code added/deleted per commit (on a branch)?

The following gets a raw list of commits for a project's master branch:
https://api.github.com/repos/<organization_name>/<repo_name/commits?page=0&per_page=30
Question 1: How can one get a similar list but for a specific <branchname>?
Question 2: The list of commits above doesn't include any data about the lines of code added/deleted per commit (i.e., a very rough productivity metric). Is there a way to get this data in the query?
You can fetch the specific branch with sha={branchname} param in the /commits params;
sha string SHA or branch to start listing commits from. Default: the repository’s default branch (usually master).
https://api.github.com/repos/<org_name>/<repo_name>/commits?sha=<branchName>&page=0&per_page=30
To get per-file specific changes for each commit, you'd need to check url variable for each commit entity in the response of above URL. From that new endpoint call, you will get a more detailed information of that single commit. files variable in there will contain the changes contained in that commit. Both added & removed codes per file.
An example with my repo;
https://api.github.com/repos/buraequete/orikautomation/commits?sha=master&page=0&per_page=30
If we get the first commits url;
https://api.github.com/repos/buraequete/orikautomation/commits/89792e6256dfccc5e9151d81bf04145ba02fef8f
Which contains the changes you want in files variable as a list.
"files": [
{
"sha": "8aaaa7de53bed57fc2865d2fd84897211c3e70b6",
"filename": "lombok.config",
"status": "added",
"additions": 1,
"deletions": 0,
"changes": 1,
"blob_url": "https://github.com/buraequete/orikautomation/blob/89792e6256dfccc5e9151d81bf04145ba02fef8f/lombok.config",
"raw_url": "https://github.com/buraequete/orikautomation/raw/89792e6256dfccc5e9151d81bf04145ba02fef8f/lombok.config",
"contents_url": "https://api.github.com/repos/buraequete/orikautomation/contents/lombok.config?ref=89792e6256dfccc5e9151d81bf04145ba02fef8f",
"patch": "## -0,0 +1 ##\n+lombok.accessors.chain = true"
},
...
]
Sorry but I don't think there is a way to get those per file changes in the original /commits endpoint call, you have to do multiple calls...

How to get a commit SHA from a release or tag on Github API V3

The release nor tag response don't seem to have information (SHA) about the commit they were made from. How can I get it if I only have a tag/release like v1.2.3?
There's no specific endpoint in GitHub API v3 to get the commit SHA from tag/release name.
For your use-case, you can use the List tags endpoint to get all the tags for a particular repo, iterate over the response and get the desired tag details with the commit SHA.
Endpoint: GET /repos/:owner/:repo/tags
Sample response below:
[
{
"name": "v0.1",
"commit": {
"sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc",
"url": "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"
},
"zipball_url": "https://github.com/octocat/Hello-World/zipball/v0.1",
"tarball_url": "https://github.com/octocat/Hello-World/tarball/v0.1"
}
]

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",
}
}