-
Notifications
You must be signed in to change notification settings - Fork 0
/
release
executable file
·201 lines (163 loc) · 6.65 KB
/
release
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
#!/bin/sh
#
# handle release of a new version from tip-of-branch in GitHub and Jira:
#
# * tag tip of current branch with the new version
# * push the tag to GitHub
# * find Jira tags in commit-subjects since the last release and update their fix version
#
# usage: $0 <JIRA-PROJECT> <JIRA-release> [start-commit] [end-commit]
#
# start-commit defaults to the most recent tag, end-commit to tip-of-branch
#
# credentials for Jira are pulled from the MacOS keychain stored with the
# service string "jira-apitoken". The JIRA email address must be stored in the
# account field; the API token must be stored in the password field. Alternatively,
# username may be stored in the environment variable JIRA_USERNAME or USER; API token
# may be stored in the environment variable JIRA_APITOKEN.
#
# Generate an Atlassian API token at https://id.atlassian.com/manage-profile/security/api-tokens
#
quit()
{
if [[ -n $1 ]]; then echo $1; fi;
echo "usage: $0 <JIRA-NAME> <version-number> [start-hash] [end-hash]"
echo "start-hash defaults to the most recent tag, end-hash to tip-of-current-branch"
# //$2 && rm -rf $2;
exit 1;
}
#
# getAttr - retrieve attribute from MacOS security service
# $1 service-name, e.g. jira-apitoken
# $2 field, e.g. acct
#
getAttr()
{
security find-generic-password -s "$1" | grep $2 | cut -d\" -f 4
}
#
# getPassword - retrieve password from MacOS security service
# $1 service-name, e.g. jira-apitoken
# $2 account, e.g. someusername
#
getPassword()
{
security find-generic-password -s "$1" -a "$2" -w
}
JIRA=$1 # jira project, e.g. STCOR
TAG=$2 # release as tagged on Jira, e.g. 1.2.3
HASH_FROM=$3 # retrieve commits from here...
HASH_TO=$4 # ... to here
# check on required arguments
if [[ -z $JIRA || -z $TAG ]]; then
quit
fi
# check on required helpers: git, jq, curl
if ! [ -x "$(command -v git)" ]; then
quit "git is required but could not be found in your path"
fi
if ! [ -x "$(command -v jq)" ]; then
quit "jq is required but could not be found in your path"
fi
if ! [ -x "$(command -v curl)" ]; then
quit "curl is required but could not be found in your path"
fi
# jira version must match npm version
NPM_VERSION=`grep version package.json | cut -d\" -f 4`
if [ ! "$NPM_VERSION" = "$TAG" ]; then
quit "The npm version ($NPM_VERSION) and Jira version ($TAG) must match."
fi
# validate optional arguments
# hash-from defaults to the common ancestor of the last tag and HEAD
if [[ -z $HASH_FROM ]]; then
LAST_TAG=`git tag -l --sort=creatordate | egrep '^v' | tail -1`
HASH_FROM=`git merge-base $LAST_TAG HEAD`
fi
if [ ! git show $HASH_FROM > /dev/null 2>&1 ]; then
quit "Sorry; $HASH_FROM was not recognized as a tag or commit hash."
fi
# hash-to defaults to HEAD
if [[ -z $HASH_TO ]]; then
HASH_TO=`git rev-parse --abbrev-ref HEAD`
fi
if [ ! git show $HASH_TO > /dev/null 2>&1 ]; then
quit "Sorry; $HASH_TO was not recognized as a tag, commit hash, or branch-name."
fi
GHTAG="v$TAG" # release as tagged in git, e.g. v1.2.3
# jira username from keychain, else $JIRA_USERNAME, else $USER
JIRA_USER=`getAttr "jira-apitoken" "acct"`
if [[ -z $JIRA_USER ]]; then JIRA_USER=$JIRA_USERNAME; fi
if [[ -z $JIRA_USER ]]; then JIRA_USER=$USER; fi
JIRA_PASS=`getPassword "jira-apitoken" "$JIRA_USER"`
if [[ -z $JIRA_PASS ]]; then JIRA_PASS=$JIRA_APITOKEN; fi
if [[ -z $JIRA_USER || -z $JIRA_PASS ]]; then
quit "could not find jira credentials"
fi
# extract the repo name from the GitHub URL
REPO=`git config remote.origin.url | sed -E 's/.*\/([^\/]+)\.git$/\1/'`
# get JIRAs from commit headers between previous and current releases
TICKETS=`git log --pretty=oneline ${HASH_FROM}..${HASH_TO} | cut -c42- | egrep "^$JIRA\-[0-9]+" | sed -E "s/^($JIRA\-[0-9]+).*/\1/g" | sort | uniq`
# base64 auth token for Jira
JIRA_AUTH_HEADER=`printf "${JIRA_USER}:${JIRA_PASS}" | base64`
echo "tagging release $GHTAG and pushing it to GitHub..."
git tag -s $GHTAG -m "Release $GHTAG"
git push -q
git push --tags -q
# create Jira version if it doesn't exist
VERSIONS=`curl -s --location -X GET https://folio-org.atlassian.net/rest/api/2/project/$JIRA/versions`
echo $VERSIONS | jq -r '.[].name ' | grep -q $TAG
if [ ! "$?" = "0" ]; then
echo "Creating Jira version $TAG"
PROJECT_ID=`echo $VERSIONS | jq -r '.[].projectId' | uniq`
curl -s --request POST \
--url 'https://folio-org.atlassian.net/rest/api/2/version' \
-H "Authorization: Basic $JIRA_AUTH_HEADER" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data "{
\"archived\": false,
\"description\": \"$TAG\",
\"name\": \"$TAG\",
\"projectId\": $PROJECT_ID
}" > /dev/null
else
echo "Jira version $TAG already exists!"
fi
# update fix-version for each ticket where resolution is "Done"
for ticket in $TICKETS; do
NEW_VERSIONS="{\"name\": \"$TAG\"}";
TICKET_JSON=`curl -s --location -X GET https://folio-org.atlassian.net/rest/api/2/issue/$ticket > ./$ticket`
RESOLUTION=`cat $ticket | jq -r '.fields.resolution.name '`
STATUS=`cat $ticket | jq -r '.fields.status.name '`
if [[ "$RESOLUTION" == "Done" && ( "$STATUS" == "Closed" || "$STATUS" == "Awaiting release" ) ]]; then
OLD_VERSIONS=`cat $ticket | jq -r '.fields.fixVersions[] | .name '`
if [[ -n $OLD_VERSIONS ]]; then
for version in $OLD_VERSIONS; do
NEW_VERSIONS="${NEW_VERSIONS}, {\"name\": \"$version\"}";
done
fi
curl -s --location \
-X PUT "https://folio-org.atlassian.net/rest/api/2/issue/$ticket" \
-H "content-type: application/json" \
-H "Authorization: Basic $JIRA_AUTH_HEADER" \
-d "{\"fields\": { \"fixVersions\": [$NEW_VERSIONS] } }"
echo "added $TAG to $ticket"
else
echo "skipping $ticket; $RESOLUTION/$STATUS is not Done/Closed";
fi
if [ -f $ticket ]; then rm $ticket; fi;
done;
# mark jira version as "released"
echo "Releasing Jira version $TAG"
RELEASE_DATE=`date +%Y-%m-%d`
# re-retrieve versions; if this version was just created then it isn't
# included in the original response.
VERSIONS=`curl -s --location -X GET https://folio-org.atlassian.net/rest/api/2/project/$JIRA/versions`
JIRA_VERSION=`echo $VERSIONS | jq -c ".[] | select(.name == \"$TAG\") | .released = true | .releaseDate = \"$RELEASE_DATE\" | del(.userReleaseDate) " | sed 's/\"/\\"/g'`
JIRA_VERSION_URL=`echo $VERSIONS | jq -c ".[] | select(.name == \"$TAG\") | .self"`
curl -s --location \
-X PUT $JIRA_VERSION_URL \
-H "content-type: application/json" \
-H "Authorization: Basic $JIRA_AUTH_HEADER" \
-d "$JIRA_VERSION" > /dev/null
echo 'done!'