-
Notifications
You must be signed in to change notification settings - Fork 0
/
release-version.sh
executable file
·260 lines (217 loc) · 9.25 KB
/
release-version.sh
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
set -euo pipefail
DEBUG="${DEBUG:-0}"
if [[ "1" = "${DEBUG}" ]]; then
set -x
fi
DRY_RUN="${DRY_RUN:-0}"
if [[ "1" = "${DRY_RUN}" ]]; then
echo "Dry running.."
fi
if [[ "" = "$CURRENT_VERSION" ]]; then
CURRENT_VERSION="$(git describe --abbrev=0 --tags | sed -E 's/v(.*)/\1/' || echo "0.0.0")"
fi
# Safe check - skips relese commit generation when already tagged commit
if [[ $(git name-rev --name-only --tags HEAD) = "v$CURRENT_VERSION" ]]; then
echo "Already tagged or no new commits introduced. Skipping.."
exit 0
fi
# Set GH env variables
GH_COMMITER_NAME="${GH_COMMITER_NAME:-symfony-swoole-bot}"
GH_COMMITER_EMAIL="${GH_COMMITER_EMAIL:-147276322+symfony-swoole-bot@users.noreply.github.com}"
GH_COMMITER_SIGNING_KEY="${GH_COMMITER_SIGNING_KEY:-xxxx}"
GH_REPOSITORY="${GH_REPOSITORY:-symfony-swoole/swoole-bundle}"
GH_TOKEN="${GH_TOKEN:?"Provide \"GH_TOKEN\" variable with GitHub Personal Access Token"}"
# Configure git
git config user.name "${GH_COMMITER_NAME}"
git config user.email "${GH_COMMITER_EMAIL}"
git config user.signingkey "${GH_COMMITER_SIGNING_KEY}"
git config commit.gpgsign true
GIT_COMMIT_MESSAGE_FIRST_LINE="$(git show-branch --no-name HEAD)"
GIT_COMMIT_MESSAGE_RELEASE_COMMIT_MATCHED="$(echo "$GIT_COMMIT_MESSAGE_FIRST_LINE" | sed -E 's/^chore\(release\)\: v([a-zA-Z0-9\.\-]+) \:tada\:/\1/')"
# If sed matches, it means it is a release commit, otherwise strings should be equal
if [[ "$GIT_COMMIT_MESSAGE_FIRST_LINE" != "$GIT_COMMIT_MESSAGE_RELEASE_COMMIT_MATCHED" ]]; then
NEW_VERSION="$GIT_COMMIT_MESSAGE_RELEASE_COMMIT_MATCHED"
RELEASE_TAG="v$NEW_VERSION"
echo "Matched release commit: $GIT_COMMIT_MESSAGE_FIRST_LINE"
echo "Releasing version: $NEW_VERSION"
GH_RELEASE_NOTES="$(conventional-changelog -p angular | awk 'NR > 3 { print }')"
if [ "" = "$(echo -n "$GH_RELEASE_NOTES" | tr '\n' ' ')" ]; then
GH_RELEASE_NOTES="### Miscellaneous
* Minor fixes"
fi
# Create and push tag
git remote add authorized "https://${GH_COMMITER_NAME}:${GH_TOKEN}@github.com/${GH_REPOSITORY}.git"
if [ "0" = "$DRY_RUN" ]; then
git tag "$RELEASE_TAG"
git push authorized "$RELEASE_TAG"
else
echo "Pushing $RELEASE_TAG.."
fi
git remote remove authorized
# Make github release
GH_RELEASE_DRAFT="${GH_RELEASE_DRAFT:-false}"
GH_RELEASE_PRERELEASE="${GH_RELEASE_PRERELEASE:-false}"
GH_RELEASE_DESCRIPTION="## Changelog
[Full changelog](https://github.com/${GH_REPOSITORY}/compare/v${CURRENT_VERSION}...v${NEW_VERSION})
${GH_RELEASE_NOTES}
## Installation
\`\`\`sh
composer require ${GH_REPOSITORY} ^${NEW_VERSION}
\`\`\`
"
GH_RELEASE_DESCRIPTION_ESCAPED="${GH_RELEASE_DESCRIPTION//\"/\\\"}"
GH_RELEASE_DESCRIPTION_ESCAPED="${GH_RELEASE_DESCRIPTION_ESCAPED//$'\n'/\\n}"
GH_RELEASE_REQUEST_BODY="{
\"tag_name\": \"${RELEASE_TAG}\",
\"target_commitish\": \"master\",
\"name\": \"${RELEASE_TAG}\",
\"body\": \"${GH_RELEASE_DESCRIPTION_ESCAPED}\",
\"draft\": ${GH_RELEASE_DRAFT},
\"prerelease\": ${GH_RELEASE_PRERELEASE}
}"
if [ "0" = "$DRY_RUN" ]; then
curl -s -u "${GH_COMMITER_NAME}:${GH_TOKEN}" -X POST "https://api.github.com/repos/${GH_REPOSITORY}/releases" \
-H "Content-Type: application/vnd.github.v3+json" \
--data "${GH_RELEASE_REQUEST_BODY}" | jq
else
echo "Release description:"
echo "⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽"
echo "${GH_RELEASE_DESCRIPTION}"
echo "⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺"
echo "Release request body:"
echo "⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽"
echo "${GH_RELEASE_REQUEST_BODY}"
echo "⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺"
fi
exit 0
fi
# Guess new version number
RECOMMENDED_BUMP=$(conventional-recommended-bump -p angular)
# Split version by dots
V[0]=""
V[1]=""
V[2]=""
IFS='.' read -r -a V <<< "$CURRENT_VERSION"
# Ignore postfix like "-dev"
V[2]=$(( V[2]+1 ))
V[2]=$(( V[2]-1 ))
OLD_VERSION_SEM="${V[0]}.${V[1]}.${V[2]}"
# When version is 0.x.x it is allowed to make braking changes on minor version
if [[ "0" = "${V[0]}" ]] && [[ "${RECOMMENDED_BUMP}" = "major" ]]; then
RECOMMENDED_BUMP="minor";
fi;
# Increment semantic version numbers major.minor.patch
if [[ "${RECOMMENDED_BUMP}" = "major" ]]; then
V[0]=$(( V[0]+1 ));
V[1]=0;
V[2]=0;
elif [[ "${RECOMMENDED_BUMP}" = "minor" ]]; then
V[1]=$(( V[1]+1 ));
V[2]=0;
elif [[ "${RECOMMENDED_BUMP}" = "patch" ]]; then
V[2]=$(( V[2]+1 ));
else
echo "Could not bump version"
exit 1
fi
NEW_VERSION_SEM="${V[0]}.${V[1]}.${V[2]}"
NEW_VERSION=${CURRENT_VERSION//${OLD_VERSION_SEM}/${NEW_VERSION_SEM}}
echo "Preparing release of version: ${NEW_VERSION}"
RELEASE_TAG="v${NEW_VERSION}"
# Save release notes
git tag "${RELEASE_TAG}" > /dev/null 2>&1
GH_RELEASE_NOTES_HEADER="$(conventional-changelog -p angular -r 2 | head -n 5 | tail -n 1)"
git tag -d "${RELEASE_TAG}" > /dev/null 2>&1
GH_RELEASE_NOTES="$(conventional-changelog -p angular | awk 'NR > 3 { print }')"
if [ "" = "$(echo -n "$GH_RELEASE_NOTES" | tr '\n' ' ')" ]; then
GH_RELEASE_NOTES="### Miscellaneous
* Minor fixes"
fi
# Save changelog
CHANGELOG="$GH_RELEASE_NOTES_HEADER
[Full changelog](https://github.com/${GH_REPOSITORY}/compare/v${CURRENT_VERSION}...v${NEW_VERSION})
$GH_RELEASE_NOTES
"
NEXT_LINES="10"
LINES="$(wc -l <<< "$CHANGELOG")"
LINES=$((LINES+NEXT_LINES))
# Update CHANGELOG.md
if [ "0" = "$DRY_RUN" ]; then
echo -e "$CHANGELOG\n$(cat CHANGELOG.md)" > CHANGELOG.md
else
echo "Changelog file: (first $LINES lines)"
echo "⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽"
echo "$CHANGELOG"
head -n "$NEXT_LINES" < CHANGELOG.md
echo ""
echo "⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺"
fi
# Create release commit
COMMIT_MESSAGE="chore(release): ${RELEASE_TAG} :tada:
$(conventional-changelog | awk 'NR > 1 { print }')
"
if [ "0" = "$DRY_RUN" ]; then
# init and run GPG
echo "${GH_COMMITER_PGP_PRIVATE_KEY}" | \
gpg --no-default-keyring --keyring trustedkeys.gpg --import
# commit changes with GPG signature
git add CHANGELOG.md
git commit -m "${COMMIT_MESSAGE}"
else
echo "Commit message:"
echo "⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽"
echo "${COMMIT_MESSAGE}"
echo "⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺"
fi
# Create pull requests
RELEASE_BRANCH="${RELEASE_BRANCH:-"master"}"
DEFAULT_BRANCH="${DEFAULT_BRANCH:-"develop"}"
BACKPORT_RELEASE_COMMIT_BRANCH_TEMPLATE="chore/release-$RELEASE_TAG"
PR_BASES="${PR_BASES:-"$RELEASE_BRANCH $DEFAULT_BRANCH"}"
git remote add authorized "https://${GH_COMMITER_NAME}:${GH_TOKEN}@github.com/${GH_REPOSITORY}.git"
for PR_BASE in $PR_BASES; do
HEAD_BRANCH="$BACKPORT_RELEASE_COMMIT_BRANCH_TEMPLATE-$PR_BASE"
GH_PR_BODY="$CHANGELOG
----
## Fast-forward merge instructions
1. Approve PR
2. Then run these commands in your local git repository:
\`\`\`sh
git fetch --all
git switch $PR_BASE
git pull origin $PR_BASE
git merge origin/$HEAD_BRANCH --ff-only
git push origin $PR_BASE
\`\`\`
"
GH_PR_BODY_ESCAPED="${GH_PR_BODY//\"/\\\"}"
GH_PR_BODY_ESCAPED="${GH_PR_BODY_ESCAPED//$'\n'/\\n}"
GH_PULL_REQUEST_TITLE="chore(release): ${RELEASE_TAG} [$PR_BASE]"
GH_PULL_REQUEST_BODY="{
\"title\": \"${GH_PULL_REQUEST_TITLE}\",
\"body\": \"${GH_PR_BODY_ESCAPED}\",
\"head\": \"${HEAD_BRANCH}\",
\"base\": \"${PR_BASE}\"
}"
if [ "0" = "$DRY_RUN" ]; then
git push authorized "HEAD:refs/heads/$HEAD_BRANCH"
curl -s -u "${GH_COMMITER_NAME}:${GH_TOKEN}" -X POST "https://api.github.com/repos/${GH_REPOSITORY}/pulls" \
-H "Content-Type: application/vnd.github.v3+json" \
--data "${GH_PULL_REQUEST_BODY}" | jq
else
echo "Push release commit to head branch '$HEAD_BRANCH"
echo "Create release pull request to '$PR_BASE' from head branch '$HEAD_BRANCH'"
echo "Pull request title: $GH_PULL_REQUEST_TITLE"
echo "Pull request body:"
echo "⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽"
echo "${GH_PULL_REQUEST_BODY}"
echo "⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺"
fi
done
git remote remove authorized
git config --unset user.name
git config --unset user.email
git config --unset user.signingkey
git config --unset commit.gpgsign
echo "Please approve and fast-forward merge release pull requests!"