Skip to content

feat: raise error when GitLab CLI is required but not installed #852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,8 @@ Please make sure the GitLab user associated with the token has the [permission t

Please make sure to create a [GitLab personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html) and to set it in the \`GL_TOKEN\` or \`GITLAB_TOKEN\` environment variable on your CI environment. The token must allow to push to the repository ${repositoryUrl}.`,
}),
EGLABNOTINSTALLED: () => ({
message: 'GitLab CLI not installed.',
details: 'The [GitLab CLI needs to be installed](https://gitlab.com/gitlab-org/cli#installation) so that the project\'s CI components can be published.',
}),
};
5 changes: 5 additions & 0 deletions lib/glab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { execa } from "execa";

export default async (args, options) => {
return execa("glab", args, options);
};
4 changes: 2 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import resolveConfig from "./resolve-config.js";
import getAssets from "./glob-assets.js";
import { RELEASE_NAME } from "./definitions/constants.js";
import getProjectContext from "./get-project-context.js";
import { execa } from "execa";
import glab from "./glab.js";
const isUrlScheme = (value) => /^(https|http|ftp):\/\//.test(value);

export default async (pluginConfig, context) => {
Expand Down Expand Up @@ -195,7 +195,7 @@ export default async (pluginConfig, context) => {

if (publishToCatalog) {
try {
await execa("glab", ["repo", "publish", "catalog", gitTag], {
await glab(["repo", "publish", "catalog", gitTag], {
cwd,
timeout: 30 * 1000,
env: { GITLAB_TOKEN: gitlabToken },
Expand Down
17 changes: 16 additions & 1 deletion lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ export default async (pluginConfig, context) => {
options: { repositoryUrl },
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(pluginConfig, context);
const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, publishToCatalog, ...options } = resolveConfig(
pluginConfig,
context
);
const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);

debug("apiUrl: %o", gitlabApiUrl);
Expand Down Expand Up @@ -89,6 +92,18 @@ export default async (pluginConfig, context) => {
}
}

if (publishToCatalog === true) {
try {
logger.log("Verifying that the GitLab CLI is installed");
await glab(["version"], {
timeout: 5 * 1000,
});
} catch (error) {
logger.error("The GitLab CLI is required but failed to run:\n%O", error);
errors.push(getError("EGLABNOTINSTALLED"));
}
}

if (errors.length > 0) {
throw new AggregateError(errors);
}
Expand Down
32 changes: 32 additions & 0 deletions test/verify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import nock from "nock";
import { stub } from "sinon";
import verify from "../lib/verify.js";
import authenticate from "./helpers/mock-gitlab.js";
import * as td from "testdouble";

/* eslint camelcase: ["error", {properties: "never"}] */

Expand Down Expand Up @@ -988,3 +989,34 @@ test.serial(
t.true(gitlab.isDone());
}
);

test.serial(
'Throw SemanticReleaseError if "publishToCatalog" option is set and the GitLab CLI is not installed.',
async (t) => {
const owner = "test_user";
const repo = "test_repo";
const env = { GITLAB_TOKEN: "gitlab_token" };
const gitlab = authenticate(env)
.get(`/projects/${owner}%2F${repo}`)
.reply(200, { permissions: { project_access: { access_level: 40 } } });

const execa = (await td.replaceEsm("execa")).execa;
td.when(
execa("glab", ["version"], {
timeout: 5000,
})
).thenReject();
const verifyWithMockExeca = (await import("../lib/verify.js")).default;
const {
errors: [error],
} = await t.throwsAsync(
verifyWithMockExeca(
{ publishToCatalog: true },
{ env, options: { repositoryUrl: `https://gitlab.com/${owner}/${repo}.git` }, logger: t.context.logger }
)
);
t.is(error.name, "SemanticReleaseError");
t.is(error.code, "EGLABNOTINSTALLED");
t.true(gitlab.isDone());
}
);