Skip to content

Instantly share code, notes, and snippets.

@marcosborges
Created September 29, 2022 13:41
Show Gist options
  • Save marcosborges/3653376919222dca6e703f91099a0932 to your computer and use it in GitHub Desktop.
Save marcosborges/3653376919222dca6e703f91099a0932 to your computer and use it in GitHub Desktop.
steps:
- bash: |
currentBranch=$(git branch --show-current)
commitMsgLine=$(git log -1 --oneline $currentBranch)
lastCommitHash=$(git rev-parse --short HEAD)
#Remove the commit # from the message
commitMsg="${commitMsgLine##$lastCommitHash}"
#Remove the extra space before the message body
commitMsg="${commitMsg## }"
title=$commitMsg
description=$commitMsg
devopsPRUri="${API_BASE_URL}/_apis/git/repositories/${REPOSITORYID}/pullrequests?api-version=6.0"
read -r -d '' requestBody <<EOM
{
'sourceRefName': 'refs/heads/$currentBranch',
'targetRefName': 'refs/heads/main',
'title':'$title',
'description':'$description',
'isDraft':false,
'labels':[],
'reviewers':[],
'completionOptions': {
'autoCompleteIgnoreConfigIds':[],
'bypassPolicy':false,
'deleteSourceBranch':true,
'mergeCommitMessage':'Merged PR 180: Auto Commit\n\nAuto Commit',
'mergeStrategy':2,
'transitionWorkItems':true
},
'autoCompleteSetBy': {
'id':'06eaebd0-c272-45eb-9555-302746cbdf87'
}
}
EOM
# ABRINDO PR
result=$(
curl -s -u ":${AZUREDEVOPSPAT}" \
-H "Content-Type:application/json" \
--data-raw "$requestBody" \
-X POST \
"$devopsPRUri"
)
prTitle=$(echo $result | jq -r '.title')
prDescription=$(echo $result | jq -r '.description')
pullRequestId=$(echo $result | jq -r '.pullRequestId')
prStatus=$(echo $result | jq -r '.status')
prCreationDate=$(echo $result | jq -r '.creationDate')
prSourceRefName=$(echo $result | jq -r '.sourceRefName')
prTargetRefName=$(echo $result | jq -r '.targetRefName')
prUrl=$(echo $result | jq -r '.url')
createdBy=$(echo $result | jq -r '.createdBy.id')
echo prTitle=$prTitle
echo prDescription=$prDescription
echo pullRequestId=$pullRequestId
echo prStatus=$prStatus
echo prCreationDate=$prCreationDate
echo prSourceRefName=$prSourceRefName
echo prTargetRefName=$prTargetRefName
echo prUrl=$prUrl
# Launch default browser with the pr url
echo "${API_BASE_URL}/_git/${REPOSITORYID}/pullrequest/${pullRequestId}"
read -r -d '' requestBody2 <<EOM
{
'completionOptions':{
'autoCompleteIgnoreConfigIds':[],
'bypassPolicy':false,
'deleteSourceBranch':true,
'mergeCommitMessage':'Merged PR 186: Auto Commit\n\nAuto Commit',
'mergeStrategy':2,
'transitionWorkItems':true
},
'autoCompleteSetBy': {
'id':'${createdBy}'
}
}
EOM
# SET AUTOCOMPLETE
devopsPRUri2="${API_BASE_URL}/_apis/git/repositories/${REPOSITORYID}/pullrequests/${pullRequestId}?api-version=5.0-preview.1"
echo $(curl -s -u ":${AZUREDEVOPSPAT}" \
-H "Content-Type:application/json" \
--data-raw "$requestBody2" \
-X 'PATCH' \
"$devopsPRUri2")
# APPROVE
devopsPRUri3="${API_BASE_URL}/_apis/git/repositories/${REPOSITORYID}/pullrequests/${pullRequestId}/reviewers/${createdBy}?api-version=5.0-preview.1"
echo $(curl -s -u ":${AZUREDEVOPSPAT}" \
-H "Content-Type:application/json" \
--data-raw "{\"hasDeclined\":false,\"id\":\"${createdBy}\",\"vote\":10}" \
-X 'PUT' \
"$devopsPRUri3")
env:
API_BASE_URL: $(System.CollectionUri)$(System.TeamProject)
REPOSITORYID: gitops
AZUREDEVOPSPAT: $(System.AccessToken)
workingDirectory: $(Agent.BuildDirectory)/gitops
displayName: PR GitOps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment