diff --git a/src/config.ts b/src/config.ts index b621f20..66061bd 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 } diff --git a/src/main.ts b/src/main.ts index ed433fc..7539227 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,15 +14,29 @@ async function run(): Promise { 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) {