-
Notifications
You must be signed in to change notification settings - Fork 4.7k
110 lines (93 loc) · 3.67 KB
/
close-prs.yml
File metadata and controls
110 lines (93 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Close Open Pull Requests
# This workflow can be triggered manually to close all open PRs
# in the repository with a standard message.
on:
workflow_dispatch:
inputs:
dry_run:
description: 'Dry run (only list PRs, do not close them)'
required: false
default: 'true'
type: choice
options:
- 'true'
- 'false'
jobs:
close-prs:
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Close all open PRs
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const dryRun = '${{ inputs.dry_run }}' === 'true';
const commentMessage = 'This is a template repo, with changes owned by the Codespaces team.';
console.log(`Dry run mode: ${dryRun}`);
console.log('Fetching open pull requests...');
// Get all open PRs (handle pagination)
const allPullRequests = [];
let page = 1;
let hasMore = true;
while (hasMore) {
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100,
page: page
});
allPullRequests.push(...pullRequests);
// If we got less than 100 PRs, we've reached the end
if (pullRequests.length < 100) {
hasMore = false;
} else {
page++;
}
}
if (allPullRequests.length === 0) {
console.log('No open pull requests found.');
return;
}
console.log(`Found ${allPullRequests.length} open pull request(s):`);
for (const pr of allPullRequests) {
console.log(`\nPR #${pr.number}: ${pr.title}`);
console.log(` Author: ${pr.user.login}`);
console.log(` Created: ${pr.created_at}`);
if (!dryRun) {
try {
// Add comment to the PR
console.log(` Adding comment...`);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: commentMessage
});
// Close the PR
console.log(` Closing PR...`);
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
state: 'closed'
});
console.log(` ✓ Successfully closed PR #${pr.number}`);
} catch (error) {
console.error(` ✗ Failed to close PR #${pr.number}:`, error.message);
}
} else {
console.log(` (Dry run - would close this PR)`);
}
}
if (dryRun) {
console.log('\n⚠️ This was a dry run. No PRs were actually closed.');
console.log('To close PRs, run this workflow again with dry_run=false');
} else {
console.log('\n✅ All open PRs have been processed.');
}