Skip to content

feat: auto-close stale sync PRs when source file reverts to match target#192

Merged
joshjohanning merged 16 commits into
mainfrom
copilot/auto-close-stale-sync-prs
Apr 24, 2026
Merged

feat: auto-close stale sync PRs when source file reverts to match target#192
joshjohanning merged 16 commits into
mainfrom
copilot/auto-close-stale-sync-prs

Conversation

Copilot AI commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #189. Auto-closes stale sync PRs when the source file has been reverted to match the target.

How it works

When a file sync determines "no changes needed" (source matches target), it checks for open PRs on the sync branch. If found and created by the same user/app running the action, it closes them with an explanatory comment and cleans up the branch.

Safety checks

  • Author matching: Only closes PRs created by the same authenticated user/app. PRs from other users are skipped with a warning.
  • Branch cleanup: Only deletes the branch when no open PRs remain on it.
  • Dry-run: Reports would-close without mutating.
  • Non-fatal: Errors during stale PR cleanup are logged as warnings, not failures.

Changes

  • New closeStaleActionPrs() helper function
  • Fetches authenticated user login once at startup (users.getAuthenticated())
  • Threads authenticatedLogin through all sync functions
  • Called from syncFilesViaPullRequest() (covers dependabot, gitignore, PR template, workflow files, copilot instructions, codeowners) and syncPackageJson()
  • New sync statuses: stale-pr-closed, would-close-stale-pr
  • Warning subResults propagated for skipped PRs (different author)
  • Summary table formatting for stale PR actions

Tests

  • 399 tests pass (11 new tests for stale PR closing)
  • Covers: close, author mismatch warning, dry-run, branch deletion, API errors, multi-PR handling, graceful degradation

Add closeStaleActionPrs helper that searches for open PRs by branch name
and closes them with an explanatory comment when the source matches target.

- Warns instead of closing if PR has more commits than expected (safety check)
- Supports dry-run mode (logs which PRs would be closed)
- Integrates into syncFilesViaPullRequest (covers dependabot, gitignore,
  PR template, workflow files, copilot-instructions, CODEOWNERS)
- Integrates into syncPackageJson
- Reports closed/would-close PRs as subResults in job summary
- Cleans up sync branch after closing PR (best-effort)

Bump version to 2.9.0

Agent-Logs-Url: https://github.com/joshjohanning/bulk-github-repo-settings-sync-action/sessions/c0c32f54-fdb6-4368-bac4-25de5bf8188b

Co-authored-by: joshjohanning <19912012+joshjohanning@users.noreply.github.com>
Copilot AI changed the title [WIP] Add auto-close logic for stale sync PRs Auto-close stale sync PRs when source file reverts to match target Apr 23, 2026
Copilot AI requested a review from joshjohanning April 23, 2026 03:40
- Use fixed threshold of 1 commit instead of unreliable expectedCommits
  parameter (action always creates exactly 1 commit per sync PR)
- Only delete branch when exactly 1 PR exists on it (prevents orphaning
  other PRs sharing the same branch)
- Add tests for multi-commit warning and multi-PR branch safety
@joshjohanning joshjohanning marked this pull request as ready for review April 23, 2026 14:22
Copilot AI review requested due to automatic review settings April 23, 2026 14:22
@joshjohanning joshjohanning changed the title Auto-close stale sync PRs when source file reverts to match target feat: auto-close stale sync PRs when source file reverts to match target Apr 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds automatic cleanup for stale sync pull requests when a previously-changed source file is reverted to match the target repo again, preventing orphaned “sync” PRs from lingering open.

Changes:

  • Introduces closeStaleActionPrs() and integrates it into the “unchanged” early-return paths for file-sync PR flows and package.json sync.
  • Extends summary/status reporting with stale-pr-closed / would-close-stale-pr and propagates stale-PR warnings into subResults.
  • Updates tests and bumps package version (2.8.0 → 2.9.0); refreshes coverage badge.
Show a summary per file
File Description
src/index.js Adds stale-PR auto-close helper + integrates it into sync paths and job summary messaging.
tests/index.test.js Adds unit/integration tests for stale-PR closing behavior and updates Octokit mocks.
package.json Version bump to 2.9.0 for published behavior change.
package-lock.json Lockfile version bump alignment with package.json.
badges/coverage.svg Updates coverage badge output.

Copilot's findings

  • Files reviewed: 3/5 changed files
  • Comments generated: 3

Comment thread src/index.js Outdated
Comment thread src/index.js Outdated
Comment thread src/index.js
- Restore expectedCommits parameter (default 1, callers pass fileInfos.length)
- Iterate and close all matching stale PRs instead of just first
- Only delete branch when no open PRs remain after closing
- Add tests for multi-file expected commits, close-all, mixed warn/close

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/5 changed files
  • Comments generated: 1

Comment thread src/index.js
The action can update PR branches multiple times, so commit count is
unreliable for detecting manual changes. Instead, compare PR author
against the authenticated user/app login.

- Fetch authenticated user once at startup via users.getAuthenticated()
- Thread authenticatedLogin through all sync functions
- Only close PRs created by the same user/app
- Warn (skip) PRs created by different users
- Graceful degradation when authenticatedLogin is empty

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/5 changed files
  • Comments generated: 5

Comment thread src/index.js
Comment thread src/index.js Outdated
Comment thread src/index.js Outdated
Comment thread src/index.js Outdated
Comment thread __tests__/index.test.js Outdated
- Skip stale PR check entirely when authenticatedLogin is empty
  (prevents closing unrelated PRs when auth fails)
- Fix stale comments referencing old commit-count check
- Update test for empty authenticatedLogin to expect skip

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/5 changed files
  • Comments generated: 3

Comment thread src/index.js
Comment thread src/index.js
Comment thread src/index.js
- Check authenticatedLogin before calling pulls.list (saves API call)
- Track closedResult separately to prefer closed/would-close over
  warned when multiple PRs exist with mixed authors
@joshjohanning joshjohanning requested a review from Copilot April 23, 2026 18:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 3/5 changed files
  • Comments generated: 1

Comment thread src/index.js

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 3

Comment thread src/index.js
Comment thread src/index.js
Comment thread src/index.js

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 5

Comment thread src/index.js
Comment thread src/index.js
Comment thread src/index.js
Comment thread src/index.js
Comment thread src/index.js

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 5

Comment thread src/index.js Outdated
Comment thread src/index.js Outdated
Comment thread src/index.js
Comment thread README.md Outdated
Comment thread README.md
- Mark authenticatedLogin as optional in closeStaleActionPrs JSDoc
- Document authenticatedLogin and contentTransformer in options JSDoc
- Clarify branch deletion condition in README
- Document issues:write permission requirement

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 2

Comment thread src/index.js
Comment thread README.md Outdated
- Swap order: close PR first, then add comment as best-effort
- Comment failure no longer prevents PR from being closed
- Fix README permission key to use hyphenated format (pull-requests)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 1

Comment thread __tests__/index.test.js

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 4/6 changed files
  • Comments generated: 0 new

@joshjohanning joshjohanning merged commit a26b420 into main Apr 24, 2026
5 checks passed
@joshjohanning joshjohanning deleted the copilot/auto-close-stale-sync-prs branch April 24, 2026 18:55
@github-actions

Copy link
Copy Markdown

📦 Draft Release Created

A draft release v2.9.0 has been created for this PR.

🔗 View Draft Release

Next Steps

  • Review the release notes
  • Publish the release to make it permanent

This is an automated reminder from the publish-github-action workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-close stale sync PRs when source file reverts to match target

3 participants