2024-05-27 16:57:05 +02:00
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
|
|
|
|
trap "rm -fr $TMPDIR" EXIT
|
|
|
|
|
|
|
|
source $EXAMPLE_DIR/../../lib/lib.sh
|
|
|
|
|
|
|
|
api=$url/api/v1
|
2024-05-27 23:08:39 +02:00
|
|
|
repo=root/example-automerge
|
2024-05-27 16:57:05 +02:00
|
|
|
export d=/srv/example/automerge
|
|
|
|
|
2024-05-27 23:08:39 +02:00
|
|
|
function reset_automerge_pr() {
|
2024-05-27 16:57:05 +02:00
|
|
|
#
|
|
|
|
# repository with a pull_request event workflow that always succeeds
|
|
|
|
#
|
|
|
|
mkdir -p $d
|
|
|
|
|
2024-05-27 23:08:39 +02:00
|
|
|
forgejo-curl.sh api_json -X DELETE $api/repos/$repo >&/dev/null || true
|
2024-05-27 16:57:05 +02:00
|
|
|
forgejo-test-helper.sh push_workflow actions/example-$example $url root example-$example setup-forgejo $token
|
|
|
|
|
|
|
|
forgejo-curl.sh api_json -X DELETE $api/repos/$repo/actions/variables/SCRIPT >&/dev/null || true
|
|
|
|
forgejo-curl.sh api_json -X POST --data-raw '{"value":"true"}' $api/repos/$repo/actions/variables/SCRIPT
|
|
|
|
|
|
|
|
(
|
|
|
|
cd $d
|
2024-08-07 08:31:33 +02:00
|
|
|
rm -fr example-automerge
|
2024-05-27 16:57:05 +02:00
|
|
|
git clone $url/$repo
|
|
|
|
cd example-automerge
|
|
|
|
git checkout -b other
|
|
|
|
git config user.email root@example.com
|
|
|
|
git config user.name username
|
|
|
|
touch file-unique-to-the-pr-branch
|
2024-05-27 23:08:39 +02:00
|
|
|
echo other >>README
|
2024-05-27 16:57:05 +02:00
|
|
|
git add .
|
|
|
|
git commit -m 'other change'
|
|
|
|
git push --force -u origin other
|
|
|
|
)
|
|
|
|
|
|
|
|
#
|
|
|
|
# make sure the runner won't race with the sequence that follows
|
|
|
|
#
|
|
|
|
forgejo-runner.sh teardown
|
|
|
|
#
|
|
|
|
# create a PR and schedule it for automerge when the workflow succeeds
|
|
|
|
#
|
|
|
|
api_pr_delete_all $api $repo
|
|
|
|
forgejo-curl.sh api_json --data-raw '{"title":"PR title","base":"main","head":"other"}' $api/repos/$repo/pulls >$TMPDIR/pr.json
|
|
|
|
local pr=$(jq -r .number <$TMPDIR/pr.json)
|
|
|
|
forgejo-curl.sh api_json --data-raw '{"Do":"merge","merge_when_checks_succeed":true}' $api/repos/$repo/pulls/$pr/merge
|
|
|
|
if api_pr_is_merged $api $repo $pr; then
|
|
|
|
echo pull request already merged although it should not be
|
|
|
|
return 1
|
|
|
|
fi
|
2024-05-27 23:08:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function verify_automerge_on_status_success() {
|
|
|
|
reset_automerge_pr
|
|
|
|
local pr=$(jq -r .number <$TMPDIR/pr.json)
|
2024-05-27 16:57:05 +02:00
|
|
|
#
|
|
|
|
# run the workflow
|
|
|
|
#
|
|
|
|
forgejo-runner.sh run
|
|
|
|
local sha=$(api_branch_tip $api $repo other)
|
|
|
|
api_pr_wait_success $api $repo $sha
|
|
|
|
#
|
|
|
|
# verify the PR was automerged
|
|
|
|
#
|
|
|
|
if ! retry api_pr_is_merged $api $repo $pr; then
|
|
|
|
echo pull request is not automerged as expected
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:08:39 +02:00
|
|
|
function verify_automerge_on_reviewer_approval() {
|
|
|
|
reset_automerge_pr
|
|
|
|
local pr=$(jq -r .number <$TMPDIR/pr.json)
|
|
|
|
#
|
|
|
|
# require at least one review for a PR to be merged
|
|
|
|
#
|
|
|
|
api_branch_protect $api $repo main
|
|
|
|
#
|
|
|
|
# run the workflow
|
|
|
|
#
|
|
|
|
forgejo-runner.sh run
|
|
|
|
local sha=$(api_branch_tip $api $repo other)
|
|
|
|
api_pr_wait_success $api $repo $sha
|
|
|
|
#
|
|
|
|
# approve the PR
|
|
|
|
#
|
|
|
|
local username=user1
|
|
|
|
api_user_create $api $username $username@example.com
|
|
|
|
api_user_make_admin $api $username
|
|
|
|
user_login $username
|
|
|
|
DOT=$API_TMPDIR/$username api_pr_approve $api $repo $pr
|
|
|
|
#
|
|
|
|
# verify the PR was automerged
|
|
|
|
#
|
|
|
|
if ! retry api_pr_is_merged $api $repo $pr; then
|
|
|
|
echo pull request is not automerged as expected
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function main() {
|
|
|
|
verify_automerge_on_status_success
|
|
|
|
verify_automerge_on_reviewer_approval
|
|
|
|
}
|
|
|
|
|
2024-05-27 16:57:05 +02:00
|
|
|
main
|