github-pages-deploy-action/lib/util.js

54 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-03-02 22:38:52 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@actions/core");
/* Utility function that checks to see if a value is undefined or not. */
2020-03-07 11:45:40 +08:00
exports.isNullOrUndefined = (value) => typeof value === 'undefined' || value === null || value === '';
2020-03-02 22:38:52 +08:00
/* Generates a token type used for the action. */
exports.generateTokenType = (action) => action.ssh
2020-03-07 11:45:40 +08:00
? 'SSH Deploy Key'
2020-03-02 22:38:52 +08:00
: action.accessToken
2020-03-07 11:45:40 +08:00
? 'Access Token'
2020-03-02 22:38:52 +08:00
: action.gitHubToken
2020-03-07 11:45:40 +08:00
? 'GitHub Token'
: '...';
2020-03-02 22:38:52 +08:00
/* Generates a the repository path used to make the commits. */
exports.generateRepositoryPath = (action) => action.ssh
? `git@github.com:${action.repositoryName}`
2020-03-31 20:48:28 +08:00
: `https://${action.accessToken || `x-access-token:${action.gitHubToken}`}@github.com/${action.repositoryName}.git`;
2020-03-02 22:38:52 +08:00
/* Checks for the required tokens and formatting. Throws an error if any case is matched. */
exports.hasRequiredParameters = (action) => {
if ((exports.isNullOrUndefined(action.accessToken) &&
exports.isNullOrUndefined(action.gitHubToken) &&
exports.isNullOrUndefined(action.ssh)) ||
exports.isNullOrUndefined(action.repositoryPath)) {
2020-03-07 11:45:40 +08:00
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.');
2020-03-02 22:38:52 +08:00
}
if (exports.isNullOrUndefined(action.branch)) {
2020-03-07 11:45:40 +08:00
throw new Error('Branch is required.');
2020-03-02 22:38:52 +08:00
}
if (!action.folder || exports.isNullOrUndefined(action.folder)) {
2020-03-07 11:45:40 +08:00
throw new Error('You must provide the action with a folder to deploy.');
2020-03-02 22:38:52 +08:00
}
2020-03-07 11:45:40 +08:00
if (action.folder.startsWith('/') || action.folder.startsWith('./')) {
2020-03-02 22:38:52 +08:00
throw new Error("Incorrectly formatted build folder. The deployment folder cannot be prefixed with '/' or './'. Instead reference the folder name directly.");
}
};
/* Suppresses sensitive information from being exposed in error messages. */
exports.suppressSensitiveInformation = (str, action) => {
let value = str;
2020-03-31 20:48:28 +08:00
if (core_1.isDebug()) {
2020-03-02 22:38:52 +08:00
// Data is unmasked in debug mode.
return value;
}
if (action.accessToken) {
2020-03-07 11:45:40 +08:00
value = value.replace(action.accessToken, '***');
2020-03-02 22:38:52 +08:00
}
if (action.gitHubToken) {
2020-03-07 11:45:40 +08:00
value = value.replace(action.gitHubToken, '***');
2020-03-02 22:38:52 +08:00
}
if (action.repositoryPath) {
2020-03-07 11:45:40 +08:00
value = value.replace(action.repositoryPath, '***');
2020-03-02 22:38:52 +08:00
}
return value;
};