-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move from actions-toolkit to @actions/core
- Loading branch information
Showing
7 changed files
with
578 additions
and
822 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ jobs: | |
|
||
strategy: | ||
matrix: | ||
node-version: [14.x, 16.x, 18.x] | ||
node-version: [16.x, 18.x] | ||
|
||
env: | ||
CI: 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
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 |
---|---|---|
@@ -1,109 +1,111 @@ | ||
const { Toolkit } = require("actions-toolkit"); | ||
|
||
Toolkit.run(async (tools) => { | ||
// Process inputs for use later | ||
const mode = tools.inputs.mode; | ||
const count = parseInt(tools.inputs.count, 10); | ||
const allowedLabels = tools.inputs.labels | ||
.split(",") | ||
.map((l) => l.trim()) | ||
.filter((r) => r); | ||
|
||
const exitType = tools.inputs.exit_type || "failure"; | ||
|
||
// Validate inputs | ||
if (tools.inputs.count === "") { | ||
tools.exit.failure(`[count] input is not provided`); | ||
return; | ||
} | ||
const core = require("@actions/core"); | ||
const github = require("@actions/github"); | ||
|
||
if (allowedLabels.length === 0) { | ||
tools.exit.failure("[labels] input is empty or not provided"); | ||
return; | ||
} | ||
async function action() { | ||
try { | ||
const token = core.getInput("token", { required: true }); | ||
const octokit = github.getOctokit(token); | ||
|
||
const allowedModes = ["exactly", "minimum", "maximum"]; | ||
if (!allowedModes.includes(mode)) { | ||
tools.exit.failure( | ||
`Unknown mode input [${mode}]. Must be one of: ${allowedModes.join(", ")}` | ||
); | ||
return; | ||
} | ||
// Process inputs for use later | ||
const mode = core.getInput("mode", { required: true }); | ||
const count = parseInt(core.getInput("count", { required: true }), 10); | ||
const allowedLabels = core | ||
.getInput("labels", { required: true }) | ||
.split(",") | ||
.map((l) => l.trim()) | ||
.filter((r) => r); | ||
|
||
const allowedExitCodes = ["success", "neutral", "failure"]; | ||
if (!allowedExitCodes.includes(exitType)) { | ||
tools.exit.failure( | ||
`Unknown exit_code input [${exitType}]. Must be one of: ${allowedExitCodes.join( | ||
", " | ||
)}` | ||
); | ||
return; | ||
} | ||
const exitType = core.getInput("exit_type") || "failure"; | ||
const shouldAddComment = core.getInput("add_comment") == "true"; | ||
|
||
// If a token is provided, call the API, otherwise read the event.json file | ||
let labels; | ||
if (process.env.GITHUB_TOKEN) { | ||
labels = (await tools.github.issues.listLabelsOnIssue(tools.context.issue)) | ||
.data; | ||
} else { | ||
labels = tools.context.payload.pull_request.labels; | ||
} | ||
const allowedModes = ["exactly", "minimum", "maximum"]; | ||
if (!allowedModes.includes(mode)) { | ||
await exitWithError( | ||
exitType, | ||
octokit, | ||
shouldAddComment, | ||
`Unknown mode input [${mode}]. Must be one of: ${allowedModes.join( | ||
", " | ||
)}` | ||
); | ||
return; | ||
} | ||
|
||
const allowedExitCodes = ["success", "failure"]; | ||
if (!allowedExitCodes.includes(exitType)) { | ||
await exitWithError( | ||
exitType, | ||
octokit, | ||
shouldAddComment, | ||
`Unknown exit_code input [${exitType}]. Must be one of: ${allowedExitCodes.join( | ||
", " | ||
)}` | ||
); | ||
return; | ||
} | ||
|
||
// Fetch the labels using the API | ||
// We use the API rather than read event.json in case earlier steps | ||
// added a label | ||
const labels = ( | ||
await octokit.rest.issues.listLabelsOnIssue({ | ||
...github.context.repo, | ||
issue_number: github.context.issue.number, | ||
}) | ||
).data; | ||
|
||
const appliedLabels = labels.map((label) => label.name); | ||
const appliedLabels = labels.map((label) => label.name); | ||
|
||
// How many labels overlap? | ||
let intersection = allowedLabels.filter((x) => appliedLabels.includes(x)); | ||
// How many labels overlap? | ||
let intersection = allowedLabels.filter((x) => appliedLabels.includes(x)); | ||
|
||
if (mode === "exactly" && intersection.length !== count) { | ||
await exitWithError( | ||
tools, | ||
exitType, | ||
`Label error. Requires exactly ${count} of: ${allowedLabels.join( | ||
// Is there an error? | ||
let errorMode; | ||
if (mode === "exactly" && intersection.length !== count) { | ||
errorMode = "exactly"; | ||
} else if (mode === "minimum" && intersection.length < count) { | ||
errorMode = "at least"; | ||
} else if (mode === "maximum" && intersection.length > count) { | ||
errorMode = "at most"; | ||
} | ||
|
||
// If so, add a comment (if enabled) and fail the run | ||
if (errorMode !== undefined) { | ||
const errorMessage = `Label error. Requires ${errorMode} ${count} of: ${allowedLabels.join( | ||
", " | ||
)}. Found: ${appliedLabels.join(", ")}` | ||
); | ||
return; | ||
)}. Found: ${appliedLabels.join(", ")}`; | ||
await exitWithError(exitType, octokit, shouldAddComment, errorMessage); | ||
return; | ||
} | ||
|
||
core.setOutput("status", "success"); | ||
} catch (e) { | ||
core.setFailed(e.message); | ||
} | ||
} | ||
|
||
if (mode === "minimum" && intersection.length < count) { | ||
await exitWithError( | ||
tools, | ||
exitType, | ||
`Label error. Requires at least ${count} of: ${allowedLabels.join( | ||
", " | ||
)}. Found: ${appliedLabels.join(", ")}` | ||
); | ||
return; | ||
async function exitWithError(exitType, octokit, shouldAddComment, message) { | ||
if (shouldAddComment) { | ||
await octokit.rest.issues.createComment({ | ||
...github.context.repo, | ||
issue_number: github.context.issue.number, | ||
body: message, | ||
}); | ||
} | ||
|
||
if (mode === "maximum" && intersection.length > count) { | ||
await exitWithError( | ||
tools, | ||
exitType, | ||
`Label error. Requires at most ${count} of: ${allowedLabels.join( | ||
", " | ||
)}. Found: ${appliedLabels.join(", ")}` | ||
); | ||
core.setOutput("status", "failure"); | ||
|
||
if (exitType === "success") { | ||
core.warning(message); | ||
return; | ||
} | ||
|
||
tools.outputs.status = "success"; | ||
tools.exit.success("Complete"); | ||
}); | ||
|
||
async function exitWithError(tools, exitType, message) { | ||
if (tools.inputs.add_comment == "true") { | ||
if (process.env.GITHUB_TOKEN) { | ||
await tools.github.issues.createComment({ | ||
...tools.context.issue, | ||
body: message, | ||
}); | ||
} else { | ||
throw new Error( | ||
"The GITHUB_TOKEN environment variable must be set to add a comment" | ||
); | ||
} | ||
} | ||
tools.outputs.status = "failure"; | ||
tools.exit[exitType](message); | ||
core.setFailed(message); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
if (require.main === module) { | ||
action(); | ||
} | ||
|
||
module.exports = action; |
Oops, something went wrong.