Created
October 25, 2022 09:43
-
-
Save nirgeier/2302b6caa0e890cd1d0f9f741a3807cb to your computer and use it in GitHub Desktop.
Replace text (ex: server url) in AzureDevOps comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
## Global variables | |
## | |
# The string which we wish to replace | |
STR_TO_REPLACE="STR_TO_REPLACE" | |
# The new value | |
STR_REPLACMENT="New Server URL" | |
## | |
## Azure-DevOps properties | |
## You can define them here or in a .env file | |
## . .env | |
# Set the PAT token for the login, | |
# You can generate the token under your | |
# dev.azure.com account > Settings > Personal Access Token | |
TOKEN=<Your PAT token> | |
USERNAME=<Azure DevOps username> | |
ORGANIZATION=<Azure DevOps Organization> | |
PROJECT=<Azure DevOps Project> | |
# Or load from file if you store them in env file | |
#. .env | |
###### Test Work item. | |
## >>> In real life it should be a loop over all the work items | |
TEST_ISSUE_ID=137 | |
## | |
## The api to for fetching/updating the issue | |
## >> TEST_ISSUE_ID is used here for the test | |
## | |
BASE_URL="https://dev.azure.com/$ORGANIZATION/$PROJECT/_apis/wit/workitems" | |
## | |
## Load the Work Item | |
## | |
workitem=$( curl -X \ | |
GET -u $USERNAME:$TOKEN \ | |
$BASE_URL\?ids=$TEST_ISSUE_ID) | |
## | |
## Load Work Item comments. | |
## | |
comments=$( curl -X \ | |
GET -u $USERNAME:$TOKEN \ | |
$BASE_URL/$TEST_ISSUE_ID/comments) | |
## | |
## Extract the comments text from the response | |
## >> In this demo we only process the 1st one -> '.comments[0].text' | |
## | |
commentsText=$(echo $comments | jq '.comments[0].text') | |
## | |
## Remove double quotes from output | |
## | |
# Remove 1st quote | |
commentsText=${commentsText#?} | |
# Remove last quote | |
commentsText=${commentsText%?} | |
### | |
### Replace the desired content | |
### | |
commentsText=$(echo $commentsText | sed "s/$STR_TO_REPLACE/$STR_REPLACMENT/g") | |
## | |
## Update the WorkItem comments | |
## >> 'api-version=6.0-preview.3' can be passed a sheader instead | |
curl -X POST \ | |
-u $USERNAME:$TOKEN \ | |
-H "Content-Type: application/json" \ | |
--data "{'text' : '$commentsText'}" \ | |
$BASE_URL/$TEST_ISSUE_ID/comments?api-version=6.0-preview.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment