feat: update already open PRs

This commit is contained in:
Alessandro Pezzè 2020-06-04 17:46:42 +02:00
parent 69141a5bb3
commit 89ef60bc95

View file

@ -16,6 +16,7 @@ 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'
auth_header="-H \"Authorization: token $MACHINE_USER_GITHUB_API_TOKEN\""
# Exit the script notifying the user about its success
cleanexit() {
@ -38,7 +39,7 @@ cleanexit() {
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')
no_deploy_label=$(curl -f "$auth_header" -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
@ -77,7 +78,7 @@ get_invokator_pr_number_from_graphql() {
last_commit_sha="$(git rev-parse HEAD)"
query="$(pr_associated_with_sha_graphql_query_content "$last_commit_sha")"
query=$(echo $query) # echo strips all IFS characters (newline, space)
pr_number=$(curl -s -H "Content-Type: application/json" -H "Authorization: token $MACHINE_USER_GITHUB_API_TOKEN" -X POST --data "{\"query\": \"$query\"}" "https://api.github.com/graphql" | jq ".data.repository.commit.associatedPullRequests.edges[0].node.number" )
pr_number=$(curl -s -H "Content-Type: application/json" "$auth_header" -X POST --data "{\"query\": \"$query\"}" "https://api.github.com/graphql" | jq ".data.repository.commit.associatedPullRequests.edges[0].node.number" )
echo "$pr_number"
}
@ -145,7 +146,7 @@ notify_engine_pr() {
if [[ $1 == "start" || $1 == "end_failed" || $1 == "end_success" || $1 == "end_no_deploy" ]]; then
engine_repo_pr_number=$(get_invokator_pr_number)
if [ "$engine_repo_pr_number" != "null" ]; then
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"
curl -f "$auth_header" -X POST --data "$(pr_input_updater_$1)" "https://api.github.com/repos/$org/$engine_repo/issues/$engine_repo_pr_number/comments"
fi
fi
}
@ -176,12 +177,25 @@ 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"
curl -f "$auth_header" -X GET "https://api.github.com/repos/$org/$data_repo/branches/$1"
if [ $? -ne 0 ]; then
cleanexit 'fail' "The updater script failed to push the new data"
fi
}
pr_input_alredy_open() {
cat <<EOF
{
"base": "$branch_name"
}
EOF
}
check_pr_already_open() {
data_repo_pr_number=$(curl "$auth_header" -X GET --data "$(pr_input_alredy_open)" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.[0].number')
echo "$data_repo_pr_number"
}
# Generate the input content that will be sent to Github's API to open a refresh-data PR towards PokeAPI/api-data
pr_input_content_with_pr_number() {
cat <<EOF
@ -219,13 +233,23 @@ pr_input_content_without_pr_number() {
EOF
}
# Create a Pull Request to merge the branch recently pushed by the updater with the master branch
# Create/update 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')
data_repo_pr_already_open_number=$(check_pr_already_open)
if [ "$data_repo_pr_already_open_number" != "null" ]; then
data_repo_pr_number="$data_repo_pr_already_open_number"
if [ "$engine_repo_pr_number" != "null" ]; then
data_repo_pr_number=$(curl "$auth_header" -X PATCH --data "$(pr_input_content_with_pr_number "$engine_repo_pr_number")" "https://api.github.com/repos/$org/$data_repo/pulls/$data_repo_pr_already_open_number" | jq '.number')
else
data_repo_pr_number=$(curl "$auth_header" -X PATCH --data "$(pr_input_content_without_pr_number)" "https://api.github.com/repos/$org/$data_repo/pulls/$data_repo_pr_already_open_number" | jq '.number')
fi
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')
if [ "$engine_repo_pr_number" != "null" ]; then
data_repo_pr_number=$(curl "$auth_header" -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 "$auth_header" -X POST --data "$(pr_input_content_without_pr_number)" "https://api.github.com/repos/$org/$data_repo/pulls" | jq '.number')
fi
fi
if [[ "$data_repo_pr_number" = "null" ]]; then
cleanexit 'fail' "Couldn't create the Pull Request"
@ -252,7 +276,7 @@ customize_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"
curl "$auth_header" -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
@ -274,7 +298,7 @@ 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"
curl "$auth_header" -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