Search code examples
gitazure-devopsazure-pipelines

How to get commit message for current PR in azure devops instead of "Merge pull request"


I was given the task to enforce semantic versioning on our azure devops pipeline and I'm struggling to find a way to get the PR commit message.

This is the format we are going for

feat(EMV-000): this commit will do stuff

This is every way I've tried to get the commit message from the pr, while doing research:

git log --format=%B -n 1
git log -1 --pretty=format:%s ${gitCommitId})
git log -n 1 ${branchName}
git log -1 --format=%H --grep=$(Build.SourceVersionMessage)
git log -1 --format=%P $(Build.SourceVersion)
git rev-parse $(Build.SourceVersion)^
git log --pretty=oneline --abbrev-commit -n 1 --merges $(Build.SourceVersion)
git log --pretty=oneline --abbrev-commit -n 1 $(Build.SourceVersion)
git log --no-merges --first-parent -n 1
git log -1 --format=%s

running these locally I get the desired output:

chore(EMV-186): this commit will be approved by the pipeline

running these on the pipeline I get:

Merge pull request 61 from enforce_git_message into develop

Maybe its relevant to know that we squash our commits and rebase our branches.

Below is the way I am trying to run the script on the azure pipelines yml

- script: |
    COMMIT_MSG=$(git log -1 --format=%s)
    echo "Commit message: $COMMIT_MSG"
    if [[ $COMMIT_MSG =~ ^(feat|fix|chore|docs|style|refactor|test|build|ci|perf) ]]; then
      echo "Commit message type is valid"
    else
      echo "Error: Commit message must start with one of the following: feat, fix, chore, docs, style, refactor, test, build, ci, perf"
      exit 1
    fi
  displayName: 'Check commit message type'

I'm not a devops dev, if you have found alternative ways to solve this task, I would be glad to hear it. Thanks a lot in advance


Solution

  • You can get the pull request commit message via rest api Pull Request Commits - Get Pull Request Commits.

    GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/commits?api-version=7.1
    

    Devops task sample:

    pool:
      vmImage: ubuntu-latest
    
    steps:
      - checkout: none
      - powershell: |
          # Get the commits for the pull request
          $commitsUrl = "$(System.CollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.Name)/pullRequests/$(System.PullRequest.PullRequestId)/commits?api-version=7.1"
          echo $commitsUrl
          $commitsResponse = Invoke-RestMethod -Uri $commitsUrl -Method Get  -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
          # Extract and print the commit messages
          $commitMessages = $commitsResponse.value.comment
          $commitMessages
        env:
          SYSTEM_ACCESSTOKEN: $(system.accesstoken)
        displayName: 'Get Pull Request Commit Messages'
    

    It will grant all commit messages in Pull request as below:

    enter image description here

    If you would like only the latest commit message, add "`$top=1" in the url.

    $commitsUrl = "$(System.CollectionUri)$(System.TeamProject)/_apis/git/repositories/$(Build.Repository.Name)/pullRequests/$(System.PullRequest.PullRequestId)/commits?`$top=1&api-version=7.1"
    

    enter image description here