#!/bin/bash # Executed when the master branch of PokeAPI/pokeapi gets updated # Runs in CircleCI # Generates new data using the latest changes of PokeAPI/pokeapi in order to open a Pull Request towards PokeAPI/api-data set -o pipefail org='PokeAPI' data_repo='api-data' engine_repo='pokeapi' branch_name='staging' username='pokeapi-machine-user' email='pokeapi.co@gmail.com' staging_environment_url='https://staging.pokeapi.co/api/v2/' production_environment_url='https://pokeapi.co/api/v2/' data_repo_url='https://github.com/PokeAPI/api-data' engine_circleci_status_url='https://app.circleci.com/pipelines/github/PokeAPI/pokeapi' deploy_circleci_status_url='https://app.circleci.com/pipelines/github/PokeAPI/deploy' # Exit the script notifying the user about its success cleanexit() { echo "Exiting" echo "$2" if [ "$1" = "success" ]; then notify_engine_pr "end_success" exit 0 elif [ "$1" = "no-deploy" ]; then notify_engine_pr "end_no_deploy" exit 0 else notify_engine_pr "end_failed" exit 1 fi } # Checks if the PR carries a special `no-deploy` label # If so it aborts the deployment assert_no_deploy_label() { engine_repo_pr_number=$(get_invokator_pr_number) if [ "$engine_repo_pr_number" != "null" ]; then no_deploy_label=$(curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X GET "https://api.github.com/repos/$org/$engine_repo/pulls/$engine_repo_pr_number" | jq --raw-output '.labels | .[] | select(.name == "no-deploy") | .name') if [ "$no_deploy_label" == 'no-deploy' ]; then cleanexit 'no-deploy' 'No-deploy label is present thus no need to deploy the project' fi fi } # Create and use a personal folder prepare() { mkdir -p ./repositories cd repositories || cleanexit 'fail' "Failed to cd" } # GraphQL query to retrieve a PR's number based on a commit hash pr_associated_with_sha_graphql_query_content() { cat <
The staging branch will be deployed in our [staging environment]($staging_environment_url) and you will be able to review the entire API.

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)." } EOF } pr_input_updater_end_success() { cat <
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_)." } EOF } pr_input_updater_end_no_deploy() { cat <
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}\`.

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.", "head": "$branch_name", "base": "master", "assignees": [ "Naramsim" ], "labels": [ "api-data-update" ] } EOF } # Copy of pr_input_content_with_pr_number # run when the script cannot detect the merged PR number that started the CircleCI job pr_input_content_without_pr_number() { cat <
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}\`.

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.", "head": "$branch_name", "base": "master", "assignees": [ "Naramsim" ], "labels": [ "api-data-update" ] } EOF } # Create a Pull Request to merge the branch recently pushed by the updater with the master branch create_pr() { engine_repo_pr_number=$(get_invokator_pr_number) if [ "$engine_repo_pr_number" != "null" ]; then data_repo_pr_number=$(curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_input_content_with_pr_number "$engine_repo_pr_number")" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.number') else data_repo_pr_number=$(curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_input_content_without_pr_number)" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.number') fi if [[ "$data_repo_pr_number" = "null" ]]; then cleanexit 'fail' "Couldn't create the Pull Request" fi echo "$data_repo_pr_number" } pr_input_assignees_and_labels() { cat <