forked from claranet/terraform-datadog-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangelog.sh
executable file
·145 lines (131 loc) · 6.09 KB
/
changelog.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
#/bin/bash
set -euo pipefail
source "$(dirname $0)/utils.sh"
goto_root
## Check parameters and environment variables
# PARAMETER: The next version to release (i.e. v3.0.0)
# JIRA_API_TOKEN: the user api token to login jira
# JIRA_LOGIN: the user email to login on jira
if [ $# -eq 0 ]; then
echo "Target tag is required as parameter"
exit 1
fi
if [ -z ${JIRA_API_TOKEN:-} ]; then
echo "Environment variable JIRA_API_TOKEN needs to be defined: https://confluence.atlassian.com/cloud/api-tokens-938839638.html"
exit 2
fi
if [ -z ${JIRA_LOGIN:-} ]; then
echo "Environment variable JIRA_LOGIN needs to be defined: jira user email"
exit 3
fi
if ! command -v jira >/dev/null; then
echo "go-jira command is required: https://github.com/go-jira/jira#install"
fi
TAG_TARGET=$1
TAG_SOURCE=${TAG_SOURCE:-$(git describe --tags --abbrev=0)}
JIRA_ENDPOINT=${JIRA_ENDPOINT:-https://claranet-morea.atlassian.net}
JIRA_STATUS=${JIRA_STATUS:-Done Closed}
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
TMP=$(mktemp -d)
# Clean when exit
err() {
rm -fr ${TMP}
IFS=$SAVEIFS
}
trap 'err $LINENO' ERR TERM EXIT INT
# Create the go-jira template with issue, status and summary as CSV
mkdir -p ${HOME}/.jira.d/templates
cat > ${HOME}/.jira.d/templates/changelog <<EOF
{{ .fields.issuetype.name }};{{ .key }};{{if .fields.status -}}{{ .fields.status.name }}{{end -}};{{ .fields.summary }}
EOF
# Create the go-jira template to list allowed versions
cat > ${HOME}/.jira.d/templates/versions <<EOF
{{ range .fields.fixVersions.allowedValues }}{{.name}}
{{end}}
EOF
# Create the go-jira template to edit issue fixing version
cp -f ${HOME}/.jira.d/templates/edit ${HOME}/.jira.d/templates/fixversion
cat >> ${HOME}/.jira.d/templates/fixversion <<EOF
{{if .meta.fields.fixVersions -}}
{{if .meta.fields.fixVersions.allowedValues}}
fixVersions: # Values: {{ range .meta.fields.fixVersions.allowedValues }}{{.name}}, {{end}}{{if .overrides.fixVersions}}{{ range (split "," .overrides.fixVersions)}}
- name: {{.}}{{end}}{{else}}{{range .fields.fixVersions}}
- name: {{.name}}{{end}}{{end}}
{{- end -}}
{{- end -}}
EOF
# List all issues in commits from previous tag
TMP_ISSUES=$TMP/issues.txt
for commit in $(git log $(git describe --tags --abbrev=0)..HEAD --oneline); do
[[ $commit =~ ^.*(MON-[0-9]+).*$ ]] && echo "${BASH_REMATCH[1]}"
done | sort -u > $TMP_ISSUES
TMP_INFO_ISSUES=${TMP}/info-issues.txt
for issue in $(cat $TMP_ISSUES); do
# retrieve jira information from go-jira template
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} view $issue --template=changelog
done | sort > $TMP_INFO_ISSUES
TMP_TREATED_ISSUES=${TMP}/treated-issues.txt
TMP_CHANGELOG=${TMP}/changelog.md
# init changelog for next version
echo -e "\n# $TAG_TARGET ($(LANG=eng date +"%B %d, %Y"))" >> $TMP_CHANGELOG
for line in $(cat $TMP_INFO_ISSUES); do
# retrieve jira information from go-jira template
IFS=';' read -r type issue status summary <<< $line
# Ignore if issue is not in required status
if ! [[ "$JIRA_STATUS" == *"$status"* ]]; then
echo "Issue $issue with status \"$status\" not in [$JIRA_STATUS] ($summary)"
read -p 'Would you like to transition issue to "Done" status ? if no, the issue will be ignored ([y]/n): ' -r answer
if [[ "$answer" != "n" ]]; then
if [[ "$status" == "Qualif" ]]; then
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} transition "Approve" $issue --noedit > /dev/null
status="To Do"
fi
if [[ "$status" == "To Do" ]]; then
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} transition "In Progress" $issue --noedit > /dev/null
status="In Progress"
fi
if [[ "$status" == "In Progress" ]]; then
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} transition "Review" $issue --noedit > /dev/null
status="In Review"
fi
if [[ "$status" == "In Review" ]]; then
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} transition "Done" $issue --noedit > /dev/null
fi
fi
fi
# add line for type only once
if ! grep -q "^## $type" $TMP_CHANGELOG; then
echo -e "\n## $type\n" >> $TMP_CHANGELOG
fi
# add jira issue line to changelog
echo "* [[$issue](${JIRA_ENDPOINT}/browse/${issue})] - $summary" >> $TMP_CHANGELOG
echo $issue >> $TMP_TREATED_ISSUES
done
cat $TMP_CHANGELOG
# Ask for confirmation to update changelog
read -p 'Update CHANGELOG.md with this ? (y/[n]): ' -r answer
if [[ "$answer" == "y" ]]; then
separator='\n'
# Remove target tag changelog if already exist
if grep -q $TAG_TARGET CHANGELOG.md; then
prev_tag=$(grep '^# v' CHANGELOG.md | sed -n 2p)
sed -i "/${prev_tag}/,\$!d" CHANGELOG.md
separator="${separator}\n"
fi
# Add target tag changelog to final changelog
echo -e "$(cat $TMP_CHANGELOG)${separator}$(cat CHANGELOG.md)" > CHANGELOG.md
fi
read -p "Close all issues and fix version $TAG_TARGET ? (y/[n]): " -r answer
if [[ "$answer" == "y" ]]; then
# Create version if does not exists
one_issue=$(head -n 1 $TMP_TREATED_ISSUES)
auth_header=$(printf "${JIRA_LOGIN}:${JIRA_API_TOKEN}" | base64)
if ! jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} editmeta $one_issue --template=versions | grep -q $TAG_TARGET; then
curl -H "Authorization: Basic $auth_header" -H "Content-Type: application/json" -X POST -d "{\"name\": \"${TAG_TARGET}\",\"userReleaseDate\": \"$(echo -n $(LANG=eng date +'%-d/%b/%Y'))\",\"project\": \"$(echo -n $one_issue | cut -d'-' -f1)\",\"archived\": false,\"released\": true}" ${JIRA_ENDPOINT}/rest/api/latest/version
fi
for issue in $(cat $TMP_TREATED_ISSUES); do
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} transition "Close" $issue --noedit > /dev/null 2>&1 && echo "OK $issue closed" || echo "OK $issue already closed"
jira --endpoint=${JIRA_ENDPOINT} --login=${JIRA_LOGIN} edit $issue --template=fixversion --override fixVersions=${TAG_TARGET} --noedit > /dev/null && echo "OK $issue fix version $TAG_TARGET"
done
fi