-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate out github-scripts to external files (#187)
- Loading branch information
Showing
9 changed files
with
206 additions
and
155 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,20 @@ | ||
module.exports = async ({ github, context, core }) => { | ||
// Fetch the pull request to get the head SHA. | ||
const { data: get_pull_request } = await github.rest.pulls.get({ | ||
owner: context.repo.owner, | ||
pull_number: context.issue.number, | ||
repo: context.repo.repo, | ||
}); | ||
|
||
// Add a pending status to the pull request. | ||
const add_pending_status = await github.rest.repos.createCommitStatus({ | ||
context: context.workflow, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
sha: get_pull_request.head.sha, | ||
state: "pending", | ||
target_url: `${context.payload.repository.html_url}/actions/runs/${context.runId}`, | ||
}); | ||
|
||
core.setOutput("sha", get_pull_request.head.sha); | ||
}; |
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,19 @@ | ||
module.exports = async ({ github, context, label, cli_uses }) => { | ||
// Add a TF command label to the pull request. | ||
const add_label = await github.rest.issues.addLabels({ | ||
issue_number: context.issue.number, | ||
labels: [`tf:${label}`], | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
|
||
// Update the TF command label color to match the CLI used. | ||
const color = cli_uses === "tofu" ? "FFDA18" : "5C4EE5"; | ||
const update_label = await github.rest.issues.updateLabel({ | ||
color: color, | ||
description: `Pull requests that ${label} TF code`, | ||
name: `tf:${label}`, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
}; |
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,12 @@ | ||
module.exports = async ({ github, context, core }) => { | ||
// Add a reaction to the comment to indicate it is being processed. | ||
const { data: add_reaction } = | ||
await github.rest.reactions.createForIssueComment({ | ||
comment_id: process.env.comment_id, | ||
content: "eyes", | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
|
||
core.setOutput("id", add_reaction.id); | ||
}; |
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,13 @@ | ||
module.exports = async ({ github, context, core }) => { | ||
// Fetch the pull request to get the list of reviews. | ||
const { data: list_reviews } = await github.rest.pulls.listReviews({ | ||
owner: context.repo.owner, | ||
pull_number: context.issue.number, | ||
repo: context.repo.repo, | ||
}); | ||
|
||
// Check if the latest review is approved. | ||
if (list_reviews.at(-1)?.state !== "APPROVED") { | ||
core.setFailed("PR review approval is required when apply_require_approval is set to true."); | ||
} | ||
}; |
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,74 @@ | ||
module.exports = async ({ github, context }) => { | ||
// Display latest TF change summary as the output header. | ||
const comment_summary = process.env.tf_output | ||
.split("\n") | ||
.reverse() | ||
.find((line) => /^(Apply|Plan|Error|No changes)/.test(line)) || | ||
"View TF result…"; | ||
|
||
// Display truncated TF fmt diff, if present. | ||
const comment_fmt = process.env.tf_fmt ? | ||
`<details><summary>Diff of format changes.</summary> | ||
\`\`\`diff | ||
${process.env.tf_fmt} | ||
\`\`\` | ||
</details>` : | ||
""; | ||
|
||
// Display the: TF command, TF output, and workflow authorship. | ||
// Include the TFPLAN name in a hidden footer as a unique identifier. | ||
const comment_body = ` | ||
\`${process.env.tf_command}\` | ||
${comment_fmt} | ||
<details><summary>${comment_summary}</br> | ||
###### ${context.workflow} by @${context.actor} via [${context.eventName}](${context.payload.repository.html_url}/actions/runs/${context.runId}) at ${context.payload.pull_request?.updated_at || context.payload.comment?.updated_at}.</summary> | ||
\`\`\`hcl | ||
${process.env.tf_output} | ||
\`\`\` | ||
</details> | ||
<!-- ${process.env.tf_plan_id} -->`; | ||
|
||
// Check if the bot has commented on the PR using the TFPLAN identifier. | ||
const { data: list_comments } = await github.rest.issues.listComments({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
per_page: 100, | ||
repo: context.repo.repo, | ||
}); | ||
const bot_comment = list_comments.find((comment) => { | ||
return ( | ||
comment.user.type === "Bot" && | ||
comment.body.includes(`<!-- ${process.env.tf_plan_id} -->`) | ||
); | ||
}); | ||
|
||
// Delete PR comment reaction to indicate that the workflow has ended. | ||
const delete_reaction = await github.rest.reactions.deleteForIssueComment({ | ||
comment_id: process.env.comment_id, | ||
owner: context.repo.owner, | ||
reaction_id: process.env.reaction_id, | ||
repo: context.repo.repo, | ||
}); | ||
|
||
// If a bot comment exists with a matching TFPLAN identifier, then update | ||
// the comment, otherwise create a new comment. This prevents the bot | ||
// from creating a new comment on every run of this workflow. | ||
if (bot_comment) { | ||
await github.rest.issues.updateComment({ | ||
body: comment_body, | ||
comment_id: bot_comment.id, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
} else { | ||
await github.rest.issues.createComment({ | ||
body: comment_body, | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
}); | ||
} | ||
}; |
Oops, something went wrong.