-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.changelog-generator.sh
159 lines (128 loc) · 5.12 KB
/
.changelog-generator.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
## This is and example of changelog generator for projects that use JIRA.
## Script calls a python script that uses JIRA package to fetch issues from JIRA API
## Generated changelog is in format
##
## * [<issue number> <issue title>](<link to issue>)
## * [<issue number> <issue title>](<link to issue>)
## ...
##
## depending on how many issue numbers are provided.
## Script can also be configured to parse the task number for the branch name.
## In order to use this script you'll need to follow these steps
##
## 1. Install JIRA package ("pip install jira" will do the trick)
## 2. Create a JIRA token (https://id.atlassian.com/manage-profile/security/api-tokens)
## 3. Open .zshrc (or .bash_profile)
## 4. Add these two lines:
## export JIRA_EMAIL="<your email on jira>"
## export JIRA_TOKEN="<your jira token>"
## 5. Save and close .zshrc (or .bash_profile) and restart terminal (or run source ~/.zshrc or source ~/.bash_profile)
##
## Now you should be all set to use this script in your app-deploy.
# Regex used to find task numbers in branch name.
# Replace <project> with prefix used in task numbers on your project.
# Set to "" if you don't want to use this feature.
BRANCH_NAME_TASK_MATCHING_REGEX="(<project>-[0-9]+)"
# Path to python script that will fetch issues from JIRA.
PYTHON_SCRIPT_PATH=".changelog-generator.py"
# JIRA project URL. Used by python script to construct a url to specific issue.
# Replace <project> with your JIRA project name.
JIRA_PROJECT_URL="https://<project>.atlassian.net"
function generate_changelog {
if [[ -z "$JIRA_EMAIL" || -z "$JIRA_TOKEN" ]]; then
echo "Missing JIRA_EMAIL or JIRA_TOKEN. Please add it to your .zshrc or .bash_profile configuration file."
echo
return
fi
local task_numbers=""
if [[ ! -z $BRANCH_NAME_TASK_MATCHING_REGEX ]]; then
# Task number contained in branch name
current_branch=`git rev-parse --abbrev-ref HEAD`
if [[ $current_branch =~ $BRANCH_NAME_TASK_MATCHING_REGEX ]]; then
task_numbers=$(printf "%s " "${BASH_REMATCH[@]}")
fi
fi
# Manually enter task number
if [[ -z $task_numbers ]]; then
echo
echo "Enter task number contained in this build, separated by space (e.g., <project>-1234 <project>-5678)."
echo "For manual changelog entry, leave input empty and press enter."
echo
read -r -p "Tasks contained in this build (e.g. <project>-1234 <project>-5678): " task_numbers
fi
# Select or edit changelog, edit tasks list and generate again
while true; do
if [[ -z "$task_numbers" ]]; then
break
fi
__call_python_script "$task_numbers"
if [[ -z "$CHANGELOG" ]]; then
# reason for failure already printed so just break
break
fi
local user_input=""
__print_generated_changelog
read -r -p "Press enter to use generated changelog or select one of options (e - edit changelog, a - change task numbers): " user_input
# Enter
if [ -z "$user_input" ]; then
break # Enter pressed -> Exit loop
# Edit tasks list
elif [ "$user_input" == "a" ]; then
echo
echo "Enter a new list of tasks."
echo "If an already entered task is needed, please copy it from the list, as the new list will override the existing one."
echo
echo "Entered tasks so far: $task_numbers"
echo
read -r -p "Tasks contained in this build: " task_numbers
continue
# Edit changelog
elif [ "$user_input" == "e" ]; then
temp_file=$(mktemp)
echo "$CHANGELOG" > "$temp_file"
if [[ $EDITOR ]]; then
$EDITOR "$temp_file"
else
nano "$temp_file"
fi
CHANGELOG=$(cat "$temp_file")
rm "$temp_file"
# Wrong input
else
echo "Oh no! Wrong input... try again!"
fi
done
}
function __call_python_script {
local task_numbers=$1
local generated_changelog=$(python3 $PYTHON_SCRIPT_PATH "$task_numbers" "$JIRA_PROJECT_URL" "$JIRA_EMAIL" "$JIRA_TOKEN")
if [[ $generated_changelog == "-99" ]]; then
echo "JIRA configuration isn't valid. Current configurations:"
echo "JIRA_PROJECT_URL: $JIRA_PROJECT_URL"
echo "JIRA_EMAIL: $JIRA_EMAIL"
echo "JIRA_TOKEN: $JIRA_TOKEN"
echo
echo "Please check your configuration and try again."
echo
return
fi
if [[ $generated_changelog == "-1001" ]]; then
echo "Failed to connect to JIRA. Please check your configuration and permissions and try again."
echo
return
fi
if [[ -z $generated_changelog ]]; then
echo "Generated changelog is empty."
echo
return
fi
CHANGELOG=$generated_changelog
}
function __print_generated_changelog() {
echo
echo "Generated changelog:"
echo "---------------------------------------------------------------"
echo "$CHANGELOG"
echo "---------------------------------------------------------------"
echo
}