Skip to content

Hello, this is an amazing title, Fixes AB#1234 #61

Hello, this is an amazing title, Fixes AB#1234

Hello, this is an amazing title, Fixes AB#1234 #61

Workflow file for this run

# This workflow detects if the AB validation check has succeeded
# (It is considered a success when a comment is posted with the code lcc-200),
# If a success message has been posted by the plugin (https://github.com/marketplace/actions/azure-boards-check-for-ab),
# The workflow will extract the AB ID from the message
# then it will update the PR title adding ',Fixed AB#ID' at the end.
# Why?
# Adding ',Fixed AB#ID' to the PR title will make the PR to automatically resolve the associated ADO item when the PR is completed.
name: "Append 'Fix AB#ID' to PR title"
# Triggers the workflow when a new issue comment is created
on:
issue_comment:
types: [created]
jobs:
# This job assert that:
# 1- The comment is on a pull request
# 2- The comment contains the string 'lcc-200'
# 3- The PR does not contains the 'skip AB ID validation' label
validation:
runs-on: ubuntu-latest
if: |
github.event.issue.pull_request &&
contains(github.event.comment.body, 'lcc-200') &&
!contains(github.event.issue.labels.*.name, 'skip AB ID validation')
steps:
- name: Check if workflow needs to be triggered
run: echo "Trigger workflow"
# Get AB#ID
get_ab_id:
runs-on: ubuntu-latest
needs: validation
outputs:
abid: ${{ steps.get.outputs.id }}
steps:
# Extracts the AB ID from the comment body using regex and store the result using outputs
- name: Get AB ID from comment
id: get
run: |
result=$(echo "${{ github.event.comment.body }}" | grep -oP '(?<=#)\d+')
echo "id=${result}" >> "$GITHUB_OUTPUT"
# Get Pull request ID
get_pr_id:
runs-on: ubuntu-latest
needs: validation
outputs:
prid: ${{ steps.pr.outputs.id }}
steps:
# Extracts the pull request number and store the result using outputs
- name: Extract pull request number
id: pr
run: echo "id=${{ github.event.issue.number }}" >> $GITHUB_OUTPUT
# Get Pull request title
get_pr_title:
runs-on: ubuntu-latest
needs: get_pr_id
outputs:
prtitle: ${{ steps.current-title.outputs.title }}
steps:
# Get current PR title
- name: Get the current PR title
id: current-title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_ID: ${{ needs.get_pr_id.outputs.prid }}
run: |
PR_TITLE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ env.PR_ID }} \
| jq -r .title)
echo "title=$PR_TITLE" >> $GITHUB_OUTPUT
# Checks if the Pull request contains Fixed AB#ID in the title,
# if not cretaes a new title appending ',Fixed AB#ID' to the current title
check_if_title_needs_to_be_updated:
runs-on: ubuntu-latest
needs: [get_pr_title, get_ab_id]
outputs:
new_pr_title: ${{ steps.new-title.outputs.new_pr_title }}
update_required: ${{ steps.check.outputs.update_required }}
steps:
- name: Check if PR title contains fix keyword with AB# using grep
id: check
env:
PR_TITLE: ${{ needs.get_pr_title.outputs.prtitle }}
run: |
if echo "$PR_TITLE" | grep -i -q '\bfix\(ed\|es\)\? AB#[0-9]'; then
echo "Pattern found in PR title!, no need to update."
echo "update_required=false" >> $GITHUB_OUTPUT
else
echo "Pattern not found in PR title!"
echo "update_required=true" >> $GITHUB_OUTPUT
fi
- name: Generate new PR title
env:
AB_ID: ${{ needs.get_ab_id.outputs.abid }}
CURRENT_PR_TITLE: ${{ needs.get_pr_title.outputs.prtitle }}
if: steps.check.outputs.update_required == 'true'
id: new-title
run: |
NEW_TITLE="$CURRENT_PR_TITLE, Fixes AB#$AB_ID"
echo "new_pr_title=${NEW_TITLE}" >> $GITHUB_OUTPUT
# Update new title
update_title:
runs-on: ubuntu-latest
needs: [check_if_title_needs_to_be_updated, get_pr_id]
if: needs.check_if_title_needs_to_be_updated.outputs.update_required == 'true'
steps:
- name: Update the pull request title
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_ID: ${{ needs.get_pr_id.outputs.prid }}
NEW_TITLE: ${{ needs.check_if_title_needs_to_be_updated.outputs.new_pr_title }}
run: |
curl -X PATCH \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ env.PR_ID }} \
-d '{"title":"'"${{ env.NEW_TITLE }}"'"}'