Skip to content

armgit / mocha

Public Imported from GitHub
0

☕️ Classic, reliable, trusted test framework for Node.js and the browser

chore: add "stale branch" workflow (#6315)

Copilotad17f9f5f8f5
diff --git a/.github/workflows/stale-branches.yml b/.github/workflows/stale-branches.yml
new file mode 100644
index 00000000..f8d769a6
--- /dev/null
+++ b/.github/workflows/stale-branches.yml
@@ -0,0 +1,99 @@
+name: Manage stale branches
+
+on:
+ schedule:
+ - cron: "0 0 * * *" # Run daily at midnight UTC
+ workflow_dispatch:
+
+permissions:
+ contents: read
+ issues: write
+ pull-requests: read
+
+jobs:
+ stale-branches:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Open issues for stale branches that need maintainer review
+ uses: actions/github-script@v9
+ with:
+ script: |
+ const protectedBranches = new Set(['main', '11.x']);
+ const staleAfterMs = 1000 * 60 * 60 * 24 * 7;
+ const now = Date.now();
+ const repoUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}`;
+
+ const { data: openPullRequests } = await github.paginate(github.rest.pulls.list, {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ state: 'open',
+ per_page: 100,
+ });
+
+ const openBranchRefs = new Set(
+ openPullRequests
+ .filter(pr => pr.head && pr.head.repo && pr.head.repo.full_name === `${context.repo.owner}/${context.repo.repo}`)
+ .map(pr => pr.head.ref)
+ );
+
+ const { data: openIssues } = await github.paginate(github.rest.issues.listForRepo, {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ state: 'open',
+ per_page: 100,
+ });
+
+ const { data: branches } = await github.paginate(github.rest.repos.listBranches, {
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ per_page: 100,
+ });
+
+ for (const branch of branches) {
+ if (protectedBranches.has(branch.name) || openBranchRefs.has(branch.name)) {
+ continue;
+ }
+
+ const { data: branchDetails } = await github.rest.repos.getBranch({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ branch: branch.name,
+ });
+
+ const lastUpdated = Date.parse(branchDetails.commit.commit.committer.date);
+
+ if (Number.isNaN(lastUpdated) || now - lastUpdated < staleAfterMs) {
+ continue;
+ }
+
+ const issueTitle = `Review stale branch: ${branch.name}`;
+ const existingIssue = openIssues.find(issue => issue.title === issueTitle);
+
+ if (existingIssue) {
+ continue;
+ }
+
+ const issueBody = [
+ `This branch has been inactive for more than 7 days and is not associated with an open PR.`,
+ '',
+ `- Branch: \`${branch.name}\``,
+ `- Last updated: ${new Date(lastUpdated).toISOString()}`,
+ `- Branch URL: ${repoUrl}/tree/${encodeURIComponent(branch.name)}`,
+ '',
+ 'A maintainer should review whether this branch should be kept, merged, or removed.',
+ ].join('\n');
+
+ try {
+ const issue = await github.rest.issues.create({
+ owner: context.repo.owner,
+ repo: context.repo.repo,
+ title: issueTitle,
+ body: issueBody,
+ labels: ['status: in triage', 'area: repository tooling'],
+ });
+
+ core.info(`Created stale-branch review issue: #${issue.data.number}`);
+ } catch (error) {
+ core.warning(`Unable to create review issue for stale branch ${branch.name}: ${error.message}`);
+ }
+ }