env/index.js

179 lines
5.8 KiB
JavaScript
Raw Normal View History

2020-07-13 06:12:00 -04:00
// Franz Diebold
const core = require('@actions/core');
2020-07-16 05:13:18 -04:00
/**
* Slugify a given string.
* @param {string} inputString
* @return {string} The slugified string.
*/
function slugify(inputString) {
return inputString
.toLowerCase()
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
.replace(/^\s+|\s+$/g, '') // trim
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
}
2020-07-14 06:06:58 -04:00
/**
* Get the repository owner from the repository string.
* @param {string} repository
* @return {string} The owner of the repository.
*/
function getRepositoryOwner(repository) {
2020-07-14 06:06:58 -04:00
return repository ? repository.split('/')[0] : null;
}
2020-07-14 06:06:58 -04:00
/**
* Get the repository name from the repository string.
* @param {string} repository
* @return {string} The name of the repository.
*/
function getRepositoryName(repository) {
2020-07-14 06:06:58 -04:00
return repository ? repository.split('/')[1] : null;
}
2020-07-14 06:06:58 -04:00
/**
* Get the ref name from the ref string.
* @param {string} ref
* @return {string} The ref name.
*/
function getRefName(ref) {
2020-07-14 06:06:58 -04:00
return ref ? ref.split('/').slice(2).join('/') : null;
}
/**
* Get the short SHA from the full SHA.
* @param {string} fullSha
* @return {string} The short SHA.
*/
function getShaShort(fullSha) {
return fullSha ? fullSha.substring(0, 8) : null;
2020-07-13 06:12:00 -04:00
}
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables
try {
2020-07-14 06:06:58 -04:00
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
2020-07-16 05:13:18 -04:00
if (repository) {
core.exportVariable('GITHUB_REPOSITORY_SLUG', slugify(repository));
core.info(`Set GITHUB_REPOSITORY_SLUG=` +
`${process.env.GITHUB_REPOSITORY_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_SLUG".');
}
2020-07-14 06:06:58 -04:00
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
2020-07-16 05:13:18 -04:00
core.exportVariable('GITHUB_REPOSITORY_OWNER_SLUG',
slugify(repositoryOwner));
core.info(`Set GITHUB_REPOSITORY_OWNER_SLUG=` +
`${process.env.GITHUB_REPOSITORY_OWNER_SLUG}`);
2020-07-14 06:06:58 -04:00
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
2020-07-16 05:13:18 -04:00
'Cannot set "GITHUB_REPOSITORY_OWNER" and ' +
'"GITHUB_REPOSITORY_OWNER_SLUG".');
2020-07-14 06:06:58 -04:00
}
repositoryName = getRepositoryName(repository);
if (repositoryName) {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
2020-07-16 05:13:18 -04:00
core.exportVariable('GITHUB_REPOSITORY_NAME_SLUG',
slugify(repositoryName));
core.info(`Set GITHUB_REPOSITORY_NAME_SLUG=` +
`${process.env.GITHUB_REPOSITORY_NAME_SLUG}`);
2020-07-14 06:06:58 -04:00
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
2020-07-16 05:13:18 -04:00
'Cannot set "GITHUB_REPOSITORY_NAME" and ' +
'"GITHUB_REPOSITORY_NAME_SLUG".');
2020-07-14 06:06:58 -04:00
}
// i.e. refs/heads/feat/feature-branch-1
2020-07-16 05:13:18 -04:00
ref = process.env.GITHUB_REF;
if (ref) {
2020-07-16 05:38:50 -04:00
core.exportVariable('GITHUB_REF_SLUG', slugify(ref));
2020-07-16 05:13:18 -04:00
core.info(`Set GITHUB_REF_SLUG=${process.env.GITHUB_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_SLUG".');
}
refName = getRefName(ref);
2020-07-14 06:06:58 -04:00
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
2020-07-16 05:13:18 -04:00
2020-07-16 05:38:50 -04:00
core.exportVariable('GITHUB_REF_NAME_SLUG', slugify(refName));
2020-07-16 05:13:18 -04:00
core.info(`Set GITHUB_REF_NAME_SLUG=${process.env.GITHUB_REF_NAME_SLUG}`);
2020-07-14 06:06:58 -04:00
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
2020-07-16 05:13:18 -04:00
'Cannot set "GITHUB_REF_NAME" and ' +
'"GITHUB_REF_NAME_SLUG".');
2020-07-14 06:06:58 -04:00
}
2020-11-26 14:44:14 -05:00
headRef = process.env.GITHUB_HEAD_REF;
branchName = headRef || refName;
if (branchName) {
core.exportVariable('GITHUB_BRANCH_NAME', branchName);
core.info(`Set GITHUB_BRANCH_NAME=${process.env.GITHUB_BRANCH_NAME}`);
core.exportVariable('GITHUB_BRANCH_NAME_SLUG', slugify(branchName));
core.info('Set GITHUB_BRANCH_NAME_SLUG=' +
`${process.env.GITHUB_BRANCH_NAME_SLUG}`);
} else {
core.warning('Environment variables "GITHUB_REF" and ' +
'"GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_BRANCH_NAME" and ' +
'"GITHUB_BRANCH_NAME_SLUG".');
}
if (headRef) {
core.exportVariable('GITHUB_HEAD_REF_SLUG', slugify(headRef));
core.info(`Set GITHUB_HEAD_REF_SLUG=${process.env.GITHUB_HEAD_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_HEAD_REF" not set. ' +
'Cannot set "GITHUB_HEAD_REF_SLUG".');
}
baseRef = process.env.GITHUB_BASE_REF;
if (baseRef) {
core.exportVariable('GITHUB_BASE_REF_SLUG', slugify(baseRef));
core.info(`Set GITHUB_BASE_REF_SLUG=${process.env.GITHUB_BASE_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_BASE_REF" not set. ' +
'Cannot set "GITHUB_BASE_REF_SLUG".');
}
2020-07-14 06:06:58 -04:00
// i.e. ffac537e6cbbf934b08745a378932722df287a53
shaShort = getShaShort(process.env.GITHUB_SHA);
if (shaShort) {
core.exportVariable('GITHUB_SHA_SHORT', shaShort);
core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`);
} else {
core.warning('Environment variable "GITHUB_SHA" not set. ' +
'Cannot set "GITHUB_SHA_SHORT".');
}
2020-07-13 06:12:00 -04:00
} catch (error) {
2020-07-14 06:06:58 -04:00
core.setFailed(error.message);
2020-07-13 06:12:00 -04:00
}
2020-07-14 06:06:58 -04:00
module.exports = {
2020-07-16 05:13:18 -04:00
slugify,
2020-07-14 06:06:58 -04:00
getRepositoryOwner,
getRepositoryName,
getRefName,
getShaShort,
};