Skip to content

Commit b18a11b

Browse files
committed
Add a function to find the /git-artifacts PR comment for a given commit SHA
To update the `/git-artifacts` comment when cascading `git-artifacts-<arch>` runs are triggered, we need a way to get from the commit SHA (which is part of the `check_run.completed` webhook) to the corresponding PR comment ID. Here, we introduce a function to do that, by using GitHub API's search functionality. The commit SHA is given verbatim, which should be good enough according to the documentation at https://docs.github.com/en/search-github/searching-on-github/searching-issues-and-pull-requests#search-by-commit-sha Further, we make use of the text match metadata (https://docs.github.com/en/rest/search/search#text-match-metadata) to match the desired PR comment and to extract the PR comment ID. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent f48a238 commit b18a11b

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

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,

0 commit comments

Comments
 (0)