|
| 1 | +#!/bin/bash |
| 2 | +# Executed when the master branch of PokeAPI/pokeapi gets updated |
| 3 | +# Runs in CircleCI |
| 4 | +# Generates new data using the latest changes of PokeAPI/pokeapi in order to open a Pull Request towards PokeAPI/api-data |
| 5 | + |
| 6 | +set -o pipefail |
| 7 | + |
| 8 | +org='PokeAPI' |
| 9 | +data_repo='api-data' |
| 10 | +engine_repo='pokeapi' |
| 11 | +branch_name='staging' |
| 12 | +username='pokeapi-machine-user' |
| 13 | +email='pokeapi.co@gmail.com' |
| 14 | +staging_environment_url='https://pokeapi-test-b6137.firebaseapp.com/api/v2/' |
| 15 | +production_environment_url='https://pokeapi.co/api/v2/' |
| 16 | +data_repo_url='https://github.com/PokeAPI/api-data' |
| 17 | +engine_circleci_status_url='https://app.circleci.com/pipelines/github/PokeAPI/pokeapi' |
| 18 | +deploy_circleci_status_url='https://app.circleci.com/pipelines/github/PokeAPI/deploy' |
| 19 | + |
| 20 | +# Exit the script notifying the user about its success |
| 21 | +cleanexit() { |
| 22 | + echo "Exiting" |
| 23 | + echo "$2" |
| 24 | + if [ "$1" -gt "0" ]; then |
| 25 | + notify_engine_pr "end_failed" |
| 26 | + else |
| 27 | + notify_engine_pr "end_success" |
| 28 | + fi |
| 29 | + exit $1 |
| 30 | +} |
| 31 | + |
| 32 | +# Create and use a personal folder |
| 33 | +prepare() { |
| 34 | + mkdir -p ./repositories |
| 35 | + cd repositories || cleanexit 1 "Failed to cd" |
| 36 | +} |
| 37 | + |
| 38 | +# Check and return the number of the Pull Request that started this job |
| 39 | +get_invokator_pr_number() { |
| 40 | + if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then |
| 41 | + echo "${CIRCLE_PULL_REQUEST##*/}" |
| 42 | + else |
| 43 | + echo 'null' |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# Clone the repository containing the static JSON files |
| 48 | +clone() { |
| 49 | + git clone "https://github.com/${org}/${data_repo}.git" "$data_repo" |
| 50 | +} |
| 51 | + |
| 52 | +# Configure git to use the supplied user when committing |
| 53 | +configure_git() { |
| 54 | + git config --global user.name "$username" |
| 55 | + git config --global user.email "$email" |
| 56 | +} |
| 57 | + |
| 58 | +pr_input_updater_start() { |
| 59 | + cat <<EOF |
| 60 | +{ |
| 61 | + "body": "A [PokeAPI/api-data](${data_repo_url}) refresh has started. In 45 minutes the staging branch of [PokeAPI/api-data](${data_repo_url}/tree/staging) will be pushed with the new generated data. <br><br> The staging branch will be deployed in our [staging environment]($staging_environment_url) and you will be able to review the entire API. <br><br> A Pull Request ([master](${data_repo_url}/tree/master)<-[staging](${data_repo_url}/tree/staging)) will be also created at [PokeAPI/api-data](${data_repo_url}/pulls) and assigned to the PokeAPI Core team to be reviewed. If approved and merged new data will soon be available worldwide at [pokeapi.co]($production_environment_url)" |
| 62 | +} |
| 63 | +EOF |
| 64 | +} |
| 65 | + |
| 66 | +pr_input_updater_end_success() { |
| 67 | + cat <<EOF |
| 68 | +{ |
| 69 | + "body": "The updater script has finished its job and has now opened a Pull Request towards [PokeAPI/api-data](${data_repo_url}/pulls) with the updated data. <br><br> You can see the Pull Request deployed at our [staging environment]($staging_environment_url) when [CircleCI deploy]($deploy_circleci_status_url) will be finished (_check the started time of the last build_)" |
| 70 | +} |
| 71 | +EOF |
| 72 | +} |
| 73 | + |
| 74 | +pr_input_updater_end_failed() { |
| 75 | + cat <<EOF |
| 76 | +{ |
| 77 | + "body": "The updater script couldn't finish its job. Please check [CircleCI's builds]($engine_circleci_status_url) and [logs](${CIRCLE_BUILD_URL})." |
| 78 | +} |
| 79 | +EOF |
| 80 | +} |
| 81 | + |
| 82 | +# If the job was started by a Pull Request, add a comment to notify the users |
| 83 | +notify_engine_pr() { |
| 84 | + if [[ $1 == "start" || $1 == "end_failed" || $1 == "end_success" ]]; then |
| 85 | + if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then |
| 86 | + engine_repo_pr_number="${CIRCLE_PULL_REQUEST##*/}" |
| 87 | + curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_input_updater_$1)" "https://api.github.com/repos/$org/$engine_repo/issues/${engine_repo_pr_number}/comments" |
| 88 | + fi |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Run the updater script (https://github.com/PokeAPI/api-data/blob/master/updater/cmd.bash) which will generate the new pokeapi data and push it to the api-data repository under a new branch |
| 93 | +run_updater() { |
| 94 | + cd "${data_repo}/updater" || cleanexit 1 "Failed to cd" |
| 95 | + # Wait to be sure PokeAPI/pokeapi's master branch has been updated on Github with the lastest merged PR content |
| 96 | + sleep 10 |
| 97 | + |
| 98 | + # Build the updater image |
| 99 | + docker build -t pokeapi-updater . |
| 100 | + if [ $? -ne 0 ]; then |
| 101 | + cleanexit 1 "Failed to build the pokeapi-updater image" |
| 102 | + fi |
| 103 | + |
| 104 | + # Run the updater |
| 105 | + docker run --privileged -e COMMIT_EMAIL="$email" -e COMMIT_NAME="$username" -e BRANCH_NAME="$branch_name" -e REPO_POKEAPI="https://github.com/${org}/${engine_repo}.git" -e REPO_DATA="https://${MACHINE_USER_GITHUB_API_TOKEN}@github.com/${org}/${data_repo}.git" pokeapi-updater |
| 106 | + if [ $? -ne 0 ]; then |
| 107 | + cleanexit 1 "Failed to run the pokeapi-updater container" |
| 108 | + fi |
| 109 | + |
| 110 | + cd .. || cleanexit 1 "Failed to cd" |
| 111 | +} |
| 112 | + |
| 113 | +# Check if the updater script has pushed the data to a new branch |
| 114 | +check_remote_branch() { |
| 115 | + # Wait for Github to update origin/${branch_name} |
| 116 | + sleep 10 |
| 117 | + |
| 118 | + curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X GET "https://api.github.com/repos/$org/$data_repo/branches/$1" |
| 119 | + if [ $? -ne 0 ]; then |
| 120 | + cleanexit 1 "The updater script failed to push the new data" |
| 121 | + fi |
| 122 | +} |
| 123 | + |
| 124 | +pr_input_content() { |
| 125 | + cat <<EOF |
| 126 | +{ |
| 127 | + "title": "API data update from \`${org}/${engine_repo}#${1}\`", |
| 128 | + "body": "Incoming data generated by [${org}/${engine_repo}](https://github.com/${org}/${engine_repo})'s CircleCI worker since Pull Request [#${1}](https://github.com/${org}/${engine_repo}/pull/${1}) was merged into the \`master\` branch. <br><br> The new data was generated using a copy of [${org}/${engine_repo}](https://github.com/${org}/${engine_repo}/commits/master) repository when the \`HEAD\` was pointing to \`${CIRCLE_SHA1}\`. <br><br> This Pull Request should have been deployed in our [staging environment]($staging_environment_url), check [here]($deploy_circleci_status_url) for the lastest build timestamp and status code.", |
| 129 | + "head": "$branch_name", |
| 130 | + "base": "master", |
| 131 | + "assignees": [ |
| 132 | + "Naramsim" |
| 133 | + ], |
| 134 | + "labels": [ |
| 135 | + "api-data-update" |
| 136 | + ] |
| 137 | +} |
| 138 | +EOF |
| 139 | +} |
| 140 | + |
| 141 | +# Create a Pull Request to merge the branch recently pushed by the updater with the master branch |
| 142 | +create_pr() { |
| 143 | + engine_repo_pr_number=$(get_invokator_pr_number) |
| 144 | + data_repo_pr_number=$(curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_input_content "$engine_repo_pr_number")" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.number') |
| 145 | + if [[ "$data_repo_pr_number" = "null" ]]; then |
| 146 | + cleanexit 1 "Couldn't create the Pull Request" |
| 147 | + fi |
| 148 | + echo "$data_repo_pr_number" |
| 149 | +} |
| 150 | + |
| 151 | +pr_input_assignees_and_labels() { |
| 152 | + cat <<EOF |
| 153 | +{ |
| 154 | + "assignees": [ |
| 155 | + "Naramsim" |
| 156 | + ], |
| 157 | + "labels": [ |
| 158 | + "api-data-update" |
| 159 | + ] |
| 160 | +} |
| 161 | +EOF |
| 162 | +} |
| 163 | + |
| 164 | +# Assign the PR to Naramsim and add a label |
| 165 | +customize_pr() { |
| 166 | + # Wait for Github to open the PR |
| 167 | + sleep 10 |
| 168 | + |
| 169 | + data_repo_pr_number=$1 |
| 170 | + curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X PATCH --data "$(pr_input_assignees_and_labels)" "https://api.github.com/repos/$org/$data_repo/issues/$data_repo_pr_number" |
| 171 | + if [ $? -ne 0 ]; then |
| 172 | + echo "Couldn't add Assignees and Labes to the Pull Request" |
| 173 | + fi |
| 174 | +} |
| 175 | + |
| 176 | +pr_input_reviewers() { |
| 177 | + cat <<EOF |
| 178 | +{ |
| 179 | + "reviewers": [ |
| 180 | + "Naramsim" |
| 181 | + ], |
| 182 | + "team_reviewers": [ |
| 183 | + "core-team" |
| 184 | + ] |
| 185 | +} |
| 186 | +EOF |
| 187 | +} |
| 188 | + |
| 189 | +# Request the Core team to review the Pull Request |
| 190 | +add_reviewers_to_pr() { |
| 191 | + data_repo_pr_number=$1 |
| 192 | + curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_input_reviewers)" "https://api.github.com/repos/$org/$data_repo/pulls/$data_repo_pr_number/requested_reviewers" |
| 193 | + if [ $? -ne 0 ]; then |
| 194 | + echo "Couldn't add Reviewers to the Pull Request" |
| 195 | + fi |
| 196 | +} |
| 197 | + |
| 198 | +prepare |
| 199 | +configure_git |
| 200 | +clone |
| 201 | +notify_engine_pr "start" |
| 202 | +run_updater |
| 203 | +check_remote_branch "$branch_name" |
| 204 | +if [ "$CIRCLE_BRANCH" = 'master' ]; then |
| 205 | + sleep 300 # 5 minutes, the time it takes for CircleCI's api-data script to generate the data and for CircleCI's deploy script to deploy it to the staging environment |
| 206 | + check_remote_branch "$branch_name" |
| 207 | + data_repo_pr_number=$(create_pr) |
| 208 | + customize_pr "$data_repo_pr_number" |
| 209 | + add_reviewers_to_pr "$data_repo_pr_number" |
| 210 | +fi |
| 211 | +cleanexit 0 'Done' |
0 commit comments