2020-07-13 06:12:00 -04:00
|
|
|
// Franz Diebold
|
|
|
|
|
|
|
|
const core = require('@actions/core');
|
2021-05-03 04:24:10 -04:00
|
|
|
const github = require('@actions/github');
|
2020-07-13 06:12:00 -04:00
|
|
|
|
2020-07-16 05:13:18 -04:00
|
|
|
/**
|
2022-09-11 10:19:45 -04:00
|
|
|
* Slugify a given string to a maximum length.
|
2020-07-16 05:13:18 -04:00
|
|
|
* @param {string} inputString
|
2022-09-11 10:19:45 -04:00
|
|
|
* @param {int} maxLength
|
2020-07-16 05:13:18 -04:00
|
|
|
* @return {string} The slugified string.
|
|
|
|
*/
|
2022-09-11 10:19:45 -04:00
|
|
|
function slugify(inputString, maxLength = 63) {
|
2020-07-16 05:13:18 -04:00
|
|
|
return inputString
|
2021-05-02 14:11:22 -04:00
|
|
|
.toLowerCase()
|
|
|
|
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
|
|
|
|
.replace(/^\s+|\s+$/g, '') // trim
|
|
|
|
.replace(/\s+/g, '-') // collapse whitespace and replace by -
|
2022-09-11 10:19:45 -04:00
|
|
|
.replace(/-+/g, '-') // collapse dashes
|
|
|
|
.slice(0, maxLength) // truncate to maximum length
|
|
|
|
.replace(/[-]+$/g, ''); // trim trailing -
|
2020-07-16 05:13:18 -04:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-07-14 04:27:45 -04:00
|
|
|
function getRepositoryOwner(repository) {
|
2020-07-14 06:06:58 -04:00
|
|
|
return repository ? repository.split('/')[0] : null;
|
2020-07-14 04:27:45 -04:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-07-14 04:27:45 -04:00
|
|
|
function getRepositoryName(repository) {
|
2020-07-14 06:06:58 -04:00
|
|
|
return repository ? repository.split('/')[1] : null;
|
2020-07-14 04:27:45 -04:00
|
|
|
}
|
|
|
|
|
2020-07-14 06:06:58 -04:00
|
|
|
/**
|
|
|
|
* Get the ref name from the ref string.
|
|
|
|
* @param {string} ref
|
|
|
|
* @return {string} The ref name.
|
|
|
|
*/
|
2020-07-14 04:27:45 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
// https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#default-environment-variables
|
|
|
|
|
2020-07-13 06:12:00 -04:00
|
|
|
try {
|
2020-07-14 06:06:58 -04:00
|
|
|
// i.e. FranzDiebold/github-env-vars-action
|
2021-05-03 04:24:10 -04:00
|
|
|
const repository = process.env.GITHUB_REPOSITORY;
|
2020-07-14 06:06:58 -04:00
|
|
|
|
2020-07-16 05:13:18 -04:00
|
|
|
if (repository) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY_SLUG', slugify(repository));
|
|
|
|
core.info(`Set CI_REPOSITORY_SLUG=` +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_REPOSITORY_SLUG}`);
|
2020-07-16 05:13:18 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
|
|
|
|
'Cannot set "CI_REPOSITORY_SLUG".');
|
2020-07-16 05:13:18 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const repositoryOwner = getRepositoryOwner(repository);
|
2020-07-14 06:06:58 -04:00
|
|
|
if (repositoryOwner) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY_OWNER', repositoryOwner);
|
|
|
|
core.info(`Set CI_REPOSITORY_OWNER=` +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_REPOSITORY_OWNER}`);
|
2020-07-16 05:13:18 -04:00
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY_OWNER_SLUG',
|
2021-05-02 14:11:22 -04:00
|
|
|
slugify(repositoryOwner));
|
2020-12-01 06:41:42 -05:00
|
|
|
core.info(`Set CI_REPOSITORY_OWNER_SLUG=` +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_REPOSITORY_OWNER_SLUG}`);
|
2020-07-14 06:06:58 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
|
|
|
|
'Cannot set "CI_REPOSITORY_OWNER" and ' +
|
|
|
|
'"CI_REPOSITORY_OWNER_SLUG".');
|
2020-07-14 06:06:58 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const repositoryName = getRepositoryName(repository);
|
2020-07-14 06:06:58 -04:00
|
|
|
if (repositoryName) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY_NAME', repositoryName);
|
|
|
|
core.info(`Set CI_REPOSITORY_NAME=` +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_REPOSITORY_NAME}`);
|
2020-07-16 05:13:18 -04:00
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY_NAME_SLUG',
|
2021-05-02 14:11:22 -04:00
|
|
|
slugify(repositoryName));
|
2020-12-01 06:41:42 -05:00
|
|
|
core.info(`Set CI_REPOSITORY_NAME_SLUG=` +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_REPOSITORY_NAME_SLUG}`);
|
2020-07-14 06:06:58 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_REPOSITORY" not set. ' +
|
|
|
|
'Cannot set "CI_REPOSITORY_NAME" and ' +
|
|
|
|
'"CI_REPOSITORY_NAME_SLUG".');
|
2020-07-14 06:06:58 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REPOSITORY', repository);
|
|
|
|
core.info(`Set CI_REPOSITORY=${process.env.CI_REPOSITORY}`);
|
|
|
|
|
2020-07-14 06:06:58 -04:00
|
|
|
// i.e. refs/heads/feat/feature-branch-1
|
2021-05-03 04:24:10 -04:00
|
|
|
const ref = process.env.GITHUB_REF;
|
2020-07-16 05:13:18 -04:00
|
|
|
|
|
|
|
if (ref) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REF_SLUG', slugify(ref));
|
|
|
|
core.info(`Set CI_REF_SLUG=${process.env.CI_REF_SLUG}`);
|
2020-07-16 05:13:18 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_REF" not set. ' +
|
|
|
|
'Cannot set "CI_REF_SLUG".');
|
2020-07-16 05:13:18 -04:00
|
|
|
}
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const refName = getRefName(ref);
|
2020-07-14 06:06:58 -04:00
|
|
|
if (refName) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REF_NAME', refName);
|
|
|
|
core.info(`Set CI_REF_NAME=${process.env.CI_REF_NAME}`);
|
2020-07-16 05:13:18 -04:00
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REF_NAME_SLUG', slugify(refName));
|
|
|
|
core.info(`Set CI_REF_NAME_SLUG=${process.env.CI_REF_NAME_SLUG}`);
|
2020-07-14 06:06:58 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_REF" not set. ' +
|
|
|
|
'Cannot set "CI_REF_NAME" and ' +
|
|
|
|
'"CI_REF_NAME_SLUG".');
|
2020-07-14 06:06:58 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_REF', ref);
|
|
|
|
core.info(`Set CI_REF=${process.env.CI_REF}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const headRef = process.env.GITHUB_HEAD_REF;
|
2020-11-26 14:44:14 -05:00
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const branchName = headRef || refName;
|
2020-11-26 14:44:14 -05:00
|
|
|
if (branchName) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_ACTION_REF_NAME', branchName);
|
|
|
|
core.info(`Set CI_ACTION_REF_NAME=${process.env.CI_ACTION_REF_NAME}`);
|
2020-11-26 14:44:14 -05:00
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_ACTION_REF_NAME_SLUG', slugify(branchName));
|
|
|
|
core.info('Set CI_ACTION_REF_NAME_SLUG=' +
|
2021-05-02 13:50:33 -04:00
|
|
|
`${process.env.CI_ACTION_REF_NAME_SLUG}`);
|
2020-11-26 14:44:14 -05:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variables "GITHUB_REF" and ' +
|
|
|
|
'"GITHUB_HEAD_REF" not set. ' +
|
|
|
|
'Cannot set "CI_ACTION_REF_NAME" and ' +
|
|
|
|
'"CI_ACTION_REF_NAME_SLUG".');
|
2020-11-26 14:44:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (headRef) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_HEAD_REF_SLUG', slugify(headRef));
|
|
|
|
core.info(`Set CI_HEAD_REF_SLUG=${process.env.CI_HEAD_REF_SLUG}`);
|
2020-11-26 14:44:14 -05:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_HEAD_REF" not set. ' +
|
|
|
|
'Cannot set "CI_HEAD_REF_SLUG".');
|
2020-11-26 14:44:14 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_HEAD_REF', headRef);
|
|
|
|
core.info(`Set CI_HEAD_REF=${process.env.CI_HEAD_REF}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const baseRef = process.env.GITHUB_BASE_REF;
|
2020-11-26 14:44:14 -05:00
|
|
|
if (baseRef) {
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_BASE_REF_SLUG', slugify(baseRef));
|
|
|
|
core.info(`Set CI_BASE_REF_SLUG=${process.env.CI_BASE_REF_SLUG}`);
|
2020-11-26 14:44:14 -05:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_BASE_REF" not set. ' +
|
|
|
|
'Cannot set "CI_BASE_REF_SLUG".');
|
2020-11-26 14:44:14 -05:00
|
|
|
}
|
|
|
|
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_BASE_REF', baseRef);
|
|
|
|
core.info(`Set CI_BASE_REF=${process.env.CI_BASE_REF}`);
|
|
|
|
|
2020-07-14 06:06:58 -04:00
|
|
|
// i.e. ffac537e6cbbf934b08745a378932722df287a53
|
2021-05-03 04:24:10 -04:00
|
|
|
const sha = process.env.GITHUB_SHA;
|
2020-12-01 06:41:42 -05:00
|
|
|
if (sha) {
|
|
|
|
core.exportVariable('CI_SHA_SHORT', getShaShort(sha));
|
|
|
|
core.info(`Set CI_SHA_SHORT=${process.env.CI_SHA_SHORT}`);
|
2020-07-14 06:06:58 -04:00
|
|
|
} else {
|
2021-05-02 13:50:33 -04:00
|
|
|
core.info('Environment variable "GITHUB_SHA" not set. ' +
|
|
|
|
'Cannot set "CI_SHA_SHORT".');
|
2020-07-14 06:06:58 -04:00
|
|
|
}
|
2020-12-01 06:41:42 -05:00
|
|
|
|
|
|
|
core.exportVariable('CI_SHA', sha);
|
|
|
|
core.info(`Set CI_SHA=${process.env.CI_SHA}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const pullRequest = github.context.payload &&
|
|
|
|
github.context.payload.pull_request;
|
|
|
|
if (pullRequest) {
|
2022-07-17 16:50:53 -04:00
|
|
|
const prSha = pullRequest.head.sha;
|
2022-07-12 04:21:56 -04:00
|
|
|
core.exportVariable('CI_PR_SHA_SHORT', getShaShort(prSha));
|
|
|
|
core.info(`Set CI_PR_SHA_SHORT=${process.env.CI_PR_SHA_SHORT}`);
|
|
|
|
|
|
|
|
core.exportVariable('CI_PR_SHA', prSha);
|
|
|
|
core.info(`Set CI_PR_SHA=${process.env.CI_PR_SHA}`);
|
|
|
|
|
2022-07-05 11:39:22 -04:00
|
|
|
const prNumber = pullRequest.number;
|
|
|
|
core.exportVariable('CI_PR_NUMBER', prNumber);
|
|
|
|
core.info(`Set CI_PR_NUMBER=${process.env.CI_PR_NUMBER}`);
|
|
|
|
|
|
|
|
core.exportVariable('CI_PR_ID', prNumber);
|
|
|
|
core.info(`Set CI_PR_ID=${process.env.CI_PR_ID}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const prTitle = pullRequest.title;
|
|
|
|
core.exportVariable('CI_PR_TITLE', prTitle);
|
|
|
|
core.info(`Set CI_PR_TITLE=${process.env.CI_PR_TITLE}`);
|
|
|
|
|
|
|
|
const prDescription = pullRequest.body;
|
|
|
|
core.exportVariable('CI_PR_DESCRIPTION', prDescription);
|
|
|
|
core.info(`Set CI_PR_DESCRIPTION=${process.env.CI_PR_DESCRIPTION}`);
|
|
|
|
} else {
|
|
|
|
core.info('No pull request. ' +
|
2022-07-17 16:50:53 -04:00
|
|
|
'Cannot set "CI_PR_SHA_SHORT", "CI_PR_SHA", "CI_PR_NUMBER", ' +
|
|
|
|
'"CI_PR_ID", "CI_PR_TITLE" and "CI_PR_DESCRIPTION".');
|
2021-05-03 04:24:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const actor = process.env.GITHUB_ACTOR;
|
2020-12-01 06:41:42 -05:00
|
|
|
core.exportVariable('CI_ACTOR', actor);
|
|
|
|
core.info(`Set CI_ACTOR=${process.env.CI_ACTOR}`);
|
2020-12-04 01:14:10 -05:00
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const eventName = process.env.GITHUB_EVENT_NAME;
|
2020-12-04 01:14:10 -05:00
|
|
|
core.exportVariable('CI_EVENT_NAME', eventName);
|
|
|
|
core.info(`Set CI_EVENT_NAME=${process.env.CI_EVENT_NAME}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const runId = process.env.GITHUB_RUN_ID;
|
2020-12-04 01:14:10 -05:00
|
|
|
core.exportVariable('CI_RUN_ID', runId);
|
|
|
|
core.info(`Set CI_RUN_ID=${process.env.CI_RUN_ID}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const runNumber = process.env.GITHUB_RUN_NUMBER;
|
2020-12-04 01:14:10 -05:00
|
|
|
core.exportVariable('CI_RUN_NUMBER', runNumber);
|
|
|
|
core.info(`Set CI_RUN_NUMBER=${process.env.CI_RUN_NUMBER}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const workflow = process.env.GITHUB_WORKFLOW;
|
2020-12-04 01:14:10 -05:00
|
|
|
core.exportVariable('CI_WORKFLOW', workflow);
|
|
|
|
core.info(`Set CI_WORKFLOW=${process.env.CI_WORKFLOW}`);
|
|
|
|
|
2021-05-03 04:24:10 -04:00
|
|
|
const action = process.env.GITHUB_ACTION;
|
2020-12-04 01:14:10 -05:00
|
|
|
core.exportVariable('CI_ACTION', action);
|
|
|
|
core.info(`Set CI_ACTION=${process.env.CI_ACTION}`);
|
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,
|
|
|
|
};
|