1

I'm working on a localization project where we are attempting to move all of our project's static strings to resource kvp files, which are generated from a 3rd party service. As a safeguard against missing translations, when a PR is made I would like to check that said resource files have a corresponding kvp for each key in the source code.

I understand from this documentation you can set up a web hook to call an Azure Function that can in turn set the status of the PR that triggered it. However, I am having difficulty in accessing the actual code of the source branch. It seems that all that is sent to the Azure Function is metadata about the PR itself.

Is there a way to use this functionality to validate the source code, or is there another method of running custom validation on an Azure DevOps PR?

Thanks!

vball
  • 359
  • 1
  • 14
  • 1
    As the [API documentation](https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20request?view=azure-devops-rest-6.0) states, the PR endpoint returns an [object containing various fields](https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20request?view=azure-devops-rest-6.0#gitpullrequest). One of these is `commits` which contains a reference to all commits in that PR, from which you can get the list of `changes`, from which you can get the `newContent` of the changed item. – Ian Kemp Sep 16 '20 at 15:48
  • Would this allow me to retrieve code that hasn’t been changed? For instance if a new instance of an existing key is added to a page, but the resource file itself hasn’t been altered, I should still be able to access the resource file – vball Sep 16 '20 at 15:51
  • 2
    A commit will only contain references to changed files, yes. But the pull request will contain a `sourceRefName` which is a reference to the branch that the PR was made from. You can use that together with [this answer](https://stackoverflow.com/questions/54228312/how-to-download-a-file-in-a-branch-from-a-git-repo-using-azure-devops-rest-api) to get the other file(s) you're interested in. – Ian Kemp Sep 16 '20 at 16:04
  • Great, that's exactly what I was looking for. If you can post your comment as an answer I'll mark it as accepted – vball Sep 16 '20 at 16:25
  • @lan Kemp Please consider adding the answer as an answer instead of comments, so that members with similar issue can easily find correct solution. Just a reminder :) – LoLance Sep 17 '20 at 07:22
  • Since **lan Kemp** won't add the answer, would you please adding the answer yourself and accept it so that members can benefit from marked answer and we can archive this thread for you. See [can I answer my own question](https://stackoverflow.com/help/self-answer)~ – LoLance Sep 18 '20 at 13:16

1 Answers1

1

From Ian Kemp's solution in the comments:

As the API documentation states, the PR endpoint returns an object containing various fields. The pull request will contain a sourceRefName which is a reference to the branch that the PR was made from. You can use that together with this answer to get the other file(s) you're interested in.

vball
  • 359
  • 1
  • 14
  • Thanks for sharing the answer here, would you please [accept it as the answer](https://stackoverflow.blog/2009/01/06/accept-your-own-answers/) since you've met all the requirements to accept own answer? So it would be helpful for other members who get the same issue to find the solution easily. Have a nice day:) – LoLance Sep 21 '20 at 09:45