pokeapi/Resources/scripts/updater.sh
2020-04-25 21:23:58 +02:00

159 lines
4 KiB
Bash

#!/bin/bash
set -o pipefail
org='PokeAPI'
data_repo='api-data'
engine_repo='pokeapi'
branch_name='testbranch'
username='pokeapi-machine-user'
email='pokeapi.co@gmail.com'
function cleanexit {
echo "Exiting"
echo "$2"
exit "$1"
}
prepare() {
mkdir -p ./repositories
cd repositories || cleanexit 1 "Failed to cd"
}
get_invokator_pr_number() {
if [ -z "$CIRCLE_PULL_REQUEST" ]; then
echo "${CIRCLE_PULL_REQUEST##*/}"
fi
}
clone() {
git clone "https://github.com/${org}/${data_repo}.git" "$data_repo"
}
configure_git() {
git config --global user.name "$username"
git config --global user.email "$email"
}
notification_comment_content() {
cat <<EOF
{
"body": "A [PokeAPI/api-data](https://github.com/PokeAPI/api-data) refresh has started. If everything works out in 30 minutes a Pull Request will be created and assigned to the PokeAPI Core team to be reviewed. If approved and merged new data will soon be available worldwide."
}
EOF
}
notify_updater_start() {
if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then
pr_number="${CIRCLE_PULL_REQUEST##*/}"
curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(notification_comment_content)" "https://api.github.com/repos/$org/$data_repo/issues/${pr_number}/comments"
fi
}
run_updater() {
cd "${data_repo}/updater" || cleanexit 1 "Failed to cd"
# Wait to be sure PokeAPI/pokeapi:origin/master has been updated on Github with the lastest merged PR content
sleep 10
# Switch to test branch # TODO: don't checkout
git fetch
git checkout test
# 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 2 "Failed to run the pokeapi-updater container"
fi
cd .. || cleanexit 1 "Failed to cd"
}
check_remote_branch() {
sleep 10 # Wait for Github to update origin/${branch_name}
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 updated data"
fi
}
pr_content() {
cat <<EOF
{
"title": "API data update",
"body": "Incoming data generated by https://github.com/${org}/${engine_repo} CircleCI worker",
"head": "$branch_name",
"base": "master",
"assignees": [
"Naramsim"
],
"labels": [
"api-data-update"
]
}
EOF
}
assignees_and_labels() {
cat <<EOF
{
"assignees": [
"Naramsim"
],
"labels": [
"api-data-update"
]
}
EOF
}
reviewers() { # TODO: Add core team
cat <<EOF
{
"reviewers": [
"Naramsim"
]
}
EOF
}
create_pr() {
pr_number=$(curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(pr_content)" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.number')
if [[ "$pr_number" = "null" ]]; then
cleanexit 1 "Couldn't create the Pull Request"
fi
echo "$pr_number"
}
customize_pr() {
sleep 10 # Wait for Github to open the PR
pr_number=$1
curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X PATCH --data "$(assignees_and_labels)" "https://api.github.com/repos/$org/$data_repo/issues/$pr_number"
if [ $? -ne 0 ]; then
echo "Couldn't add Assignees and Labes to the Pull Request"
fi
}
assign_pr() {
pr_number=$1
curl -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(reviewers)" "https://api.github.com/repos/$org/$data_repo/pulls/$pr_number/requested_reviewers"
if [ $? -ne 0 ]; then
echo "Couldn't add Reviewers to the Pull Request"
fi
}
prepare
clone
configure_git
notify_updater_start
run_updater
check_remote_branch "$branch_name"
pr_number=$(create_pr)
customize_pr "$pr_number"
assign_pr "$pr_number"