-
Notifications
You must be signed in to change notification settings - Fork 0
/
chanjelog
227 lines (190 loc) · 6.63 KB
/
chanjelog
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
#!/bin/bash
version=1.3.0
set -o errexit -o nounset -o pipefail
usage="
___ _ _ __
/ __\ |__ __ _ _ __ (_) ___ / / ___ __ _
/ / | '_ \ / _' | '_ \ | |/ _ \/ / / _ \ / _' |
/ /___| | | | (_| | | | || | __/ /__| (_) | (_| |
\____/|_| |_|\__,_|_| |_|/ |\___\____/\___/ \__, |
|__/ |___/
v: $version - https://github.con/sayjeyhi/chanjelog
flag | description
-----------------------------------------------------
-h | show help
-r | *{string} git repo url e.g: git.com/project.git
-f | {string} a file path to set version init with -s search format e.g: 'docker-compose.yml'
-s | {string} search pattern in version file e.g: '- DEPLOY_VERSION=[0-9]*\.[0-9]*\.[0-9]*'
-p | {string} search replace pattern in version file e.g: '- DEPLOY_VERSION='
-u | {string} jira tasks prefix to automaticly convery #[0-9]* to jira links in commit messages e.g: 'https:\/\/jira.site.com\/browse\/SP-'
-g | {boolean} to control adding tag on git or not e.g: [1|0]
-v | {string} to control version increase type e.g: ['patch'|'minor'|'major'|'prere'|'build']
-c | {boolean} to control update changeslog.md file e.g: [1|0]
You should have CI_USER and CI_ACCESS_TOKEN variables to allow ci to update your git.
---
An open source hack by @sayjeyhi - https://sayjeyhi.com with SemVer2.0.0
"
# Helper vairables
NAT='0|[1-9][0-9]*'
ALPHANUM='[0-9]*[A-Za-z-][0-9A-Za-z-]*'
IDENT="$NAT|$ALPHANUM"
FIELD='[0-9A-Za-z-]+'
dir="$(pwd)"
NOW=$(date +'%Y-%m-%d %H:%M:%S')
ADD_GIT_TAG=1
GENERATE_CHANGES_LOG=1
VERSION_BUMP_TYPE="patch"
SEMVER_REGEX="\
^[vV]?\
($NAT)\\.($NAT)\\.($NAT)\
(\\-(${IDENT})(\\.(${IDENT}))*)?\
(\\+${FIELD}(\\.${FIELD})*)?$"
# validate supplied version
function validate-version {
local version=$1
if [[ "$version" =~ $SEMVER_REGEX ]]; then
# if a second argument is passed, store the result in var named by $2
if [ "$#" -eq "2" ]; then
local major=${BASH_REMATCH[1]}
local minor=${BASH_REMATCH[2]}
local patch=${BASH_REMATCH[3]}
local prere=${BASH_REMATCH[4]}
local build=${BASH_REMATCH[8]}
eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")"
else
echo "$version"
fi
else
echo "version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. See help for more information."
fi
}
# generate new version
function generateVersion {
local new; local version; local sub_version; local command;
case $# in
2) case $1 in
major|minor|patch|release) command=$1; version=$2;;
*) usage-help;;
esac ;;
3) case $1 in
prerel|build) command=$1; sub_version=$2 version=$3 ;;
*) usage-help;;
esac ;;
*) usage-help;;
esac
validate-version "$version" parts
# shellcheck disable=SC2154
local major="${parts[0]}"
local minor="${parts[1]}"
local patch="${parts[2]}"
local prere="${parts[3]}"
local build="${parts[4]}"
case "$command" in
major) new="$((major + 1)).0.0";;
minor) new="${major}.$((minor + 1)).0";;
patch) new="${major}.${minor}.$((patch + 1))";;
release) new="${major}.${minor}.${patch}";;
prerel) new=$(validate-version "${major}.${minor}.${patch}-${sub_version}");;
build) new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}");;
*) echo "Invalid param";;
esac
echo "$new"
exit 0
}
if [ -z "$1" ] && [ "$1" == "-h" ]; then
echo "$usage"
exit 0
fi
while getopts :f:u:g:v:c:r:s:p: flag
do
# shellcheck disable=SC2220
case "${flag}" in
f) VERSION_CHANGE_FILE=${OPTARG};;
u) URL_PREFIX=${OPTARG};;
g) ADD_GIT_TAG=${OPTARG};;
v) VERSION_BUMP_TYPE=${OPTARG};;
c) GENERATE_CHANGES_LOG=${OPTARG};;
r) GIT_REPO=${OPTARG};;
s) FILE_SEARCH_PATTERN=${OPTARG};;
p) FILE_REPLACE_PATTERN=${OPTARG};;
:) printf "missing argument for -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
echo "$usage" >&2
exit 1
;;
esac
done
echo "$usage"
# update current local git
echo "✔ Pulling changes from repo"
git pull origin master > /dev/null 2>&1
echo "✔ Calculating lastest version and new version"
CURRENT_GIT_VERSION=$(git tag| sort -V | tail -n1 || '1.0.0')
NEW_GIT_VERSION=$(generateVersion "$VERSION_BUMP_TYPE" "$CURRENT_GIT_VERSION")
NEW_TAG_CHANGES=$(git log --no-merges --pretty=format:"- %s (**%cn** - %cr)" HEAD..."$CURRENT_GIT_VERSION")
# Main script content
function main {
# Make changes log file
if [ "$GENERATE_CHANGES_LOG" != "0" ]; then
echo "✔ Generating changeslog file"
CHANGES_LOG_FILE="$dir/changeslog.md"
CHANGES_LOG_TEMP_FILE="$dir/_changeslog.md"
CHANGES_LOG_CONTENT=""
if [ -e "$CHANGES_LOG_FILE" ]
then
CHANGES_LOG_CONTENT=$(cat "$CHANGES_LOG_FILE")
fi
echo "## $NEW_GIT_VERSION" > "$CHANGES_LOG_TEMP_FILE"
{
echo "$NOW"
echo ""
echo "$NEW_TAG_CHANGES"
echo ""
echo ""
echo "$CHANGES_LOG_CONTENT"
} >> "$CHANGES_LOG_TEMP_FILE"
echo "✔ Writing changeslog file"
mv "$CHANGES_LOG_TEMP_FILE" "$CHANGES_LOG_FILE"
# Find task ids and link to them
if [ "${URL_PREFIX+1}" ];then
sed -i -e "s/#[0-9]/$URL_PREFIX/" "$CHANGES_LOG_FILE"
fi
# add file to git
git add changeslog.md
fi
# Update docker env to use version in runtime
if [ "${VERSION_CHANGE_FILE+1}" ];then
VERSION_FILE="$dir/$VERSION_CHANGE_FILE"
echo "✔ Updating version $NEW_GIT_VERSION on $VERSION_CHANGE_FILE"
sed -i -e "s/$FILE_SEARCH_PATTERN/$FILE_REPLACE_PATTERN$NEW_GIT_VERSION/g" "$VERSION_FILE"
fi
# Update remote branch
declare -a commitParams
commitParams=(changeslog.md )
if [ "${VERSION_CHANGE_FILE+1}" ];then
# shellcheck disable=SC2206
commitParams+=($VERSION_CHANGE_FILE)
fi
commitParams+=(-m "🎉 Release v$NEW_GIT_VERSION [skip ci]")
echo "✔ Committing changes..."
git commit "${commitParams[@]}" > /dev/null 2>&1
echo "✔ Pushing changes to git repo..."
git push "http://$CI_USER:$CI_ACCESS_TOKEN@$GIT_REPO" HEAD:master -o ci.skip --follow-tags > /dev/null 2>&1
# add release tag and note
if [ "${ADD_GIT_TAG+1}" ] && [ "$ADD_GIT_TAG" != "0" ]; then
echo "✔ Adding tag $NEW_GIT_VERSION to git..."
git tag -a "$NEW_GIT_VERSION" -m "$NEW_TAG_CHANGES"
git push "http://$CI_USER:$CI_ACCESS_TOKEN@$GIT_REPO" HEAD:master --tags -o ci.skip > /dev/null 2>&1
fi
# Show message of push
echo "🚀 Version $NEW_GIT_VERSION pushed successfully"
}
echo " 🦑 Cuurent version: $CURRENT_GIT_VERSION > new version: $NEW_GIT_VERSION"
if [ -z "$NEW_TAG_CHANGES" ]; then
echo "✔ No changes detected."
else
main
fi