Skip to content

Commit

Permalink
Add a function to find the /git-artifacts PR comment for a given co…
Browse files Browse the repository at this point in the history
…mmit 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 <[email protected]>
  • Loading branch information
dscho committed Nov 10, 2023
1 parent f48a238 commit b18a11b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit b18a11b

Please sign in to comment.