-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update job to comment built resources on PR
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"bot": { | ||
"issues": { | ||
"enabled": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Used in `/.github/workflows/test-build-resources-with-pandoc.yml` | ||
* @param {object} params | ||
* @param {import('@actions/github/lib/utils').GitHub} params.github | ||
* @param {import('@actions/github/lib/context').Context} params.context | ||
* @param {string} params.url | ||
*/ | ||
export async function postCustomForArchiveResources({ github, context, url }) { | ||
const sha = | ||
context.eventName === "pull_request" | ||
? context.payload.pull_request.head.sha | ||
: context.payload.after; | ||
const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`; | ||
|
||
const pullRequestNumber = await getPullRequestNumber(); | ||
|
||
const botCommentIdentifier = | ||
"<!-- posted by scripts/pr-comment.mjs#postCustomForArchiveResources -->"; | ||
|
||
const body = `${botCommentIdentifier} | ||
## Pandocで生成したリソースの確認 | ||
<${url}> | ||
--- | ||
[View Commit](${commitUrl})`; | ||
|
||
if (pullRequestNumber) { | ||
await createOrUpdateComment(pullRequestNumber); | ||
} else { | ||
console.log( | ||
"No open pull request found for this push. Logging publish information to console:" | ||
); | ||
console.log(`\n${"=".repeat(50)}`); | ||
console.log(body); | ||
console.log(`\n${"=".repeat(50)}`); | ||
} | ||
|
||
async function getPullRequestNumber() { | ||
if (context.eventName === "pull_request") { | ||
if (context.issue.number) { | ||
return context.issue.number; | ||
} | ||
} else if (context.eventName === "push") { | ||
const pullRequests = await github.rest.pulls.list({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
state: "open", | ||
head: `${context.repo.owner}:${context.ref.replace("refs/heads/", "")}`, | ||
}); | ||
|
||
if (pullRequests.data.length > 0) { | ||
return pullRequests.data[0].number; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
async function findBotComment(issueNumber) { | ||
const comments = await github.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: issueNumber, | ||
}); | ||
|
||
return comments.data.find((comment) => | ||
comment.body.includes(botCommentIdentifier) | ||
); | ||
} | ||
|
||
async function createOrUpdateComment(issueNumber) { | ||
const existingComment = await findBotComment(issueNumber); | ||
|
||
if (existingComment) { | ||
await github.rest.issues.updateComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
comment_id: existingComment.id, | ||
body, | ||
}); | ||
} else { | ||
await github.rest.issues.createComment({ | ||
issue_number: issueNumber, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body, | ||
}); | ||
} | ||
} | ||
} |