Skip to content

Commit

Permalink
Make Action.res more idiomatic
Browse files Browse the repository at this point in the history
  • Loading branch information
brettcannon committed May 19, 2024
1 parent ffe662c commit dcd2c40
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
25 changes: 17 additions & 8 deletions src/Action.res
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type inputsType = {
filePattern: string,
preReqPattern: string,
skipLabel: string,
skipLabel: option<string>,
failureMessage: string,
token: string,
token: option<string>,
}

// Could use a `.resi` file to control visibility, but it requires repeating
Expand All @@ -14,10 +14,19 @@ type inputsType = {
)

let inputs = () => {
filePattern: getInput("file-pattern"),
preReqPattern: getInput("prereq-pattern"),
failureMessage: getInput("failure-message"),
// Optional
skipLabel: getInput("skip-label"),
token: getInput("token"),
let maybe = val => {
if val != "" {
Some(val)
} else {
None
}
}

{
filePattern: getInput("file-pattern"),
preReqPattern: getInput("prereq-pattern"),
failureMessage: getInput("failure-message"),
skipLabel: getInput("skip-label")->maybe,
token: getInput("token")->maybe,
}
}
4 changes: 2 additions & 2 deletions src/GH.res
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,9 @@ let pullRequestLabels = payload =>
*/
let changedFiles = async (payload, inputs: Action.inputsType) => {
let octokit: octokitType = switch inputs.token {
| "" => %raw(`new (Octokit.plugin(paginateRest))()`)
| None => %raw(`new (Octokit.plugin(paginateRest))()`)
// While marked as ignored, `_token` is used inside the %raw() call.
| _token => %raw(`new (Octokit.plugin(paginateRest))({ auth: _token })`)
| Some(_token) => %raw(`new (Octokit.plugin(paginateRest))({ auth: _token })`)
}

await octokit->paginate(
Expand Down
10 changes: 5 additions & 5 deletions src/Main.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let formatFailureMessage = (inputs: Action.inputsType) =>
inputs.failureMessage
->String.replaceAll("${prereq-pattern}", repr(inputs.preReqPattern))
->String.replaceAll("${file-pattern}", repr(inputs.filePattern))
->String.replaceAll("${skip-label}", repr(inputs.skipLabel))
->String.replaceAll("${skip-label}", repr(inputs.skipLabel->Option.getOr("")))

/**
Check if the workflow has succeeded in passing the check.
Expand All @@ -33,10 +33,10 @@ let checkforChangedFiles = async (
)} is not a full 'pull_request' event; skipping`,
)
| Some(payload) =>
// Because `skip-label` defaults to `""`,
// this check will implicitly fail if no label is specified.
switch payload->GH.pullRequestLabels->Matching.hasLabelMatch(inputs.skipLabel) {
| true => Ok(`the skip label ${repr(inputs.skipLabel)} is set`)
let prLabels = payload->GH.pullRequestLabels
let hasLabelMatch = inputs.skipLabel->Option.mapOr(false, Matching.hasLabelMatch(prLabels, _))
switch hasLabelMatch {
| true => Ok(`the skip label ${repr(inputs.skipLabel->Option.getOr(""))} is set`)
| false => {
let filePaths = await payload->_changedFilesImpl(inputs)

Expand Down
4 changes: 2 additions & 2 deletions tests/Action_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ zora("action.yml", async t => {
failureMessage: template,
preReqPattern,
filePattern,
skipLabel,
token: "",
skipLabel: Some(skipLabel),
token: None,
}

let errorMessage = inputs->Main.formatFailureMessage
Expand Down
14 changes: 7 additions & 7 deletions tests/Main_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ zora("formatFailureMessage()", async t => {
failureMessage: template,
preReqPattern,
filePattern,
skipLabel,
token: "",
skipLabel: Some(skipLabel),
token: None,
}

let errorMessage = inputs->Main.formatFailureMessage
Expand Down Expand Up @@ -54,9 +54,9 @@ zora("checkForChangedFiles()", async t => {
let inputs: Action.inputsType = {
filePattern: "Dir2/B",
preReqPattern: "**",
skipLabel: "Label B",
skipLabel: Some("Label B"),
failureMessage: "${prereq-pattern} ${file-pattern} ${skip-label}",
token: "",
token: None,
}

let fakeChangedFiles = async (_, _) => ["Dir3/C"]
Expand All @@ -65,17 +65,17 @@ zora("checkForChangedFiles()", async t => {
let inputs: Action.inputsType = {
filePattern: "Dir1/B",
preReqPattern: "**",
skipLabel: "",
skipLabel: None,
failureMessage: "${prereq-pattern} ${file-pattern} ${skip-label}",
token: "",
token: None,
}

t->okContains(await None->Main.checkforChangedFiles(inputs), "pull_request")
})

t->test("skip label set", async t => {
let skipLabel = "Label A"
let inputs = {...inputs, skipLabel}
let inputs = {...inputs, skipLabel: Some(skipLabel)}

t->okContains(
await payload->Main.checkforChangedFiles(inputs, ~_changedFilesImpl=fakeChangedFiles),
Expand Down

0 comments on commit dcd2c40

Please sign in to comment.