From f49dcb92cc54324df01f441bf8c5682637f7fb55 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 30 Dec 2023 23:56:59 +0000 Subject: [PATCH 01/10] You do need to pass the branch name here --- .github/workflows/merge_pull_request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 395ea0677..792d2b4f3 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -25,7 +25,7 @@ def current_merge_commit(): return subprocess.check_output(cmd).decode("utf8").strip() -def other_checks_are_running(): +def other_checks_are_running(branch_name): # Now look for other checks running on the same branch. # # See https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#list-check-runs-for-a-git-reference @@ -92,7 +92,7 @@ def other_checks_are_running(): # until they've all finished. time.sleep(20) - while other_checks_are_running(): + while other_checks_are_running(branch_name): time.sleep(2) # Now look for other checks and see if they succeeded. From f9987b1197e72646ebfa52e03eeca962f37f3aa3 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 30 Dec 2023 23:57:29 +0000 Subject: [PATCH 02/10] Log the name of the succeeded checks --- .github/workflows/merge_pull_request.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 792d2b4f3..8e9e9819e 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -103,6 +103,8 @@ def other_checks_are_running(branch_name): ) checks_resp.raise_for_status() + succeeded_checks = set() + for check_run in checks_resp.json()["check_runs"]: if check_run["name"] == "Build the website": continue @@ -115,10 +117,12 @@ def other_checks_are_running(branch_name): print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) sys.exit(1) - if len(checks_resp.json()["check_runs"]) == 1: + succeeded_checks.add(check_run['name']) + + if len(succeeded_checks) == 0: print("No other check runs triggered, okay to merge") else: - print("All other check runs succeeded, okay to merge") + print(f"All other check runs succeeded, okay to merge ({succeeded_checks})") api_client.put( f"/repos/alexwlchan/alexwlchan.net/pulls/{pr_number}/merge", From 4c0108e9a6e467e6c1fce39699704a687bc9d6a2 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 30 Dec 2023 23:59:04 +0000 Subject: [PATCH 03/10] Add another rubocop failure to catch --- image_creator/create_images.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image_creator/create_images.rb b/image_creator/create_images.rb index d2ed9da6b..b219ca064 100755 --- a/image_creator/create_images.rb +++ b/image_creator/create_images.rb @@ -9,7 +9,7 @@ if File.exist? ".missing_images.json" jobs = Queue.new - File.readlines('.missing_images.json').uniq.each do |line| + File.readlines(".missing_images.json").uniq.each do |line| jobs.push(JSON.parse(line)) end From 11dab76869794387a318da2c12b52d7671edce98 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 30 Dec 2023 23:59:30 +0000 Subject: [PATCH 04/10] Don't look for the Git commit until check runs are done --- .github/workflows/merge_pull_request.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 8e9e9819e..6b7ddaa65 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -77,6 +77,13 @@ def other_checks_are_running(branch_name): print("This is a draft PR, so not merging") sys.exit(0) + # Wait 20 seconds for any other check runs to be triggered, then wait + # until they've all finished. + time.sleep(20) + + while other_checks_are_running(branch_name): + time.sleep(2) + # Check if the branch has been updated since this build started; # if so, the build on the newer commit takes precedent. merge_commit_id = pr_resp.json()["merge_commit_sha"] @@ -88,13 +95,6 @@ def other_checks_are_running(branch_name): ) sys.exit(0) - # Wait 20 seconds for any other check runs to be triggered, then wait - # until they've all finished. - time.sleep(20) - - while other_checks_are_running(branch_name): - time.sleep(2) - # Now look for other checks and see if they succeeded. # # See https://docs.github.com/en/rest/checks/runs?apiVersion=2022-11-28#list-check-runs-for-a-git-reference From 8e80a6135e72c45358c64f0fb5e46f918e02fc29 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:03:46 +0000 Subject: [PATCH 05/10] We can abort as soon as a check run fails --- .github/workflows/merge_pull_request.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 6b7ddaa65..ce197d456 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -34,18 +34,22 @@ def other_checks_are_running(branch_name): ) checks_resp.raise_for_status() - for check_run in checks_resp.json()["check_runs"]: - if check_run["name"] == "Merge pull request": - continue + other_checks = { + check_run + for check_run in checks_resp.json()["check_runs"] + if check_run["name"] != "Merge pull request" + } + + for cr in other_checks: + if cr["status"] == "completed" and cr["conclusion"] != "success": + print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) + sys.exit(1) - if check_run["status"] != "completed": + for cr in other_checks: + if cr["status"] != "completed": print(f"Still waiting for {check_run['name']!r}...") return True - if check_run["conclusion"] != "success": - print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) - sys.exit(1) - return False @@ -117,7 +121,7 @@ def other_checks_are_running(branch_name): print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) sys.exit(1) - succeeded_checks.add(check_run['name']) + succeeded_checks.add(check_run["name"]) if len(succeeded_checks) == 0: print("No other check runs triggered, okay to merge") From f4f33deed06b3dae2032a3bb73a006757ce10b6a Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:05:53 +0000 Subject: [PATCH 06/10] This should be a list, not a set --- .github/workflows/merge_pull_request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index ce197d456..3304b6a99 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -34,11 +34,11 @@ def other_checks_are_running(branch_name): ) checks_resp.raise_for_status() - other_checks = { + other_checks = [ check_run for check_run in checks_resp.json()["check_runs"] if check_run["name"] != "Merge pull request" - } + ] for cr in other_checks: if cr["status"] == "completed" and cr["conclusion"] != "success": From 6f7a8fe4110544f12a3c4d56559046483e5ff413 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:07:33 +0000 Subject: [PATCH 07/10] Fix this variable name --- .github/workflows/merge_pull_request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 3304b6a99..a3fa994b6 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -42,12 +42,12 @@ def other_checks_are_running(branch_name): for cr in other_checks: if cr["status"] == "completed" and cr["conclusion"] != "success": - print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) + print(f"Check run {cr['name']!r} did not succeed", file=sys.stderr) sys.exit(1) for cr in other_checks: if cr["status"] != "completed": - print(f"Still waiting for {check_run['name']!r}...") + print(f"Still waiting for {cr['name']!r}...") return True return False From cfd36c142bfb3ba8aa1d40261e9a943357c0c85c Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:09:37 +0000 Subject: [PATCH 08/10] Highlight errors; make rubocop happy --- .github/workflows/merge_pull_request.py | 6 +++--- image_creator/create_images.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index a3fa994b6..0c7042327 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -42,7 +42,7 @@ def other_checks_are_running(branch_name): for cr in other_checks: if cr["status"] == "completed" and cr["conclusion"] != "success": - print(f"Check run {cr['name']!r} did not succeed", file=sys.stderr) + print(f"!!! Check run {cr['name']!r} did not succeed", file=sys.stderr) sys.exit(1) for cr in other_checks: @@ -114,11 +114,11 @@ def other_checks_are_running(branch_name): continue if check_run["status"] != "completed": - print(f"Check run {check_run['name']!r} has not completed", file=sys.stderr) + print(f"!!! Check run {check_run['name']!r} has not completed", file=sys.stderr) sys.exit(1) if check_run["conclusion"] != "success": - print(f"Check run {check_run['name']!r} did not succeed", file=sys.stderr) + print(f"!!! Check run {check_run['name']!r} did not succeed", file=sys.stderr) sys.exit(1) succeeded_checks.add(check_run["name"]) diff --git a/image_creator/create_images.rb b/image_creator/create_images.rb index b219ca064..9e728cd89 100755 --- a/image_creator/create_images.rb +++ b/image_creator/create_images.rb @@ -6,10 +6,10 @@ require 'shell/executer' -if File.exist? ".missing_images.json" +if File.exist? '.missing_images.json' jobs = Queue.new - File.readlines(".missing_images.json").uniq.each do |line| + File.readlines('.missing_images.json').uniq.each do |line| jobs.push(JSON.parse(line)) end From 131d4d994447b129f2d81db0932f6031e386361f Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:13:39 +0000 Subject: [PATCH 09/10] Use an API token to get higher rate limits --- .github/workflows/merge_pull_request.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index 0c7042327..f1f8aac14 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -12,6 +12,7 @@ base_url="https://api.github.com/", headers={ "Accept": "application/vnd.github.v3+json", + "Authorization": f"token {os.environ['GITHUB_TOKEN']}", "X-GitHub-Api-Version": "2022-11-28", }, ) @@ -130,10 +131,8 @@ def other_checks_are_running(branch_name): api_client.put( f"/repos/alexwlchan/alexwlchan.net/pulls/{pr_number}/merge", - headers={"Authorization": f"token {os.environ['GITHUB_TOKEN']}"}, ) api_client.delete( f"/repos/alexwlchan/alexwlchan.net/git/refs/heads/{branch_name}", - headers={"Authorization": f"token {os.environ['GITHUB_TOKEN']}"}, ) From d3aab6f130e9d54599329a5b3ff8bd225fdffa4c Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 31 Dec 2023 00:17:37 +0000 Subject: [PATCH 10/10] Remember to skip itself --- .github/workflows/merge_pull_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/merge_pull_request.py b/.github/workflows/merge_pull_request.py index f1f8aac14..d37cd2842 100755 --- a/.github/workflows/merge_pull_request.py +++ b/.github/workflows/merge_pull_request.py @@ -111,7 +111,7 @@ def other_checks_are_running(branch_name): succeeded_checks = set() for check_run in checks_resp.json()["check_runs"]: - if check_run["name"] == "Build the website": + if check_run["name"] == "Merge pull request": continue if check_run["status"] != "completed":