2020-10-15 22:20:15 +08:00
import { existsSync } from 'fs'
2020-10-07 21:34:20 +08:00
import path from 'path'
2020-03-28 22:35:26 +08:00
import { isDebug } from '@actions/core'
2020-10-15 22:20:15 +08:00
import { ActionInterface , RequiredActionParameters } from './constants'
2020-03-02 20:52:38 +08:00
2020-09-13 05:05:57 +08:00
const replaceAll = ( input : string , find : string , replace : string ) : string = >
input . split ( find ) . join ( replace )
2020-03-02 20:52:38 +08:00
/* 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 === ''
2020-03-02 20:52:38 +08:00
/* Generates a token type used for the action. */
2020-03-07 11:36:56 +08:00
export const generateTokenType = ( action : ActionInterface ) : string = >
2020-03-02 20:52:38 +08:00
action . ssh
2020-03-05 21:19:45 +08:00
? 'SSH Deploy Key'
2020-03-02 20:52:38 +08:00
: action . accessToken
2020-03-05 21:19:45 +08:00
? 'Access Token'
2020-03-02 20:52:38 +08:00
: action . gitHubToken
2020-03-05 21:19:45 +08:00
? 'GitHub Token'
2020-05-11 01:34:14 +08:00
: '…'
2020-03-02 20:52:38 +08:00
/* Generates a the repository path used to make the commits. */
2020-03-07 11:36:56 +08:00
export const generateRepositoryPath = ( action : ActionInterface ) : string = >
2020-03-02 20:52:38 +08:00
action . ssh
2020-03-02 21:37:55 +08:00
? ` git@github.com: ${ action . repositoryName } `
2020-09-13 06:11:16 +08:00
: ` https:// ${
action . accessToken || ` x-access-token: ${ action . gitHubToken } `
} @github . com / $ { action . repositoryName } . git `
2020-03-02 20:52:38 +08:00
2020-10-07 21:34:20 +08:00
/* Genetate absolute folder path by the provided folder name */
2020-10-15 22:20:15 +08:00
export const generateFolderPath = ( action : ActionInterface ) : string = > {
const folderName = action [ 'folder' ]
return path . isAbsolute ( folderName )
2020-10-07 21:34:20 +08:00
? folderName
: folderName . startsWith ( '~' )
? folderName . replace ( '~' , process . env . HOME as string )
: path . join ( action . workspace , folderName )
}
2020-03-02 20:52:38 +08:00
/* Checks for the required tokens and formatting. Throws an error if any case is matched. */
2020-10-15 22:20:15 +08:00
const hasRequiredParameters = < K extends keyof RequiredActionParameters > (
action : ActionInterface ,
params : K [ ]
) : boolean = > {
const nonNullParams = params . filter (
param = > ! isNullOrUndefined ( action [ param ] )
)
return Boolean ( nonNullParams . length )
}
export const checkParameters = ( action : ActionInterface ) : void = > {
if ( ! hasRequiredParameters ( action , [ 'accessToken' , 'gitHubToken' , 'ssh' ] ) ) {
2020-03-02 20:52:38 +08:00
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.'
)
2020-03-02 20:52:38 +08:00
}
2020-10-15 22:20:15 +08:00
if ( ! hasRequiredParameters ( action , [ 'branch' ] ) ) {
2020-03-05 21:19:45 +08:00
throw new Error ( 'Branch is required.' )
2020-03-02 20:52:38 +08:00
}
2020-10-15 22:20:15 +08:00
if ( ! hasRequiredParameters ( 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-02 20:52:38 +08:00
}
2020-10-15 22:20:15 +08:00
if ( ! existsSync ( action . folderPath as string ) ) {
throw new Error (
` The ${ action . folderPath } directory you're trying to deploy doesn't exist. ❗ `
)
}
2020-03-05 21:19:45 +08:00
}
2020-03-02 20:52:38 +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-02 20:52:38 +08:00
2020-03-28 22:35:26 +08:00
if ( isDebug ( ) ) {
2020-03-02 20:52:38 +08:00
// Data is unmasked in debug mode.
2020-03-05 21:19:45 +08:00
return value
2020-03-02 20:52:38 +08:00
}
2020-09-13 05:05:57 +08:00
const orderedByLength = ( [
action . accessToken ,
action . gitHubToken ,
action . repositoryPath
] . filter ( Boolean ) as string [ ] ) . sort ( ( a , b ) = > b . length - a . length )
2020-03-02 20:52:38 +08:00
2020-09-13 05:05:57 +08:00
for ( const find of orderedByLength ) {
value = replaceAll ( value , find , '***' )
2020-03-02 20:52:38 +08:00
}
2020-03-05 21:19:45 +08:00
return value
}