Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.0.9

- Add `merge` command
- Add `--api-key` flag to `auto-pr run` command
- Allows you to pass the API key as a flag (or as the env APR_API_KEY variable) instead of storing
it in the configuration file
Expand Down
48 changes: 48 additions & 0 deletions autopr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,5 +349,53 @@ def reopen():
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:
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
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
try:
click.secho(f"Attempting to merge {repository.name}")
pr.merge(merge_method="squash")
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()