Repository navigation
152 lines (127 loc) · 5.73 KB
/
Copy pathenforce-issue-linked-pr.yml
File metadata and controls
152 lines (127 loc) · 5.73 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
name: Enforce Issue-Linked PRs
on:
pull_request_target:
types: [opened, reopened, edited, synchronize]
permissions:
pull-requests: write
issues: read
jobs:
check-issue-link:
runs-on: ubuntu-latest
name: Check PR has linked issue
steps:
- name: Verify PR has linked issue in Development section
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr = context.payload.pull_request;
const prNumber = pr.number;
const repo = context.repo;
console.log(`Checking PR #${prNumber} by ${pr.user.login} for linked issues in Development section`);
// Check for exempt users (including all GitHub Copilot bot variations)
const exemptUsers = ['A1L13N', 'Copilot', 'copilot', 'copilot-swe-agent[bot]', 'copilot[bot]', 'github-copilot[bot]'];
if (exemptUsers.includes(pr.user.login)) {
console.log(`PR author ${pr.user.login} is exempt from issue link requirement`);
return;
}
// Check if PR is from a copilot branch
const branchName = pr.head.ref;
if (branchName.startsWith('copilot/')) {
console.log(`PR branch ${branchName} starts with 'copilot/', exempt from issue link requirement`);
return;
}
// Check for exempt labels
const exemptLabels = ['dependencies', 'documentation', 'typo', 'ci', 'chore'];
const hasExemptLabel = pr.labels?.some(label => exemptLabels.includes(label.name.toLowerCase()));
if (hasExemptLabel) {
console.log(`PR has exempt label, skipping issue link check`);
return;
}
// Check for exempt title prefixes
const exemptPrefixes = ['docs:', 'chore:', 'deps:', 'ci:'];
if (exemptPrefixes.some(prefix => pr.title.toLowerCase().startsWith(prefix))) {
console.log(`PR has exempt prefix in title, skipping issue link check`);
return;
}
// Check linked issues in the Development section using GraphQL
try {
const query = `
query($owner:String!, $repo:String!, $pr_number:Int!) {
repository(owner:$owner, name:$repo) {
pullRequest(number:$pr_number) {
closingIssuesReferences(first:10) {
nodes {
number
title
state
}
}
}
}
}
`;
const variables = {
owner: repo.owner,
repo: repo.repo,
pr_number: prNumber
};
const result = await github.graphql(query, variables);
const linkedIssues = result.repository.pullRequest.closingIssuesReferences.nodes;
if (linkedIssues && linkedIssues.length > 0) {
const openIssues = linkedIssues.filter(issue => issue.state === 'OPEN');
if (openIssues.length > 0) {
console.log(`Found ${openIssues.length} open issue(s) linked via Development section:`);
for (const issue of openIssues) {
console.log(`- #${issue.number}: ${issue.title}`);
}
return;
} else {
console.log(`All linked issues are closed. PRs must link to an open issue.`);
}
} else {
console.log(`No issues linked via the Development section`);
}
} catch (error) {
console.log(`GraphQL error when checking linked issues: ${error.message}`);
}
// If no valid open issues are linked via Development section, comment and close
const { data: comments } = await github.rest.issues.listComments({
owner: repo.owner,
repo: repo.repo,
issue_number: prNumber,
});
const botUserName = 'github-actions[bot]';
const previousComment = comments.find(comment =>
comment.user.login === botUserName &&
comment.body.includes('🚨 Missing Open Issue Link')
);
if (!previousComment) {
const message = `
## 🚨 Missing Open Issue Link
This pull request does not reference any **open GitHub issue via the Development section**.
PRs must be linked to an open issue using GitHub’s "Development" section (on the right sidebar).
**How to fix this:**
- Go to the PR page.
- Use the "Link issue" option in the Development section.
- Select an appropriate open issue.
This PR will be automatically closed. You can reopen it after linking an open issue.
Thank you!
`;
await github.rest.issues.createComment({
owner: repo.owner,
repo: repo.repo,
issue_number: prNumber,
body: message
});
console.log(`Commented on PR #${prNumber} about missing Development section link`);
} else {
console.log(`Already commented about missing link, skipping duplicate comment`);
}
await github.rest.pulls.update({
owner: repo.owner,
repo: repo.repo,
pull_number: prNumber,
state: 'closed'
});
console.log(`Closed PR #${prNumber} due to missing Development section link`);