Streamlining Cross-Repo Workflows: Auto-Closing GitHub Issues with External PRs

GitHub is a cornerstone of modern software development, facilitating collaboration across countless projects. However, as projects grow in complexity and span multiple repositories, developers often encounter nuanced challenges. One such challenge, recently highlighted in a community discussion, revolves around the automatic closure of issues in one repository based on the status of a linked Pull Request (PR) in a different, third-party repository.

Illustration showing GitHub's native limitation in auto-closing issues from external pull requests.
Illustration showing GitHub's native limitation in auto-closing issues from external pull requests.

The Cross-Repository Auto-Closure Conundrum

The discussion, initiated by Amirhan-Taipovjan-Greatest-I, posed a direct question: Can an issue created on an "own" repository automatically close when a linked PR from a "3rd-Party (non-Own)" repository is closed or merged? Amirhan's experience indicated this functionality was missing, prompting a feature request.

Why Native Auto-Closure Has Limitations

As Divyansh089 and RAAJK20Pro elaborated, GitHub's native automatic issue closing mechanism, triggered by keywords like Fixes #123 or Closes #123, primarily functions reliably when both the PR and the issue reside within repositories where GitHub can directly resolve their relationship. This typically means within the same repository or closely associated repositories under the same organization with appropriate permissions.

RAAJK20Pro provided a deeper insight into the platform's design, explaining that "cross-repository referencing" and "automatic issue closure" are treated as distinct systems. While GitHub successfully parses keywords to create cross-references (making the PR ↔ issue relationship visible and enabling navigation), it intentionally restricts automatic state changes across unrelated repositories. This design choice likely serves to prevent:

  • Unintended issue closures
  • Potential abuse or spam from external sources
  • Permission boundary conflicts between independent projects

Therefore, when a PR in an external repository uses a keyword like Fixes owner/repository#123, the cross-reference is created, and the mention appears on the target issue, but the issue's state itself remains unchanged after the PR merge. This limitation, while understandable from a security and platform stability perspective, presents a hurdle for modern multi-repository development workflows, especially those involving dependency ecosystems, modular projects, or external contribution pipelines. Integrating robust engineering intelligence tools often requires overcoming such manual gaps.

GitHub Actions workflow automating issue closure based on external pull request status.
GitHub Actions workflow automating issue closure based on external pull request status.

A Practical Solution: GitHub Actions to the Rescue

Recognizing the native limitation, Gecko51 offered a powerful and immediate workaround: a custom GitHub Actions workflow. This solution allows your issue repository to actively monitor the status of linked external PRs and close issues accordingly, providing a valuable boost to your software development measurement and workflow efficiency.

The proposed workflow, designed to run on a schedule (e.g., every two hours) or manually via workflow_dispatch, scans open issues for external PR links. If a linked PR is found to be closed (merged), the corresponding issue is then automatically closed.

name: Close issues when external PRs close
on:
  schedule:
    - cron: '0 */2 * * *' # every 2 hours
  workflow_dispatch:
jobs:
  check-prs:
    runs-on: ubuntu-latest
    steps:
      - name: Close issues with closed external PRs
        uses: actions/github-script@v7
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const issues = await github.paginate(github.rest.issues.listForRepo, {
              owner: context.repo.owner,
              repo: context.repo.repo,
              state: 'open',
            });
            for (const issue of issues) {
              const body = issue.body || '';
              // Match https://github.com/owner/repo/pull/123
              const prPattern = /https:\/\/github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/g;
              let match;
              while ((match = prPattern.exec(body)) !== null) {
                const [, prOwner, prRepo, prNumber] = match;
                try {
                  const { data: pr } = await github.rest.pulls.get({
                    owner: prOwner,
                    repo: prRepo,
                    pull_number: parseInt(prNumber),
                  });
                  if (pr.state === 'closed') {
                    await github.rest.issues.update({
                      owner: context.repo.owner,
                      repo: context.repo.repo,
                      issue_number: issue.number,
                      state: 'closed',
                    });
                    console.log(`Closed issue #${issue.number} (external PR ${prOwner}/${prRepo}#${prNumber} is closed)`);
                    break;
                  }
                } catch (e) {
                  console.log(`Skipping ${prOwner}/${prRepo}#${prNumber}: ${e.message}`);
                }
              }
            }

Key Considerations for Implementation:

  • Authentication: The default GITHUB_TOKEN works for public external repositories. For private external repos, a Personal Access Token (PAT) stored as a secret (e.g., secrets.EXTERNAL_PAT) is required.
  • Regex Matching: The provided regex specifically looks for full PR URLs in the issue body. Adjust it if your linking convention differs (e.g., just owner/repo#123).
  • Closure Logic: The break statement ensures the issue closes upon the first detected closed PR. Remove it if you require all linked PRs in an issue body to be closed before the issue itself closes.

While native support for cross-repository auto-closure remains a desirable feature that would enhance github stats and overall platform utility, this GitHub Action provides a robust and immediate solution for teams seeking to streamline their multi-repository workflows and improve their developer productivity today.

|

Dashboards, alerts, and review-ready summaries built on your GitHub activity.

 Install GitHub App to Start
Dashboard with engineering activity trends