Skip to content

Commit

Permalink
Merge pull request #71 from dscho/dscho/update-tag-git-comment-in-cas…
Browse files Browse the repository at this point in the history
…cading-more-robustly

Find the `/git-artifacts` comment even more reliably
  • Loading branch information
dscho authored Apr 25, 2024
2 parents 9100442 + be10408 commit cf22786
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
9 changes: 8 additions & 1 deletion GitForWindowsHelper/cascading-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,14 @@ const cascadingRuns = async (context, req) => {

const token = await getToken(context, checkRunOwner, checkRunRepo)
const { getGitArtifactsCommentID, appendToIssueComment } = require('./issues')
const gitArtifactsCommentID = await getGitArtifactsCommentID(context, token, checkRunOwner, checkRunRepo, req.body.check_run.head_sha)
const gitArtifactsCommentID = await getGitArtifactsCommentID(
context,
token,
checkRunOwner,
checkRunRepo,
req.body.check_run.head_sha,
checkRun.details_url,
)

if (gitArtifactsCommentID) {
await appendToIssueComment(context, token, checkRunOwner, checkRunRepo, gitArtifactsCommentID, comment)
Expand Down
41 changes: 36 additions & 5 deletions GitForWindowsHelper/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,46 @@ const getIssueComment = async (context, token, owner, repo, comment_id) => {
return await sendGitHubAPIRequest(context, token, 'GET', `/repos/${owner}/${repo}/issues/comments/${comment_id}`)
}

const getGitArtifactsCommentID = async (context, token, owner, repo, headSHA) => {
const getGitArtifactsCommentID = async (context, token, owner, repo, headSHA, tagGitWorkflowRunURL) => {
const answer = await sendGitHubAPIRequest(context, token, 'GET', `/search/issues?q=repo:${owner}/${repo}+${headSHA}+type:pr+%22git-artifacts%22`, null, {
Accept: 'application/vnd.github.text-match+json'
})
const items = answer.items.filter(item =>
item.text_matches.length === 1
&& item.text_matches[0].fragment.trim() === '/git-artifacts\n\nThe tag-git workflow run was started'
let commentID = false
for (const item of answer.items) {
for (const text_match of item.text_matches) {
if (text_match.fragment.startsWith('/git-artifacts')) {
if (commentID !== false) return false // more than one match, maybe a trickster at play, ignore altogether
else {
commentID = text_match.object_url.replace(/^.*\/(\d+)$/, '$1')
break // continue with outer loop, to see whether another PR matches, too
}
}
}
}
if (commentID === false) return false

// ensure that this is the correct comment; It should contain the URL of the actual tag-git workflow run
const comment = await getIssueComment(context, token, owner, repo, commentID)
if (!comment) return false
const needle = `The \`tag-git\` workflow run [was started](${tagGitWorkflowRunURL})`
if (comment.body.includes(needle)) return commentID

// nope, so let's look for other comments on the same PR
commentID = false
const comments = await sendGitHubAPIRequest(
context,
token,
'GET',
`/repos/${owner}/${repo}/issues/${comment.issue_url.replace(/^.*\/(\d+)$/, '$1')}/comments`
)
return items.length === 1 && items[0].text_matches[0].object_url.replace(/^.*\/(\d+)$/, '$1')
for (const comment2 of comments) {
if (comment2.body.startsWith(`/git-artifacts`) && comment2.body.includes(needle)) {
if (commentID !== false) return false // more than one match, maybe a trickster at play, ignore altogether
commentID = comment2.id
}
}

return commentID
}

const appendToIssueComment = async (context, token, owner, repo, comment_id, append) => {
Expand Down
5 changes: 3 additions & 2 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ let mockGitHubApiRequest = jest.fn((_context, _token, method, requestPath, paylo
}]
}
if (method === 'GET' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') return {
body: '/git-artifacts\n\nThe tag-git workflow run [was started](https://url-to-tag-git/)'
body: '/git-artifacts\n\nThe `tag-git` workflow run [was started](https://url-to-tag-git/)'
}
if (method === 'PATCH' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') {
expect(payload.body).toEqual(`/git-artifacts
The tag-git workflow run [was started](https://url-to-tag-git/)
The \`tag-git\` workflow run [was started](https://url-to-tag-git/)
git-artifacts-x86_64 run already exists at <url-to-existing-x86_64-run>.
The \`git-artifacts-i686\` workflow run [was started](dispatched-workflow-git-artifacts.yml).
Expand Down Expand Up @@ -635,6 +635,7 @@ test('a completed `tag-git` run triggers `git-artifacts` runs', async () => {
name: 'tag-git',
head_sha: 'c8edb521bdabec14b07e9142e48cab77a40ba339',
conclusion: 'success',
details_url: 'https://url-to-tag-git/',
output: {
title: 'Tag Git v2.40.0-rc1.windows.1 @c8edb521bdabec14b07e9142e48cab77a40ba339',
summary: 'Tag Git v2.40.0-rc1.windows.1 @c8edb521bdabec14b07e9142e48cab77a40ba339',
Expand Down

0 comments on commit cf22786

Please sign in to comment.