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

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

Related

Fetch AzureDevops build details by Pull Request

My requirement is to tell the user if his/her PR got deployed successfully or not
So I thought,
I can ask user about his PR#
Using PR# - Fetch Build Details
Using BuildID from Step 2, find deployment information
I stuck at step 2
I am using below documentation to fetch build details.
https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/list?view=azure-devops-rest-6.0
Not able to find correct usage of reasonFilter parameter to pass pull request id to fetch build details
API URL Info
Build Reson
Can someone help how exactly to use reasonFilter parameter here
tried this _apis/build/builds?api-version=5.1&$top=1&reasonFilter=pullrequest=20056
Regarding the point you're stuck with: The reasonFilter is simply a number of predefined string values you can choose from. For instance, if you specify reasonFilter=pullrequest, you get all builds started by a pull request. Thus, you can't specify the exact PR ID here.
Regarding your requirement in general: I don't think there's an API to strictly tie the PR ID and its build(s). You should look into the Status API instead. The build is just one of the statuses that can be associated with the pull request or, more specifically, with the commit.
So, I would approach your task the following way (note that it's not an exact algorithm, but a number of steps I would try to find the solution):
get the pull request by the PR ID provided by user
find the lastMergeCommit in the response
get the statuses of that merge commit
investigate the collection of GitStatus objects returned back and find out how to filter the build you're looking for (I don't know the format of that response for sure, but I'm confident there's some attribute to judge by)
Hope this can lead you to the right direction.
how exactly to use reasonFilter parameter here tried this _apis/build/builds?api-version=5.1&$top=1&reasonFilter=pullrequest=20056
Based on your requirement, you need to filter the build via build reason and Pull Request ID.
I'm afraid there is no out-of-box parameter to filter by pull request ID in the Rest API.
For a workaround, you can try to add the tag to your build.
Here are the steps:
You can add a step in your build to use Pull Request ID as Build tag. Refer to this doc: AddBuildTag: Add a tag to the build
For example:
- powershell: |
Write-Host "##vso[build.addbuildtag]$env:SYSTEM_PULLREQUEST_PULLREQUESTID"
displayName: 'PowerShell Script'
condition: eq(variables['Build.reason'], 'pullrequest')
You can set the condition to make sure that when the pipeline is triggered by pull request, it will add build tag.
You can add the tagFilters in Rest API to filter the related build.
For example:
Get https://dev.azure.com/org/project/_apis/build/builds?reasonFilter=pullrequest&tagFilters=PullrequestID&api-version=6.0

How can I modify the state a work item is changed to after PR?

When we create a pull request and link a work item, after the PR is completed the status of the work items are automatically changed to "invalid". How can we change that behavior to a different status?
In a recent update to Azure DevOps, you can now customize work item state when pull request is merged.
When you create a PR, in the description, you can set the state value
of the linked work items. You must follow the specific syntax.
{state value}: #ID When you merge the PR, the system reads through
the description and updates the work item state accordingly. In the
follow example we set work items #300 and #301 to Resolved, #323 and
#324 to Closed.
The Set work item state in pull request feature is also works for custom states. Here is my sample:
Add a custom state to task.
Create a new pull request and add the Description like:
Ready for QA: #id
Create pull request and complete the merge. Do not check "Complete linked work items after merging" option.
Now, the status of my task has been updated to Ready for QA.
In addition, the product group is still improving this feature. If you have any concern or suggestions, you can share it in this suggestion ticket and this one.

Trying to delete all records in a list based on a where clause

All of my workflows for a site have the ability to log data in the Workflow History list on my site. This is controlled at run-time with a parameter in list based on the workflow Name. I would like to be able to run a workflow on this setup table that will delete all records in the History list. My understanding is I can do this via REST call in SharePoint Designer workflow.
I have attempted many times to configure the REST Post command in Designer and I am never able to get this to work. I've searched over and over for a solution and not able to find such a solution.
I first tried deleting all that had the Workflow Association ID equal to the Workflow Name, but could not get that to work. Then I thought I would try selection each value using REST and then deleting that value using REST by the ID I received from the GET.
This is my delete (POST).
[%Workflow Context:Current Site URL%]/_api/lists/GetByTitle('Workflow History')/GetItemObject('[%Variable: HistoryID%]')
I also tried the DeleteListItem
[%Workflow Context:Current Site URL%]/_api/lists/GetByTitle('Workflow History')/DeleteListItem('[%Variable: HistoryID%]')
I want the user to be able to run this on any workflow from the setupWorkflow list I have. The workflow should delete all history for the current workflow.
We can get the list items in Workflow History list base on the WorkflowInstance, in designer workflow, we can get the Instance ID from the Workflow Context.
/_api/web/lists/getbytitle('Workflow%20History')/items?$filter=WorkflowInstance eq 'b87b131e-ce22-43f5-85be-ec81d1045bc7'
Then delete list item using REST API below.
/_api/web/lists/getbytitle('Workflow%20History')/GetItemById(ID)
The following articles for your reference.
Using HTTP Call/ Rest API from SharePoint Designer workflow to create list
workflow to delete list item on subsite( Sharepoint designer call http web service )

Azure DevOps/VSTS REST API does not get changes of a changeset

I'm trying to to get the changes of a changeset but it returns 404. I used this:
https://<myname>.visualstudio.com/<projectname>/_apis/tfvc/changesets/291/changes
changeset exists
without the '/changes' it works, returns the changeset info but I also need the merge sources
tried to specify the API version (e.g.: api-version-5.0)
I created a full control Personal Access Token for the client app but no luck. I tried to use this link in the browser and I got the same result: it works only without '/changes'.
What did I wrong?
As this is an old Q, this is for anyone else who has same problem, The projectname needs to be removed from the request.
https://<myname>.visualstudio.com/_apis/tfvc/changesets/291/changes
You look at the docs and sure enough it's not there but most other REST calls require a project name, so it can be confusing.
Also the docs are not very clear that you can interchange https://{myName}.visualstudio.com/ for the documented https://dev.azure.com/{organization}

Bitbucket web hook not posting

I have set up a web hook on Bitbucket to POST to my webserver to log some details. There is two webhooks on the repo, my custom one, and one for FTPloy.com. I have been pushing random commits to test the POST on my server etc, but after a while it stopped.
Never FTPloy.com or my own webserver receives a post request from Bitbucket after I pushed.
How is this possible/how can I fix it?
I had a hard time testing my web hook as well. I figured out that the best way to test is via command line. I copied the sample data shown here, added payload= to the beginning and saved it to a file: bb.post
Then via command line I submitted a POST request to my web hook using that file as the POST data: curl --data #bb.post http://mydomain.com/customWebHook.php
This is the best way I found to debug it.
Note: They never mention that when the last commit was from a merge, the "branch" attribute from that commit is null. Instead you need to look at "branches" attribute which is an array of the two branches that created the merge.