Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
09ff84c
play: test machine user
Naramsim Apr 23, 2020
96478a2
fix: add indentation
Naramsim Apr 23, 2020
0951d79
play: open PR towards api-data
Naramsim Apr 23, 2020
49b4e7b
play: better token name
Naramsim Apr 23, 2020
cf3802c
play: add -x flag
Naramsim Apr 23, 2020
016f4be
play: invoke create_pr function
Naramsim Apr 23, 2020
773a042
play: use " in JSON data/modify PR
Naramsim Apr 23, 2020
ea05a2c
feat: add checks on curl requests
Naramsim Apr 24, 2020
5185694
play: split circle test phase
Naramsim Apr 24, 2020
d04da76
play: format
Naramsim Apr 24, 2020
6cebda5
play: format
Naramsim Apr 24, 2020
55ee13b
feat: run the updater on CircleCI
Naramsim Apr 24, 2020
e493557
play: fix ssh bad credentials
Naramsim Apr 24, 2020
c3b1e4f
play: use https for cloning
Naramsim Apr 24, 2020
5be98b3
play: assign env variables to their values
Naramsim Apr 24, 2020
c1af304
play: use test branch
Naramsim Apr 24, 2020
628058e
play: fetches remotes
Naramsim Apr 24, 2020
a84a48e
play: move git commands
Naramsim Apr 24, 2020
03a32c6
play: ask CircleCI to use newer version of Docker
Naramsim Apr 24, 2020
e577b77
play: try to use quay image
Naramsim Apr 24, 2020
af88ee0
play: decrease Docker version
Naramsim Apr 24, 2020
11715de
play: use Docker 18.09.3
Naramsim Apr 25, 2020
ab18921
play: use ssh/copy .ssh
Naramsim Apr 25, 2020
9dcabc0
play: run container in background
Naramsim Apr 25, 2020
7541adb
refactor: remove ssh keys/use https and API token
Naramsim Apr 25, 2020
41cf810
multi: refactor/notify updater start
Naramsim Apr 25, 2020
0f5a877
fix: add not operator
Naramsim Apr 25, 2020
5e06cb2
fix: send notification to pokeapi
Naramsim Apr 25, 2020
56a01dd
refactor: comments/cleanup/better branch name
Naramsim Apr 25, 2020
6d92385
refactor: add core team/comments
Naramsim Apr 25, 2020
d69151c
refactor: don't use api-data's test branch
Naramsim Apr 29, 2020
7d8ea5c
feat: end notification/refactor
Naramsim Apr 30, 2020
a911895
fix: allow fail and success messages
Naramsim Apr 30, 2020
6842c66
refactor: clean/better messages
Naramsim Apr 30, 2020
4b2e0d8
feat: run the updater only on master
Naramsim Apr 30, 2020
5b8e4e1
Merge branch 'master' into circleci-hook
Naramsim Apr 30, 2020
2839595
refactor: differentiate workflow from job name
Naramsim May 1, 2020
686ed9c
fix: change name only for the workflow
Naramsim May 1, 2020
cad6c2e
feat: push to staging/better messages/wait deploy
Naramsim May 2, 2020
60813db
refactor: update config to 2.1/test run
Naramsim May 2, 2020
b2ccfb8
play: checkout staging
Naramsim May 2, 2020
b7902ee
refactor: run staging deploy only on master
Naramsim May 2, 2020
44acf88
feat: deploy staging branch to staging env
Naramsim May 3, 2020
bada6cd
refactor: address todos
Naramsim May 13, 2020
79ebc3e
refactor: use some variables in PR comments
Naramsim May 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 2
version: 2.1

jobs:
test:
Expand All @@ -16,8 +16,28 @@ jobs:
name: Run tests
command: make test

deploy:
machine:
image: ubuntu-1604:201903-01 # https://circleci.com/docs/2.0/configuration-reference/#available-machine-images with Docker 18.09.3
steps:
- checkout
- add_ssh_keys:
fingerprints:
- "d3:5f:9d:68:ea:12:9a:42:d3:d3:e1:db:b0:6a:fc:d2"
- run:
name: Generate new data, push to PokeAPI/api-data's staging branch, if master open a Pull Request towards PokeAPI/api-data's master branch
command: bash -x Resources/scripts/updater.sh

workflows:
version: 2
test:
test-and-deploy:
jobs:
- test
- deploy:
requires:
- test
filters:
branches:
only:
- master
- staging
211 changes: 211 additions & 0 deletions Resources/scripts/updater.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
#!/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://pokeapi-test-b6137.firebaseapp.com/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" -gt "0" ]; then
notify_engine_pr "end_failed"
else
notify_engine_pr "end_success"
fi
exit $1
}

# Create and use a personal folder
prepare() {
mkdir -p ./repositories
cd repositories || cleanexit 1 "Failed to cd"
}

# Check and return the number of the Pull Request that started this job
get_invokator_pr_number() {
if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then
echo "${CIRCLE_PULL_REQUEST##*/}"
else
echo 'null'
fi
}

# Clone the repository containing the static JSON files
clone() {
git clone "https://github.com/${org}/${data_repo}.git" "$data_repo"
}

# Configure git to use the supplied user when committing
configure_git() {
git config --global user.name "$username"
git config --global user.email "$email"
}

pr_input_updater_start() {
cat <<EOF
{
"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)"
}
EOF
}

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

pr_input_updater_end_failed() {
cat <<EOF
{
"body": "The updater script couldn't finish its job. Please check [CircleCI's builds]($engine_circleci_status_url) and [logs](${CIRCLE_BUILD_URL})."
}
EOF
}

# If the job was started by a Pull Request, add a comment to notify the users
notify_engine_pr() {
if [[ $1 == "start" || $1 == "end_failed" || $1 == "end_success" ]]; then
if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then
engine_repo_pr_number="${CIRCLE_PULL_REQUEST##*/}"
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"
fi
fi
}

# 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
run_updater() {
cd "${data_repo}/updater" || cleanexit 1 "Failed to cd"
# Wait to be sure PokeAPI/pokeapi's master branch has been updated on Github with the lastest merged PR content
sleep 10

# Build the updater image
docker build -t pokeapi-updater .
if [ $? -ne 0 ]; then
cleanexit 1 "Failed to build the pokeapi-updater image"
fi

# Run the updater
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
if [ $? -ne 0 ]; then
cleanexit 1 "Failed to run the pokeapi-updater container"
fi

cd .. || cleanexit 1 "Failed to cd"
}

# Check if the updater script has pushed the data to a new branch
check_remote_branch() {
# Wait for Github to update origin/${branch_name}
sleep 10

curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X GET "https://api.github.com/repos/$org/$data_repo/branches/$1"
if [ $? -ne 0 ]; then
cleanexit 1 "The updater script failed to push the new data"
fi
}

pr_input_content() {
cat <<EOF
{
"title": "API data update from \`${org}/${engine_repo}#${1}\`",
"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.",
"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)
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')
if [[ "$data_repo_pr_number" = "null" ]]; then
cleanexit 1 "Couldn't create the Pull Request"
fi
echo "$data_repo_pr_number"
}

pr_input_assignees_and_labels() {
cat <<EOF
{
"assignees": [
"Naramsim"
],
"labels": [
"api-data-update"
]
}
EOF
}

# Assign the PR to Naramsim and add a label
customize_pr() {
# Wait for Github to open the PR
sleep 10

data_repo_pr_number=$1
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"
if [ $? -ne 0 ]; then
echo "Couldn't add Assignees and Labes to the Pull Request"
fi
}

pr_input_reviewers() {
cat <<EOF
{
"reviewers": [
"Naramsim"
],
"team_reviewers": [
"core-team"
]
}
EOF
}

# Request the Core team to review the Pull Request
add_reviewers_to_pr() {
data_repo_pr_number=$1
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"
if [ $? -ne 0 ]; then
echo "Couldn't add Reviewers to the Pull Request"
fi
}

prepare
configure_git
clone
notify_engine_pr "start"
run_updater
check_remote_branch "$branch_name"
if [ "$CIRCLE_BRANCH" = 'master' ]; then
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
check_remote_branch "$branch_name"
data_repo_pr_number=$(create_pr)
customize_pr "$data_repo_pr_number"
add_reviewers_to_pr "$data_repo_pr_number"
fi
cleanexit 0 'Done'