-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (135 loc) · 5.7 KB
/
updateChangelog.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
name: Update Changelog with new commit
permissions:
pull-requests: write
contents: write
on:
pull_request:
# Inputs the workflow accepts.
types: [closed]
jobs:
pull-commit-message:
if: github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
message: ${{ steps.pull.outputs.message }}
steps:
- uses: actions/checkout@v4
- name: get merge commit message
id: pull
run: echo message="$(git log --pretty="format:%b")" >> $GITHUB_OUTPUT
# check-for-changelog-entry
changelog-entry:
# if contains to check for bug, enhancement, breaking change, feature
if: ${{ contains(needs.pull-commit-message.outputs.message, '[BUG]') || contains(needs.pull-commit-message.outputs.message, '[ENHANCEMENT]') || contains(needs.pull-commit-message.outputs.message, '[FEATURE]') || contains(needs.pull-commit-message.outputs.message, '[BREAKING]')}}
runs-on: ubuntu-latest
needs: pull-commit-message
outputs:
optIn: ${{ steps.in.outputs.bool }}
entry: ${{ needs.pull-commit-message.outputs.message }}
steps:
- name: changelog entry opt in
id: in
run: echo "opted in to changelog entry" | echo bool="true" >> $GITHUB_OUTPUT
# if there is a changelog entry, check for PR Open
update-changelog:
if: needs.changelog-entry.outputs.optIn
runs-on: ubuntu-latest
needs: changelog-entry
steps:
- name: Check if PR exists
id: check
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
prs=$(gh pr list \
--repo "$GITHUB_REPOSITORY" \
--json title \
--label "changelog" \
--jq 'length')
if [[ $prs -gt 0 ]]; then
echo "existing=true" >> "$GITHUB_OUTPUT"
fi
- uses: actions/checkout@v4
with:
ref: automated-changelog
- name: Create pull request
#if changelog PR isn't already open, open one
#create a new PR, start with appending the release number and (unreleased)
if: '!steps.check.outputs.existing'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
#new pull request for new release needs the headers all added to the top
FILE="CHANGELOG.md"
version=$(head -n 1 "$FILE")
IFS='.' read major minor patch <<< "$version"
((minor++))
patch=$(echo $patch | sed 's/ (.*)//')
new_version="${major}.$minor.${patch} (Unreleased)"
headers="${new_version}\n\nBREAKING CHANGES:\n\nENHANCEMENTS:\n\nFEATURES:\n\nBUG FIXES:\n"
temp_file=$(mktemp)
echo -e "$headers" > "$temp_file"
cat "$FILE" >> "$temp_file"
mv "$temp_file" "$FILE"
echo "File has been updated."
git add CHANGELOG.md
git commit -m "staring new changelog PR"
git push --set-upstream origin automated-changelog
echo "Creating a new pull request"
gh pr create \
--repo "$GITHUB_REPOSITORY" \
--base main \
--head automated-changelog \
-l "changelog" \
-t "CHANGELOG.md updates" \
-b "changelog for next release"
- name: Add commit message to changelog pull request
# at this point a PR is opened for sure, now add entry
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config user.name "github-actions[bot]"
# place the entry within the correct header
input_string='${{ needs.changelog-entry.outputs.entry }}'
changelog_file="CHANGELOG.md"
# Extract the type (e.g., BUG or ENHANCEMENT) from the input string
if [[ $input_string =~ ^\[(BUG|ENHANCEMENT|FEATURE|BREAKING)\] ]]; then
change_type="${BASH_REMATCH[1]}"
else
echo "Invalid input string format. Expected '[BUG]' or '[ENHANCEMENT]' or '[FEATURE]' or '[BREAKING]'."
exit 1
fi
# Define the header based on the change type
if [[ "$change_type" == "BUG" ]]; then
header="BUG FIXES:"
elif [[ "$change_type" == "ENHANCEMENT" ]]; then
header="ENHANCEMENTS:"
elif [[ "$change_type" == "FEATURE" ]]; then
header="FEATURES:"
elif [[ "$change_type" == "BREAKING" ]]; then
header="BREAKING CHANGES:"
fi
input_string=$(echo $input_string | sed -e 's/^\[BUG\] //' -e 's/^\[ENHANCEMENT\] //' -e 's/^\[FEATURE\] //' -e 's/^\[BREAKING\] //')
# Find the header and insert the input string under the first occurrence
awk -v header="$header" -v input_string="$input_string" '
BEGIN {
found_header = 0
}
# Look for the first occurrence of the header
$0 ~ header && found_header == 0 {
found_header = 1
print $0
print input_string
next
}
# After inserting the input_string, continue printing the rest of the file normally
{ print $0 }
' $changelog_file > "${changelog_file}.tmp"
mv "${changelog_file}.tmp" $changelog_file
echo "The change has been added to the changelog under the first occurrence of $header."
git add CHANGELOG.md
git commit -m "Update changelog"
git push --set-upstream origin automated-changelog