Copy pull request description to work item comments on Azure Devops - 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.

Related

How to get Pull Requests associated with a Work Item via the Azure DevOps API

I am trying to list all Pull Requests associated with a Work Item but according to the Work Items API there doesn't seem to be a way to get it:
GET https://dev.azure.com/{org}/{project}/_apis/build/builds/123456/workitems?api-version=6.0
The above returns list of work items, such as:
{
"count": 40,
"value": [
{
"id": "156267",
"url": "https://dev.azure.com/xxx/_apis/wit/workItems/12345"
},
...
]
}
Now, if I still decide to query each returned work item I still don't see a Pull Request.
For example:
GET https://dev.azure.com/xxx/_apis/wit/workItems/12345
The above returns a JSON object about updates done to the workitem, but this can be a commit, state update or comments.
Is there a way to get a list of PRs per work item?
Thanks
You need to add &$expand=relations:
GET https://dev.azure.com/xxx/_apis/wit/workItems/12345?$expand=relations
Now in the response you will get the linked PR under the relations.

How to trigger azure pipeline via API in a way it does not report it was manually triggered

We have an Azure pipeline building a static site. When there is a change in a content repository the site needs to be rebuilt. For that, we're using webhooks and Azure DevOps API. The request to queue the build is very simple and is illustrated for example here.
What I don't like about this is that int the build listing it says "Manually triggered for person XY", where the person XY is the one who generated the credentials used in the API request. It seems quite confusing because any API request seems strange to be labeled as "manually requested". What would be the best way how to achieve more semantically correct message?
I've found there is a reason property which can be sent in the request. But none of the values seems to represent what I want and some of them do not work (probably need additional properties and there is no documentation for that).
Based on my test, when you use the Rest API to queue a build and set the build reason, the reason could be shown in the Build(except:batchedCI and buildCompletion).
Here is the Rest API example:
Post https://dev.azure.com/Organization/Project/_apis/build/builds?api-version=4.1
Request Body:
{
"definition": {
"id": 372
},
"reason":"pullRequest"
}
The value : checkInShelveset individualCI pullRequest schedule could show their own names.
The value: manual and none could show manually trigger.
The other value(e.g. All, userCreated) will show Other Build Reason.
For the value: batchedCI and buildCompletion.
BatchedCI: Continuous integration (CI) triggered by a Git push or a TFVC check-in, and the Batch changes was selected.
This means that batch changes are required to achieve this trigger. So it doesn't support to queue build in Rest API .
buildCompletion: you could refer to this ticket This reason doesn't support in Rest API-queue Build.
Note: If you enter a custom value or misspelling, it will always display manual trigger.
In the end, I went with all value and also overriding the person via requestedFor property. This leads to the message "Other build reason", which seems usable to me.
{
"definition": {
"id": 17
},
"reason":"all",
"requestedFor": {
"id": "4f9ff423-0e0d-4bfb-9f6b-e76d2e9cd3ae"
}
}
However, I'm not sure if there aren't any unwanted consequences of this "All reasons" value.

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.

Obtain TFS GIT Commit Details From TFS Work Item Artifact Link

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

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