@reelout/gitlab 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/dist/index.js +23 -12
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/gitlab.ts +28 -15
package/README.md
CHANGED
|
@@ -18,7 +18,11 @@ This ESM-only package requires Node.js `>=24.15`.
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
The default configuration includes this plugin. It activates only when the
|
|
22
|
+
repository is hosted on GitLab — read from the `origin` remote, or from the
|
|
23
|
+
root manifest's `repository` field when it declares one — and stays inactive
|
|
24
|
+
otherwise, so a GitHub-hosted repository runs it without side effects.
|
|
25
|
+
Reference it explicitly in the `plugins` array to pin or reorder it:
|
|
22
26
|
|
|
23
27
|
```ts
|
|
24
28
|
export default {
|
|
@@ -27,7 +31,9 @@ export default {
|
|
|
27
31
|
'@reelout/core/builder',
|
|
28
32
|
'@reelout/core/changelog',
|
|
29
33
|
'@reelout/npm',
|
|
34
|
+
'@reelout/cargo',
|
|
30
35
|
'@reelout/git',
|
|
36
|
+
'@reelout/github',
|
|
31
37
|
'@reelout/gitlab',
|
|
32
38
|
],
|
|
33
39
|
}
|
package/dist/index.js
CHANGED
|
@@ -119,7 +119,7 @@ async function createRelease(context, plan, token, repository) {
|
|
|
119
119
|
return () => deleteRelease(plan, token, repository);
|
|
120
120
|
}
|
|
121
121
|
// package.json
|
|
122
|
-
var version = "0.7.
|
|
122
|
+
var version = "0.7.1";
|
|
123
123
|
// src/verifyToken.ts
|
|
124
124
|
/**
|
|
125
125
|
* Verify that a GitLab token can access the project it releases to.
|
|
@@ -164,15 +164,18 @@ var readGitlabToken = (env) => {
|
|
|
164
164
|
*
|
|
165
165
|
* @remarks
|
|
166
166
|
* **Lifecycle Stages:**
|
|
167
|
-
* - `configure`:
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
*
|
|
172
|
-
* -
|
|
173
|
-
*
|
|
174
|
-
*
|
|
175
|
-
*
|
|
167
|
+
* - `configure`: Leaves the plugin inactive when no GitLab-hosted repository
|
|
168
|
+
* was detected. Otherwise verifies the `GITLAB_TOKEN` environment variable:
|
|
169
|
+
* a missing token skips the plugin's `publish` step with a warning — or
|
|
170
|
+
* fails the run in strict mode — and a provided token is verified against
|
|
171
|
+
* the release repository so an invalid token fails before any mutation,
|
|
172
|
+
* including under dry-run.
|
|
173
|
+
* - `publish`: Skips release creation when the plugin is inactive. Creates
|
|
174
|
+
* GitLab releases in reverse alphabetical package order (ensuring the root
|
|
175
|
+
* package release is created last), overwriting the name and notes of any
|
|
176
|
+
* existing release for a planned tag. Automatically rolls back when
|
|
177
|
+
* subsequent publishing stages fail: releases created by this run are
|
|
178
|
+
* deleted and overwritten releases are restored to their original content.
|
|
176
179
|
*
|
|
177
180
|
* @returns A release plugin configured for GitLab release creation.
|
|
178
181
|
*/
|
|
@@ -180,17 +183,25 @@ var gitlab = () => ({
|
|
|
180
183
|
name: "@reelout/gitlab",
|
|
181
184
|
version,
|
|
182
185
|
configure: async (context, next) => {
|
|
186
|
+
const repository = context.repository;
|
|
187
|
+
if (repository?.host !== "gitlab") {
|
|
188
|
+
context.logger.debug("GitLab releases will not be created: no GitLab-hosted repository was detected.");
|
|
189
|
+
return next(context);
|
|
190
|
+
}
|
|
183
191
|
const token = readGitlabToken(context.env);
|
|
184
192
|
if (!token) {
|
|
185
193
|
if (context.strict) throw new Error("The GITLAB_TOKEN environment variable is required in strict mode.");
|
|
186
194
|
context.logger.warn("GitLab releases will not be created: GITLAB_TOKEN is not set.");
|
|
187
195
|
return next(context);
|
|
188
196
|
}
|
|
189
|
-
|
|
197
|
+
await verifyToken(token, repository);
|
|
190
198
|
return next(context);
|
|
191
199
|
},
|
|
192
200
|
publish: async (context, next) => {
|
|
193
|
-
if (
|
|
201
|
+
if (context.repository?.host !== "gitlab") {
|
|
202
|
+
context.logger.debug("GitLab publishing was skipped: no GitLab-hosted repository was detected.");
|
|
203
|
+
return next(context);
|
|
204
|
+
}
|
|
194
205
|
const token = readGitlabToken(context.env);
|
|
195
206
|
if (!token) return next(context);
|
|
196
207
|
if (context.dryRun) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/isNotFoundError.ts","../src/deleteRelease.ts","../src/fetchRelease.ts","../src/restoreRelease.ts","../src/createRelease.ts","../src/verifyToken.ts","../src/gitlab.ts","../src/isRepositoryPublic.ts"],"sourcesContent":["// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\n/**\n * Check whether an error represents a GitLab API \"not found\" response.\n *\n * GitBeaker exposes the HTTP status of a failed request on the `cause`\n * property of the thrown error.\n *\n * @param err Error thrown by a GitBeaker API call\n * @returns True when the error represents a 404 response\n */\nexport function isNotFoundError(err: unknown): boolean {\n const status = (err as { cause?: { response?: { status?: number } } })?.cause\n ?.response?.status\n\n return status === 404\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleasePlan } from '@reelout/core'\nimport { isNotFoundError } from './isNotFoundError'\n\n/**\n * Delete a GitLab release associated with a package tag.\n *\n * Skips deletion if the plan does not contain a valid tag name. Resolves gracefully\n * if the release does not exist on GitLab (e.g., 404 status).\n *\n * @param plan Release plan containing the tag associated with the release to delete\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves when the release is deleted\n */\nexport async function deleteRelease(\n plan: ReleasePlan,\n token: string,\n repository: GitRepository\n): Promise<void> {\n if (!plan.release?.tag?.name) {\n return\n }\n\n try {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(\n `${repository.owner}/${repository.repo}`\n )\n\n await api.ProjectReleases.remove(projectId, plan.release.tag.name)\n } catch (err) {\n if (isNotFoundError(err)) {\n return\n }\n\n throw err\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport type { Gitlab } from '@gitbeaker/rest'\nimport { isNotFoundError } from './isNotFoundError'\n\n/**\n * GitLab release content captured before an overwrite.\n */\nexport interface GitLabRelease {\n /** Release name as it existed before the overwrite. */\n name: string | null\n /** Release description as it existed before the overwrite. */\n description: string | null\n}\n\n/**\n * Fetch the GitLab release that exists for a tag.\n *\n * @param api Authenticated GitLab client\n * @param projectId Encoded project id the release belongs to\n * @param tagName Tag the release is looked up by\n * @returns The GitLab release content, or null when the tag has no release\n */\nexport async function fetchRelease(\n api: Gitlab,\n projectId: string,\n tagName: string\n): Promise<GitLabRelease | null> {\n try {\n const release = await api.ProjectReleases.show(projectId, tagName)\n\n return { name: release.name, description: release.description }\n } catch (err) {\n if (isNotFoundError(err)) {\n return null\n }\n\n throw err\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleasePlan } from '@reelout/core'\nimport type { GitLabRelease } from './fetchRelease'\n\n/**\n * Restore an overwritten GitLab release to its original content.\n *\n * Reapplies the captured original name and description via the update\n * endpoint. Skips the restore when the plan does not contain a valid tag\n * name.\n *\n * @param plan Release plan containing the tag associated with the release to restore\n * @param original Original release content captured before the overwrite\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves when the release is restored\n */\nexport async function restoreRelease(\n plan: ReleasePlan,\n original: GitLabRelease,\n token: string,\n repository: GitRepository\n): Promise<void> {\n if (!plan.release?.tag?.name) {\n return\n }\n\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n await api.ProjectReleases.edit(projectId, plan.release.tag.name, {\n name: original.name,\n description: original.description,\n })\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleaseContext, ReleasePlan } from '@reelout/core'\nimport { deleteRelease } from './deleteRelease'\nimport { fetchRelease } from './fetchRelease'\nimport { restoreRelease } from './restoreRelease'\n\n/**\n * Rollback callback returned after a release was created or overwritten.\n */\nexport type ReleaseRollback = () => Promise<void>\n\n/**\n * Create or overwrite a GitLab release for a package tag.\n *\n * Skips the write when the tag has no message. Fetches the release for the\n * tag first: an existing release is overwritten with the generated content\n * via the update endpoint, and a missing release is created. Returns a\n * rollback callback that restores an overwritten release or deletes a\n * release created by this call.\n *\n * @param context Release context containing commit and prerelease information\n * @param plan Release plan containing the tag to publish\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves to a rollback callback for the release, or\n * undefined when the write was skipped\n */\nexport async function createRelease(\n context: ReleaseContext,\n plan: ReleasePlan,\n token: string,\n repository: GitRepository\n): Promise<ReleaseRollback | void> {\n if (!plan.release?.tag?.message) {\n return\n }\n\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n const tagName = plan.release.tag.name\n\n const existing = await fetchRelease(api, projectId, tagName)\n\n if (existing) {\n await api.ProjectReleases.edit(projectId, tagName, {\n name: tagName,\n description: plan.release.tag.message,\n })\n\n context.logger.warn(\n `GitLab release for tag \"${tagName}\" already exists: its name and description were overwritten with the freshly generated content.`\n )\n\n return () => restoreRelease(plan, existing, token, repository)\n }\n\n await api.ProjectReleases.create(projectId, {\n name: tagName,\n tag_name: tagName,\n description: plan.release.tag.message,\n ref: context.commit.hash,\n })\n\n return () => deleteRelease(plan, token, repository)\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository } from '@reelout/core'\n\n/**\n * Verify that a GitLab token can access the project it releases to.\n *\n * Performs the token pre-flight for the GitLab plugin by reading the target\n * project, so a provided-but-invalid or expired token fails during the\n * configure stage, before any release is created. The project is read\n * instead of the current user because tokens scoped to a project or a CI\n * job are not always permitted to read the current user while still being\n * able to create releases for the project. The check runs identically\n * under dry-run.\n *\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns A promise that resolves when GitLab accepts the token\n * @throws If reading the project fails, carrying the original error as the cause\n */\nexport async function verifyToken(\n token: string,\n repository: GitRepository\n): Promise<void> {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n try {\n await api.Projects.show(projectId)\n } catch (err) {\n throw new Error(\n 'The GITLAB_TOKEN environment variable could not be verified.',\n { cause: err }\n )\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport type { ReleaseContext, ReleasePlugin } from '@reelout/core'\nimport { version } from '../package.json'\nimport { createRelease, type ReleaseRollback } from './createRelease'\nimport { verifyToken } from './verifyToken'\n\n/**\n * Read the GitLab token from the release environment.\n *\n * Treats unset and whitespace-only values as missing.\n *\n * @param env Release environment variables\n * @returns The trimmed token, or undefined when it is not set\n */\nconst readGitlabToken = (env: ReleaseContext['env']): string | undefined => {\n const token = env.GITLAB_TOKEN?.trim()\n\n return token ? token : undefined\n}\n\n/**\n * Built-in plugin that creates GitLab releases for finalized release plans.\n *\n * @remarks\n * **Lifecycle Stages:**\n * - `configure`: Checks the `GITLAB_TOKEN` environment variable. A missing token\n * skips GitLab release creation with a warning — or throws under strict mode —\n * and a provided token is verified against the release project so an invalid\n * token fails before any mutation. Skips the pre-flight when repository\n * context is unavailable. Runs under dry-run.\n * - `publish`: Creates GitLab releases in reverse alphabetical package order (ensuring the root package release\n * is created last), overwriting existing releases for the same tag. Automatically rolls back if subsequent\n * publishing stages fail: releases created by this run are deleted, and overwritten releases are restored\n * to their original content.\n *\n * @returns A release plugin configured for GitLab release creation.\n */\nconst gitlab = (): ReleasePlugin => ({\n name: '@reelout/gitlab',\n version,\n configure: async (context, next) => {\n const token = readGitlabToken(context.env)\n\n if (!token) {\n if (context.strict) {\n throw new Error(\n 'The GITLAB_TOKEN environment variable is required in strict mode.'\n )\n }\n\n context.logger.warn(\n 'GitLab releases will not be created: GITLAB_TOKEN is not set.'\n )\n\n return next(context)\n }\n\n if (context.repository) {\n await verifyToken(token, context.repository)\n }\n\n return next(context)\n },\n publish: async (context, next) => {\n if (!context.repository) {\n throw new Error(\n 'A Git repository is required to create a GitLab release.'\n )\n }\n\n const token = readGitlabToken(context.env)\n\n if (!token) {\n return next(context)\n }\n\n if (context.dryRun) {\n context.logger.debug('[DRY RUN] GitLab releases will not be created.')\n return next(context)\n }\n\n const plans = context.plans.toSorted((a, b) =>\n (a.package.name || '').localeCompare(b.package.name || '')\n )\n\n const repository = context.repository\n\n const rollbacks: ReleaseRollback[] = []\n\n // Create releases in reverse order\n // so that the root package release is created last,\n // as it is often the most important one.\n for (const plan of plans.toReversed()) {\n const rollback = await createRelease(context, plan, token, repository)\n\n if (rollback) {\n rollbacks.push(rollback)\n }\n }\n\n try {\n return await next(context)\n } catch (err) {\n await Promise.allSettled(rollbacks.map((rollback) => rollback()))\n\n throw err\n }\n },\n})\n\nexport default gitlab\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository } from '@reelout/core'\n\n/**\n * Check if the target GitLab repository is public.\n *\n * Uses the GitBeaker REST API with the provided token to fetch the\n * visibility status of the specified repository.\n *\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves to `true` if the repository is public\n */\nexport async function isRepositoryPublic(\n token: string,\n repository: GitRepository\n): Promise<boolean> {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n const project = await api.Projects.show(projectId)\n\n return project.visibility === 'public'\n}\n"],"mappings":";;;;;;;;;;;AAYA,SAAgB,gBAAgB,KAAuB;CAIrD,OAHgB,KAAwD,OACpE,UAAU,WAEI;AACpB;;;;;;;;;;;;;ACCA,eAAsB,cACpB,MACA,OACA,YACe;CACf,IAAI,CAAC,KAAK,SAAS,KAAK,MACtB;CAGF,IAAI;EACF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Q,OAAO,MAAM,gDAAgD;GACrE,OAAO,KAAK,OAAO;EACrB;EAEA,MAAM,QAAQ,QAAQ,MAAM,UAAU,GAAG,OACtC,EAAE,QAAQ,QAAQ,GAAA,CAAI,cAAc,EAAE,QAAQ,QAAQ,EAAE,CAC3D;EAEA,MAAM,aAAa,QAAQ;EAE3B,MAAM,YAA+B,CAAC;EAKtC,KAAK,MAAM,QAAQ,MAAM,WAAW,GAAG;GACrC,MAAM,WAAW,MAAM,cAAc,SAAS,MAAM,OAAO,UAAU;GAErE,IAAI,UACF,UAAU,KAAK,QAAQ;EAE3B;EAEA,IAAI;GACF,OAAO,MAAM,KAAK,OAAO;EAC3B,SAAS,KAAK;GACZ,MAAM,QAAQ,WAAW,UAAU,KAAK,aAAa,SAAS,CAAC,CAAC;GAEhE,MAAM;EACR;CACF;AACF;;;;;;;;;;;;AC9FA,eAAsB,mBACpB,OACA,YACkB;CAClB,MAAM,MAAM,IAAI,OAAO,EAAE,MAAM,CAAC;CAEhC,MAAM,YAAY,mBAAmB,GAAG,WAAW,MAAM,GAAG,WAAW,MAAM;CAI7E,QAAO,MAFe,IAAI,SAAS,KAAK,SAAS,EAAA,CAElC,eAAe;AAChC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/isNotFoundError.ts","../src/deleteRelease.ts","../src/fetchRelease.ts","../src/restoreRelease.ts","../src/createRelease.ts","../src/verifyToken.ts","../src/gitlab.ts","../src/isRepositoryPublic.ts"],"sourcesContent":["// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\n/**\n * Check whether an error represents a GitLab API \"not found\" response.\n *\n * GitBeaker exposes the HTTP status of a failed request on the `cause`\n * property of the thrown error.\n *\n * @param err Error thrown by a GitBeaker API call\n * @returns True when the error represents a 404 response\n */\nexport function isNotFoundError(err: unknown): boolean {\n const status = (err as { cause?: { response?: { status?: number } } })?.cause\n ?.response?.status\n\n return status === 404\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleasePlan } from '@reelout/core'\nimport { isNotFoundError } from './isNotFoundError'\n\n/**\n * Delete a GitLab release associated with a package tag.\n *\n * Skips deletion if the plan does not contain a valid tag name. Resolves gracefully\n * if the release does not exist on GitLab (e.g., 404 status).\n *\n * @param plan Release plan containing the tag associated with the release to delete\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves when the release is deleted\n */\nexport async function deleteRelease(\n plan: ReleasePlan,\n token: string,\n repository: GitRepository\n): Promise<void> {\n if (!plan.release?.tag?.name) {\n return\n }\n\n try {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(\n `${repository.owner}/${repository.repo}`\n )\n\n await api.ProjectReleases.remove(projectId, plan.release.tag.name)\n } catch (err) {\n if (isNotFoundError(err)) {\n return\n }\n\n throw err\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport type { Gitlab } from '@gitbeaker/rest'\nimport { isNotFoundError } from './isNotFoundError'\n\n/**\n * GitLab release content captured before an overwrite.\n */\nexport interface GitLabRelease {\n /** Release name as it existed before the overwrite. */\n name: string | null\n /** Release description as it existed before the overwrite. */\n description: string | null\n}\n\n/**\n * Fetch the GitLab release that exists for a tag.\n *\n * @param api Authenticated GitLab client\n * @param projectId Encoded project id the release belongs to\n * @param tagName Tag the release is looked up by\n * @returns The GitLab release content, or null when the tag has no release\n */\nexport async function fetchRelease(\n api: Gitlab,\n projectId: string,\n tagName: string\n): Promise<GitLabRelease | null> {\n try {\n const release = await api.ProjectReleases.show(projectId, tagName)\n\n return { name: release.name, description: release.description }\n } catch (err) {\n if (isNotFoundError(err)) {\n return null\n }\n\n throw err\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleasePlan } from '@reelout/core'\nimport type { GitLabRelease } from './fetchRelease'\n\n/**\n * Restore an overwritten GitLab release to its original content.\n *\n * Reapplies the captured original name and description via the update\n * endpoint. Skips the restore when the plan does not contain a valid tag\n * name.\n *\n * @param plan Release plan containing the tag associated with the release to restore\n * @param original Original release content captured before the overwrite\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves when the release is restored\n */\nexport async function restoreRelease(\n plan: ReleasePlan,\n original: GitLabRelease,\n token: string,\n repository: GitRepository\n): Promise<void> {\n if (!plan.release?.tag?.name) {\n return\n }\n\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n await api.ProjectReleases.edit(projectId, plan.release.tag.name, {\n name: original.name,\n description: original.description,\n })\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleaseContext, ReleasePlan } from '@reelout/core'\nimport { deleteRelease } from './deleteRelease'\nimport { fetchRelease } from './fetchRelease'\nimport { restoreRelease } from './restoreRelease'\n\n/**\n * Rollback callback returned after a release was created or overwritten.\n */\nexport type ReleaseRollback = () => Promise<void>\n\n/**\n * Create or overwrite a GitLab release for a package tag.\n *\n * Skips the write when the tag has no message. Fetches the release for the\n * tag first: an existing release is overwritten with the generated content\n * via the update endpoint, and a missing release is created. Returns a\n * rollback callback that restores an overwritten release or deletes a\n * release created by this call.\n *\n * @param context Release context containing commit and prerelease information\n * @param plan Release plan containing the tag to publish\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves to a rollback callback for the release, or\n * undefined when the write was skipped\n */\nexport async function createRelease(\n context: ReleaseContext,\n plan: ReleasePlan,\n token: string,\n repository: GitRepository\n): Promise<ReleaseRollback | void> {\n if (!plan.release?.tag?.message) {\n return\n }\n\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n const tagName = plan.release.tag.name\n\n const existing = await fetchRelease(api, projectId, tagName)\n\n if (existing) {\n await api.ProjectReleases.edit(projectId, tagName, {\n name: tagName,\n description: plan.release.tag.message,\n })\n\n context.logger.warn(\n `GitLab release for tag \"${tagName}\" already exists: its name and description were overwritten with the freshly generated content.`\n )\n\n return () => restoreRelease(plan, existing, token, repository)\n }\n\n await api.ProjectReleases.create(projectId, {\n name: tagName,\n tag_name: tagName,\n description: plan.release.tag.message,\n ref: context.commit.hash,\n })\n\n return () => deleteRelease(plan, token, repository)\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository } from '@reelout/core'\n\n/**\n * Verify that a GitLab token can access the project it releases to.\n *\n * Performs the token pre-flight for the GitLab plugin by reading the target\n * project, so a provided-but-invalid or expired token fails during the\n * configure stage, before any release is created. The project is read\n * instead of the current user because tokens scoped to a project or a CI\n * job are not always permitted to read the current user while still being\n * able to create releases for the project. The check runs identically\n * under dry-run.\n *\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns A promise that resolves when GitLab accepts the token\n * @throws If reading the project fails, carrying the original error as the cause\n */\nexport async function verifyToken(\n token: string,\n repository: GitRepository\n): Promise<void> {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n try {\n await api.Projects.show(projectId)\n } catch (err) {\n throw new Error(\n 'The GITLAB_TOKEN environment variable could not be verified.',\n { cause: err }\n )\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport type { ReleaseContext, ReleasePlugin } from '@reelout/core'\nimport { version } from '../package.json'\nimport { createRelease, type ReleaseRollback } from './createRelease'\nimport { verifyToken } from './verifyToken'\n\n/**\n * Read the GitLab token from the release environment.\n *\n * Treats unset and whitespace-only values as missing.\n *\n * @param env Release environment variables\n * @returns The trimmed token, or undefined when it is not set\n */\nconst readGitlabToken = (env: ReleaseContext['env']): string | undefined => {\n const token = env.GITLAB_TOKEN?.trim()\n\n return token ? token : undefined\n}\n\n/**\n * Built-in plugin that creates GitLab releases for finalized release plans.\n *\n * @remarks\n * **Lifecycle Stages:**\n * - `configure`: Leaves the plugin inactive when no GitLab-hosted repository\n * was detected. Otherwise verifies the `GITLAB_TOKEN` environment variable:\n * a missing token skips the plugin's `publish` step with a warning — or\n * fails the run in strict mode — and a provided token is verified against\n * the release repository so an invalid token fails before any mutation,\n * including under dry-run.\n * - `publish`: Skips release creation when the plugin is inactive. Creates\n * GitLab releases in reverse alphabetical package order (ensuring the root\n * package release is created last), overwriting the name and notes of any\n * existing release for a planned tag. Automatically rolls back when\n * subsequent publishing stages fail: releases created by this run are\n * deleted and overwritten releases are restored to their original content.\n *\n * @returns A release plugin configured for GitLab release creation.\n */\nconst gitlab = (): ReleasePlugin => ({\n name: '@reelout/gitlab',\n version,\n configure: async (context, next) => {\n const repository = context.repository\n\n if (repository?.host !== 'gitlab') {\n context.logger.debug(\n 'GitLab releases will not be created: no GitLab-hosted repository was detected.'\n )\n\n return next(context)\n }\n\n const token = readGitlabToken(context.env)\n\n if (!token) {\n if (context.strict) {\n throw new Error(\n 'The GITLAB_TOKEN environment variable is required in strict mode.'\n )\n }\n\n context.logger.warn(\n 'GitLab releases will not be created: GITLAB_TOKEN is not set.'\n )\n\n return next(context)\n }\n\n await verifyToken(token, repository)\n\n return next(context)\n },\n publish: async (context, next) => {\n if (context.repository?.host !== 'gitlab') {\n context.logger.debug(\n 'GitLab publishing was skipped: no GitLab-hosted repository was detected.'\n )\n\n return next(context)\n }\n\n const token = readGitlabToken(context.env)\n\n if (!token) {\n return next(context)\n }\n\n if (context.dryRun) {\n context.logger.debug('[DRY RUN] GitLab releases will not be created.')\n return next(context)\n }\n\n const plans = context.plans.toSorted((a, b) =>\n (a.package.name || '').localeCompare(b.package.name || '')\n )\n\n const repository = context.repository\n\n const rollbacks: ReleaseRollback[] = []\n\n // Create releases in reverse order\n // so that the root package release is created last,\n // as it is often the most important one.\n for (const plan of plans.toReversed()) {\n const rollback = await createRelease(context, plan, token, repository)\n\n if (rollback) {\n rollbacks.push(rollback)\n }\n }\n\n try {\n return await next(context)\n } catch (err) {\n await Promise.allSettled(rollbacks.map((rollback) => rollback()))\n\n throw err\n }\n },\n})\n\nexport default gitlab\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository } from '@reelout/core'\n\n/**\n * Check if the target GitLab repository is public.\n *\n * Uses the GitBeaker REST API with the provided token to fetch the\n * visibility status of the specified repository.\n *\n * @param token GitLab authentication token\n * @param repository Target repository\n * @returns Promise that resolves to `true` if the repository is public\n */\nexport async function isRepositoryPublic(\n token: string,\n repository: GitRepository\n): Promise<boolean> {\n const api = new Gitlab({ token })\n\n const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)\n\n const project = await api.Projects.show(projectId)\n\n return project.visibility === 'public'\n}\n"],"mappings":";;;;;;;;;;;AAYA,SAAgB,gBAAgB,KAAuB;CAIrD,OAHgB,KAAwD,OACpE,UAAU,WAEI;AACpB;;;;;;;;;;;;;ACCA,eAAsB,cACpB,MACA,OACA,YACe;CACf,IAAI,CAAC,KAAK,SAAS,KAAK,MACtB;CAGF,IAAI;EACF,MAAM,MAAM,IAAI,OAAO,EAAE,MAAM,CAAC;EAEhC,MAAM,YAAY,mBAChB,GAAG,WAAW,MAAM,GAAG,WAAW,MACpC;EAEA,MAAM,IAAI,gBAAgB,OAAO,WAAW,KAAK,QAAQ,IAAI,IAAI;CACnE,SAAS,KAAK;EACZ,IAAI,gBAAgB,GAAG,GACrB;EAGF,MAAM;CACR;AACF;;;;;;;;;;AClBA,eAAsB,aACpB,KACA,WACA,SAC+B;CAC/B,IAAI;EACF,MAAM,UAAU,MAAM,IAAI,gBAAgB,KAAK,WAAW,OAAO;EAEjE,OAAO;GAAE,MAAM,QAAQ;GAAM,aAAa,QAAQ;EAAY;CAChE,SAAS,KAAK;EACZ,IAAI,gBAAgB,GAAG,GACrB,OAAO;EAGT,MAAM;CACR;AACF;;;;;;;;;;;;;;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;EAEA,IAAI;GACF,OAAO,MAAM,KAAK,OAAO;EAC3B,SAAS,KAAK;GACZ,MAAM,QAAQ,WAAW,UAAU,KAAK,aAAa,SAAS,CAAC,CAAC;GAEhE,MAAM;EACR;CACF;AACF;;;;;;;;;;;;AC3GA,eAAsB,mBACpB,OACA,YACkB;CAClB,MAAM,MAAM,IAAI,OAAO,EAAE,MAAM,CAAC;CAEhC,MAAM,YAAY,mBAAmB,GAAG,WAAW,MAAM,GAAG,WAAW,MAAM;CAI7E,QAAO,MAFe,IAAI,SAAS,KAAK,SAAS,EAAA,CAElC,eAAe;AAChC"}
|
package/package.json
CHANGED
package/src/gitlab.ts
CHANGED
|
@@ -25,15 +25,18 @@ const readGitlabToken = (env: ReleaseContext['env']): string | undefined => {
|
|
|
25
25
|
*
|
|
26
26
|
* @remarks
|
|
27
27
|
* **Lifecycle Stages:**
|
|
28
|
-
* - `configure`:
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* -
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
28
|
+
* - `configure`: Leaves the plugin inactive when no GitLab-hosted repository
|
|
29
|
+
* was detected. Otherwise verifies the `GITLAB_TOKEN` environment variable:
|
|
30
|
+
* a missing token skips the plugin's `publish` step with a warning — or
|
|
31
|
+
* fails the run in strict mode — and a provided token is verified against
|
|
32
|
+
* the release repository so an invalid token fails before any mutation,
|
|
33
|
+
* including under dry-run.
|
|
34
|
+
* - `publish`: Skips release creation when the plugin is inactive. Creates
|
|
35
|
+
* GitLab releases in reverse alphabetical package order (ensuring the root
|
|
36
|
+
* package release is created last), overwriting the name and notes of any
|
|
37
|
+
* existing release for a planned tag. Automatically rolls back when
|
|
38
|
+
* subsequent publishing stages fail: releases created by this run are
|
|
39
|
+
* deleted and overwritten releases are restored to their original content.
|
|
37
40
|
*
|
|
38
41
|
* @returns A release plugin configured for GitLab release creation.
|
|
39
42
|
*/
|
|
@@ -41,6 +44,16 @@ const gitlab = (): ReleasePlugin => ({
|
|
|
41
44
|
name: '@reelout/gitlab',
|
|
42
45
|
version,
|
|
43
46
|
configure: async (context, next) => {
|
|
47
|
+
const repository = context.repository
|
|
48
|
+
|
|
49
|
+
if (repository?.host !== 'gitlab') {
|
|
50
|
+
context.logger.debug(
|
|
51
|
+
'GitLab releases will not be created: no GitLab-hosted repository was detected.'
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
return next(context)
|
|
55
|
+
}
|
|
56
|
+
|
|
44
57
|
const token = readGitlabToken(context.env)
|
|
45
58
|
|
|
46
59
|
if (!token) {
|
|
@@ -57,17 +70,17 @@ const gitlab = (): ReleasePlugin => ({
|
|
|
57
70
|
return next(context)
|
|
58
71
|
}
|
|
59
72
|
|
|
60
|
-
|
|
61
|
-
await verifyToken(token, context.repository)
|
|
62
|
-
}
|
|
73
|
+
await verifyToken(token, repository)
|
|
63
74
|
|
|
64
75
|
return next(context)
|
|
65
76
|
},
|
|
66
77
|
publish: async (context, next) => {
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
'
|
|
78
|
+
if (context.repository?.host !== 'gitlab') {
|
|
79
|
+
context.logger.debug(
|
|
80
|
+
'GitLab publishing was skipped: no GitLab-hosted repository was detected.'
|
|
70
81
|
)
|
|
82
|
+
|
|
83
|
+
return next(context)
|
|
71
84
|
}
|
|
72
85
|
|
|
73
86
|
const token = readGitlabToken(context.env)
|