Skip to content

Commit

Permalink
Merge pull request #47 from dscho/update-tag-git-comment-in-cascading…
Browse files Browse the repository at this point in the history
…-runs

Update the `/git-artifacts` PR comment even in cascading runs
  • Loading branch information
dscho authored Nov 11, 2023
2 parents f48a238 + 4dbae12 commit 2be02dc
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
12 changes: 11 additions & 1 deletion GitForWindowsHelper/cascading-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,17 @@ const cascadingRuns = async (context, req) => {
throw new Error(`Refusing to handle cascading run in ${checkRunOwner}/${checkRunRepo}`)
}

return await triggerGitArtifactsRuns(context, checkRunOwner, checkRunRepo, checkRun)
const comment = await triggerGitArtifactsRuns(context, checkRunOwner, checkRunRepo, checkRun)

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)

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

return comment
}
return `Not a cascading run: ${name}; Doing nothing.`
}
Expand Down
6 changes: 4 additions & 2 deletions GitForWindowsHelper/github-api-request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module.exports = async (context, token, method, requestPath, payload) => {
module.exports = async (context, token, method, requestPath, payload, extraHeaders) => {
const { httpsRequest } = require('./https-request')
const headers = token ? { Authorization: `Bearer ${token}` } : null
const headers = token
? { ...(extraHeaders || {}), Authorization: `Bearer ${token}` }
: extraHeaders
const answer = await httpsRequest(context, null, method, requestPath, payload, headers)
if (answer.error) throw answer.error
return answer
Expand Down
12 changes: 12 additions & 0 deletions GitForWindowsHelper/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ 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 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 === '/git-artifacts\n\nThe tag-git workflow run was started\n'
)
return items.length === 1 && items[0].text_matches[0].object_url.replace(/^.*\/(\d+)$/, '$1')
}

const appendToIssueComment = async (context, token, owner, repo, comment_id, append) => {
const data = await getIssueComment(context, token, owner, repo, comment_id)
const answer = await sendGitHubAPIRequest(
Expand Down Expand Up @@ -65,6 +76,7 @@ const getPRCommitSHA = async (context, token, owner, repo, pullRequestNumber) =>
module.exports = {
addIssueComment,
getIssue,
getGitArtifactsCommentID,
getIssueComment,
appendToIssueComment,
createReactionForIssueComment,
Expand Down
21 changes: 21 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ let mockGitHubApiRequest = jest.fn((_context, _token, method, requestPath, paylo
if (method === 'GET' && requestPath.endsWith('/pulls/4323')) return {
head: { sha: 'dee501d15' }
}
if (method === 'GET' && requestPath === '/search/issues?q=repo:git-for-windows/git+c8edb521bdabec14b07e9142e48cab77a40ba339+type:pr+%22git-artifacts%22') return {
items: [{
text_matches: [{
object_url: 'https://api.github.com/repositories/23216272/issues/comments/1450703020',
fragment: '/git-artifacts\n\nThe tag-git workflow run was started\n'
}]
}]
}
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/)'
}
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/)
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).
`)
return { html_url: 'https://github.com/git-for-windows/git/pull/4322#issuecomment-1450703020' }
}
throw new Error(`Unhandled ${method}-${requestPath}-${JSON.stringify(payload)}`)
})
jest.mock('../GitForWindowsHelper/github-api-request', () => {
Expand Down

0 comments on commit 2be02dc

Please sign in to comment.