Skip to content

Commit 9ba2fe2

Browse files
committed
fix: update invalid work item comment to success upon validation completion
1 parent d3e52ce commit 9ba2fe2

File tree

3 files changed

+89
-31
lines changed

3 files changed

+89
-31
lines changed

__tests__/index.test.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,5 +1454,61 @@ describe('Azure DevOps Commit Validator', () => {
14541454
);
14551455
expect(mockOctokit.rest.issues.createComment).not.toHaveBeenCalled();
14561456
});
1457+
1458+
it('should combine invalid work items from both commits and PR title/body into ONE comment', async () => {
1459+
mockGetInput.mockImplementation(name => {
1460+
if (name === 'check-commits') return 'true';
1461+
if (name === 'check-pull-request') return 'true';
1462+
if (name === 'validate-work-item-exists') return 'true';
1463+
if (name === 'azure-devops-token') return 'azdo-token';
1464+
if (name === 'azure-devops-organization') return 'test-org';
1465+
if (name === 'github-token') return 'github-token';
1466+
if (name === 'comment-on-failure') return 'true';
1467+
return 'false';
1468+
});
1469+
1470+
// Commit has invalid work item AB#555558
1471+
mockOctokit.rest.pulls.listCommits.mockResolvedValue({
1472+
data: [
1473+
{
1474+
sha: 'abc123def456',
1475+
commit: {
1476+
message: 'feat: add feature AB#555558'
1477+
}
1478+
}
1479+
]
1480+
});
1481+
1482+
// PR body has invalid work item AB#55555555
1483+
mockOctokit.rest.pulls.get.mockResolvedValue({
1484+
data: {
1485+
title: 'feat: new feature',
1486+
body: 'Related to AB#55555555'
1487+
}
1488+
});
1489+
1490+
mockOctokit.rest.issues.listComments.mockResolvedValue({
1491+
data: []
1492+
});
1493+
1494+
// Mock both work items as invalid
1495+
mockValidateWorkItemExists.mockResolvedValue(false);
1496+
1497+
await run();
1498+
1499+
expect(mockSetFailed).toHaveBeenCalled();
1500+
1501+
// Should create ONE comment with both invalid work items
1502+
expect(mockOctokit.rest.issues.createComment).toHaveBeenCalledTimes(1);
1503+
expect(mockOctokit.rest.issues.updateComment).not.toHaveBeenCalled();
1504+
1505+
const commentCall = mockOctokit.rest.issues.createComment.mock.calls[0][0];
1506+
expect(commentCall.body).toContain('There are 2 work items');
1507+
expect(commentCall.body).toContain('AB#555558');
1508+
expect(commentCall.body).toContain('AB#55555555');
1509+
// Should show which came from commit vs PR body
1510+
expect(commentCall.body).toContain('`AB#555558` (commit [`abc123d`]');
1511+
expect(commentCall.body).toContain('`AB#55555555` (in PR title/body)');
1512+
});
14571513
});
14581514
});

badges/coverage.svg

Lines changed: 1 addition & 1 deletion
Loading

src/index.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ export async function run() {
131131
core.setFailed(
132132
`There ${allInvalidWorkItems.length === 1 ? 'is' : 'are'} ${allInvalidWorkItems.length} work item${allInvalidWorkItems.length === 1 ? '' : 's'} that ${allInvalidWorkItems.length === 1 ? 'does' : 'do'} not exist in Azure DevOps`
133133
);
134+
} else if (commentOnFailure && validateWorkItemExistsFlag) {
135+
// All work items are valid - check if there's an existing invalid work item comment to update to success
136+
const { owner, repo } = context.repo;
137+
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
138+
owner,
139+
repo,
140+
issue_number: pullNumber
141+
});
142+
143+
const existingInvalidWorkItemComment = comments.find(comment =>
144+
comment.body?.includes(COMMENT_MARKERS.INVALID_WORK_ITEMS)
145+
);
146+
147+
if (existingInvalidWorkItemComment) {
148+
console.log(`Found existing invalid work item comment: ${existingInvalidWorkItemComment.id}`);
149+
const currentDateTime = new Date().toISOString().replace('T', ' ').substring(0, 19);
150+
const commentExtra = `\n\n<details>\n<summary>Workflow run details</summary>\n\n[View workflow run](${context.payload.repository?.html_url}/actions/runs/${context.runId}) - _Last ran: ${currentDateTime} UTC_</details>`;
151+
const successCommentCombined =
152+
`${COMMENT_MARKERS.INVALID_WORK_ITEMS}\n:white_check_mark: All work items referenced in this pull request now exist in Azure DevOps.` +
153+
commentExtra;
154+
155+
console.log('... attempting to update the invalid work item comment to success');
156+
await octokit.rest.issues.updateComment({
157+
owner,
158+
repo,
159+
comment_id: existingInvalidWorkItemComment.id,
160+
body: successCommentCombined
161+
});
162+
console.log('... invalid work item comment updated to success');
163+
}
134164
}
135165
} catch (error) {
136166
core.setFailed(`Action failed with error: ${error}`);
@@ -306,36 +336,8 @@ async function checkCommitsForWorkItems(
306336
return { workItemToCommitMap, invalidWorkItems, hasCommitFailures: false };
307337
}
308338

309-
// All work items are valid - check if there's an existing invalid work item comment to update
310-
if (commentOnFailure) {
311-
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
312-
owner,
313-
repo,
314-
issue_number: pullNumber
315-
});
316-
317-
const existingInvalidWorkItemComment = comments.find(comment =>
318-
comment.body?.includes(COMMENT_MARKERS.INVALID_WORK_ITEMS)
319-
);
320-
321-
if (existingInvalidWorkItemComment) {
322-
console.log(`Found existing invalid work item comment: ${existingInvalidWorkItemComment.id}`);
323-
const currentDateTime = new Date().toISOString().replace('T', ' ').substring(0, 19);
324-
const commentExtra = `\n\n<details>\n<summary>Workflow run details</summary>\n\n[View workflow run](${context.payload.repository?.html_url}/actions/runs/${context.runId}) - _Last ran: ${currentDateTime} UTC_</details>`;
325-
const successCommentCombined =
326-
`${COMMENT_MARKERS.INVALID_WORK_ITEMS}\n:white_check_mark: All work items referenced in this pull request now exist in Azure DevOps.` +
327-
commentExtra;
328-
329-
console.log('... attempting to update the invalid work item comment to success');
330-
await octokit.rest.issues.updateComment({
331-
owner,
332-
repo,
333-
comment_id: existingInvalidWorkItemComment.id,
334-
body: successCommentCombined
335-
});
336-
console.log('... invalid work item comment updated to success');
337-
}
338-
}
339+
// All commit work items are valid - return empty array
340+
// (Don't update success comment here - let caller handle it after checking PR too)
339341
}
340342

341343
// Link work items to PR if enabled (after deduplication)

0 commit comments

Comments
 (0)