@process-engine/ci_tools
Version:
CI tools for process-engine.io
148 lines • 6 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBranchFromRefTag = exports.mapReleaseChannelNameToBranch = exports.isGitHubRemote = exports.isExistingTag = exports.isDirty = exports.gitPushTags = exports.gitPush = exports.gitTag = exports.gitCommit = exports.gitAdd = exports.getCurrentApiBaseUrlWithAuth = exports.getGitHubAuthPart = exports.getCurrentRepoNameWithOwner = exports.getFullCommitMessageFromRef = exports.getGitTagsFromCommit = exports.getGitBranch = exports.getGitCommitSha1 = exports.getGitCommitListSince = exports.getGitTagDate = exports.getGitTagList = void 0;
const shell_1 = require("../cli/shell");
const parse_version_1 = require("../versions/parse_version");
const CURRENT_BRANCH_MARKER = /^\* /;
const RELEASE_CHANNEL_NAME_TO_BRANCH_MAP = {
alpha: process.env.CI_TOOLS_ALPHA_BRANCH || 'alpha',
beta: process.env.CI_TOOLS_BETA_BRANCH || 'beta',
stable: process.env.CI_TOOLS_STABLE_BRANCH || 'master'
};
function getGitTagList() {
return (0, shell_1.sh)('git tag --sort=-creatordate').trim();
}
exports.getGitTagList = getGitTagList;
function getGitTagDate(tag) {
return (0, shell_1.sh)(`git log -1 --format=%aI ${tag}`).trim();
}
exports.getGitTagDate = getGitTagDate;
function getGitCommitListSince(ref, since) {
return (0, shell_1.sh)(`git log --format="%H" --since ${since} ${ref}`).trim();
}
exports.getGitCommitListSince = getGitCommitListSince;
function getGitCommitSha1(ref = 'HEAD') {
return (0, shell_1.sh)(`git rev-parse ${ref}`).trim();
}
exports.getGitCommitSha1 = getGitCommitSha1;
function getGitBranch() {
const gitRef = process.env.GIT_BRANCH || process.env.GITHUB_HEAD_REF || process.env.GITHUB_REF;
if (gitRef != null) {
const gitRefIsTagReference = gitRef.startsWith('refs/tags/');
if (gitRefIsTagReference) {
return getBranchFromRefTag(gitRef);
}
return gitRef.replace(/^refs\/heads\//, '');
}
return getGitBranchFromGit();
}
exports.getGitBranch = getGitBranch;
function getGitTagsFromCommit(ref) {
const tags = (0, shell_1.sh)(`git tag -l --points-at ${ref}`).trim();
return tags.split('\n');
}
exports.getGitTagsFromCommit = getGitTagsFromCommit;
function getFullCommitMessageFromRef(tagOrCommit) {
const output = (0, shell_1.sh)(`git show -s --format=%B ${tagOrCommit}`);
const lines = output.split('\n');
const subject = lines[0];
const body = lines
.slice(1, lines.length - 2)
.join('\n')
.trim();
return { subject, body };
}
exports.getFullCommitMessageFromRef = getFullCommitMessageFromRef;
function getCurrentRepoNameWithOwner() {
const url = (0, shell_1.sh)('git remote get-url origin');
const matchData = url.match(/github.com[:/](.+)$/m);
if (matchData == null) {
return null;
}
return matchData[1].replace(/\.git$/, '');
}
exports.getCurrentRepoNameWithOwner = getCurrentRepoNameWithOwner;
function getGitHubAuthPart() {
if (process.env.GH_USER != null && process.env.GH_TOKEN != null) {
console.log('--- Using GH_USER & GH_TOKEN');
return `${process.env.GH_USER}:${process.env.GH_TOKEN}@`;
}
return '';
}
exports.getGitHubAuthPart = getGitHubAuthPart;
function getCurrentApiBaseUrlWithAuth(route) {
const gitHubRepo = getCurrentRepoNameWithOwner();
if (gitHubRepo == null) {
return null;
}
const authPart = getGitHubAuthPart();
return `https://${authPart}api.github.com/repos/${gitHubRepo}${route}`;
}
exports.getCurrentApiBaseUrlWithAuth = getCurrentApiBaseUrlWithAuth;
function gitAdd(...files) {
return (0, shell_1.sh)(`git add ${files.join(' ')}`);
}
exports.gitAdd = gitAdd;
function gitCommit(commitMessage) {
const escapedCommitMessage = (0, shell_1.escapeForShell)(commitMessage);
return (0, shell_1.sh)(`git commit --allow-empty -m "${escapedCommitMessage}"`);
}
exports.gitCommit = gitCommit;
function gitTag(newTag) {
return (0, shell_1.sh)(`git tag ${newTag}`);
}
exports.gitTag = gitTag;
function gitPush(remoteName, branchName) {
const cmd = `git push ${remoteName} ${branchName}`;
console.log(`>> ${cmd}`);
const output = (0, shell_1.sh)(cmd).trim();
console.log(output);
return output;
}
exports.gitPush = gitPush;
function gitPushTags() {
const cmd = 'git push --tags';
console.log(`>> ${cmd}`);
const output = (0, shell_1.sh)(cmd).trim();
console.log(output);
return output;
}
exports.gitPushTags = gitPushTags;
function isDirty(...pathspec) {
return (0, shell_1.sh)(`git status --porcelain --untracked-files=no ${pathspec.join(' ')}`).trim() !== '';
}
exports.isDirty = isDirty;
function isExistingTag(name) {
const foundTag = getGitTagList()
.split('\n')
.find((line) => line === name);
return foundTag != null;
}
exports.isExistingTag = isExistingTag;
function isGitHubRemote() {
const url = (0, shell_1.sh)('git remote get-url origin');
const matchData = url.match(/github.com[:/](.+)$/m);
return matchData != null;
}
exports.isGitHubRemote = isGitHubRemote;
function mapReleaseChannelNameToBranch(releaseChannelName) {
return RELEASE_CHANNEL_NAME_TO_BRANCH_MAP[releaseChannelName];
}
exports.mapReleaseChannelNameToBranch = mapReleaseChannelNameToBranch;
function getBranchFromRefTag(gitRef) {
const adjustedGitRef = gitRef.replace(/^refs\/tags\//, '').replace(/^v/, '');
const versionFromGitRef = (0, parse_version_1.parseVersion)(adjustedGitRef);
if (versionFromGitRef == null) {
return null;
}
return mapReleaseChannelNameToBranch(versionFromGitRef.releaseChannelName);
}
exports.getBranchFromRefTag = getBranchFromRefTag;
function getGitBranchFromGit() {
const outputLines = (0, shell_1.sh)('git branch')
.trim()
.split('\n');
const branchLine = outputLines.find((name) => !!name.match(CURRENT_BRANCH_MARKER));
return branchLine.replace(CURRENT_BRANCH_MARKER, '');
}
//# sourceMappingURL=git.js.map
;