Skip to content

Commit f76de00

Browse files
committed
fix: deduplicate jira key warning comment on PRs
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent b1115b3 commit f76de00

1 file changed

Lines changed: 77 additions & 40 deletions

File tree

.github/workflows/pr-title-jira-key-lint.yml

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,99 @@ jobs:
1717
with:
1818
script: |
1919
const prTitle = context.payload.pull_request.title;
20-
console.log(`PR Title: ${prTitle}`);
21-
22-
// Regex to match Jira issue keys (CM-123 format)
23-
// Supports conventional commits format: type(CM-123): description
20+
const prNumber = context.issue.number;
21+
const { owner, repo } = context.repo;
22+
const headSha = context.payload.pull_request.head.sha;
23+
24+
const COMMENT_MARKER = '<!-- jira-key-validation-comment -->';
2425
const jiraKeyRegex = /\b[A-Z]+-\d+\b/;
25-
26-
if (!jiraKeyRegex.test(prTitle)) {
27-
const warningMessage = `⚠️ **Jira Issue Key Missing**
28-
29-
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.
30-
31-
**Example:**
32-
- \`feat: add user authentication (CM-123)\`
33-
- \`feat: add user authentication (IN-123)\`
3426
35-
**Projects:**
36-
- CM: Community Data Platform
37-
- IN: Insights
27+
console.log(`PR Title: ${prTitle}`);
3828
39-
Please add a Jira issue key to your PR title.`;
40-
41-
github.rest.issues.createComment({
42-
issue_number: context.issue.number,
43-
owner: context.repo.owner,
44-
repo: context.repo.repo,
45-
body: warningMessage
29+
const existingComment = await (async () => {
30+
const iterator = github.paginate.iterator(github.rest.issues.listComments, {
31+
owner,
32+
repo,
33+
issue_number: prNumber,
34+
per_page: 100,
4635
});
47-
48-
// Create check run with failure conclusion
49-
github.rest.checks.create({
50-
owner: context.repo.owner,
51-
repo: context.repo.repo,
52-
head_sha: context.payload.pull_request.head.sha,
36+
for await (const { data: comments } of iterator) {
37+
const found = comments.find(
38+
(c) => c.user?.type === 'Bot' && c.body?.includes(COMMENT_MARKER),
39+
);
40+
if (found) return found;
41+
}
42+
return null;
43+
})();
44+
45+
if (!jiraKeyRegex.test(prTitle)) {
46+
const warningMessage = `${COMMENT_MARKER}
47+
⚠️ **Jira Issue Key Missing**
48+
49+
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.
50+
51+
**Example:**
52+
- \`feat: add user authentication (CM-123)\`
53+
- \`feat: add user authentication (IN-123)\`
54+
55+
**Projects:**
56+
- CM: Community Data Platform
57+
- IN: Insights
58+
59+
Please add a Jira issue key to your PR title.`;
60+
61+
if (existingComment) {
62+
if (existingComment.body !== warningMessage) {
63+
await github.rest.issues.updateComment({
64+
owner,
65+
repo,
66+
comment_id: existingComment.id,
67+
body: warningMessage,
68+
});
69+
}
70+
} else {
71+
await github.rest.issues.createComment({
72+
owner,
73+
repo,
74+
issue_number: prNumber,
75+
body: warningMessage,
76+
});
77+
}
78+
79+
await github.rest.checks.create({
80+
owner,
81+
repo,
82+
head_sha: headSha,
5383
name: 'Jira PR Validation',
5484
status: 'completed',
5585
conclusion: 'neutral',
5686
output: {
5787
title: 'Jira Issue Key Missing',
58-
summary: 'PR title does not contain a Jira issue key'
59-
}
88+
summary: 'PR title does not contain a Jira issue key',
89+
},
6090
});
6191
} else {
6292
const match = prTitle.match(jiraKeyRegex);
6393
console.log(`✅ Found Jira issue key: ${match[0]}`);
64-
65-
// Create check run with success conclusion
66-
github.rest.checks.create({
67-
owner: context.repo.owner,
68-
repo: context.repo.repo,
69-
head_sha: context.payload.pull_request.head.sha,
94+
95+
if (existingComment) {
96+
await github.rest.issues.deleteComment({
97+
owner,
98+
repo,
99+
comment_id: existingComment.id,
100+
});
101+
}
102+
103+
await github.rest.checks.create({
104+
owner,
105+
repo,
106+
head_sha: headSha,
70107
name: 'Jira PR Validation',
71108
status: 'completed',
72109
conclusion: 'success',
73110
output: {
74111
title: 'Jira Issue Key Found',
75-
summary: `Found Jira issue key: ${match[0]}`
76-
}
112+
summary: `Found Jira issue key: ${match[0]}`,
113+
},
77114
});
78-
}
115+
}

0 commit comments

Comments
 (0)