|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +print_usage() { |
| 4 | + echo ">>>>> Usage: ./create_issues_versionchange_canonical.sh -t path/to/track -s path/to/problem/specifications" |
| 5 | +} |
| 6 | + |
| 7 | +# Execution begins |
| 8 | + |
| 9 | +command -v jq >/dev/null 2>&1 || { |
| 10 | + echo >&2 ">>>>> This script requires jq but it's not installed. Aborting." |
| 11 | + exit 1 |
| 12 | +} |
| 13 | + |
| 14 | +num_args=$# |
| 15 | + |
| 16 | +if [[ $num_args = 0 ]]; then |
| 17 | + print_usage |
| 18 | + exit 0 |
| 19 | +fi |
| 20 | + |
| 21 | +path_to_track= |
| 22 | +path_to_problem_specifications= |
| 23 | + |
| 24 | +while getopts ":t:s:" option; do |
| 25 | + case "$option" in |
| 26 | + "t") |
| 27 | + path_to_track="$OPTARG" |
| 28 | + ;; |
| 29 | + "s") |
| 30 | + path_to_problem_specifications="$OPTARG" |
| 31 | + ;; |
| 32 | + *) |
| 33 | + echo ">>>>> Unrecognized option. Aborting." |
| 34 | + print_usage |
| 35 | + exit 1 |
| 36 | + ;; |
| 37 | + esac |
| 38 | +done |
| 39 | + |
| 40 | +if [[ -z "$path_to_track" ]]; then |
| 41 | + echo ">>>>> Path to track missing." |
| 42 | + print_usage |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +if [[ -z "$path_to_problem_specifications" ]]; then |
| 47 | + echo ">>>>> Path to problem specifications missing." |
| 48 | + print_usage |
| 49 | + exit 1 |
| 50 | +fi |
| 51 | + |
| 52 | +config_file_path="$path_to_track/config.json" |
| 53 | + |
| 54 | +if ! [[ -f "$config_file_path" ]]; then |
| 55 | + echo ">>>>> Config file not found at ${config_file_path}." |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +track_exercise_slugs=$(jq '.exercises[] | select(has("deprecated") | not) | .slug' "$config_file_path" | tr -d "\"" | sort) |
| 60 | +update_needed_count=0 |
| 61 | + |
| 62 | + |
| 63 | +# Create a file named ".exercism-version-update-issue-script-settings.sh" |
| 64 | +# with the following variables and put in in your home directory |
| 65 | +# |
| 66 | +# TOKEN="your_token" |
| 67 | +# OWNER="your_username" |
| 68 | +# REPO="repo_name" |
| 69 | + |
| 70 | +if ! [[ -f "$HOME/.exercism-version-update-issue-script-settings.sh" ]]; then |
| 71 | + echo "Create a file named \".exercism-version-update-issue-script-settings.sh\"" |
| 72 | + echo "with the following variables and put in in your home directory" |
| 73 | + echo "TOKEN=\"your_token\"" |
| 74 | + echo "OWNER=\"repo_owner\"" |
| 75 | + echo "REPO=\"repo_name\"" |
| 76 | +fi |
| 77 | + |
| 78 | +. "$HOME/.exercism-version-update-issue-script-settings.sh" |
| 79 | + |
| 80 | +if [[ -z "$TOKEN" ]]; then |
| 81 | + echo "Please insert your personal token into \".exercism-version-update-issue-script-settings.sh\"." |
| 82 | + exit 1 |
| 83 | +elif [[ -z "$OWNER" ]]; then |
| 84 | + echo "Please insert the repo owner into \".exercism-version-update-issue-script-settings.sh\"." |
| 85 | + exit 1 |
| 86 | +elif [[ -z "$REPO" ]]; then |
| 87 | + echo "Please insert the name of the repo into \".exercism-version-update-issue-script-settings.sh\"." |
| 88 | + exit 1 |
| 89 | +fi |
| 90 | + |
| 91 | + |
| 92 | +# Fetch issues opened by us to avoid duplicate issues |
| 93 | +OPEN_ISSUES=$(curl --silent -HAuthorization:token\ ${TOKEN} https://api.github.com/repos/${OWNER}/${REPO}/issues\?state=open\&labels=exercise+version+update | jq -r '.[] |(.title | split(":")[0]) + " Issue Title: " + .title + " (" + (.comments|tostring) +" comments)" + ", URL: " + .html_url') |
| 94 | + |
| 95 | +# Check each exercise for possible difference in version and check for submission of new issue |
| 96 | +for slug in $track_exercise_slugs; do |
| 97 | + canonical_data_folder_path="${path_to_problem_specifications}/exercises/${slug}" |
| 98 | + |
| 99 | + if ! [[ -d "$canonical_data_folder_path" ]]; then |
| 100 | + echo ">>>>> Canonical data folder ${canonical_data_folder_path} not found. Aborting." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | + |
| 104 | + canonical_data_file_path="${canonical_data_folder_path}/canonical-data.json" |
| 105 | + |
| 106 | + if ! [[ -f "$canonical_data_file_path" ]]; then |
| 107 | + continue |
| 108 | + fi |
| 109 | + |
| 110 | + canonical_data_version=$(jq '.version' ${canonical_data_file_path} | tr -d "\"") |
| 111 | + |
| 112 | + track_exercise_version_file_path="${path_to_track}/exercises/${slug}/.meta/version" |
| 113 | + |
| 114 | + if ! [[ -f "$track_exercise_version_file_path" ]]; then |
| 115 | + echo ">>>>> ${slug}: needs update or version file (v${canonical_data_version})." |
| 116 | + update_needed_count=$((update_needed_count + 1)) |
| 117 | + read -p "Press enter to continue." |
| 118 | + echo |
| 119 | + continue |
| 120 | + fi |
| 121 | + |
| 122 | + track_data_version=$(cat "$track_exercise_version_file_path" | sed 's/\r$//') |
| 123 | + |
| 124 | + if [[ "$track_data_version" = "$canonical_data_version" ]]; then |
| 125 | + continue |
| 126 | + elif echo "$OPEN_ISSUES" | grep --quiet "^$slug "; then |
| 127 | + echo ">>>>> ${slug}: update tests and version file (v${track_data_version} -> v${canonical_data_version})" |
| 128 | + echo "The following issue(s) are currently open for exercise ${slug} in ${REPO}:" |
| 129 | + echo "$OPEN_ISSUES" | grep "^$slug " | cut -d' ' -f2- |
| 130 | + echo "Please check manually if the title of one of this/these issue(s) might be changed to >> ${slug}: update tests and version file (v${track_data_version} -> v${canonical_data_version}) <<" |
| 131 | + read -p "Press enter to continue." |
| 132 | + echo |
| 133 | + continue |
| 134 | + else |
| 135 | + update_needed_count=$((update_needed_count + 1)) |
| 136 | + printf ">>>>> Exercise ${slug} needs an update from v${track_data_version} -> v${canonical_data_version}\n" |
| 137 | + title="${slug}: update tests and version file (v${track_data_version} -> v${canonical_data_version})" |
| 138 | + body="The tests for the [${slug} exercise](https://github.com/exercism/java/tree/master/exercises/${slug}/src/test/java) are not up-to-date with the [canonical data](https://github.com/exercism/problem-specifications/tree/master/exercises/${slug}/canonical-data.json).\nTo check what has changed and if new tests have been added to the canonical data, please have a look at the [commit history](https://github.com/exercism/problem-specifications/commits/master/exercises/${slug}/canonical-data.json) of the canonical data file for the exercise ${slug}.\nPlease update version and/or test file, and, if the new test(s) fail, fix the reference implementation.\nFeel free to leave a comment if you have any questions. \n\nBackground info: each exercise which has canonical data should have a version file. This file states which version of the canonical data the exercise implements. The version can be found at the top of the [canonical data file](https://github.com/exercism/problem-specifications/tree/master/exercises/${slug}/canonical-data.json) for that exercise." |
| 139 | + labels='"good first issue", "help wanted", "exercise version update"' |
| 140 | + |
| 141 | + response="" |
| 142 | + while [[ $response != "y" && $response != "n" ]]; do |
| 143 | + read -r -p "Do you want to publish this new issue for exercise $slug with title: $title? [y/n] " response |
| 144 | + done |
| 145 | + echo |
| 146 | + if [[ $response == "y" ]]; then |
| 147 | + printf ">>>>> Generating new issue for exercise ${slug} with title: ${title}\n" |
| 148 | + |
| 149 | + curl --fail --silent -H 'Content-Type: application/json' -H 'Authorization: token '"$TOKEN"'' -X POST -d '{"title": "'"$title"'", "body": "'"$body"'", "labels": ['"$labels"']}' https://api.github.com/repos/${OWNER}/${REPO}/issues | jq -r '"New issue created at: " + .html_url' |
| 150 | + read -p "Press enter to continue." |
| 151 | + echo |
| 152 | + |
| 153 | + if [[ "$?" != "0" ]]; then |
| 154 | + echo ">>>>> Issue submission failed. Exit 1" |
| 155 | + exit 1 |
| 156 | + fi |
| 157 | + fi |
| 158 | + fi |
| 159 | +done |
| 160 | + |
| 161 | +if [[ "$update_needed_count" = 0 ]]; then |
| 162 | + echo "All exercises are up to date!" |
| 163 | +fi |
| 164 | + |
| 165 | +echo "All exercises have been checked. Exit" |
0 commit comments