Skip to content

Commit 188491f

Browse files
committed
Add changelog generator
1 parent 964bbf0 commit 188491f

File tree

6 files changed

+302
-12
lines changed

6 files changed

+302
-12
lines changed

app-deploy.sh

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ source /usr/local/bin/.app-deploy-sources/__constants.sh
44
source /usr/local/bin/.app-deploy-sources/__help.sh
55
if [ -z "$1" ] || [ "$1" == 'trigger' ] ; then
66
source ./.deploy-options.sh
7+
source ./.changelog-generator.sh
78
source /usr/local/bin/.app-deploy-sources/__trigger_deploy.sh
89
fi
910
source /usr/local/bin/.app-deploy-sources/__auto_update.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env python3.8
2+
3+
import sys
4+
from jira import JIRA
5+
6+
# Generate changelog
7+
def generate_changelog(issues, server, jira_email, jira_token):
8+
9+
if jira_email is None or jira_token is None or server is None:
10+
#Pass -99 from python as print inside python is not visible. Calling sys.exit will just stop execution.
11+
return "-99"
12+
13+
options = {
14+
'server': server
15+
}
16+
17+
try:
18+
jira = JIRA(options, basic_auth=(jira_email, jira_token))
19+
except:
20+
return "-1001"
21+
22+
build_changelog = ""
23+
for issueId in issues.split():
24+
issueId = issueId.upper()
25+
issue = jira.issue(issueId)
26+
taskName = issue.fields.summary
27+
taskUrl = server + "/browse/" + issueId
28+
fullName = "\n* [" + issueId + " " + taskName + "](" + taskUrl + ")"
29+
build_changelog += fullName
30+
31+
return build_changelog
32+
33+
# Main
34+
if __name__ == "__main__":
35+
36+
if len(sys.argv) != 5:
37+
sys.exit(1)
38+
39+
input_string = sys.argv[1]
40+
server = sys.argv[2]
41+
jira_email = sys.argv[3]
42+
jira_token = sys.argv[4]
43+
result = generate_changelog(input_string, server, jira_email, jira_token)
44+
print(result)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
## This is and example of changelog generator for projects that use JIRA.
2+
## Script calls a python script that uses JIRA package to fetch issues from JIRA API
3+
## Generated changelog is in format
4+
##
5+
## * [<issue number> <issue title>](<link to issue>)
6+
## * [<issue number> <issue title>](<link to issue>)
7+
## ...
8+
##
9+
## depending on how many issue numbers are provided.
10+
## Script can also be configured to parse the task number for the branch name.
11+
12+
13+
## In order to use this script you'll need to follow these steps
14+
##
15+
## 1. Install JIRA package ("pip install jira" will do the trick)
16+
## 2. Create a JIRA token (https://id.atlassian.com/manage-profile/security/api-tokens)
17+
## 3. Open .zshrc (or .bash_profile)
18+
## 4. Add these two lines:
19+
## export JIRA_EMAIL="<your email on jira>"
20+
## export JIRA_TOKEN="<your jira token>"
21+
## 5. Save and close .zshrc (or .bash_profile) and restart terminal (or run source ~/.zshrc or source ~/.bash_profile)
22+
##
23+
## Now you should be all set to use this script in your app-deploy.
24+
25+
26+
# Regex used to find task numbers in branch name.
27+
# Replace <project> with prefix used in task numbers on your project.
28+
# Set to "" if you don't want to use this feature.
29+
BRANCH_NAME_TASK_MATCHING_REGEX="(<project>-[0-9]+)"
30+
31+
# Path to python script that will fetch issues from JIRA.
32+
PYTHON_SCRIPT_PATH=".changelog-generator.py"
33+
34+
# JIRA project URL. Used by python script to construct a url to specific issue.
35+
# Replace <project> with your JIRA project name.
36+
JIRA_PROJECT_URL="https://<project>.atlassian.net"
37+
38+
function generate_changelog {
39+
40+
if [[ -z "$JIRA_EMAIL" || -z "$JIRA_TOKEN" ]]; then
41+
echo "Missing JIRA_EMAIL or JIRA_TOKEN. Please add it to your .zshrc or .bash_profile configuration file."
42+
__reset_changelog
43+
return
44+
fi
45+
46+
local task_numbers=""
47+
48+
if [[ ! -z $BRANCH_NAME_TASK_MATCHING_REGEX ]]; then
49+
# Task number contained in branch name
50+
current_branch=`git rev-parse --abbrev-ref HEAD`
51+
if [[ $current_branch =~ $BRANCH_NAME_TASK_MATCHING_REGEX ]]; then
52+
task_numbers=$(printf "%s " "${BASH_REMATCH[@]}")
53+
fi
54+
fi
55+
56+
# Manually enter task number
57+
if [[ -z $task_numbers ]]; then
58+
echo
59+
echo "Enter task number contained in this build, separated by space (e.g., NUT-1234 NUT-5678)."
60+
echo "For manual changelog entry, leave input empty and press enter."
61+
echo
62+
read -r -p "Tasks contained in this build (e.g. NUT-1234 NUT-5678): " task_numbers
63+
fi
64+
65+
# Select or edit change log, edit tasks list and generate again
66+
while true; do
67+
68+
if [[ -z "$task_numbers" ]]; then
69+
__reset_changelog
70+
break
71+
fi
72+
73+
__call_python_script "$task_numbers"
74+
75+
if [[ -z "$CHANGELOG" ]]; then
76+
# reason for failure already printed so just break
77+
__reset_changelog
78+
break
79+
fi
80+
81+
local user_input=""
82+
__print_generated_changelog
83+
read -r -p "Press enter to use generated changelog or select one of options (e - edit changelog, a - change task numbers): " user_input
84+
85+
# Enter
86+
if [ -z "$user_input" ]; then
87+
break # Enter pressed -> Exit loop
88+
89+
# Edit tasks list
90+
elif [ "$user_input" == "a" ]; then
91+
echo
92+
echo "Enter a new list of tasks."
93+
echo "If an already entered task is needed, please copy it from the list, as the new list will override the existing one."
94+
echo
95+
echo "Entered tasks so far: $task_numbers"
96+
echo
97+
read -r -p "Tasks contained in this build: " task_numbers
98+
continue
99+
100+
# Edit changelog
101+
elif [ "$user_input" == "e" ]; then
102+
temp_file=$(mktemp)
103+
echo "$CHANGELOG" > "$temp_file"
104+
if [[ $EDITOR ]]; then
105+
$EDITOR "$temp_file"
106+
else
107+
nano "$temp_file"
108+
fi
109+
CHANGELOG=$(cat "$temp_file")
110+
rm "$temp_file"
111+
112+
# Wrong input
113+
else
114+
echo "Oh no! Wrong input... try again!"
115+
fi
116+
done
117+
}
118+
119+
function __call_python_script {
120+
121+
local task_numbers=$1
122+
local generated_changelog=$(python3 $PYTHON_SCRIPT_PATH "$task_numbers" "$JIRA_PROJECT_URL" "$JIRA_EMAIL" "$JIRA_TOKEN")
123+
124+
if [[ $generated_changelog == "-99" ]]; then
125+
echo "JIRA configuration isn't valid. Current configurations:"
126+
echo "JIRA_PROJECT_URL: $JIRA_PROJECT_URL"
127+
echo "JIRA_EMAIL: $JIRA_EMAIL"
128+
echo "JIRA_TOKEN: $JIRA_TOKEN"
129+
echo
130+
echo "Please check your configuration and try again."
131+
echo
132+
133+
return
134+
fi
135+
136+
if [[ $generated_changelog == "-1001" ]]; then
137+
echo "Failed to connect to JIRA. Please check your configuration and permissions and try again."
138+
echo
139+
140+
return
141+
fi
142+
143+
if [[ -z $generated_changelog ]]; then
144+
echo "Generated changelog is empty."
145+
echo
146+
147+
return
148+
fi
149+
150+
CHANGELOG=$generated_changelog
151+
}
152+
153+
function __print_generated_changelog() {
154+
echo
155+
echo "Generated changelog:"
156+
echo "---------------------------------------------------------------"
157+
echo "--$CHANGELOG--"
158+
echo "---------------------------------------------------------------"
159+
echo
160+
}
161+
162+
function __reset_changelog {
163+
CHANGELOG=""
164+
}

sources/__init.sh

+34-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
source /usr/local/bin/.app-deploy-sources/__constants.sh
22

3+
function __init_deploy_options {
4+
cat /usr/local/bin/.app-deploy-sources/deploy-options.sh > ./.deploy-options.sh
5+
echo "The options file was generated successfully!"
6+
echo "NOTE: Change default values to the project specific."
7+
echo
8+
}
9+
10+
function __init_changelog_generator {
11+
cat /usr/local/bin/.app-deploy-sources/changelog-generator.sh > ./.changelog-generator.sh
12+
echo "Changelog generator file was generated successfully!"
13+
echo "NOTE: Change default implementation to the project specific."
14+
echo " Examples can be found https://github.com/infinum/app-deploy-script/tree/master/examples/changelog_generator."
15+
echo
16+
}
17+
318
#################################
419
# INIT NEW PROJECT #
520
#################################
@@ -13,13 +28,25 @@ function __init {
1328
echo "If you continue, stored options will be overridden!"
1429
echo
1530
read -r -p "Do you want to proceed? [y/n] " c
16-
if ! [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then
17-
exit 1
31+
if [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then
32+
__init_deploy_options
1833
fi
34+
else
35+
__init_deploy_options
1936
fi
2037

21-
cat /usr/local/bin/.app-deploy-sources/deploy-options.sh > ./.deploy-options.sh
22-
echo "The options file was generated successfully!"
23-
echo "NOTE: Change default values to the project specific."
24-
echo
25-
}
38+
if [ -e "./.changelog-generator.sh" ]; then
39+
echo "Changelog generator file already exists."
40+
echo "If you continue, current implementation will be overridden!"
41+
echo
42+
read -r -p "Do you want to proceed? [y/n] " c
43+
if [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then
44+
__init_changelog_generator
45+
fi
46+
else
47+
read -r -p "Add changelog generator file? [y/n] " c
48+
if [[ ${c} =~ ^(yes|y|Y) ]] || [ -z ${c} ]; then
49+
__init_changelog_generator
50+
fi
51+
fi
52+
}

sources/changelog-generator.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
3+
#########################################################
4+
# CHANGELOG GENERATOR #
5+
# #
6+
# Part of script that should be edited by the user. #
7+
# #
8+
# This will generate the changelog. #
9+
#########################################################
10+
11+
## Used to autmatically generate changelog.
12+
## If CHANGELOG is empty user will be asked to enter it manually.
13+
##
14+
## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
15+
## !!! Result needs to be saved in CHANGELOG variable !!!
16+
## !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
17+
##
18+
## Look at examples folder to get inspired.
19+
20+
function generate_changelog {
21+
# add logic to generate changelog
22+
CHANGELOG=""
23+
}

sources/helpers/__base_tag_handling.sh

+36-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function __create_app_version_and_build_number {
4343
fi
4444

4545
if ! [[ "$appversion" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
46-
then
46+
then
4747
echo "App version is in wrong format (use M.m.p format, e.g. 1.0.0). Aborting..."
4848
exit 5
4949
fi
@@ -86,27 +86,58 @@ function __create_trigger_ci_timestamp_tag {
8686
trigger_tag+="$target/"
8787
done
8888

89-
# Sufix timestamp
89+
# Sufix timestamp
9090
trigger_tag+="$TRIGGER_TAG_SUFIX"
9191

9292
# Assign to shared property
9393
tags_to_deploy=("$trigger_tag")
9494
}
9595

96+
function __changelog_generator_exists {
97+
if ! declare -f generate_changelog > /dev/null; then
98+
return 1
99+
fi
100+
101+
return 0
102+
}
103+
96104
# Changelog
97105
function __generate_tag_and_changelog {
98-
99106
echo
100107
echo "###############################################################"
101108
echo "# CHANGELOG #"
102109
echo "###############################################################"
103110
echo
111+
112+
# Generate changelog
113+
114+
if __changelog_generator_exists; then
115+
echo "------------------------------------------------------------"
116+
echo "Generating changelog message..."
117+
echo "------------------------------------------------------------"
118+
119+
CHANGELOG=""
120+
generate_changelog # Result will be saved in CHANGELOG
121+
122+
if [[ ! -z "$CHANGELOG" ]]; then
123+
for tag in "${tags_to_deploy[@]}"; do
124+
git tag -a "$tag" -m "${CHANGELOG}"
125+
done
126+
return # No need to ask for changelog if one is generated
127+
else
128+
echo "Failed to generate changelog"
129+
echo
130+
fi
131+
fi
132+
133+
# Enter changelog manually
134+
104135
echo "------------------------------------------------------------"
105136
echo "Enter changelog message..."
106137
echo "------------------------------------------------------------"
107138
sleep 1
108139

109-
tag_message_added=0
140+
local tag_message_added=0
110141
for tag in "${tags_to_deploy[@]}"; do
111142

112143
if [ ${tag_message_added} -eq 1 ]; then
@@ -118,4 +149,4 @@ function __generate_tag_and_changelog {
118149
tag_message_added=1
119150
fi
120151
done
121-
}
152+
}

0 commit comments

Comments
 (0)