|
| 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 | +} |
0 commit comments