-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelease-changelog
executable file
·94 lines (78 loc) · 2.69 KB
/
release-changelog
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
#!/bin/bash
set -e
if [[ -z "${INPUT_TAG_REGEX}" ]]; then
echo "No 'tag_regex' input was specified"
exit 1
fi
if [[ -z "${INPUT_VERSION_BUMP}" ]]; then
echo "No 'version_bump' input was specified. Skipping tagging commit."
fi
TAG_REGEX=${INPUT_TAG_REGEX}
VERSION_BUMP=${INPUT_VERSION_BUMP}
git_fetch() {
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
git fetch --depth=1 origin +refs/tags/*:refs/tags/* # Fetch all tags to get the previous tag in the next step.
git fetch --prune --unshallow # Fetch all history to get all commits in the release body.
}
git_previous_tag() {
local previous_tag
# shellcheck disable=SC2046 disable=SC2086
previous_tag=$(eval git tag --sort=version:refname | grep -v $(git tag --contains $(git rev-parse HEAD) | grep -E ${TAG_REGEX}) | grep -E "${TAG_REGEX}" | tail -n1)
# If there is no tag, just get the first commit on repo
[[ -z ${previous_tag} ]] && previous_tag=$(git rev-list --max-parents=0 HEAD)
echo "${previous_tag}"
}
git_generate_changelog() {
local prev_tag release_body
prev_tag=${1}
release_body=$(git log --no-merges "${prev_tag}".."${GITHUB_SHA}" --format='* [%h] %s (%an)')
# See: https://github.community/t5/GitHub-Actions/set-output-Truncates-Multiline-Strings/m-p/38372#M3322
release_body="${release_body//'%'/'%25'}"
release_body="${release_body//$'\n'/'%0A'}"
release_body="${release_body//$'\r'/'%0D'}"
echo ::set-output name=release_body::"## Changelog%0A${release_body}"
}
git_next_tag() {
local version
version=$(git describe --abbrev=0 --tags "$(git rev-list --tags --skip=0 --max-count=1)")
IFS=. read -r major minor patch <<<"$version"
# Remove "v" from major
major=${major#?}
case $VERSION_BUMP in
major)
((major++))
minor=0
patch=0
;;
minor)
((minor++))
patch=0
;;
patch)
((patch++))
;;
*)
echo -n "Version bump input can be: major, minor or patch."
exit 1
;;
esac
printf -v version 'v%d.%d.%d' "$((major))" "$((minor))" "$((patch))"
echo "${version}"
}
git_tag_commit() {
local new_tag
new_tag=${1}
echo "Tagging latest commit with ${new_tag}"
git tag "${new_tag}"
git push origin "${new_tag}"
}
git_output_latest_tag() {
echo ::set-output name=release_tag::"$(git describe --abbrev=0 --tags "$(git rev-list --tags --skip=0 --max-count=1)")"
}
main() {
git_fetch
[[ -n ${VERSION_BUMP} ]] && git_tag_commit "$(git_next_tag)"
git_generate_changelog "$(git_previous_tag)"
git_output_latest_tag
}
main "$@"