pokeapi/Resources/scripts/updater.sh

116 lines
2.7 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'
branch_name='testbranch'
prepare() {
2020-04-24 20:49:53 +00:00
mkdir -p ./repositories
cd repositories || exit
2020-04-23 19:48:55 +00:00
}
clone() {
git clone "https://github.com/$org/$data_repo.git" "$data_repo"
}
configure_git() {
git config --global user.name "pokeapi-machine-user"
git config --global user.email pokeapi.co@gmail.com
}
2020-04-24 20:49:53 +00:00
run_updater() {
sleep 10 # Wait to be sure PokeAPI/pokeapi:origin/master has been updated on Github with the lastest merged PR content
cd "${data_repo}/updater" || exit
2020-04-24 21:45:09 +00:00
git fetch
git checkout test
2020-04-24 20:49:53 +00:00
docker build -t pokeapi-updater .
docker run --privileged -e COMMIT_EMAIL=pokeapi.co@gmail.com -e COMMIT_NAME="pokeapi-machine-user" -e BRANCH_NAME="$branch_name" -e REPO_POKEAPI="https://github.com/PokeAPI/pokeapi.git" -e REPO_DATA="https://${MACHINE_USER_GITHUB_API_TOKEN}@github.com/PokeAPI/api-data.git" pokeapi-updater
2020-04-24 20:49:53 +00:00
cd .. || exit
2020-04-23 19:48:55 +00:00
}
2020-04-24 20:49:53 +00:00
# push() {
# git checkout -b "$branch_name"
# touch .gitkeeptestpr
# git add .
# git commit -m "play: add test file"
# git push -uf origin "$branch_name"
# }
2020-04-23 19:48:55 +00:00
pr_content() {
cat <<EOF
{
"title": "API data update",
2020-04-23 20:31:08 +00:00
"body": "Incoming data generated by https://github.com/PokeAPI/pokeapi 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-24 20:49:53 +00:00
sleep 10 # Wait for Github to update origin/${branch_name}
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
echo "Couldn't create the Pull Request"
exit 1
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-24 20:49:53 +00:00
run_updater
# push
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"