Tamilselvan
Tamilselvan

Reputation: 11

Shell script to create a PR after completed build pipeline in Azure devops

I would like to merge the PR once the build pipeline is completed in Azure devops. Could you please guide me on how we can design and improve this process? i ma trying to get it via rest api but its not working to my expectation

i have written a shell script to create a PR and Approve it but there is error while auto approving

echo "Creating PR..."
    PR_ID=$(curl -u :$(ACCESSTOKEN) -X POST \
      -H "Content-Type: application/json" \
      -d '{
            "sourceRefName": "$(Build.SourceBranch)",
            "targetRefName": "${{ parameters.targetBranch }}",
            "title": "Automated PR: Update Feature",
            "description": "This PR was created automatically."
          }' \
      https://dev.azure.com/${{ parameters.organization }}/${{ parameters.project_name }}/_apis/git/repositories/$(Build.Repository.Name)/pullrequests?api-version=7.1-preview.1 \
      | jq -r '.pullRequestId')

    echo "Approving PR $PR_ID..."
    curl -u :$(ACCESSTOKEN) -X POST \
      -H "Content-Type: application/json" \
      -d '{
            "status": "approved",
            "threadContext": {
              "comments": [
                {
                  "content": "fix: feature update",
                  "commentType": "text"
                }
              ]
            }
          }' \
      https://dev.azure.com/${{ parameters.organization }}/${{ parameters.project_name }}/_apis/git/repositories/$(Build.Repository.Name)/pullrequests/$PR_ID/threads?api-version=7.1-preview.1

error ---

{"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: comments","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}

Upvotes: 0

Views: 59

Answers (1)

Bright Ran-MSFT
Bright Ran-MSFT

Reputation: 13944

Instead of using script to call REST API, You can use the pipeline task extension "Create Pull Request" to easily create new PR automatically in pipeline. This extension provides the pipeline task CreatePullRequest@1 to automatically create PR in pipelines.

Suppose the current build pipeline has a build job. When the build is completed successfully on a branch (e.g, develop), if you want to automatically create a PR to merge new changes from develop to main branch, you can configure like as below:

  1. Install the extension "Create Pull Request" from above link to your Azure DevOps organization.

    enter image description here

  2. In the YAML file on the source branch (e.g, develop) from which you want to merge changes to main, update the pipeline YAML like as below.

    # azure-pipelines.yml
    
    . . .
    
    jobs:
    - job: build
      steps:
      . . .
    
    - job: createPR
      dependsOn: build
      condition: ne(variables['Build.SourceBranch'], 'refs/heads/main')
      pool:
        vmImage: windows-latest
      steps:
      - checkout: none
      - task: CreatePullRequest@1
        inputs:
          repoType: 'Azure DevOps'
          repositorySelector: 'currentBuild'
          sourceBranch: '$(Build.SourceBranch)'
          targetBranch: 'refs/heads/main'
          title: 'Merge changes.'
          description: 'Merge some changes from $(Build.SourceBranch) to refs/heads/main.'
    
  3. The CreatePullRequest@1 task provides almost all the common options for creation of PR, such as Source branch name, Target(s) branch name (can specify multiple target branches using the wildcard symbol *), Title, Description, Reviewers, Set Auto Complete, etc..

    enter image description here

  4. After above configuration, if making some updates on the source branch (e.g, develop) and then trigger the pipeline for the new updates on the source branch, once the build job completed successfully, the createPR job will run to automatically create a PR for merging the updates from the source branch to main.

    enter image description here


Upvotes: 0

Related Questions