-
Notifications
You must be signed in to change notification settings - Fork 4
67 lines (57 loc) · 2.28 KB
/
auto-close-empty-prs.yml
File metadata and controls
67 lines (57 loc) · 2.28 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
name: Auto Close Empty PRs
on:
pull_request:
types:
- opened
jobs:
check-and-close:
runs-on: ubuntu-latest
permissions:
pull-requests: write
issues: write
steps:
- name: Check PR for changes
id: check_changes
uses: actions/github-script@v7
with:
script: |
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
});
// Check if there are any files changed
const hasChanges = files.length > 0;
// Also check if all changes are 0 (sometimes PRs show files but with 0 changes)
const hasActualChanges = files.some(file =>
file.additions > 0 || file.deletions > 0 || file.changes > 0
);
console.log(`Files changed: ${files.length}`);
console.log(`Has actual changes: ${hasActualChanges}`);
return { hasChanges: hasChanges && hasActualChanges };
- name: Close PR if empty
if: steps.check_changes.outputs.result == 'false' || fromJSON(steps.check_changes.outputs.result).hasChanges == false
uses: actions/github-script@v7
with:
script: |
// Add a comment explaining why the PR is being closed
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: '⚠️ This pull request appears to contain no changes and will be automatically closed.\n\nIf you believe this is an error, please add your changes and push them to the branch.'
});
// Close the pull request
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
state: 'closed'
});
// Add a label (optional)
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['auto-closed', 'no-changes']
});