@adinsure-ops/ops-cli
Version:
Operations CLI for working with AdInsure
155 lines (154 loc) • 6.01 kB
JavaScript
import { __awaiter } from "tslib";
import { Args, Flags, ux } from '@oclif/core';
import chalk from 'chalk';
import semver from 'semver';
import CommandBase from '../../command.base.js';
import GitLab from '../../lib/gitlab.js';
import OpsConfig from '../../lib/config.js';
class GitlabNextNightlyTag extends CommandBase {
hostConfigValidation(config) {
return __awaiter(this, void 0, void 0, function* () {
const host = config.get("gitlab_host");
if (host !== undefined) {
GitlabNextNightlyTag.flags.host.default = host;
return;
}
GitlabNextNightlyTag.flags.host.default =
"https://git.adacta-fintech.com";
});
}
projectConfigValidation(config) {
return __awaiter(this, void 0, void 0, function* () {
const project = config.get("gitlab_project_id");
if (project !== undefined) {
GitlabNextNightlyTag.flags.project.default = project;
return;
}
GitlabNextNightlyTag.flags.project.default = "413";
});
}
run() {
const _super = Object.create(null, {
run: { get: () => super.run }
});
return __awaiter(this, void 0, void 0, function* () {
_super.run.call(this);
const config = new OpsConfig(this.security.getConfigPath());
this.hostConfigValidation(config);
this.projectConfigValidation(config);
const { args, flags } = yield this.parse(GitlabNextNightlyTag);
const gitlab = new GitLab(flags.host, flags.project, flags.pat);
const re = /^release\/(\w+-)?((\d+)(\.\d+)?)$/;
let prefix = '';
let inputVersion = args.version;
const match = args.version.match(re);
if (match) {
if (match[1] !== 'adinsure-') {
prefix = match[1];
}
inputVersion = match[2];
}
let version = semver.parse(inputVersion);
let nextVersion = null;
// if version is not parsable try to find something that starts with this version
if (!version) {
try {
version = yield gitlab.getLatestVersion(inputVersion, prefix);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
this.error(error.message);
}
}
// still version does not exists we will try to parse it with additional 0 as patch version
if (!version) {
version = semver.parse(`${inputVersion}.0`);
}
// last option trying to add 0 for minor and patch version
if (!version) {
version = semver.parse(`${inputVersion}.0.0`);
}
// fail because we were not able to parse this version
if (!version) {
return this.error(`${chalk.red('Version in incorrect format')}`);
}
if (!flags.outputVersionOnly) {
ux.action.start('Calculating next prerelease test version', undefined, { stdout: true });
}
const date = new Date();
const prereleaseDate = `${date.getFullYear()}${('0' + (date.getMonth() + 1)).slice(-2)}`;
let lastVersion = null;
try {
lastVersion = yield gitlab.getLatestVersion(version.version, prefix);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
this.error(error.message);
}
nextVersion = lastVersion ? `v${semver.inc(lastVersion, 'prerelease', prereleaseDate)}` : `v${semver.inc(`${version.version}-${prereleaseDate}.0`, 'prerelease', prereleaseDate)}`;
if (prefix) {
nextVersion = `${prefix}${nextVersion}`;
}
if (flags.outputVersionOnly) {
this.log(`${nextVersion}`);
}
else {
ux.action.stop(`${chalk.green(nextVersion)}`);
}
if (flags.dryRun) {
this.warn(`This is only dry run. no new tag will was created!`);
}
else {
try {
yield gitlab.createNewTag(nextVersion, flags.reference);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
}
catch (error) {
this.error(error.message);
}
}
});
}
}
GitlabNextNightlyTag.description = 'creates new gitlab tag';
GitlabNextNightlyTag.usage = 'gitlab:next-nightly-tag <version>';
GitlabNextNightlyTag.examples = [
`$ ops gitlab:next-nightly-tag 17.0.0`,
];
GitlabNextNightlyTag.args = {
version: Args.string({
required: true,
description: 'Tag version',
}),
};
GitlabNextNightlyTag.flags = {
pat: Flags.string({
required: true,
env: 'PRIVATE_TOKEN',
description: 'Token from user that has permissions to list and create tags',
}),
host: Flags.string({
default: 'https://git.adacta-fintech.com',
env: 'CI_SERVER_URL',
description: 'Server hostname',
}),
project: Flags.string({
default: '413',
env: 'CI_PROJECT_ID',
description: 'Project ID',
}),
reference: Flags.string({
required: true,
env: 'CI_COMMIT_REF_NAME',
description: 'Reference to create tag on (branch, commit, tag)',
}),
outputVersionOnly: Flags.boolean({
description: `output only version instead of pretty output`,
default: false,
}),
dryRun: Flags.boolean({
description: `Only printout version and don't create it`,
default: false,
}),
};
export default GitlabNextNightlyTag;