Skip to content

Commit 292a279

Browse files
feat(verify): Reduce access_level requirements when using --dry-run (#452)
1 parent f179e19 commit 292a279

File tree

4 files changed

+42
-5
lines changed

4 files changed

+42
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ The GitLab authentication configuration is **required** and can be set via
5353

5454
Create a [personal access token](https://docs.gitlab.com/ce/user/profile/personal_access_tokens.html) with the `api` scope and make it available in your CI environment via the `GL_TOKEN` environment variable. If you are using `GL_TOKEN` as the [remote Git repository authentication](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/ci-configuration.md#authentication) it must also have the `write_repository` scope.
5555

56+
**Note**: When running with [`dryRun`](https://semantic-release.gitbook.io/semantic-release/usage/configuration#dryrun) only `read_repository` scope is required.
57+
5658
### Environment variables
5759

5860
| Variable | Description |

lib/definitions/errors.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,20 @@ If you are using [GitLab Enterprise Edition](https://about.gitlab.com/gitlab-ee)
6161
'README.md#options'
6262
)}).`,
6363
}),
64-
EGLNOPERMISSION: ({repoId}) => ({
64+
EGLNOPUSHPERMISSION: ({repoId}) => ({
6565
message: `The GitLab token doesn't allow to push on the repository ${repoId}.`,
6666
details: `The user associated with the [GitLab token](${linkify(
6767
'README.md#gitlab-authentication'
6868
)}) configured in the \`GL_TOKEN\` or \`GITLAB_TOKEN\` environment variable must allows to push to the repository ${repoId}.
6969
70+
Please make sure the GitLab user associated with the token has the [permission to push](https://docs.gitlab.com/ee/user/permissions.html#project-members-permissions) to the repository ${repoId}.`,
71+
}),
72+
EGLNOPULLPERMISSION: ({repoId}) => ({
73+
message: `The GitLab token doesn't allow to pull from the repository ${repoId}.`,
74+
details: `The user associated with the [GitLab token](${linkify(
75+
'README.md#gitlab-authentication'
76+
)}) configured in the \`GL_TOKEN\` or \`GITLAB_TOKEN\` environment variable must allow pull from the repository ${repoId}.
77+
7078
Please make sure the GitLab user associated with the token has the [permission to push](https://docs.gitlab.com/ee/user/permissions.html#project-members-permissions) to the repository ${repoId}.`,
7179
}),
7280
ENOGLTOKEN: ({repositoryUrl}) => ({

lib/verify.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ module.exports = async (pluginConfig, context) => {
6767
...proxy,
6868
})
6969
.json());
70-
71-
if (!((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30))) {
72-
errors.push(getError('EGLNOPERMISSION', {repoId}));
70+
if (
71+
context.options.dryRun &&
72+
!((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10))
73+
) {
74+
errors.push(getError('EGLNOPULLPERMISSION', {repoId}));
75+
} else if (
76+
!((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30))
77+
) {
78+
errors.push(getError('EGLNOPUSHPERMISSION', {repoId}));
7379
}
7480
} catch (error) {
7581
if (error.response && error.response.statusCode === 401) {

test/verify.test.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,28 @@ test.serial("Throw SemanticReleaseError if token doesn't have the push permissio
479479

480480
t.is(errors.length, 0);
481481
t.is(error.name, 'SemanticReleaseError');
482-
t.is(error.code, 'EGLNOPERMISSION');
482+
t.is(error.code, 'EGLNOPUSHPERMISSION');
483+
t.true(gitlab.isDone());
484+
});
485+
486+
test.serial("Throw SemanticReleaseError if token doesn't have the pull permission on the repository", async (t) => {
487+
const owner = 'test_user';
488+
const repo = 'test_repo';
489+
const env = {GITLAB_TOKEN: 'gitlab_token'};
490+
const gitlab = authenticate(env)
491+
.get(`/projects/${owner}%2F${repo}`)
492+
.reply(200, {permissions: {project_access: {access_level: 5}, group_access: {access_level: 5}}});
493+
494+
const [error, ...errors] = await t.throwsAsync(
495+
verify(
496+
{},
497+
{env, options: {repositoryUrl: `https://gitlab.com:${owner}/${repo}.git`, dryRun: true}, logger: t.context.logger}
498+
)
499+
);
500+
501+
t.is(errors.length, 0);
502+
t.is(error.name, 'SemanticReleaseError');
503+
t.is(error.code, 'EGLNOPULLPERMISSION');
483504
t.true(gitlab.isDone());
484505
});
485506

0 commit comments

Comments
 (0)