-
Notifications
You must be signed in to change notification settings - Fork 54
147 lines (130 loc) · 5.59 KB
/
process-pr-merge.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
name: process-pr-merge
# This workflow processes commits pushed to the main branch to:
# 1. Determine if the commit is associated with a PR, and if not exit gracefully.
# 2. Extract relevant details (change type, release note, and bump type) from the PR body
# and use them to update the changelog if applicable (change type is not "OTHER").
# 3. Trigger a release action if warranted by the bump type (MAJOR, MINOR), or skip if WAIT is selected.
on:
push:
branches:
- main
env:
GH_TOKEN: ${{ secrets.ACTIONS_PAT }}
jobs:
determine-pr:
runs-on: ubuntu-latest
outputs:
pr_number: ${{ steps.check-pr.outputs.pr_number }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_PAT }}
fetch-depth: 0
- name: Check if this is a PR commit
id: check-pr
run: |
commit_sha=$(git log -1 --format="%H")
echo "Commit SHA: $commit_sha"
pr_number=$(gh api -X GET "repos/${{ github.repository }}/commits/$commit_sha/pulls" --jq '.[0].number')
if [ -z "$pr_number" ]; then
echo "No PR associated with commit $commit_sha."
echo "pr_number=" >> $GITHUB_OUTPUT
exit 0
fi
echo "PR Number: $pr_number"
echo "pr_number=${pr_number}" >> $GITHUB_OUTPUT
update-changelog:
runs-on: ubuntu-latest
needs: determine-pr
outputs:
bump_type: ${{ steps.extract-details.outputs.bump_type }}
if: needs.determine-pr.outputs.pr_number != ''
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
token: ${{ secrets.ACTIONS_PAT }}
fetch-depth: 0
- name: Extract change type, bump type, and release note
id: extract-details
run: |
pr_number="${{ needs.determine-pr.outputs.pr_number }}"
echo "Fetching PR details for PR #$pr_number..."
# Fetch PR body
pr_body=$(gh api -X GET "repos/${{ github.repository }}/pulls/$pr_number" --jq '.body' | sed 's/\r//g')
echo "Extracting details from PR Body: $pr_body"
# Extract release note
release_note=$(echo "$pr_body" | awk 'BEGIN { found=0 } /```release-note/ { found=1; next } /```/ { found=0 } found { print }' | sed '/^\s*$/d')
echo "Release Note: $release_note"
if [ -z "$release_note" ]; then
echo "No release note found."
exit 1
fi
echo "release_note=${release_note}" >> $GITHUB_OUTPUT
# Determine change type
change_type=$(echo "$pr_body" | grep -oP '(?<=- \[x\] \*\*`)[A-Z]+(?=`\*\*)' | head -n 1)
echo "Change Type: $change_type"
case "$change_type" in
"ADDITION")
section="### Added"
;;
"CHANGE")
section="### Changed"
;;
"FIX")
section="### Fixed"
;;
"OTHER")
echo "Change type is 'OTHER'. Skipping changelog update."
exit 0
;;
*)
echo "Invalid change type: $change_type."
exit 1
;;
esac
echo "section=${section}" >> $GITHUB_OUTPUT
# Check for bump type
major_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MAJOR`' && echo "true" || echo "false")
minor_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`MINOR`' && echo "true" || echo "false")
wait_checked=$(echo "$pr_body" | grep -q '\[x\] \*\*`WAIT`' && echo "true" || echo "false")
if [ "$major_checked" == "true" ]; then
echo "bump type: MAJOR"
echo "bump_type=major" >> $GITHUB_OUTPUT
elif [ "$minor_checked" == "true" ]; then
echo "bump type: MINOR"
echo "bump_type=minor" >> $GITHUB_OUTPUT
elif [ "$wait_checked" == "true" ]; then
echo "bump type WAIT was checked. No release required."
echo "bump_type=" >> $GITHUB_OUTPUT
else
echo "No valid bump type specified. Skipping release."
echo "bump_type=" >> $GITHUB_OUTPUT
fi
- name: Update changelog
run: |
echo "Updating changelog under section: ${{ steps.extract-details.outputs.section }}"
sed -i "0,/${{ steps.extract-details.outputs.section }}/s@${{ steps.extract-details.outputs.section }}@${{ steps.extract-details.outputs.section }}\n- ${{ steps.extract-details.outputs.release_note }}@" CHANGELOG.md
cat CHANGELOG.md
- name: Commit and push changelog update
uses: EndBug/add-and-commit@v7
with:
add: 'CHANGELOG.md'
message: "Update CHANGELOG.md with release note from PR #${{ needs.determine-pr.outputs.pr_number }}"
author_email: git-action-bot@example.com
author_name: Git Action Bot
token: ${{ secrets.GITHUB_TOKEN }}
push: true
dispatch-release:
runs-on: ubuntu-latest
needs: update-changelog
if: needs.update-changelog.outputs.bump_type != ''
steps:
- name: Dispatch GitHub Release Action
run: |
echo "Dispatching release action with bump type: ${{ needs.update-changelog.outputs.bump_type }}"
curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.everest-preview+json" \
https://api.github.com/repos/${{ github.repository }}/dispatches \
-d '{"event_type": "trigger-release", "client_payload": {"bump_type": "${{ needs.update-changelog.outputs.bump_type }}"}}'