Skip to content

Commit

Permalink
Find the /git-artifacts comment even yet more reliably
Browse files Browse the repository at this point in the history
It _has_ happened in the past that multiple `/git-artifacts` calls were
required until things worked, e.g.
git-for-windows/git#4330 (comment)
which did not work and was followed by
git-for-windows/git#4330 (comment)
which also did not work, and was followed by
git-for-windows/git#4330 (comment)
which pretended to work, but didn't, and was followed by
git-for-windows/git#4330 (comment)
which suceeded, at long last, to produce the Git Artifacts.

So let's make extra sure that we are finding the correct
`/git-artifacts` comment to amend.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Apr 22, 2024
1 parent 3d86a82 commit be10408
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 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
27 changes: 25 additions & 2 deletions GitForWindowsHelper/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ 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'
})
let commentID = false
for (const item of answer.items) {
for (const text_match of item.text_matches) {
if (text_match.fragment.startsWith('/git-artifacts\n\nThe tag-git workflow run was started')) {
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')
Expand All @@ -39,6 +39,29 @@ const getGitArtifactsCommentID = async (context, token, owner, repo, headSHA) =>
}
}
}
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`
)
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
}

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 be10408

Please sign in to comment.