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 |3 Answers
Reset to default 1I 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:
- 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. - 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. - 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
$(taskUrl)
variable and calling the script? – bryanbcook Commented Mar 4 at 0:24