2021-02-08 12:58:32 +08:00
"use strict" ;
var _ _importDefault = ( this && this . _ _importDefault ) || function ( mod ) {
return ( mod && mod . _ _esModule ) ? mod : { "default" : mod } ;
} ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2021-11-18 20:58:07 +08:00
exports . stripProtocolFromUrl = exports . extractErrorMessage = exports . suppressSensitiveInformation = exports . checkParameters = exports . generateFolderPath = exports . generateRepositoryPath = exports . generateTokenType = exports . isNullOrUndefined = void 0 ;
2021-02-08 12:58:32 +08:00
const core _1 = require ( "@actions/core" ) ;
const fs _1 = require ( "fs" ) ;
const path _1 = _ _importDefault ( require ( "path" ) ) ;
/* Replaces all instances of a match in a string. */
const replaceAll = ( input , find , replace ) => input . split ( find ) . join ( replace ) ;
2021-11-18 20:58:07 +08:00
/ * U t i l i t y f u n c t i o n t h a t c h e c k s t o s e e i f a v a l u e i s u n d e f i n e d o r n o t .
If allowEmptyString is passed the parameter is allowed to contain an empty string as a valid parameter . * /
const isNullOrUndefined = ( value , allowEmptyString = false ) => allowEmptyString
? typeof value === 'undefined' || value === null
: typeof value === 'undefined' || value === null || value === '' ;
2021-03-04 22:04:23 +08:00
exports . isNullOrUndefined = isNullOrUndefined ;
2021-02-08 12:58:32 +08:00
/* Generates a token type used for the action. */
2021-03-04 22:04:23 +08:00
const generateTokenType = ( action ) => action . sshKey ? 'SSH Deploy Key' : action . token ? 'Deploy Token' : '…' ;
exports . generateTokenType = generateTokenType ;
2021-02-08 12:58:32 +08:00
/* Generates a the repository path used to make the commits. */
2021-03-04 22:04:23 +08:00
const generateRepositoryPath = ( action ) => action . sshKey
? ` git@ ${ action . hostname } : ${ action . repositoryName } `
: ` https:// ${ ` x-access-token: ${ action . token } ` } @ ${ action . hostname } / ${ action . repositoryName } .git ` ;
exports . generateRepositoryPath = generateRepositoryPath ;
2021-02-08 12:58:32 +08:00
/* Genetate absolute folder path by the provided folder name */
2021-03-04 22:04:23 +08:00
const generateFolderPath = ( action ) => {
2021-02-08 12:58:32 +08:00
const folderName = action [ 'folder' ] ;
return path _1 . default . isAbsolute ( folderName )
? folderName
: folderName . startsWith ( '~' )
? folderName . replace ( '~' , process . env . HOME )
: path _1 . default . join ( action . workspace , folderName ) ;
} ;
2021-03-04 22:04:23 +08:00
exports . generateFolderPath = generateFolderPath ;
2021-02-08 12:58:32 +08:00
/* Checks for the required tokens and formatting. Throws an error if any case is matched. */
const hasRequiredParameters = ( action , params ) => {
2021-11-18 20:58:07 +08:00
const nonNullParams = params . filter ( param => ! ( 0 , exports . isNullOrUndefined ) ( action [ param ] ) ) ;
2021-02-08 12:58:32 +08:00
return Boolean ( nonNullParams . length ) ;
} ;
/* Verifies the action has the required parameters to run, otherwise throw an error. */
2021-03-04 22:04:23 +08:00
const checkParameters = ( action ) => {
2021-02-08 12:58:32 +08:00
if ( ! hasRequiredParameters ( action , [ 'token' , 'sshKey' ] ) ) {
throw new Error ( '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 ( ! hasRequiredParameters ( action , [ 'branch' ] ) ) {
throw new Error ( 'Branch is required.' ) ;
}
if ( ! hasRequiredParameters ( action , [ 'folder' ] ) ) {
throw new Error ( 'You must provide the action with a folder to deploy.' ) ;
}
2021-11-18 20:58:07 +08:00
if ( ! ( 0 , fs _1 . existsSync ) ( action . folderPath ) ) {
2021-02-08 12:58:32 +08:00
throw new Error ( ` The directory you're trying to deploy named ${ action . folderPath } doesn't exist. Please double check the path and any prerequisite build scripts and try again. ❗ ` ) ;
}
} ;
2021-03-04 22:04:23 +08:00
exports . checkParameters = checkParameters ;
2021-02-08 12:58:32 +08:00
/* Suppresses sensitive information from being exposed in error messages. */
2021-03-04 22:04:23 +08:00
const suppressSensitiveInformation = ( str , action ) => {
2021-02-08 12:58:32 +08:00
let value = str ;
2021-11-18 20:58:07 +08:00
if ( ( 0 , core _1 . isDebug ) ( ) ) {
2021-02-08 12:58:32 +08:00
// Data is unmasked in debug mode.
return value ;
}
const orderedByLength = [ action . token , action . repositoryPath ] . filter ( Boolean ) . sort ( ( a , b ) => b . length - a . length ) ;
for ( const find of orderedByLength ) {
value = replaceAll ( value , find , '***' ) ;
}
return value ;
} ;
2021-03-04 22:04:23 +08:00
exports . suppressSensitiveInformation = suppressSensitiveInformation ;
2021-11-18 20:58:07 +08:00
const extractErrorMessage = ( error ) => error instanceof Error
? error . message
: typeof error == 'string'
? error
: JSON . stringify ( error ) ;
exports . extractErrorMessage = extractErrorMessage ;
2021-03-04 22:04:23 +08:00
/** Strips the protocol from a provided URL. */
const stripProtocolFromUrl = ( url ) => url . replace ( /^(?:https?:\/\/)?(?:www\.)?/i , '' ) . split ( '/' ) [ 0 ] ;
exports . stripProtocolFromUrl = stripProtocolFromUrl ;