Skip to content

Commit 2be02dc

Browse files
authored
Merge pull request #47 from dscho/update-tag-git-comment-in-cascading-runs
Update the `/git-artifacts` PR comment even in cascading runs
2 parents f48a238 + 4dbae12 commit 2be02dc

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

GitForWindowsHelper/cascading-runs.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,17 @@ const cascadingRuns = async (context, req) => {
114114
throw new Error(`Refusing to handle cascading run in ${checkRunOwner}/${checkRunRepo}`)
115115
}
116116

117-
return await triggerGitArtifactsRuns(context, checkRunOwner, checkRunRepo, checkRun)
117+
const comment = await triggerGitArtifactsRuns(context, checkRunOwner, checkRunRepo, checkRun)
118+
119+
const token = await getToken(context, checkRunOwner, checkRunRepo)
120+
const { getGitArtifactsCommentID, appendToIssueComment } = require('./issues')
121+
const gitArtifactsCommentID = await getGitArtifactsCommentID(context, token, checkRunOwner, checkRunRepo, req.body.check_run.head_sha)
122+
123+
if (gitArtifactsCommentID) {
124+
await appendToIssueComment(context, token, checkRunOwner, checkRunRepo, gitArtifactsCommentID, comment)
125+
}
126+
127+
return comment
118128
}
119129
return `Not a cascading run: ${name}; Doing nothing.`
120130
}

GitForWindowsHelper/github-api-request.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
module.exports = async (context, token, method, requestPath, payload) => {
1+
module.exports = async (context, token, method, requestPath, payload, extraHeaders) => {
22
const { httpsRequest } = require('./https-request')
3-
const headers = token ? { Authorization: `Bearer ${token}` } : null
3+
const headers = token
4+
? { ...(extraHeaders || {}), Authorization: `Bearer ${token}` }
5+
: extraHeaders
46
const answer = await httpsRequest(context, null, method, requestPath, payload, headers)
57
if (answer.error) throw answer.error
68
return answer

GitForWindowsHelper/issues.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ 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) => {
27+
const answer = await sendGitHubAPIRequest(context, token, 'GET', `/search/issues?q=repo:${owner}/${repo}+${headSHA}+type:pr+%22git-artifacts%22`, null, {
28+
Accept: 'application/vnd.github.text-match+json'
29+
})
30+
const items = answer.items.filter(item =>
31+
item.text_matches.length === 1
32+
&& item.text_matches[0].fragment === '/git-artifacts\n\nThe tag-git workflow run was started\n'
33+
)
34+
return items.length === 1 && items[0].text_matches[0].object_url.replace(/^.*\/(\d+)$/, '$1')
35+
}
36+
2637
const appendToIssueComment = async (context, token, owner, repo, comment_id, append) => {
2738
const data = await getIssueComment(context, token, owner, repo, comment_id)
2839
const answer = await sendGitHubAPIRequest(
@@ -65,6 +76,7 @@ const getPRCommitSHA = async (context, token, owner, repo, pullRequestNumber) =>
6576
module.exports = {
6677
addIssueComment,
6778
getIssue,
79+
getGitArtifactsCommentID,
6880
getIssueComment,
6981
appendToIssueComment,
7082
createReactionForIssueComment,

__tests__/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ let mockGitHubApiRequest = jest.fn((_context, _token, method, requestPath, paylo
127127
if (method === 'GET' && requestPath.endsWith('/pulls/4323')) return {
128128
head: { sha: 'dee501d15' }
129129
}
130+
if (method === 'GET' && requestPath === '/search/issues?q=repo:git-for-windows/git+c8edb521bdabec14b07e9142e48cab77a40ba339+type:pr+%22git-artifacts%22') return {
131+
items: [{
132+
text_matches: [{
133+
object_url: 'https://api.github.com/repositories/23216272/issues/comments/1450703020',
134+
fragment: '/git-artifacts\n\nThe tag-git workflow run was started\n'
135+
}]
136+
}]
137+
}
138+
if (method === 'GET' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') return {
139+
body: '/git-artifacts\n\nThe tag-git workflow run [was started](https://url-to-tag-git/)'
140+
}
141+
if (method === 'PATCH' && requestPath === '/repos/git-for-windows/git/issues/comments/1450703020') {
142+
expect(payload.body).toEqual(`/git-artifacts
143+
144+
The tag-git workflow run [was started](https://url-to-tag-git/)
145+
146+
git-artifacts-x86_64 run already exists at <url-to-existing-x86_64-run>.
147+
The \`git-artifacts-i686\` workflow run [was started](dispatched-workflow-git-artifacts.yml).
148+
`)
149+
return { html_url: 'https://github.com/git-for-windows/git/pull/4322#issuecomment-1450703020' }
150+
}
130151
throw new Error(`Unhandled ${method}-${requestPath}-${JSON.stringify(payload)}`)
131152
})
132153
jest.mock('../GitForWindowsHelper/github-api-request', () => {

0 commit comments

Comments
 (0)