From 514f6433b29effa088696468d7130035d030789c Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Sat, 23 Apr 2022 02:45:41 +0300 Subject: [PATCH] A basic fragile script for merging a gitea PR checking only check-runs --- files/gitea-merge.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100755 files/gitea-merge.sh diff --git a/files/gitea-merge.sh b/files/gitea-merge.sh new file mode 100755 index 0000000..5e60c0b --- /dev/null +++ b/files/gitea-merge.sh @@ -0,0 +1,93 @@ +#!/bin/bash + +REPOAPIURL=https://git.bitmessage.org/api/v1/repos/Bitmessage/PyBitmessage +upstream="v0.6" + +if [ -z "${1}" ]; then + echo "Please specify a credential string" + exit 1 +fi + +if [ -z "${2}" ]; then + echo "No PR# specified, here is a list:" + curl -s -X GET -u ${1} ${REPOAPIURL}/pulls \ + | jq '.[] | select(.state == "open") | .number, .title' | paste - - + exit 1 +fi + +# Read PR and parse its fields +pr=$(curl -s -X GET -u ${1} ${REPOAPIURL}/pulls/${2}) + +branch=$(echo $pr | jq '.head | (.repo.ssh_url + " " + .label)' | tr -d '"') +title=$(echo $pr | jq '.title') +body=$(echo $pr | jq '.body') + + +echo "Merging PR ${2}" + +# Merge PR branch into the newly created branch and push it to github +git checkout v0.6 +git branch -d gitea-${2} +git checkout -b gitea-${2} v0.6 +git pull ${branch} || exit 1 +git push --set-upstream origin gitea-${2} +git checkout v0.6 + +if [ -z "${3}" ]; then + echo "No github credentials specified, stopping" + exit 1 +fi + +head=$(echo ${3} | cut -d: -f1):gitea-${2} + +REPOAPIURL=https://api.github.com/repos/Bitmessage/PyBitmessage + +# Create PR on github and parse its fields +pr=$(curl -s -u ${3} -X POST \ + -H "Accept: application/vnd.github.v3+json" ${REPOAPIURL}/pulls \ + -d "{\"title\":${title},\"body\":${body},\"head\":\"${head}\",\"base\":\"${upstream}\"}") + +commit=$(echo $pr | jq '.head.sha' | tr -d '"') +branch=(echo $pr | jq '.base.ref' | tr -d '"') +url=$(echo $pr | jq '.url' | tr -d '"') + + +# Return non-zero status if have a check with conclusion other than "success" +pr_status () { + # Need Instead compare + # required: + # curl -s -X GET $branch | jq '.protection.required_status_checks.contexts' + # done: + # curl -s -X GET ${REPOAPIURL}/commits/${commit}/status + checks=$( + curl -s -X GET \ + -H "Accept: application/vnd.github.v3+json" \ + ${REPOAPIURL}/commits/${1}/check-runs) + if [[ \ +$(echo $checks | jq \ +'(.check_runs | map(select(.conclusion == "success")) | length) == .total_count'\ +) != true ]]; then + return 1 + fi +} + +# Invoke pullrequest.sh script if pr_status is 0 within 30 tries in 5 min +for tick in {1..30}; do + sleep 10 + pr_status ${commit} + + if [[ $? -eq 0 ]]; then + ./git-tools.sh mergepullrequest $(curl -s -X GET ${url} | jq '.number') + break + fi +done + + +# curl -s -u ${3} -X POST \ +# -H "Accept: application/vnd.github.v3+json" $url/reviews \ +# -d '{"event":"APPROVE"}' +# "Can not approve your own pull request" + +# Delete branch: +# curl -s -u ${3} -X DELETE -H "Accept: application/vnd.github.v3+json" \ +# ${REPOAPIURL}/git/refs/heads/gitea-${2}