feat(forge): pr merge squash-merges a ready, passing pull request; pr checks and pr diff #14

Merged
HumanBean merged 1 commit from HumanBean/Tooling:feat/forge-pr-merge into main 2026-11-16 06:50:49 +00:00
Owner

Agents open pull requests with forge.py pr create, but when they're told to "open the PR and merge it" they can't: forge.py has no merge. The only other way is to take the stored git token and call Forgejo's API by hand, and agents must not do that. This adds the merge to forge.py, with the same checks a person makes before pressing the button.

What changed

scripts/forge.py, documented in docs/forge.md (new section Merging a pull request) and the script's help:

  • pr merge N [--delete-branch] [--force] squash-merges into main through POST /pulls/{n}/merge, the way the merge box on the PR's page does. The commit's title is the PR's title plus (#N). The message is the description, then a Co-authored-by: for each other author of the PR's commits, then Reviewed-on: <PR link>. If the API gets no message, the commit has only its title. The merge sends head_commit_id, so Forgejo refuses it if someone pushes after the checks were read. It prints the merge commit's link and the PR's. --delete-branch sends delete_branch_after_merge. If the merge goes through and only the branch deletion fails, it prints a warning and exits 0.

  • It refuses before posting anything when the PR is:

    • closed or already merged;
    • work in progress (it points to pr ready);
    • not mergeable: it conflicts with main, or Forgejo hasn't checked it yet;
    • not titled as a Conventional Commit;
    • blocked by a reviewer whose last review requested changes and wasn't dismissed.

    Unless --force is given, it also refuses when the description fails the check pr ready runs, or when a check on the head commit failed, is still running, or is waiting for a maintainer to approve the run. --force gets past only those last two, with a warning. The docs say agents use it only when the person they work for asks. Forgejo still enforces who may merge.

  • pr checks N lists the CI statuses on the head commit: state, name, what it says, and its link, then how many passed, failed and are still running. It exits 1 when a check failed or is still running. A repository with no CI shows no checks and exits 0.

  • pr diff N prints the PR's diff exactly as Forgejo gives it, the same .diff that pr review already reads.

How it was tested

  • tests/test_forge_pr.py adds 6 tests against the stand-in Forgejo:
    • pr checks output and exit codes, with passing, failing, running and no checks;
    • pr diff;
    • the exact merge request body (title, message, co-authors deduplicated by email, head_commit_id) and the printed result;
    • each refusal, with nothing posted, and the ones --force can't get past;
    • --force;
    • --delete-branch, the server refusing the merge, and a merge that stands when only the branch deletion fails.
  • LONGSHOT=<Longshot checkout> python -I -B tests/run_all.py: 82 tests in test_forge_pr.py, every new one passing. The one failure, test_create_draft_is_the_default_and_wip_is_not_doubled, is already failing on main, as #11 says. It expects no PATCH after a pr create --body, which has not been true since #2 (allow_maintainer_edit). This PR leaves it alone.
  • Against git.magicbean.dev, read-only:
    • pr checks on LongshotLauncher#137 (success and skipped, exit 0), Longshot#1062 (9 runs waiting for approval, exit 1) and Tooling#11 (no checks, exit 0);
    • pr diff on LongshotLauncher#137;
    • the refusal step and the message builder, with every non-GET request blocked. LongshotLauncher#137 was refused as merged, Longshot#1073 as work in progress, and Longshot#1062 because its checks are still running. Its message came out as the description, then Co-authored-by: for the commits' other author, then Reviewed-on:.
  • Not tested: a real merge. I can't merge a PR just to try it. The request body follows Forgejo's API handler (routers/api/v1/repo/pull.go, MergePullRequestForm), and its message matches the squash commits already on main.

🤖 Generated with Claude Code

Agents open pull requests with `forge.py pr create`, but when they're told to "open the PR and merge it" they can't: forge.py has no merge. The only other way is to take the stored git token and call Forgejo's API by hand, and agents must not do that. This adds the merge to forge.py, with the same checks a person makes before pressing the button. ## What changed `scripts/forge.py`, documented in `docs/forge.md` (new section *Merging a pull request*) and the script's help: - **`pr merge N [--delete-branch] [--force]`** squash-merges into main through `POST /pulls/{n}/merge`, the way the merge box on the PR's page does. The commit's title is the PR's title plus ` (#N)`. The message is the description, then a `Co-authored-by:` for each other author of the PR's commits, then `Reviewed-on: <PR link>`. If the API gets no message, the commit has only its title. The merge sends `head_commit_id`, so Forgejo refuses it if someone pushes after the checks were read. It prints the merge commit's link and the PR's. `--delete-branch` sends `delete_branch_after_merge`. If the merge goes through and only the branch deletion fails, it prints a warning and exits 0. - **It refuses before posting anything** when the PR is: - closed or already merged; - work in progress (it points to `pr ready`); - not mergeable: it conflicts with main, or Forgejo hasn't checked it yet; - not titled as a Conventional Commit; - blocked by a reviewer whose last review requested changes and wasn't dismissed. Unless `--force` is given, it also refuses when the description fails the check `pr ready` runs, or when a check on the head commit failed, is still running, or is waiting for a maintainer to approve the run. `--force` gets past only those last two, with a warning. The docs say agents use it only when the person they work for asks. Forgejo still enforces who may merge. - **`pr checks N`** lists the CI statuses on the head commit: state, name, what it says, and its link, then how many passed, failed and are still running. It exits 1 when a check failed or is still running. A repository with no CI shows no checks and exits 0. - **`pr diff N`** prints the PR's diff exactly as Forgejo gives it, the same `.diff` that `pr review` already reads. ## How it was tested - `tests/test_forge_pr.py` adds 6 tests against the stand-in Forgejo: - `pr checks` output and exit codes, with passing, failing, running and no checks; - `pr diff`; - the exact merge request body (title, message, co-authors deduplicated by email, `head_commit_id`) and the printed result; - each refusal, with nothing posted, and the ones `--force` can't get past; - `--force`; - `--delete-branch`, the server refusing the merge, and a merge that stands when only the branch deletion fails. - `LONGSHOT=<Longshot checkout> python -I -B tests/run_all.py`: 82 tests in `test_forge_pr.py`, every new one passing. The one failure, `test_create_draft_is_the_default_and_wip_is_not_doubled`, is already failing on main, as #11 says. It expects no PATCH after a `pr create --body`, which has not been true since #2 (`allow_maintainer_edit`). This PR leaves it alone. - Against git.magicbean.dev, read-only: - `pr checks` on LongshotLauncher#137 (success and skipped, exit 0), Longshot#1062 (9 runs waiting for approval, exit 1) and Tooling#11 (no checks, exit 0); - `pr diff` on LongshotLauncher#137; - the refusal step and the message builder, with every non-GET request blocked. LongshotLauncher#137 was refused as merged, Longshot#1073 as work in progress, and Longshot#1062 because its checks are still running. Its message came out as the description, then `Co-authored-by:` for the commits' other author, then `Reviewed-on:`. - Not tested: a real merge. I can't merge a PR just to try it. The request body follows Forgejo's API handler (`routers/api/v1/repo/pull.go`, `MergePullRequestForm`), and its message matches the squash commits already on main. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Agents can open a pull request with forge.py but not merge one, so "open the PR and merge it" had no way through
the team's tooling.

- pr merge N [--delete-branch] [--force]: squash-merges with the title (#N) as the commit and the description,
  the commits' other authors and Reviewed-on as the message, at the head it checked. Refuses closed, WIP,
  conflicting, non-Conventional-Commit titles and changes requested; --force only past a failing description
  check or failed/running checks.
- pr checks N: the CI's statuses on the head commit; exit 1 when one failed or is still running.
- pr diff N: the PR's diff, as Forgejo gives it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
HumanBean deleted branch feat/forge-pr-merge 2026-11-16 06:50:49 +00:00
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Tooling!14
No description provided.