github-pages-deploy-action/src/util.ts

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-03-05 21:19:45 +08:00
import {getInput} from '@actions/core'
2020-03-07 11:36:56 +08:00
import {ActionInterface} from './constants'
/* Utility function that checks to see if a value is undefined or not. */
export const isNullOrUndefined = (value: any): boolean =>
2020-03-05 21:19:45 +08:00
typeof value === 'undefined' || value === null || value === ''
/* Generates a token type used for the action. */
2020-03-07 11:36:56 +08:00
export const generateTokenType = (action: ActionInterface): string =>
action.ssh
2020-03-05 21:19:45 +08:00
? 'SSH Deploy Key'
: action.accessToken
2020-03-05 21:19:45 +08:00
? 'Access Token'
: action.gitHubToken
2020-03-05 21:19:45 +08:00
? 'GitHub Token'
: '...'
/* Generates a the repository path used to make the commits. */
2020-03-07 11:36:56 +08:00
export const generateRepositoryPath = (action: ActionInterface): string =>
action.ssh
2020-03-02 21:37:55 +08:00
? `git@github.com:${action.repositoryName}`
: `https://${action.accessToken ||
`x-access-token:${action.gitHubToken}`}@github.com/${
2020-03-02 21:37:55 +08:00
action.repositoryName
2020-03-05 21:19:45 +08:00
}.git`
/* Checks for the required tokens and formatting. Throws an error if any case is matched. */
2020-03-07 11:36:56 +08:00
export const hasRequiredParameters = (action: ActionInterface): void => {
if (
(isNullOrUndefined(action.accessToken) &&
isNullOrUndefined(action.gitHubToken) &&
isNullOrUndefined(action.ssh)) ||
isNullOrUndefined(action.repositoryPath)
) {
throw new Error(
2020-03-05 21:19:45 +08:00
'No deployment token/method was provided. You must provide the action with either a Personal Access Token or the GitHub Token secret in order to deploy. If you wish to use an ssh deploy token then you must set SSH to true.'
)
}
if (isNullOrUndefined(action.branch)) {
2020-03-05 21:19:45 +08:00
throw new Error('Branch is required.')
}
if (!action.folder || isNullOrUndefined(action.folder)) {
2020-03-05 21:19:45 +08:00
throw new Error('You must provide the action with a folder to deploy.')
}
2020-03-05 21:19:45 +08:00
if (action.folder.startsWith('/') || action.folder.startsWith('./')) {
throw new Error(
"Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly."
2020-03-05 21:19:45 +08:00
)
}
2020-03-05 21:19:45 +08:00
}
/* Suppresses sensitive information from being exposed in error messages. */
export const suppressSensitiveInformation = (
str: string,
2020-03-07 11:36:56 +08:00
action: ActionInterface
): string => {
2020-03-05 21:19:45 +08:00
let value = str
2020-03-05 21:19:45 +08:00
if (getInput('DEBUG')) {
// Data is unmasked in debug mode.
2020-03-05 21:19:45 +08:00
return value
}
if (action.accessToken) {
2020-03-05 21:19:45 +08:00
value = value.replace(action.accessToken, '***')
}
if (action.gitHubToken) {
2020-03-05 21:19:45 +08:00
value = value.replace(action.gitHubToken, '***')
}
if (action.repositoryPath) {
2020-03-05 21:19:45 +08:00
value = value.replace(action.repositoryPath, '***')
}
2020-03-05 21:19:45 +08:00
return value
}