最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to use Azure DevOps REST API to post link in PR comments from Azure Pipeline? - Stack Overflow

programmeradmin0浏览0评论
parameters:
      comment: >
        {
          "comments": [
            {
              "parentCommentId": 0,
              "content": "<a href\=\"$(taskUrl)\">Click here to see colored output of Terraform plan:</a>\\n\`\`\`hcl$(plan)\`\`\`",
              "commentType": "system"
            }
          ],
          "status": "byDesign"
        }

...some template...

  curl --fail \
    --request POST "$URL" \
    --header "Authorization: Bearer ${{ parameters.accessToken }}" \
    --header "Content-Type: application/json" \
    --data @- <<- EOF
  ${{ parametersment }}
  EOF

This is my code, doesn't work.

I think Azure Pipelines is sanitizing my code, when I inspect the element there's nothing after href, completely deleted.

At the same time, I've tried with Markdown [example](), but I get parsing error due to ().

Tried escaping them, but this also doesn't work. I actually tried everything I could think of for one week straight. Couldn't find any solution

parameters:
      comment: >
        {
          "comments": [
            {
              "parentCommentId": 0,
              "content": "<a href\=\"$(taskUrl)\">Click here to see colored output of Terraform plan:</a>\\n\`\`\`hcl$(plan)\`\`\`",
              "commentType": "system"
            }
          ],
          "status": "byDesign"
        }

...some template...

  curl --fail \
    --request POST "$URL" \
    --header "Authorization: Bearer ${{ parameters.accessToken }}" \
    --header "Content-Type: application/json" \
    --data @- <<- EOF
  ${{ parametersment }}
  EOF

This is my code, doesn't work.

I think Azure Pipelines is sanitizing my code, when I inspect the element there's nothing after href, completely deleted.

At the same time, I've tried with Markdown [example](https://link), but I get parsing error due to ().

Tried escaping them, but this also doesn't work. I actually tried everything I could think of for one week straight. Couldn't find any solution

Share Improve this question asked Mar 3 at 14:08 Markus PetersonMarkus Peterson 1638 bronze badges 1
  • Can you edit your post and provide more details about your pipeline, specifically how you're defining the $(taskUrl) variable and calling the script? – bryanbcook Commented Mar 4 at 0:24
Add a comment  | 

3 Answers 3

Reset to default 1

I have no issues commenting on the PR using the script you provided. I suspect the value of your variable $(taskUrL) doesn't have a value.

In the following template, I'm using a job template so that I can write the variables into the job.

parameters:
- name: taskUrl
  type: string

- name: hclPlan
  type: string

- name: comment
  type: string
  default: |-
    {
      "comments": [
        {
        "parentCommentId": 0,
          "content": "<a href=\"$(taskUrl)\">$(hclPlan)</a>",
          "commentType": "system"
        }
      ],
      "status": "byDesign"
    }

jobs:
- job: displayjob
  variables:
    taskUrl: ${{ parameters.taskUrl }}
    hclPlan: ${{ parameters.hclPlan }}
  steps:
  - script: |
      baseUrl="$(System.TeamCollectionUri)/$(System.TeamProject)"
      apiUrl="/_apis/git/repositories/$(Build.Repository.Id)/pullRequests/$(System.PullRequest.Id)/threads?api-version=6.0"
      url = $baseUrl$apiUrl
      curl --fail \
        --request POST "$url" \
        --header "Authorization: Bearer $(System.AccessToken)" \
        --header "Content-Type: application/json" \
        --data @- <<- EOF
      ${{ parametersment }}
      EOF

Under the hood, here's the order of operations:

  1. At compile time, the template is expanded and the values of the expressions are written into the YAML. At this time $(taskUrl) has no value because it's a runtime value.
  2. The script task pre-processes the inline script as it appears in the YAML and replaces all references of variables written in macro syntax $(<variable-name>) with their runtime values.
  3. The script task writes the resulting script to disk as a temporary file and then executes it.

To post a comment in a Pull Request (PR) from an Azure Pipeline using the Azure DevOps REST API, you can add the following PowerShell task in your pipeline:

- task: PowerShell@2
  displayName: Add PR comment
  inputs:
    targetType: 'inline'
    script: |
      $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/git/repositories/$env:BUILD_REPOSITORY_ID/pullRequests/$(System.PullRequest.PullRequestId)/threads?api-version=5.1"

      $prComment = @"
      ## :green_circle: Example heading :green_circle:
      - __Bullet one__: text
      - __Bullet two__: text
      "@

      $body = @"
      {
          "comments": [
            {
              "parentCommentId": 0,
              "content": "$prComment",
              "commentType": 1
            }
          ],
          "status": 4 
        }
      "@

      Invoke-RestMethod -Uri $url -Method POST -Headers @{ Authorization = "Bearer $(System.AccessToken)" } -Body $body -ContentType application/json

Environment Variables:

  • $env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI: The URI of the Azure DevOps collection.
  • $env:SYSTEM_TEAMPROJECTID: The ID of the Azure DevOps project.
  • $env:BUILD_REPOSITORY_ID: The ID of the repository where the build is running.
  • $(System.PullRequest.PullRequestId): The ID of the pull request. This is a predefined variable in Azure Pipelines that contains the ID of the PR being processed.
  • See docs here for predefined variables: https://learn.microsoft/en-us/azure/devops/pipelines/build/variables?view=azure-devops&tabs=yaml

Make sure to replace the placeholder text in the $prComment variable with your actual comment content.

See docs here for how request body can be customized: https://learn.microsoft/en-us/rest/api/azure/devops/git/pull-request-threads/create?view=azure-devops-rest-7.1&tabs=HTTP#commenttype

You can set and use the parameter for the JSON request body like as below in YAML pipeline.

parameters:
- name: reqBody
  type: object
  default:
    comments:
      - parentCommentId: 0
        content: "<a href=\"$(taskUrl)\">Click here to see colored output of Terraform plan:</a>\n```hcl$(plan)```"
        commentType: "system"
    status: "byDesign"

variables:
  taskUrl: "xxxx"
  plan: "xxxx"

jobs:
- job: callAPI
  steps:
  - bash: |
      uri="xxxx"
      curl -X POST -u :$SYSTEM_ACCESSTOKEN $uri \
      -H "Content-Type: application/json" \
      -d "${REQ_BODY}"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
      REQ_BODY: ${{ convertToJson(parameters.reqBody) }}
    displayName: 'Call API'

Related documentqtions:

  • Runtime parameters
  • Expressions

发布评论

评论列表(0)

  1. 暂无评论