-
Notifications
You must be signed in to change notification settings - Fork 4
/
zanata-release-notes-prepend
executable file
·224 lines (204 loc) · 5.88 KB
/
zanata-release-notes-prepend
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
#!/bin/bash
### NAME
### zanata-release-notes-prepend - Update the Zanata dependencies
###
### SYNOPSIS
### zanata-release-notes-prepend [Options] <module>|<artifact> <version>
###
### DESCRIPTION
### This program prepends release notes of specified version to release-notes.md
### and create new git commit of it.
###
### If the version is already in release-notes. It will do nothing but returns
### EXIT_RETURN_FAIL (40) and quit.
###
### It works on releasing branch.
###
### ARGUMENTS
### <module>: module name like zanata-api
###
### <version>: Version to be prepended
###
### OPTIONS
### -h: Show this help.
###
### -j: Just use current directory.
### This assumes that current directory is <module> work directory.
### and correct branch is checked out.
###
### -v: Verbose mode.
### Show verbose message.
###
### EXIT STATUS
### Returns EXIT_OK (0) when successfully.
###
### Returns EXIT_FATAL_INVALID_OPTIONS (3) when invalid
### options or arguments are given
###
### Returns EXIT_FATAL_MISSING_DEPENDENCY (4)
### when the docs/release-notes.md is missing
###
### Returns EXIT_FATAL_UNKNOWN_MODULE (5) when module does not exists
###
### Returns EXIT_FATAL_FAIL (6) generic failure,
### usually failed to get the resources at the net
###
### Return EXIT_RETURN_FALSE (40) when Version-name exists, but no issues.
###
shopt -s globstar
export LC_ALL=C
set -eu
ScriptDir=$(dirname $(readlink -q -f $0))
FunctionScriptFile=${ScriptDir}/zanata-functions
source "$FunctionScriptFile"
trap exit_print_error EXIT
##== function start ==
## version_greater ver1 ver2
## return 0 when ver1 > ver2
function version_greater(){
local IFS=.
local ver1=($1)
local ver2=($2)
local i=0
for ((i=0; i<${#ver1[@]} ; i++));do
if [ $i -ge ${#ver2[@]} ];then
return 0
elif [ ${ver1[$i]} -gt ${ver2[$i]} ];then
return 0
elif [ ${ver1[$i]} -lt ${ver2[$i]} ];then
return 1
fi
done
return 1
}
##== parsing Start ==
print_status -t "parse" -s "Start"
JustUseCurrentDirectoryMode=0
VerboseMode=0
JiraGetReleaseNoteOptArray=()
while getopts "hjv" opt;do
case $opt in
h )
zanata_script_help $0
exit ${EXIT_OK}
;;
j )
JustUseCurrentDirectoryMode=1
;;
v )
VerboseMode=1
JiraGetReleaseNoteOptArray+=(-v)
;;
* )
failed ${EXIT_FATAL_INVALID_OPTIONS} "$opt"
;;
esac
done
shift $((OPTIND-1))
export JustUseCurrentDirectoryMode
## Get Module
branch_prepare RELEASING "$@"
shift $ShiftOffset
print_status " Module=$Module"
ArtifactId=$(get_artifact_id $Module)
Version=${1-}
[[ -z $Version ]] && failed $EXIT_FATAL_INVALID_OPTIONS "Please specify Version"
if [[ $JustUseCurrentDirectoryMode -eq 0 ]];then
cd "$WORK_ROOT/$Module"
fi
##== prepare Start ==
print_status -t prepare -s "Start"
ReleaseNotesFile=docs/release-notes.md
if [[ ! -r $ReleaseNotesFile ]];then
failed $EXIT_FATAL_MISSING_DEPENDENCY "docs/release-notes.md does not exist"
fi
##== retrieve Jira Start ==
print_status -t "jira" -s "Start"
mkdir -p "$TMP_ROOT"
TmpFileJira=`mktemp ${TMP_ROOT}/zanata-release-notes.jira.XXXXXXXX`
set +e
$ScriptDir/zanata-jira-get-release-notes ${JiraGetReleaseNoteOptArray[@]-} "$ArtifactId-$Version" > $TmpFileJira
ExitCode=$?
set -e
case $ExitCode in
$EXIT_OK )
;;
$EXIT_RETURN_FALSE )
;;
* )
rm -f $TmpFileJira
failed $ExitCode " Failed to get related jira issues"
;;
esac
##== Split Start ==
print_status -t "release-notes parse" -s "Start"
## greater: greater version
TmpFileGreater=`mktemp ${TMP_ROOT}/zanata-release-notes.greater.XXXXXXXX`
TmpFileThis=`mktemp ${TMP_ROOT}/zanata-release-notes.this.XXXXXXXX`
TmpFileLesser=`mktemp ${TMP_ROOT}/zanata-release-notes.lesser.XXXXXXXX`
Mode=g
Keep=0
while IFS= read -r l ;do
line=$(sed -e 's/\s*$//' <<<"$l")
if [ "$line" = "## $Version" ];then
Mode=t
elif [[ "$line" =~ ^##\ [0-9] ]];then
v=$(sed -e 's/^## \(.*\)\s*$/\1/'<<<"$line")
if version_greater $v $Version;then
Mode=g
else
Mode=l
fi
fi
case $Mode in
g )
echo "$line" >> $TmpFileGreater
;;
t )
if [[ $Mode = "t" && "$line" =~ ^##\ [0-9] ]];then
Keep=0
elif [[ "$line" =~ ^###*\ [Bb]ug\ [Ff]ix ]];then
## Do not keep "Bug fixes" and "Changes"
Keep=0
elif [[ "$line" =~ ^###*\ [Cc]hanges? ]];then
Keep=0
elif [[ "$line" =~ ^###*\ ]];then
Keep=1
elif [[ "$line" =~ ^----* ]];then
## Remove split line
Keep=0
fi
if [ $Keep -eq 1 ];then
echo "$line" >> $TmpFileThis
fi
;;
l )
echo "$line" >> $TmpFileLesser
;;
esac
if [ $VerboseMode -eq 1 ];then
echo "$Mode$Keep=$line"
fi
done < <(cat $ReleaseNotesFile)
##== Combine Start ==
print_status -t "release-notes combine" -s "Start"
TmpFile=`mktemp ${TMP_ROOT}/zanata-release-notes.XXXXXXXX`
if [ -s $TmpFileGreater ];then
cat $TmpFileGreater > $TmpFile
fi
echo "## $Version">> $TmpFile
if [ -s $TmpFileThis ];then
cat $TmpFileThis >> $TmpFile
fi
if [ -s $TmpFileJira ];then
cat $TmpFileJira >> $TmpFile
fi
echo -e "\n-----------------------\n" >> $TmpFile
cat $TmpFileLesser >> $TmpFile
mv $TmpFile $ReleaseNotesFile
rm -f $TmpFileJira $TmpFileGreater $TmpFileThis $TmpFileLesser
if git diff --exit-code ;then
failed ${EXIT_RETURN_FALSE} " $ReleaseNotesFile does not need to change."
fi
git commit -m "doc(release): Update $ReleaseNotesFile for version $Version" -- $ReleaseNotesFile
exit ${EXIT_OK}