How to get PR title and description from agents - azure-devops

Is it possible to get PR title and PR description in pipeline?
The idea is to notify team which task has been deployed. Allfeature need to be tested go to branch "development"
I tried intergrate Ms Teams, subscribe pipeline, but none of the information is useful.
Build pipeline gave me branch "development" which is not helpful (not sure what was merged into "development") and who was requested the build
Deploy pipeline gave absolutely nothing useful.
Update:
The Picture below is result of querying Builds - Get Api. Trigger for a PR was completed. There is no infomation about the PR.

If your build is based on pull request commit , the sourceVersion will be displayed next to the source branch in the build summary page.
Click on this sourceVersion, you will jump to the detailed page containing pr title and description, in this page you can also see the merged changes.
Update:
or a way to query PR title in build or release process
How about getting pr title through rest api? We can first use Builds-Get rest api to get the pull request id in the specified build.
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}?api-version=5.1
Then through the obtained pull request id, we can use Get Pull Request rest api to get the pull request title.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.1
The above can be achieved by adding a powershell task(call rest api through script) in the build or release process.
Update2:
The difference between PR-triggered build and CI-triggered build:
If the build is triggered by pr, the trigger reason should be pullRequest. Below is my test in Postman:

Related

Linked work items not available in Pull Request CI build in Azure Devops

I have a CI build set up in Devops that runs with every Pull Request submitted for our master branch, and then also on a daily schedule whenever our master branch has been updated (after successful review).
When I look at the pipeline history, the builds that ran for PR's have no linked Work Items (even though items were linked in the Pull Request).
Only the builds that run AFTER the PR has completed and merged into master (ie the ones that run against master, not the PR branch) have the items from the Pull Request linked.
Is it possible to link work items from the PR to the PR build or at least access them from the CI pipeline?
I'd like to add a task to my CI pipeline that automatically updates all Work Items linked to a PR to "In Code Review" status when the CI build for the Pull Request starts, but the items are only available in the post-PR builds.
If you want to display the linked work items on the summary page of the build triggered by the pull request, you need to add the linked work items on the Commit page.
The linked work items added in the pull request page will not be displayed on the build summary page.
Currently, this is by design. If we want to get the work items from the pull request link to the build, we can get them through rest api, but they will not be displayed in the build summary.
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/workitems?api-version=6.0
Here is a ticket with similar issue you can refer to.

How can I access the artefact from a pull request for which a build was run?

I'm using azure for a windows app and so I don't really need to go as far as CD as that isn't really relevant. We eventually plan to move to the cloud but for now that is not the case.
I have now got my .net (c#) build running as a build pipeline and I have a develop branch which Pull Requests are used to merge in changes. What I want is for a tester to be able to pick up the build artefact that was created for a particular bug or product backlog item when the pull request was successfully completed. Is this possible without having a Release Pipeline? I don't currently have a subscription that would allow me to create a release pipeline.
How can I access the artefact from a pull request for which a build was run?
Indeed, just as Lucas said, if we are starting from the pull request to solve this problem, it is really difficult. But we could try reverse thinking to start with the build pipeline.
Azure devops provided us some predefined variables, like Build.BuildId, System.PullRequest.PullRequestId.
So, we could use the REST API Pull Requests - Update in the build pipeline to update the comment with the link to the artifact.
PATCH https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}?api-version=5.1
Since the current build is triggered by a pull request, we could use the predefined variable System.PullRequest.PullRequestId to get the pullRequestId directly.
Now, we just need to get the link of the artifact, we could use the Artifacts - Get to get the artifact info:
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=4.1
We could get the buildId by the predefined variable Build.BuildId, then we will get the download URL:
So the idea of summing up my solution is to use REST API Pull Requests - Update in the build pipeline to update the comment of the pull request, which contains the download path of the artifact.
Note: You could also add custom conditions in the REST API task in the build pipeline:
and(succeeded(), eq(variables['Build.Reason'], 'PullRequest'))
So, with this setting, only the build triggered by the pull request will execute this rest api task.
Hope this helps.
The pull request build that you defined can publish artifacts, see this documentation to see how to do it.
Once those artifacts are published, your tested can browse/download them on the build run page (click on header pa> "Related" > click on "#number of artifacts you publish# published").
Alternatively, you could add a task to copy your artifacts to an Azure Blob Storage, but that would require more configuration.
One can find build id with branchName filter :
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds?api-version=5.0&reasonFilter=pullRequest&definitions={buildDefinitionId}&branchName=refs/pull/{pullRequestId}/merge
Once builder id has been retrieved, get artifact by name :
GET https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/artifacts?artifactName={artifactName}&api-version=6.0

Release is not triggered upon creation of a pull request in Azure DevOps

I am not able to trigger a pull request release by creation of a pull request.
Please check what I am doing wrong.
First I set a Pull request trigger to Enabled for the required artifact.
Then I enable Pull request deployment for Testing stage
The latest build run is successful for the source pipeline:
But when I create a pull request (either manually or programatically as a final step in the source build pipeline), a new release is not triggered.
Thanks in advance for your help.
From your screenshot, your build is triggered manually, you need to set a branch policy for the master branch to make the build triggered by pull request.
This is because you select build as the artifact source type. If you choose azure repo as source type, you do not need to set this branch policy.
Here is a document you can refer to .

Triggering Pull Request Builds automatically on draft pull requests in Azure DevOps (ADO)

If I make a pull request in ADO from one of my branches then any commits I push to my branch automatically trigger the build pipeline:
This is configured in the build validation branch policies on master:
But for draft pull requests I need to trigger builds manually:
The documentation (here) does not mention any distinction between active and draft pull requests. How do I configure my project so that commits to branches in a draft pull requests automatically trigger a build?
According to the docs:
Draft pull requests do not include reviewers or run builds by default
but allow you to manually add reviewers and run builds. To promote the
pull request to a normal pull request, simply click the Publish button
from the pull request detail page.
So, it looks like you experience the expected behavior.

Showing Jenkins pipeline status against pull requests in GitHub

I have recently started using Jenkins Pipelines and have a multibranch job configured which is happily picking up Jenkinsfile on one of my branches.
I have seen in screenshots on posts\articles that Jenkins can report back to pull requests in GitHub the status of whether the branch has passed\failed checks performed in the pipeline.
I am not seeing any such feedback for my pull request I have opened, I can see in Jenkins it has triggered a pipeline build which has passed.
How can I get the notification to show the pipeline checks have passed in GitHub. Do I need to configure something else?
I found out this was simply down to me using the Git instead of GitHub sources option when configuring the multibranch pipeline job in Jenkins.
The docs for the GitHub Branch Source plugin gave me the clues!
I now get status messages on my pull requests showing whether pipeline checks are pending\failed\passed.
Check out the Github Pull Request Builder Plugin, it should do what you require: https://wiki.jenkins-ci.org/display/JENKINS/GitHub+pull+request+builder+plugin