Skip to content

Commit cf22786

Browse files
authored
Merge pull request #71 from dscho/dscho/update-tag-git-comment-in-cascading-more-robustly
Find the `/git-artifacts` comment even more reliably
2 parents 9100442 + be10408 commit cf22786

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

GitForWindowsHelper/cascading-runs.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ const cascadingRuns = async (context, req) => {
118118

119119
const token = await getToken(context, checkRunOwner, checkRunRepo)
120120
const { getGitArtifactsCommentID, appendToIssueComment } = require('./issues')
121-
const gitArtifactsCommentID = await getGitArtifactsCommentID(context, token, checkRunOwner, checkRunRepo, req.body.check_run.head_sha)
121+
const gitArtifactsCommentID = await getGitArtifactsCommentID(
122+
context,
123+
token,
124+
checkRunOwner,
125+
checkRunRepo,
126+
req.body.check_run.head_sha,
127+
checkRun.details_url,
128+
)
122129

123130
if (gitArtifactsCommentID) {
124131
await appendToIssueComment(context, token, checkRunOwner, checkRunRepo, gitArtifactsCommentID, comment)

GitForWindowsHelper/issues.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,46 @@ const getIssueComment = async (context, token, owner, repo, comment_id) => {
2323
return await sendGitHubAPIRequest(context, token, 'GET', `/repos/${owner}/${repo}/issues/comments/${comment_id}`)
2424
}
2525

26-
const getGitArtifactsCommentID = async (context, token, owner, repo, headSHA) => {
26+
const getGitArtifactsCommentID = async (context, token, owner, repo, headSHA, tagGitWorkflowRunURL) => {
2727
const answer = await sendGitHubAPIRequest(context, token, 'GET', `/search/issues?q=repo:${owner}/${repo}+${headSHA}+type:pr+%22git-artifacts%22`, null, {
2828
Accept: 'application/vnd.github.text-match+json'
2929
})
30-
const items = answer.items.filter(item =>
31-
item.text_matches.length === 1
32-
&& item.text_matches[0].fragment.trim() === '/git-artifacts\n\nThe tag-git workflow run was started'
30+
let commentID = false
31+
for (const item of answer.items) {
32+
for (const text_match of item.text_matches) {
33+
if (text_match.fragment.startsWith('/git-artifacts')) {
34+
if (commentID !== false) return false // more than one match, maybe a trickster at play, ignore altogether
35+
else {
36+
commentID = text_match.object_url.replace(/^.*\/(\d+)$/, '$1')
37+
break // continue with outer loop, to see whether another PR matches, too
38+
}
39+
}
40+
}
41+
}
42+
if (commentID === false) return false
43+
44+
// ensure that this is the correct comment; It should contain the URL of the actual tag-git workflow run
45+
const comment = await getIssueComment(context, token, owner, repo, commentID)
46+
if (!comment) return false
47+
const needle = `The \`tag-git\` workflow run [was started](${tagGitWorkflowRunURL})`
48+
if (comment.body.includes(needle)) return commentID
49+
50+
// nope, so let's look for other comments on the same PR
51+
commentID = false
52+
const comments = await sendGitHubAPIRequest(
53+
context,
54+
token,
55+
'GET',
56+
`/repos/${owner}/${repo}/issues/${comment.issue_url.replace(/^.*\/(\d+)$/, '$1')}/comments`
3357
)
34-
return items.length === 1 && items[0].text_matches[0].object_url.replace(/^.*\/(\d+)$/, '$1')
58+
for (const comment2 of comments) {
59+
if (comment2.body.startsWith(`/git-artifacts`) && comment2.body.includes(needle)) {
60+
if (commentID !== false) return false // more than one match, maybe a trickster at play, ignore altogether
61+
commentID = comment2.id
62+
}
63+
}
64+
65+
return commentID
3566
}
3667

3768
const appendToIssueComment = async (context, token, owner, repo, comment_id, append) => {

__tests__/index.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ let mockGitHubApiRequest = jest.fn((_context, _token, method, requestPath, paylo
150150
}]
151151
}
152152
if (method === 'GET' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') return {
153-
body: '/git-artifacts\n\nThe tag-git workflow run [was started](https://url-to-tag-git/)'
153+
body: '/git-artifacts\n\nThe `tag-git` workflow run [was started](https://url-to-tag-git/)'
154154
}
155155
if (method === 'PATCH' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') {
156156
expect(payload.body).toEqual(`/git-artifacts
157157
158-
The tag-git workflow run [was started](https://url-to-tag-git/)
158+
The \`tag-git\` workflow run [was started](https://url-to-tag-git/)
159159
160160
git-artifacts-x86_64 run already exists at <url-to-existing-x86_64-run>.
161161
The \`git-artifacts-i686\` workflow run [was started](dispatched-workflow-git-artifacts.yml).
@@ -635,6 +635,7 @@ test('a completed `tag-git` run triggers `git-artifacts` runs', async () => {
635635
name: 'tag-git',
636636
head_sha: 'c8edb521bdabec14b07e9142e48cab77a40ba339',
637637
conclusion: 'success',
638+
details_url: 'https://url-to-tag-git/',
638639
output: {
639640
title: 'Tag Git v2.40.0-rc1.windows.1 @c8edb521bdabec14b07e9142e48cab77a40ba339',
640641
summary: 'Tag Git v2.40.0-rc1.windows.1 @c8edb521bdabec14b07e9142e48cab77a40ba339',

0 commit comments

Comments
 (0)