Files
anki/.github/workflows/auto-close-missing-issue.yml
Luc Mcgrady fc5103bc33 chore(ci):Use commit SHAs for github actions (#4916)
<!--
Title (for the Pull Request title field at the top):
Use a short prefix so the change type is obvious. You do not need to
repeat it in the body below.

Examples:
- fix: — bugfix
- feat: — feature
- refactor: — internal change without user-facing feature
- docs: — documentation only
- chore: — tooling, CI, deps, build housekeeping
- test: — tests only
-->

## Linked issue (required)

<!-- Fixes #123 / Closes #123 / Refs #123 -->
closes #4722

## Summary / motivation (required)

<!-- What this PR does and why. For larger changes, add enough context
for reviewers. -->
I used this nice script I found:
https://gist.github.com/onnimonni/3462f958c7d235417863651974514525

For the reasons behind this change see:
- #4722

## Steps to reproduce (required, use N/A if not applicable)

<!-- Steps to reproduce: how to trigger the bug in the broken state (the
"before").
 - Mainly for bugfixes;
    - For bugs: numbered steps before the fix. For non-bugs: write N/A.
 - use N/A for features, refactors, docs, chore, etc.
-->
(N/A)

## How to test (required)

<!--- How to test: how you verified the change (checks, unit tests,
manual steps, edge cases — the "after" or general validation). --->
See it run in my repo here:
https://github.com/Luc-Mcgrady/anki/actions/runs/26718877866

### Checklist (minimum)

- [X] I ran `./ninja check` or an equivalent relevant check locally.
- [ ] I added or updated tests when the change is non-trivial or
behavior changed.

### Details

<!-- Commands, manual steps, edge cases, and what you observed -->
## Before / after behavior (optional)

<!-- For bugfixes: behavior before vs after. For other types: N/A or a
short note. -->

## Risk / compatibility / migration (optional)

<!-- Breaking changes, rollout notes, or N/A for small / low-risk PRs
-->

## UI evidence (required for visual changes; otherwise N/A)

<!-- Screenshot or short video -->

## Scope

- [X] This PR is focused on one change (no unrelated edits).
2026-06-02 10:35:32 -03:00

67 lines
2.2 KiB
YAML

name: Auto-close PRs without linked issue
on:
schedule:
- cron: '0 0 * * *' # daily at midnight UTC
workflow_dispatch:
jobs:
auto-close:
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- name: Close PRs missing linked issue for over 4 days
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b #v7
with:
script: |
const { owner, repo } = context.repo;
const fourDaysAgo = new Date(Date.now() - 4 * 24 * 60 * 60 * 1000);
const prs = await github.paginate(github.rest.issues.listForRepo, {
owner,
repo,
state: 'open',
labels: 'missing-issue',
});
for (const pr of prs) {
if (!pr.pull_request) continue;
const isExempt =
pr.labels.some(l => l.name === 'hotfix') ||
pr.user.login === 'dependabot[bot]' ||
pr.labels.some(l => l.name === 'dependencies');
if (isExempt) continue;
// Find when the missing-issue label was applied
const events = await github.paginate(github.rest.issues.listEventsForTimeline, {
owner,
repo,
issue_number: pr.number,
});
const labelEvent = events
.filter(e => e.event === 'labeled' && e.label?.name === 'missing-issue')
.pop(); // most recent application
if (!labelEvent) continue;
if (new Date(labelEvent.created_at) > fourDaysAgo) continue;
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'closed',
});
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body: `This PR has been automatically closed because no linked issue was provided within 4 days. Feel free to reopen it after linking an issue, just edit the PR description and add a line like \`Closes #123\`.`,
});
}