From 56542b25d4f6049350b774f074910e6ff8f68a4e Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 09:22:20 +0100 Subject: [PATCH 1/6] Add merge command --- CHANGELOG.md | 3 +++ autopr/__init__.py | 23 +++++++++++++++++++++++ pyproject.toml | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4dff927..7b577dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.0.8 +- Add `merge` command + ## 1.0.7 - Add `--exclude-missing` when running `auto-pr status` - Allows you to remove the `Missing PRs` section from the output diff --git a/autopr/__init__.py b/autopr/__init__.py index 36dabcc..f4937e5 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -339,6 +339,29 @@ def reopen(): _set_all_pull_requests_state(github.PullRequestState.OPEN) click.secho("Finished reopening all closed unmerged pull requests") +@cli.command() +def merge(): + """Merge all mergeable pull requests""" + cfg = workdir.read_config(WORKDIR) + db = workdir.read_database(WORKDIR) + gh = github.create_github_client(cfg.credentials.api_key) + + for repository in db.repositories: + if repository.existing_pr is not None: + details = github.get_pull_request(gh, repository) + if details.state == github.PullRequestState.OPEN.value: + if details.mergeable: + try: + github.merge_pull_request(gh, repository) + click.secho(f"Merged {repository.name}") + except ValueError as e: + click.secho(f"{e}") + else: + click.secho(f"Pull request {repository.name} is not mergeable") + else: + click.secho(f"Pull request {repository.name} is not open") + + if __name__ == "__main__": main() diff --git a/pyproject.toml b/pyproject.toml index 7b1d8a8..b78d698 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "auto-pr" -version = "1.0.7" +version = "1.0.8" description = "Perform bulk updates across repositories" license = "Apache-2.0" From 7ef3fe99892bbdc7fc1499365105f1e0449c3eb3 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 10:11:19 +0100 Subject: [PATCH 2/6] check check runs --- autopr/__init__.py | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/autopr/__init__.py b/autopr/__init__.py index f4937e5..86f2376 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -348,11 +348,29 @@ def merge(): for repository in db.repositories: if repository.existing_pr is not None: - details = github.get_pull_request(gh, repository) - if details.state == github.PullRequestState.OPEN.value: - if details.mergeable: + pr = github.get_pull_request(gh, repository) + if pr.state == github.PullRequestState.OPEN.value: + if pr.mergeable: + commits = list(pr.get_commits()) + if len(commits) == 0: + click.secho(f"Pull request {repository.name} has no commits") + break + + ok = True + check_suites = commits[-1].get_check_suites() + for check in check_suites: + if check.status == "queued": + continue + if check.status != "completed" or check.conclusion != "success": + check_runs = check.get_check_runs() + for check_run in check_runs: + if check_run.status != "completed" or check_run.conclusion != "success": + click.secho(f"Pull request {repository.name} has a check {check_run.name} (conclusion: {check_run.conclusion}) that is not successful") + ok = False + if not ok: + continue try: - github.merge_pull_request(gh, repository) + #details.merge() click.secho(f"Merged {repository.name}") except ValueError as e: click.secho(f"{e}") From d1855b07f5c17468a6056c6aa2ae7e154a897c94 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 10:12:07 +0100 Subject: [PATCH 3/6] thorough check --- autopr/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autopr/__init__.py b/autopr/__init__.py index 86f2376..9f55dcb 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -364,7 +364,7 @@ def merge(): if check.status != "completed" or check.conclusion != "success": check_runs = check.get_check_runs() for check_run in check_runs: - if check_run.status != "completed" or check_run.conclusion != "success": + if check_run.status != "completed" or check_run.conclusion not in ("success", "skipped"): click.secho(f"Pull request {repository.name} has a check {check_run.name} (conclusion: {check_run.conclusion}) that is not successful") ok = False if not ok: From b65a52d8bfc8ab6584c1eaa2f1eb24ecd844ff35 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 10:15:00 +0100 Subject: [PATCH 4/6] fixes and actual merge --- autopr/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/autopr/__init__.py b/autopr/__init__.py index 9f55dcb..988f2ca 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -365,12 +365,12 @@ def merge(): check_runs = check.get_check_runs() for check_run in check_runs: if check_run.status != "completed" or check_run.conclusion not in ("success", "skipped"): - click.secho(f"Pull request {repository.name} has a check {check_run.name} (conclusion: {check_run.conclusion}) that is not successful") + click.secho(f"Pull request {repository.name} has a check `{check_run.name}` (conclusion: {check_run.conclusion}) that is not successful") ok = False if not ok: continue try: - #details.merge() + pr.merge(merge_method="squash") click.secho(f"Merged {repository.name}") except ValueError as e: click.secho(f"{e}") From 766bfffcec7affe9157a96bf831866aacc305dfe Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 10:15:09 +0100 Subject: [PATCH 5/6] format --- autopr/__init__.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/autopr/__init__.py b/autopr/__init__.py index 988f2ca..9b96063 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -339,6 +339,7 @@ def reopen(): _set_all_pull_requests_state(github.PullRequestState.OPEN) click.secho("Finished reopening all closed unmerged pull requests") + @cli.command() def merge(): """Merge all mergeable pull requests""" @@ -355,7 +356,7 @@ def merge(): if len(commits) == 0: click.secho(f"Pull request {repository.name} has no commits") break - + ok = True check_suites = commits[-1].get_check_suites() for check in check_suites: @@ -364,8 +365,14 @@ def merge(): if check.status != "completed" or check.conclusion != "success": check_runs = check.get_check_runs() for check_run in check_runs: - if check_run.status != "completed" or check_run.conclusion not in ("success", "skipped"): - click.secho(f"Pull request {repository.name} has a check `{check_run.name}` (conclusion: {check_run.conclusion}) that is not successful") + if ( + check_run.status != "completed" + or check_run.conclusion + not in ("success", "skipped") + ): + click.secho( + f"Pull request {repository.name} has a check `{check_run.name}` (conclusion: {check_run.conclusion}) that is not successful" + ) ok = False if not ok: continue @@ -378,7 +385,6 @@ def merge(): click.secho(f"Pull request {repository.name} is not mergeable") else: click.secho(f"Pull request {repository.name} is not open") - if __name__ == "__main__": From 613e33bbb73406476be67d07647c69c7426578b1 Mon Sep 17 00:00:00 2001 From: Alex Eftimie Date: Fri, 6 Dec 2024 10:32:48 +0100 Subject: [PATCH 6/6] print --- autopr/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/autopr/__init__.py b/autopr/__init__.py index 9b96063..a3f070e 100644 --- a/autopr/__init__.py +++ b/autopr/__init__.py @@ -377,6 +377,7 @@ def merge(): if not ok: continue try: + click.secho(f"Attempting to merge {repository.name}") pr.merge(merge_method="squash") click.secho(f"Merged {repository.name}") except ValueError as e: