3

I have a repository with a protected master branch and squash commit only enabled. I'd like to enforce validation of the commit message on completion of the PR. This is to check the engineer has placed the correct content in the PR description.

I think the best approach to do this is with a commit-msg hook. However, I'm not sure how I can get Azure DevOps to include and use Git Hooks during the PR merge process.

Is this possible? If not, I'm open to alternative suggestions on how to solve the problem.

MrPanucci
  • 497
  • 2
  • 11

1 Answers1

1

How can I add Git Hook commit-msg on PR Completion in Azure DevOps?

I am afraid there is no such out of box way to achieve this at this moment. But there is a On Roadmap user voice about it, you could check it for some more info, and this feature should see us soon:

Add Git pre-receive hooks

As workaround, we could use the REST API Pull Request Commits - Get Pull Request Commits and Pull Request Commits - Get Pull Request Commits and build validation to verify the commit message or PR description.

To resolve this request, we could add a Build Validation on the target branch to invoke above REST APIs. In above REST API URLs, we could to know that if we want use the REST APIs, we need provide the pullRequestId.

In the predefined variables, there is a variable System.PullRequest.PullRequestId, which we could use it to get the pullRequestId.

After we get the pullRequestId, we could use above REST APIs to get commit message or PR description, then verify them.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • 1
    Thanks Leo. Would the pre-receive hook solve my problem? I'm not sure what happens during completion of a pull request but from your pre-receive hook suggestion, sounds like a push happens under the hood. Is that right? – MrPanucci Feb 12 '21 at 07:55
  • @MrPanucci, I'm not 100% sure what the final released feature will look like. This needs to be tested after the feature is officially released or additional processing is performed to see if it can meet our needs. But at present, there is no direct way to achieve your needs, you can consider the workaround I provide. – Leo Liu Feb 12 '21 at 07:58
  • For your suggested alternative, is there a recommended pattern for authenticating with API from a pipeline? Is it best to generate a PAT? – MrPanucci Feb 12 '21 at 09:02
  • @MrPanucci, Yes, you are right, the PAT is a good choice. – Leo Liu Feb 12 '21 at 09:46