6

I am using Azure pipelines to build and release my software through its GitHub integration. As part of monitoring, I am using Sentry to record exceptions, etc.

I want to use the "Suspect Commits" feature of Sentry (so it can point at commits that are likely to have caused a specific issue). For this to work, I need to send Sentry a release (just a version associated with a specific project) with a list of associated commits relating to it.

I've read this post:

Azure DevOps integration in Sentry: Associate commits

And this one on GitHub:

https://github.com/getsentry/sentry/issues/11127

And while both have (very different) approaches to getting a list of commits, they assume that one is using the Azure DevOps repositories feature. I have no repositories on my DevOps instance, so, though useful posts, they don't really help me directly.

In short - I need to list all the commits on GitHub associated with a specific release on Azure DevOps, so I can send them to the Sentry API.

Has anyone done this? How can I achieve that? Am I missing something obvious?

Sklivvz
  • 30,601
  • 24
  • 116
  • 172
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • Neither of these linked options assume the repository is stored in Azure Repos. Both look at the Build and Release data only. The first relies on Azure Pipelines to associate the commits, the second creates a clone of the repo and then allows the sentry-cli to compute the associated commits locally. – jessehouwing Feb 28 '20 at 14:28
  • @jessehouwing - I have tried the powershell script - I am getting no commits between builds/releases even when I can see they were triggered by different GitHub commits. – Oded Feb 28 '20 at 14:35
  • To expand - I am getting a response (indicating a valid request), but the list of commits coming from the API is empty, @jessehouwing – Oded Feb 28 '20 at 14:42
  • It's possible that Azure Pipelines doesn't track this data for GitHub. And the other method of cloning and using sentry-cli? – jessehouwing Feb 28 '20 at 15:05
  • I didn't try that, @jessehouwing - I don't see where it clones a repo? (and the comments indicate it assumes the code is run inside a git repo) – Oded Feb 28 '20 at 15:09
  • This maybe superfluous and rudimentary but have you checked this out? https://learn.microsoft.com/en-us/rest/api/azure/devops/git/commits/get%20commits?view=azure-devops-rest-5.1#all-commits You can wrap that in a powershell task and catch the json thats being returned for Sentry. Just trying to help. – jetstreamin Feb 28 '20 at 15:14
  • @jetstreamin - I did see that, however, where do I get the repositoryId? That is a Azure DevOps repository id, and I don't see how that would help with getting a list of commits from a GitHub repo. – Oded Feb 28 '20 at 15:21
  • @Oded if you look at the doc, it also name or id, that might work to just pass the name in – jetstreamin Feb 28 '20 at 15:28
  • You can easily clone the repo yourself or add the repo as an artifact to your release pipeline. – jessehouwing Feb 28 '20 at 20:15
  • @jessehouwing - thought about that, but wanted to avoid having to clone the whole repo (trying to have a *fast* release pipeline). – Oded Feb 28 '20 at 20:21
  • Just put it in a parallel stage and don't worry about it. – jessehouwing Feb 28 '20 at 20:45
  • (The replier of first link coming...) How's your pipeline structure in Azure devops? CI + CD? Or just CD? And what's the trigger type? The get-changes api I mentioned in first link you shared does not suitable for github source. It only available for the git repos which in VSTS. BUT, in pipeline, it has build-in variable can represent the associated commit id from github. And just for one. – Mengdi Liang Mar 02 '20 at 05:08
  • Thanks for chiming in, @MerlinLiang-MSFT - this is a CI + CD pipeline. It is triggered on GitHub commits to the master branch. – Oded Mar 02 '20 at 09:04

1 Answers1

0

As I mentioned in comment, the get-changes api which used in this ticket does not suitable for the build pipeline that has github repos source.

But, what fortunate is, we has fully feature support for github cloud. So here you can use another one to get such associate commits list, the one which does not be documented by us.

GET https://dev.azure.com/{org name}/{project name}/_traceability/runview/changes?currentRunId={build id}&__rt=fps&__ver=2

Most of time, you can consider to catch some records from F12, when you find no way from documents we public. Above api can be got from F12 while you click on the Changes link in Build Summary page:

enter image description here


I wrote a complete powershell script, which you can directly make use it in release pipeline to get commits id from its response body:

$token = "{PAT token}"

$url="https://dev.azure.com/{org name}/{project name}/_traceability/runview/changes?currentRunId={build id}&__rt=fps&__ver=2"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get

Write-Host "results = $($response.fps.dataProviders.data.'ms.vss-traceability-web.traceability-run-changes-data-provider'.artifactsData.data.id | ConvertTo-Json -Depth 100)"

enter image description here


In release pipeline, we provide one built-in environment variable that you can directly get the corresponding triggered Build id: $(Build.Buildid). And you can inject this into api to make the build id can be get automatically during the CI+CD process.


Update on 3/4/2020:

Based on the screenshot you shared in our discussion, the data structure on yours is for git repo (not sure why, will dig that) :

Please transfer the pipeline with YAML. Then trigger it and write the commits with the scripts I shared above. You will see commits data from the results of YAML.

Mengdi Liang
  • 17,577
  • 2
  • 28
  • 35
  • Getting 401 Unauthorized for that URL. – Oded Mar 03 '20 at 09:33
  • @Oded 401 is a token error. Does the token you used is available? – Mengdi Liang Mar 03 '20 at 09:37
  • It is available and works for other calls, for example with `https://dev.azure.com/{organization}/{project}/_apis/build/changes?fromBuildId={fromBuildId}&toBuildId={toBuildId}&api-version=5.1-preview.2` - could this be a permissions issue with the token? – Oded Mar 03 '20 at 09:53
  • I've enabled all read options for the token and am still getting 401 for this call (when other calls with same token work). – Oded Mar 03 '20 at 09:57
  • @Oded This is different from the one you said just now. What about generate a new PAT by setting the scope to all? I did with such scope. – Mengdi Liang Mar 03 '20 at 09:59
  • Full access token works (in that I don't get a 401), but the result is empty. – Oded Mar 03 '20 at 10:08
  • @Oded Great. Does the summary page of the build you used in that api having actual changes? – Mengdi Liang Mar 03 '20 at 10:10
  • Yes it does - 1 commit. – Oded Mar 03 '20 at 10:14
  • @Oded.Just run again with one build that only contains 1 commit. I can get it successfully: https://imgur.com/a/urb548D. What about print out with `Write-Host "results = $($response.fps.dataProviders.data.'ms.vss-traceability-web.traceability-run-changes-data-provider'.artifactsData.data | ConvertTo-Json -Depth 100)"`. Here it will print out the complete commit message https://imgur.com/a/GRtOmeA. – Mengdi Liang Mar 03 '20 at 10:44
  • I get a completely empty response - Just `results = ` – Oded Mar 03 '20 at 10:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208908/discussion-between-merlin-liang-msft-and-oded). – Mengdi Liang Mar 03 '20 at 10:48
  • The powershell script returns the same incomplete results as Microsoft's Get-Changes API :( I get the ".artifactsData.data" only for the first repository. So this is not working for builds that have multiple Git repos. – PainElemental Nov 18 '22 at 14:20