Skip to content

Commit

Permalink
provide error checking on inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
airtonix committed Oct 23, 2023
1 parent 313a222 commit 565067f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
67 changes: 62 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,77 @@
import * as core from '@actions/core'

export type Action = 'check' | 'uncheck' | 'detect' | 'current-detect'
/**
* Action
*/
export const actions = ['check', 'uncheck', 'detect', 'current-detect'] as const

const isAction = (action: string): action is Action => {
return actions.includes(action as Action)
}

const getAction = (): Action => {
const action = core.getInput('action', {required: true})
if (!isAction(action)) {
throw new Error(`Invalid action: ${action}`)
}
return action
}

export type Action = (typeof actions)[number]
export const action = getAction()

/**
* Error Level
*/
const errors = ['error', 'warn', 'ignore'] as const
const isError = (error: string): error is ErrorLevel => {
return errors.includes(error as ErrorLevel)
}
const getError = (): ErrorLevel => {
const error = core.getInput('error', {required: true})
if (!isError(error)) {
throw new Error(`Invalid error: ${error}`)
}
return error
}

/**
* How verbose should feedback be?
*/
export type ErrorLevel = 'error' | 'warn' | 'ignore'
export const error = getError()

/**
* List of checkboxes to operate on
*/
export const list = core.getMultilineInput('list', {required: false})

/**
* A regex to match checkboxes which should be operated on
*/
export const matches = core.getInput('matches', {required: false})
export const action: Action = core.getInput('action', {
required: true
}) as Action
export const error = core.getInput('error', {required: true}) as ErrorLevel

export const token = core.getInput('GITHUB_TOKEN', {required: true})

export type ConfigPayload = {
/**
* @see {@link list}
*/
list: string[]
/**
* @see {@link matches}
*/
matches: string
/**
* @see {@link Action}
*/
action: Action
/**
* @see {@link ErrorLevel}
*/
error: ErrorLevel
/**
* @see {@link token}
*/
token: string
}
22 changes: 18 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ async function run(): Promise<void> {
if (action === 'detect') {
const previousBody = getPreviousBody()
const currentBody = getCurrentBody()
const changed = getDiff(previousBody, currentBody)
const checked = getCurrentChecked(currentBody)
const unchecked = getCurrentUnchecked(currentBody)
const {checked, unchecked} = getDiff(
previousBody.split('\n'),
currentBody.split('\n')
)

core.setOutput('checked', JSON.stringify(checked))
core.setOutput('unchecked', JSON.stringify(unchecked))
core.setOutput('changed', JSON.stringify(changed))
return
}

if (action === 'current-detect') {
const currentBody = getCurrentBody()
const checked = getCurrentChecked(currentBody.split('\n'))
const unchecked = getCurrentUnchecked(currentBody.split('\n'))

core.setOutput('checked', JSON.stringify(checked))
core.setOutput('unchecked', JSON.stringify(unchecked))
return
}

/**
* check or uncheck action
*/
let body = await getBody()
if (!contains(body)) {
switch (error) {
Expand Down

0 comments on commit 565067f

Please sign in to comment.