pokeapi/Resources/scripts/updater.sh

160 lines
4 KiB
Bash
Raw Normal View History

2020-04-23 19:48:55 +00:00
#!/bin/bash
set -o pipefail
org='PokeAPI'
data_repo='api-data'
2020-04-25 19:15:41 +00:00
engine_repo='pokeapi'
2020-04-23 19:48:55 +00:00
branch_name='testbranch'
2020-04-25 19:15:41 +00:00
username='pokeapi-machine-user'
email='pokeapi.co@gmail.com'
function cleanexit {
echo "Exiting"
echo "$2"
exit "$1"
}
2020-04-23 19:48:55 +00:00
prepare() {
2020-04-24 20:49:53 +00:00
mkdir -p ./repositories
2020-04-25 19:15:41 +00:00
cd repositories || cleanexit 1 "Failed to cd"
}
get_invokator_pr_number() {
if [ -z "$CIRCLE_PULL_REQUEST" ]; then
echo "${CIRCLE_PULL_REQUEST##*/}"
fi
2020-04-23 19:48:55 +00:00
}
clone() {
2020-04-25 19:15:41 +00:00
git clone "https://github.com/${org}/${data_repo}.git" "$data_repo"
2020-04-23 19:48:55 +00:00
}
configure_git() {
2020-04-25 19:15:41 +00:00
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() {
2020-04-25 19:23:58 +00:00
if ! [ -z "$CIRCLE_PULL_REQUEST" ]; then
2020-04-25 19:15:41 +00:00
pr_number="${CIRCLE_PULL_REQUEST##*/}"
2020-04-25 19:28:28 +00:00
curl -f -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "$(notification_comment_content)" "https://api.github.com/repos/$org/$engine_repo/issues/${pr_number}/comments"
2020-04-25 19:15:41 +00:00
fi
2020-04-23 19:48:55 +00:00
}
2020-04-24 20:49:53 +00:00
run_updater() {
2020-04-25 19:15:41 +00:00
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
2020-04-24 21:45:09 +00:00
git fetch
2020-04-25 19:15:41 +00:00
git checkout test
# Build the updater image
2020-04-24 20:49:53 +00:00
docker build -t pokeapi-updater .
2020-04-25 19:15:41 +00:00
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"
2020-04-23 19:48:55 +00:00
}
2020-04-25 19:15:41 +00:00
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
}
2020-04-24 20:49:53 +00:00
2020-04-23 19:48:55 +00:00
pr_content() {
cat <<EOF
{
"title": "API data update",
2020-04-25 19:15:41 +00:00
"body": "Incoming data generated by https://github.com/${org}/${engine_repo} CircleCI worker",
2020-04-23 19:48:55 +00:00
"head": "$branch_name",
"base": "master",
"assignees": [
"Naramsim"
],
"labels": [
"api-data-update"
]
}
EOF
}
2020-04-23 20:31:08 +00:00
assignees_and_labels() {
cat <<EOF
{
"assignees": [
"Naramsim"
],
"labels": [
"api-data-update"
]
}
EOF
}
2020-04-24 19:18:26 +00:00
reviewers() { # TODO: Add core team
2020-04-23 19:48:55 +00:00
cat <<EOF
{
"reviewers": [
"Naramsim"
]
}
EOF
}
create_pr() {
2020-04-23 19:51:17 +00:00
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')
2020-04-24 19:18:26 +00:00
if [[ "$pr_number" = "null" ]]; then
2020-04-25 19:15:41 +00:00
cleanexit 1 "Couldn't create the Pull Request"
2020-04-24 19:18:26 +00:00
fi
2020-04-23 20:31:08 +00:00
echo "$pr_number"
}
customize_pr() {
2020-04-24 20:49:53 +00:00
sleep 10 # Wait for Github to open the PR
2020-04-23 20:31:08 +00:00
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"
2020-04-24 19:18:26 +00:00
if [ $? -ne 0 ]; then
echo "Couldn't add Assignees and Labes to the Pull Request"
fi
2020-04-23 19:48:55 +00:00
}
assign_pr() {
pr_number=$1
2020-04-23 19:51:17 +00:00
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"
2020-04-24 19:18:26 +00:00
if [ $? -ne 0 ]; then
echo "Couldn't add Reviewers to the Pull Request"
fi
2020-04-23 19:48:55 +00:00
}
prepare
clone
configure_git
2020-04-25 19:15:41 +00:00
notify_updater_start
2020-04-24 20:49:53 +00:00
run_updater
2020-04-25 19:15:41 +00:00
check_remote_branch "$branch_name"
2020-04-23 20:02:53 +00:00
pr_number=$(create_pr)
2020-04-23 20:31:08 +00:00
customize_pr "$pr_number"
2020-04-23 19:48:55 +00:00
assign_pr "$pr_number"