/azdo_commit_message_validator

GitHub Action to enforce commits are linked to Azure Boards work items, and link the work item to the pull request

Primary LanguageJavaScriptMIT LicenseMIT

Azure DevOps Commit Validator and Pull Request Linker Action

This is an action to be ran in a pull request to make sure that all commits have a AB#123 in the commit message.

image

It also automatically links pull request to all of the Azure DevOps work item(s).

image

Screenshot of validating the logs and creating pull requests:

image

Usage

This should only be triggered via pull requests.

name: pr-commit-message-enforcer-and-linker

on:
  pull_request:
    branches: [ "main" ]

jobs:
  pr-commit-message-enforcer-and-linker:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v3
    - name: Azure DevOps Commit Validator and Pull Request Linker
      uses: joshjohanning/azdo_commit_message_validator@v1
      with:
        azure-devops-organization: myorg # The name of the Azure DevOps organization
        azure-devops-token: ${{ secrets.AZURE_DEVOPS_PAT }} # "Azure DevOps Personal Access Token (needs to be a full PAT)
        fail-if-missing-workitem-commit-link: true # Fail the action if a commit in the pull request is missing AB# in the commit message
        link-commits-to-pull-request: true # Link the work items found in commits to the pull request

Setup

  1. Create a repository secret titled AZURE_DEVOPS_PAT - it needs to be a full PAT
  2. Pass the Azure DevOps organization to the azure-devops-organization input parameter

Note: jq needs to be installed on the runner running this action

How this works

The action loops through each commit and:

  1. makes sure it has AB#123 in the commit message
  2. if yes, add a GitHub Pull Request link to the work item in Azure DevOps

Adding the link to the GitHub Pull Request was the tricky part.

If you use an API to look at the links of a work item with a GitHub pull request link, you will see:

      "attributes": {
        "authorizedDate": "2022-08-02T18:45:03.567Z",
        "id": 3916078,
        "name": "GitHub Pull Request",
        "resourceCreatedDate": "2022-08-02T18:45:03.567Z",
        "resourceModifiedDate": "2022-08-02T18:45:03.567Z",
        "revisedDate": "9999-01-01T00:00:00Z"
      },
      "rel": "Artifact Link",
      "url": "vstfs:///GitHub/PullRequest/62f33e8a-c421-441d-88e1-06c46c4ffbbb%2f7"

Note the url field - vstfs:///GitHub/PullRequest/62f33e8a-c421-441d-88e1-06c46c4ffbbb%2f7

Creating a new link is (relatively) easy with the API, but you can't just use the regular GitHub pull request link. They use a garbled GUID that isn't the GUID or ID of the repo in GitHub.

The GUID can be found using an (undocumented) API:

POST https://dev.azure.com/%DEVOPS_ORG%/_apis/Contribution/dataProviders/query?api-version=7.1-preview.1

See this thread for slightly more info.

Found the javascript sample here. Other samples are here.