How can I get repository resources associated with the given build in Azure DevOps using REST Api? - azure-devops-rest-api

Please, observe the following image:
It shows a Pull Request Build summary page. There we can see the build has two repository resources. This is because the build checks out multiple repositories. The first one is self, the other one is auxiliary.
I am trying to figure out what REST API I can use to get the information about the auxiliary repository associated with the given build. This is one of the rare cases when using the network capture proved useless to deduce the right API.
I tried the Pipeline Run API - http://server:8080/tfs/DefaultCollection/{OurProject}/_apis/pipelines/{BuildDefinitionID}/runs/{BuildID}?api-version=6.0-preview.1 - it does not return what I need.
So how do we do it?
(The build is a YAML build)
We use Azure DevOps Server 2020 on-prem.

Related

Using Rest API to trigger a specific stage within a yaml pipeline

Is there a way to execute a specific stage within a running yaml pipeline which uses an environment with approvals?
I have an on-prem deploy and an on-prem destroy stage both have manual approvals.
What I would like to do is run on-prem destroy stage in the past builds using rest api.
What I achieved so far is get 10 recent builds in descending order for a specific source branch lets call it feature/on-prem-enterprise. Then I do some parsing and find past builds that had a successful deployment but failed, cancelled, or skipped destroy stage, using these results from timeline endpoint, I want to use rest api to run/re-run a destroy stage in those builds.
We get into a situation where we have several deployments but nobody is manually running the destroy stage and because this pipeline is shared amongst all developers for dev builds, its very difficult to find those older builds manually.
If it cannot be achieved, then other solution may be to compile this list of builds and send an email out, but would prefer to have less manual intervention here.
Is there a way to execute a specific stage within a running yaml pipeline which uses an environment with approvals?
The answer is yes.
You could use the REST API Runs - Run Pipeline with below request body to skip other stages to trigger stage which you wanted:
POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1
Request Body:
{
"stagesToSkip":["Dev","Test"]
}
Postman test result:
And the test result for the pipeline runs:
You can use the Stages - Update REST API method. This is part of the Build resource methods but works fine for YAML Pipelines as well.
PATCH https://dev.azure.com/{organization}/{project}/_apis/build/builds/{buildId}/stages/{stageRefName}?api-version=7.1-preview.1
It sounds like you're already getting the buildId programmatically. The stageRefName is the name of the stage as defined in your YAML. Your URI will look something like:
https://dev.azure.com/myorg/myproject/_apis/build/builds/1234/stages/DestroyStageName?api-version=7.1-preview.1
In the request body you'll need:
{
forceRetryAllJobs = $false
state = 1 # state 1 is retry
}
forceRetryAllJobs may be unnecessary. There's an example implementation in PowerShell here.
If you're struggling to identify the appropriate API method to replicate something you do in the Azure DevOps GUI opening your browser's debugger tools and inspecting the requests in the network tab can often help you identify the call that's being used.

Programmatically Create Runner for BitBucket Pipelines

Is it possible to programmatically create and register a runner in bitbucket pipelines, in other words without having to create it first via the BitBucket UI.
The docker command provided requires a runner UUID, which must be created when creating the runner through the UI. Is there a way to programmatically create it through the BitBucket API? It seems a bit backward to have to create the runner first just to get the UUID so you can then deploy it.
With GitHub Actions Self Hosted runners, a runner can be created and registered to GitHub using a temporary token, but it does not seem like BitBucket have a dopted this approach, at least yet.
At the time of writing the Bitbucket API does not allow for this. There are two open feature requests for Bitbucket Runner APIs, BCLOUD-21708 and BCLOUD-21309, that may benefit from some votes.

Publish the Screenshot/PDF of SonarQube Analysis Result from Jenkins to Confluence

I have a Jenkins job that will invoke SonarQube analysis on code pulled from a Bitbucket repository whenever there is any changes on the Bitbucket repository. At the moment, I was able to use the Confluence Publisher plugin to publish the URL of the build job to Confluence as a comment.
I was wondering if there is any way I could use Jenkins to screenshot the SonarQube analysis report (like in SonarQube web UI) and publish it directly to Confluence? (So that the user does not need to use the URL to go to the specific Jenkins build job, and then get the SonarQube analysis report URL, and navigate to the page to view the report)
Thank you.
Instead of using a screenshot, you can send a curl request from Jenkins to SonarQube REST API (5.3 and above) to get the project status from the quality gate after the code has been uploaded and analysed. The endpoint you'd likely want to use is: /api/qualitygates/project_status?prjectKey=<projectKey>&branch=<branch>
According to the web API docs:
Get the quality gate status of a project or a Compute Engine task.
Either 'analysisId', 'projectId' or 'projectKey' must be provided
The different statuses returned are: OK, WARN, ERROR, NONE. The NONE status is returned when there is no quality gate associated with the analysis.
Returns an HTTP code 404 if the analysis associated with the task is not found or does not exist.
Requires one of the following permissions:
'Administer System'
'Administer' rights on the specified project
'Browse' on the specified project
Once you have these results available, you can create a Jenkins user in Confluence and have Jenkins send a POST request to create a page or a comment with the results obtained.

Getting git items for a particular branch/tag via TFS rest api

Now that TFS 2015 comes with the same new Rest API of VS Team Services, I've taken a look at the API doc:
https://www.visualstudio.com/en-us/integrate/api/git/overview
One question naturally raised is that most queries do not expose an parameter for git branch or tag (e.g. download /path/to/my/file with tag 'release_v1.0'), which looks like a show stopper. As in my case, I need programatically pull out some source file under a certain branch/tag.
Is it not supported yet?
Yes you can. As or the link that you have above you can use the provided so to retrieve both branches and yes. In git they are really all the same thing, pointers. This use the "refs" api.
https://www.visualstudio.com/integrate/api/git/refs

Need help getting artifact names out of Jenkins for email

I have three Jenkins projects (Maven jobs, to be exact) that have many available parameters. I've set up a way to automate running these weekly with a set of parameters using the Build Flow plugin so that I end up running a total of 12 builds. I also have a follow-up project that exists only to send an email containing some information about the builds that ran, including a link to all 12 of the artifacts.
My problem is figuring out a way to get the artifact names from all 12 jobs. I think the best way to do that is using the REST API, but I could use some help with that.
I am using the Editable Email Notification plugin, and I have access to the job name and build number. I probably just need a script to grab the artifact name from each job.
Does anyone have experience with this?
I haven't tried the exact case you want, but you should be able to use the depth and tree parameters of the REST API to narrow down the data you need. Start with the API URL for one of the builds, which will be of the form
http://jenkins/job/jobname/buildnumber/api/json

Resources