-
Notifications
You must be signed in to change notification settings - Fork 125
154 lines (146 loc) · 5.72 KB
/
validate-pr-ab-id.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# This workflow checks if an ADO ID (AB#ID) has been set successfully (https://github.com/marketplace/actions/azure-boards-check-for-ab)
# If so, 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: 'AB#ID Check'
on:
pull_request:
types: [opened, reopened, edited, labeled, synchronize]
jobs:
# This action checks your pull request to make sure it is linked to a work item using AB# before you can merge.
validation:
name: Check if PR has a valid ADO ID
permissions:
contents: read
issues: write
pull-requests: write
runs-on: ubuntu-latest
if: "!contains(github.event.pull_request.labels.*.name, 'skip AB ID validation')"
steps:
- uses: danhellem/github-actions-pr-is-linked-to-work-item@main
# Get AB#ID
get_ab_id:
name: Get AB ID
runs-on: ubuntu-latest
needs: validation
outputs:
abid: ${{ steps.get.outputs.id }}
steps:
# Extracts the AB ID from the pull_request body using regex and store the result using outputs
- name: Get AB ID from comment
id: get
run: |
result=$(echo "${{ github.event.pull_request.body }}" | grep -oP '(?<=#)\d+')
echo "id=${result}" >> "$GITHUB_OUTPUT"
# Get Pull request ID
get_pr_id:
name: Get PR ID
runs-on: ubuntu-latest
needs: validation
outputs:
prid: ${{ steps.pr.outputs.id }}
steps:
# Extracts the pull request id and store the result using outputs
- name: Extract pull request id
id: pr
run: echo "id=${{ github.event.pull_request.number }}" >> $GITHUB_OUTPUT
# Get Pull request title
get_pr_title:
name: Get PR title
permissions:
contents: read
issues: read
pull-requests: read
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:
name: 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:
name: Update title
permissions:
contents: read
issues: write
pull-requests: write
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 }}"'"}'
# This job will determine the final status of the workflow and can be made a requirement before merging.
# The job will succeed if any of the following conditions are met:
# 1. The pull request title was successfully updated.
# 2. The pull request has the 'skip AB ID validation' label.
# 3. The title was already updated.
status_report:
name: AB ID validation Check
runs-on: ubuntu-latest
needs: [update_title, check_if_title_needs_to_be_updated, validation]
if: always()
steps:
- name: Check status of job1
id: check_status
run: |
if [[ "${{ needs.update_title.result }}" == "success" ]] ||
[[ "${{ needs.validation.result }}" == "skipped" ]] ||
[[ "${{ needs.update_title.result }}" == "skipped" && "${{ needs.check_if_title_needs_to_be_updated.result }}" == "success" ]]; then
echo "Job succeeded or was skipped."
else
echo "Job failed or was cancelled."
exit 1
fi