@reelout/gitlab 0.5.0 → 0.6.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 +9 -3
- package/dist/index.js +162 -29
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/createRelease.ts +38 -7
- package/src/deleteRelease.ts +2 -4
- package/src/fetchRelease.ts +41 -0
- package/src/gitlab.ts +61 -13
- package/src/index.ts +3 -0
- package/src/isNotFoundError.ts +18 -0
- package/src/restoreRelease.ts +39 -0
- package/src/verifyToken.ts +39 -0
package/README.md
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# @reelout/gitlab
|
|
2
2
|
|
|
3
3
|
GitLab Releases integration for [ReelOut](https://github.com/reelout/reelout).
|
|
4
|
-
The plugin creates a GitLab release for each release tag
|
|
5
|
-
|
|
4
|
+
The plugin creates a GitLab release for each release tag, overwrites an
|
|
5
|
+
existing release for a planned tag with the freshly generated name and
|
|
6
|
+
description, and restores or removes what it changed if a later step fails.
|
|
6
7
|
|
|
7
8
|
## Installation
|
|
8
9
|
|
|
@@ -33,13 +34,18 @@ export default {
|
|
|
33
34
|
```
|
|
34
35
|
|
|
35
36
|
Set the `GITLAB_TOKEN` environment variable to authenticate with GitLab.
|
|
37
|
+
The plugin verifies the token during the `configure` stage: without it, the
|
|
38
|
+
GitLab publishing step is skipped (or fails the release in strict mode), and
|
|
39
|
+
a token GitLab rejects fails the release before any release is created.
|
|
36
40
|
|
|
37
41
|
## API
|
|
38
42
|
|
|
39
43
|
- `gitlab` (default export) — the release plugin.
|
|
40
|
-
- `createRelease(...)` — create a GitLab release for a tag.
|
|
44
|
+
- `createRelease(...)` — create or overwrite a GitLab release for a tag.
|
|
41
45
|
- `deleteRelease(...)` — delete a release by tag.
|
|
42
46
|
- `isRepositoryPublic(...)` — check project visibility via the GitLab API.
|
|
47
|
+
- `restoreRelease(...)` — restore an overwritten release to its original content.
|
|
48
|
+
- `verifyToken(...)` — verify a GitLab token is accepted for the release project.
|
|
43
49
|
|
|
44
50
|
## Documentation
|
|
45
51
|
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,16 @@
|
|
|
1
1
|
import { Gitlab } from "@gitbeaker/rest";
|
|
2
|
-
// src/
|
|
2
|
+
// src/isNotFoundError.ts
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Check whether an error represents a GitLab API "not found" response.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* GitBeaker exposes the HTTP status of a failed request on the `cause`
|
|
7
|
+
* property of the thrown error.
|
|
8
8
|
*
|
|
9
|
-
* @param
|
|
10
|
-
* @
|
|
11
|
-
* @param token GitLab authentication token
|
|
12
|
-
* @param repository Target repository
|
|
13
|
-
* @returns Promise that resolves when the release is created
|
|
9
|
+
* @param err Error thrown by a GitBeaker API call
|
|
10
|
+
* @returns True when the error represents a 404 response
|
|
14
11
|
*/
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const api = new Gitlab({ token });
|
|
18
|
-
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
19
|
-
await api.ProjectReleases.create(projectId, {
|
|
20
|
-
name: plan.release.tag.name,
|
|
21
|
-
tag_name: plan.release.tag.name,
|
|
22
|
-
description: plan.release.tag.message,
|
|
23
|
-
ref: context.commit.hash
|
|
24
|
-
});
|
|
12
|
+
function isNotFoundError(err) {
|
|
13
|
+
return err?.cause?.response?.status === 404;
|
|
25
14
|
}
|
|
26
15
|
// src/deleteRelease.ts
|
|
27
16
|
/**
|
|
@@ -42,39 +31,183 @@ async function deleteRelease(plan, token, repository) {
|
|
|
42
31
|
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
43
32
|
await api.ProjectReleases.remove(projectId, plan.release.tag.name);
|
|
44
33
|
} catch (err) {
|
|
45
|
-
if (err
|
|
34
|
+
if (isNotFoundError(err)) return;
|
|
46
35
|
throw err;
|
|
47
36
|
}
|
|
48
37
|
}
|
|
38
|
+
// src/fetchRelease.ts
|
|
39
|
+
/**
|
|
40
|
+
* Fetch the GitLab release that exists for a tag.
|
|
41
|
+
*
|
|
42
|
+
* @param api Authenticated GitLab client
|
|
43
|
+
* @param projectId Encoded project id the release belongs to
|
|
44
|
+
* @param tagName Tag the release is looked up by
|
|
45
|
+
* @returns The GitLab release content, or null when the tag has no release
|
|
46
|
+
*/
|
|
47
|
+
async function fetchRelease(api, projectId, tagName) {
|
|
48
|
+
try {
|
|
49
|
+
const release = await api.ProjectReleases.show(projectId, tagName);
|
|
50
|
+
return {
|
|
51
|
+
name: release.name,
|
|
52
|
+
description: release.description
|
|
53
|
+
};
|
|
54
|
+
} catch (err) {
|
|
55
|
+
if (isNotFoundError(err)) return null;
|
|
56
|
+
throw err;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// src/restoreRelease.ts
|
|
60
|
+
/**
|
|
61
|
+
* Restore an overwritten GitLab release to its original content.
|
|
62
|
+
*
|
|
63
|
+
* Reapplies the captured original name and description via the update
|
|
64
|
+
* endpoint. Skips the restore when the plan does not contain a valid tag
|
|
65
|
+
* name.
|
|
66
|
+
*
|
|
67
|
+
* @param plan Release plan containing the tag associated with the release to restore
|
|
68
|
+
* @param original Original release content captured before the overwrite
|
|
69
|
+
* @param token GitLab authentication token
|
|
70
|
+
* @param repository Target repository
|
|
71
|
+
* @returns Promise that resolves when the release is restored
|
|
72
|
+
*/
|
|
73
|
+
async function restoreRelease(plan, original, token, repository) {
|
|
74
|
+
if (!plan.release?.tag?.name) return;
|
|
75
|
+
const api = new Gitlab({ token });
|
|
76
|
+
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
77
|
+
await api.ProjectReleases.edit(projectId, plan.release.tag.name, {
|
|
78
|
+
name: original.name,
|
|
79
|
+
description: original.description
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
// src/createRelease.ts
|
|
83
|
+
/**
|
|
84
|
+
* Create or overwrite a GitLab release for a package tag.
|
|
85
|
+
*
|
|
86
|
+
* Skips the write when the tag has no message. Fetches the release for the
|
|
87
|
+
* tag first: an existing release is overwritten with the generated content
|
|
88
|
+
* via the update endpoint, and a missing release is created. Returns a
|
|
89
|
+
* rollback callback that restores an overwritten release or deletes a
|
|
90
|
+
* release created by this call.
|
|
91
|
+
*
|
|
92
|
+
* @param context Release context containing commit and prerelease information
|
|
93
|
+
* @param plan Release plan containing the tag to publish
|
|
94
|
+
* @param token GitLab authentication token
|
|
95
|
+
* @param repository Target repository
|
|
96
|
+
* @returns Promise that resolves to a rollback callback for the release, or
|
|
97
|
+
* undefined when the write was skipped
|
|
98
|
+
*/
|
|
99
|
+
async function createRelease(context, plan, token, repository) {
|
|
100
|
+
if (!plan.release?.tag?.message) return;
|
|
101
|
+
const api = new Gitlab({ token });
|
|
102
|
+
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
103
|
+
const tagName = plan.release.tag.name;
|
|
104
|
+
const existing = await fetchRelease(api, projectId, tagName);
|
|
105
|
+
if (existing) {
|
|
106
|
+
await api.ProjectReleases.edit(projectId, tagName, {
|
|
107
|
+
name: tagName,
|
|
108
|
+
description: plan.release.tag.message
|
|
109
|
+
});
|
|
110
|
+
context.logger.warn(`GitLab release for tag "${tagName}" already exists: its name and description were overwritten with the freshly generated content.`);
|
|
111
|
+
return () => restoreRelease(plan, existing, token, repository);
|
|
112
|
+
}
|
|
113
|
+
await api.ProjectReleases.create(projectId, {
|
|
114
|
+
name: tagName,
|
|
115
|
+
tag_name: tagName,
|
|
116
|
+
description: plan.release.tag.message,
|
|
117
|
+
ref: context.commit.hash
|
|
118
|
+
});
|
|
119
|
+
return () => deleteRelease(plan, token, repository);
|
|
120
|
+
}
|
|
49
121
|
// package.json
|
|
50
|
-
var version = "0.
|
|
122
|
+
var version = "0.5.0";
|
|
123
|
+
// src/verifyToken.ts
|
|
124
|
+
/**
|
|
125
|
+
* Verify that a GitLab token can access the project it releases to.
|
|
126
|
+
*
|
|
127
|
+
* Performs the token pre-flight for the GitLab plugin by reading the target
|
|
128
|
+
* project, so a provided-but-invalid or expired token fails during the
|
|
129
|
+
* configure stage, before any release is created. The project is read
|
|
130
|
+
* instead of the current user because tokens scoped to a project or a CI
|
|
131
|
+
* job are not always permitted to read the current user while still being
|
|
132
|
+
* able to create releases for the project. The check runs identically
|
|
133
|
+
* under dry-run.
|
|
134
|
+
*
|
|
135
|
+
* @param token GitLab authentication token
|
|
136
|
+
* @param repository Target repository
|
|
137
|
+
* @returns A promise that resolves when GitLab accepts the token
|
|
138
|
+
* @throws If reading the project fails, carrying the original error as the cause
|
|
139
|
+
*/
|
|
140
|
+
async function verifyToken(token, repository) {
|
|
141
|
+
const api = new Gitlab({ token });
|
|
142
|
+
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
143
|
+
try {
|
|
144
|
+
await api.Projects.show(projectId);
|
|
145
|
+
} catch (err) {
|
|
146
|
+
throw new Error("The GITLAB_TOKEN environment variable could not be verified.", { cause: err });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
51
149
|
// src/gitlab.ts
|
|
52
150
|
/**
|
|
151
|
+
* Read the GitLab token from the release environment.
|
|
152
|
+
*
|
|
153
|
+
* Treats unset and whitespace-only values as missing.
|
|
154
|
+
*
|
|
155
|
+
* @param env Release environment variables
|
|
156
|
+
* @returns The trimmed token, or undefined when it is not set
|
|
157
|
+
*/
|
|
158
|
+
var readGitlabToken = (env) => {
|
|
159
|
+
const token = env.GITLAB_TOKEN?.trim();
|
|
160
|
+
return token ? token : void 0;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
53
163
|
* Built-in plugin that creates GitLab releases for finalized release plans.
|
|
54
164
|
*
|
|
55
165
|
* @remarks
|
|
56
166
|
* **Lifecycle Stages:**
|
|
167
|
+
* - `configure`: Checks the `GITLAB_TOKEN` environment variable. A missing token
|
|
168
|
+
* skips GitLab release creation with a warning — or throws under strict mode —
|
|
169
|
+
* and a provided token is verified against the release project so an invalid
|
|
170
|
+
* token fails before any mutation. Skips the pre-flight when repository
|
|
171
|
+
* context is unavailable. Runs under dry-run.
|
|
57
172
|
* - `publish`: Creates GitLab releases in reverse alphabetical package order (ensuring the root package release
|
|
58
|
-
* is created last)
|
|
173
|
+
* is created last), overwriting existing releases for the same tag. Automatically rolls back if subsequent
|
|
174
|
+
* publishing stages fail: releases created by this run are deleted, and overwritten releases are restored
|
|
175
|
+
* to their original content.
|
|
59
176
|
*
|
|
60
|
-
* @throws {Error} If `context.repository` is not configured or `GITLAB_TOKEN` environment variable is missing.
|
|
61
177
|
* @returns A release plugin configured for GitLab release creation.
|
|
62
178
|
*/
|
|
63
179
|
var gitlab = () => ({
|
|
64
180
|
name: "@reelout/gitlab",
|
|
65
181
|
version,
|
|
182
|
+
configure: async (context, next) => {
|
|
183
|
+
const token = readGitlabToken(context.env);
|
|
184
|
+
if (!token) {
|
|
185
|
+
if (context.strict) throw new Error("The GITLAB_TOKEN environment variable is required in strict mode.");
|
|
186
|
+
context.logger.warn("GitLab releases will not be created: GITLAB_TOKEN is not set.");
|
|
187
|
+
return next(context);
|
|
188
|
+
}
|
|
189
|
+
if (context.repository) await verifyToken(token, context.repository);
|
|
190
|
+
return next(context);
|
|
191
|
+
},
|
|
66
192
|
publish: async (context, next) => {
|
|
67
193
|
if (!context.repository) throw new Error("A Git repository is required to create a GitLab release.");
|
|
68
|
-
|
|
69
|
-
if (
|
|
194
|
+
const token = readGitlabToken(context.env);
|
|
195
|
+
if (!token) return next(context);
|
|
196
|
+
if (context.dryRun) {
|
|
197
|
+
context.logger.debug("[DRY RUN] GitLab releases will not be created.");
|
|
198
|
+
return next(context);
|
|
199
|
+
}
|
|
70
200
|
const plans = context.plans.toSorted((a, b) => (a.package.name || "").localeCompare(b.package.name || ""));
|
|
71
|
-
const token = context.env.GITLAB_TOKEN;
|
|
72
201
|
const repository = context.repository;
|
|
73
|
-
|
|
202
|
+
const rollbacks = [];
|
|
203
|
+
for (const plan of plans.toReversed()) {
|
|
204
|
+
const rollback = await createRelease(context, plan, token, repository);
|
|
205
|
+
if (rollback) rollbacks.push(rollback);
|
|
206
|
+
}
|
|
74
207
|
try {
|
|
75
208
|
return await next(context);
|
|
76
209
|
} catch (err) {
|
|
77
|
-
await Promise.allSettled(
|
|
210
|
+
await Promise.allSettled(rollbacks.map((rollback) => rollback()));
|
|
78
211
|
throw err;
|
|
79
212
|
}
|
|
80
213
|
}
|
|
@@ -95,6 +228,6 @@ async function isRepositoryPublic(token, repository) {
|
|
|
95
228
|
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`);
|
|
96
229
|
return (await api.Projects.show(projectId)).visibility === "public";
|
|
97
230
|
}
|
|
98
|
-
export { createRelease, gitlab as default, deleteRelease, isRepositoryPublic };
|
|
231
|
+
export { createRelease, gitlab as default, deleteRelease, fetchRelease, isRepositoryPublic, restoreRelease, verifyToken };
|
|
99
232
|
|
|
100
233
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/createRelease.ts","../src/deleteRelease.ts","../src/gitlab.ts","../src/isRepositoryPublic.ts"],"sourcesContent":["// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport { Gitlab } from '@gitbeaker/rest'\nimport type { GitRepository, ReleaseContext, ReleasePlan } from '@reelout/core'\n\n/**\n * Create a GitLab release for a package tag.\n *\n * Skips creation when the tag has no message. Uses the GitBeaker REST API\n * with the provided token to create the release.\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 when the release is created\n */\nexport async function createRelease(\n context: ReleaseContext,\n plan: ReleasePlan,\n token: string,\n repository: GitRepository\n): Promise<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 await api.ProjectReleases.create(projectId, {\n name: plan.release.tag.name,\n tag_name: plan.release.tag.name,\n description: plan.release.tag.message,\n ref: context.commit.hash,\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'\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 const status = (err as { cause?: { response?: { status?: number } } })\n ?.cause?.response?.status\n\n if (status === 404) {\n return\n }\n\n throw err\n }\n}\n","// SPDX-License-Identifier: MIT\n// Copyright (c) 2026 Fai\n\nimport type { ReleasePlugin } from '@reelout/core'\nimport { version } from '../package.json'\nimport { createRelease } from './createRelease'\nimport { deleteRelease } from './deleteRelease'\n\n/**\n * Built-in plugin that creates GitLab releases for finalized release plans.\n *\n * @remarks\n * **Lifecycle Stages:**\n * - `publish`: Creates GitLab releases in reverse alphabetical package order (ensuring the root package release\n * is created last). Automatically rolls back and deletes created releases if subsequent publishing stages fail.\n *\n * @throws {Error} If `context.repository` is not configured or `GITLAB_TOKEN` environment variable is missing.\n * @returns A release plugin configured for GitLab release creation.\n */\nconst gitlab = (): ReleasePlugin => ({\n name: '@reelout/gitlab',\n version,\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 if (!context.env.GITLAB_TOKEN) {\n throw new Error('The GITLAB_TOKEN environment variable is required.')\n }\n\n if (context.dryRun) {\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 token = context.env.GITLAB_TOKEN\n\n const repository = context.repository\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 await createRelease(context, plan, token, repository)\n }\n\n try {\n return await next(context)\n } catch (err) {\n await Promise.allSettled(\n plans.map((plan) => deleteRelease(plan, token, repository))\n )\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":";;;;;;;;;;;;;;AAkBA,eAAsB,cACpB,SACA,MACA,OACA,YACe;CACf,IAAI,CAAC,KAAK,SAAS,KAAK,SACtB;CAGF,MAAM,MAAM,IAAI,OAAO,EAAE,MAAM,CAAC;CAEhC,MAAM,YAAY,mBAAmB,GAAG,WAAW,MAAM,GAAG,WAAW,MAAM;CAE7E,MAAM,IAAI,gBAAgB,OAAO,WAAW;EAC1C,MAAM,KAAK,QAAQ,IAAI;EACvB,UAAU,KAAK,QAAQ,IAAI;EAC3B,aAAa,KAAK,QAAQ,IAAI;EAC9B,KAAK,QAAQ,OAAO;CACtB,CAAC;AACH;;;;;;;;;;;;;ACrBA,eAAsB,cACpB,MACA,OACA,YACe;CACf,IAAI,CAAC,KAA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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,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;;;;;;;;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AQ,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reelout/gitlab",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "GitLab plugin for publishing releases from ReelOut release plans.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
],
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@gitbeaker/rest": "^43.8.0",
|
|
36
|
-
"@reelout/core": "^0.
|
|
36
|
+
"@reelout/core": "^0.11.0"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=24.15"
|
package/src/createRelease.ts
CHANGED
|
@@ -3,25 +3,37 @@
|
|
|
3
3
|
|
|
4
4
|
import { Gitlab } from '@gitbeaker/rest'
|
|
5
5
|
import type { GitRepository, ReleaseContext, ReleasePlan } from '@reelout/core'
|
|
6
|
+
import { deleteRelease } from './deleteRelease'
|
|
7
|
+
import { fetchRelease } from './fetchRelease'
|
|
8
|
+
import { restoreRelease } from './restoreRelease'
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
|
-
*
|
|
11
|
+
* Rollback callback returned after a release was created or overwritten.
|
|
12
|
+
*/
|
|
13
|
+
export type ReleaseRollback = () => Promise<void>
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create or overwrite a GitLab release for a package tag.
|
|
9
17
|
*
|
|
10
|
-
* Skips
|
|
11
|
-
*
|
|
18
|
+
* Skips the write when the tag has no message. Fetches the release for the
|
|
19
|
+
* tag first: an existing release is overwritten with the generated content
|
|
20
|
+
* via the update endpoint, and a missing release is created. Returns a
|
|
21
|
+
* rollback callback that restores an overwritten release or deletes a
|
|
22
|
+
* release created by this call.
|
|
12
23
|
*
|
|
13
24
|
* @param context Release context containing commit and prerelease information
|
|
14
25
|
* @param plan Release plan containing the tag to publish
|
|
15
26
|
* @param token GitLab authentication token
|
|
16
27
|
* @param repository Target repository
|
|
17
|
-
* @returns Promise that resolves
|
|
28
|
+
* @returns Promise that resolves to a rollback callback for the release, or
|
|
29
|
+
* undefined when the write was skipped
|
|
18
30
|
*/
|
|
19
31
|
export async function createRelease(
|
|
20
32
|
context: ReleaseContext,
|
|
21
33
|
plan: ReleasePlan,
|
|
22
34
|
token: string,
|
|
23
35
|
repository: GitRepository
|
|
24
|
-
): Promise<void> {
|
|
36
|
+
): Promise<ReleaseRollback | void> {
|
|
25
37
|
if (!plan.release?.tag?.message) {
|
|
26
38
|
return
|
|
27
39
|
}
|
|
@@ -30,10 +42,29 @@ export async function createRelease(
|
|
|
30
42
|
|
|
31
43
|
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)
|
|
32
44
|
|
|
45
|
+
const tagName = plan.release.tag.name
|
|
46
|
+
|
|
47
|
+
const existing = await fetchRelease(api, projectId, tagName)
|
|
48
|
+
|
|
49
|
+
if (existing) {
|
|
50
|
+
await api.ProjectReleases.edit(projectId, tagName, {
|
|
51
|
+
name: tagName,
|
|
52
|
+
description: plan.release.tag.message,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
context.logger.warn(
|
|
56
|
+
`GitLab release for tag "${tagName}" already exists: its name and description were overwritten with the freshly generated content.`
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
return () => restoreRelease(plan, existing, token, repository)
|
|
60
|
+
}
|
|
61
|
+
|
|
33
62
|
await api.ProjectReleases.create(projectId, {
|
|
34
|
-
name:
|
|
35
|
-
tag_name:
|
|
63
|
+
name: tagName,
|
|
64
|
+
tag_name: tagName,
|
|
36
65
|
description: plan.release.tag.message,
|
|
37
66
|
ref: context.commit.hash,
|
|
38
67
|
})
|
|
68
|
+
|
|
69
|
+
return () => deleteRelease(plan, token, repository)
|
|
39
70
|
}
|
package/src/deleteRelease.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { Gitlab } from '@gitbeaker/rest'
|
|
5
5
|
import type { GitRepository, ReleasePlan } from '@reelout/core'
|
|
6
|
+
import { isNotFoundError } from './isNotFoundError'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Delete a GitLab release associated with a package tag.
|
|
@@ -33,10 +34,7 @@ export async function deleteRelease(
|
|
|
33
34
|
|
|
34
35
|
await api.ProjectReleases.remove(projectId, plan.release.tag.name)
|
|
35
36
|
} catch (err) {
|
|
36
|
-
|
|
37
|
-
?.cause?.response?.status
|
|
38
|
-
|
|
39
|
-
if (status === 404) {
|
|
37
|
+
if (isNotFoundError(err)) {
|
|
40
38
|
return
|
|
41
39
|
}
|
|
42
40
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import type { Gitlab } from '@gitbeaker/rest'
|
|
5
|
+
import { isNotFoundError } from './isNotFoundError'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* GitLab release content captured before an overwrite.
|
|
9
|
+
*/
|
|
10
|
+
export interface GitLabRelease {
|
|
11
|
+
/** Release name as it existed before the overwrite. */
|
|
12
|
+
name: string | null
|
|
13
|
+
/** Release description as it existed before the overwrite. */
|
|
14
|
+
description: string | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fetch the GitLab release that exists for a tag.
|
|
19
|
+
*
|
|
20
|
+
* @param api Authenticated GitLab client
|
|
21
|
+
* @param projectId Encoded project id the release belongs to
|
|
22
|
+
* @param tagName Tag the release is looked up by
|
|
23
|
+
* @returns The GitLab release content, or null when the tag has no release
|
|
24
|
+
*/
|
|
25
|
+
export async function fetchRelease(
|
|
26
|
+
api: Gitlab,
|
|
27
|
+
projectId: string,
|
|
28
|
+
tagName: string
|
|
29
|
+
): Promise<GitLabRelease | null> {
|
|
30
|
+
try {
|
|
31
|
+
const release = await api.ProjectReleases.show(projectId, tagName)
|
|
32
|
+
|
|
33
|
+
return { name: release.name, description: release.description }
|
|
34
|
+
} catch (err) {
|
|
35
|
+
if (isNotFoundError(err)) {
|
|
36
|
+
return null
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw err
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/gitlab.ts
CHANGED
|
@@ -1,25 +1,68 @@
|
|
|
1
1
|
// SPDX-License-Identifier: MIT
|
|
2
2
|
// Copyright (c) 2026 Fai
|
|
3
3
|
|
|
4
|
-
import type { ReleasePlugin } from '@reelout/core'
|
|
4
|
+
import type { ReleaseContext, ReleasePlugin } from '@reelout/core'
|
|
5
5
|
import { version } from '../package.json'
|
|
6
|
-
import { createRelease } from './createRelease'
|
|
7
|
-
import {
|
|
6
|
+
import { createRelease, type ReleaseRollback } from './createRelease'
|
|
7
|
+
import { verifyToken } from './verifyToken'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Read the GitLab token from the release environment.
|
|
11
|
+
*
|
|
12
|
+
* Treats unset and whitespace-only values as missing.
|
|
13
|
+
*
|
|
14
|
+
* @param env Release environment variables
|
|
15
|
+
* @returns The trimmed token, or undefined when it is not set
|
|
16
|
+
*/
|
|
17
|
+
const readGitlabToken = (env: ReleaseContext['env']): string | undefined => {
|
|
18
|
+
const token = env.GITLAB_TOKEN?.trim()
|
|
19
|
+
|
|
20
|
+
return token ? token : undefined
|
|
21
|
+
}
|
|
8
22
|
|
|
9
23
|
/**
|
|
10
24
|
* Built-in plugin that creates GitLab releases for finalized release plans.
|
|
11
25
|
*
|
|
12
26
|
* @remarks
|
|
13
27
|
* **Lifecycle Stages:**
|
|
28
|
+
* - `configure`: Checks the `GITLAB_TOKEN` environment variable. A missing token
|
|
29
|
+
* skips GitLab release creation with a warning — or throws under strict mode —
|
|
30
|
+
* and a provided token is verified against the release project so an invalid
|
|
31
|
+
* token fails before any mutation. Skips the pre-flight when repository
|
|
32
|
+
* context is unavailable. Runs under dry-run.
|
|
14
33
|
* - `publish`: Creates GitLab releases in reverse alphabetical package order (ensuring the root package release
|
|
15
|
-
* is created last)
|
|
34
|
+
* is created last), overwriting existing releases for the same tag. Automatically rolls back if subsequent
|
|
35
|
+
* publishing stages fail: releases created by this run are deleted, and overwritten releases are restored
|
|
36
|
+
* to their original content.
|
|
16
37
|
*
|
|
17
|
-
* @throws {Error} If `context.repository` is not configured or `GITLAB_TOKEN` environment variable is missing.
|
|
18
38
|
* @returns A release plugin configured for GitLab release creation.
|
|
19
39
|
*/
|
|
20
40
|
const gitlab = (): ReleasePlugin => ({
|
|
21
41
|
name: '@reelout/gitlab',
|
|
22
42
|
version,
|
|
43
|
+
configure: async (context, next) => {
|
|
44
|
+
const token = readGitlabToken(context.env)
|
|
45
|
+
|
|
46
|
+
if (!token) {
|
|
47
|
+
if (context.strict) {
|
|
48
|
+
throw new Error(
|
|
49
|
+
'The GITLAB_TOKEN environment variable is required in strict mode.'
|
|
50
|
+
)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
context.logger.warn(
|
|
54
|
+
'GitLab releases will not be created: GITLAB_TOKEN is not set.'
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
return next(context)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (context.repository) {
|
|
61
|
+
await verifyToken(token, context.repository)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return next(context)
|
|
65
|
+
},
|
|
23
66
|
publish: async (context, next) => {
|
|
24
67
|
if (!context.repository) {
|
|
25
68
|
throw new Error(
|
|
@@ -27,11 +70,14 @@ const gitlab = (): ReleasePlugin => ({
|
|
|
27
70
|
)
|
|
28
71
|
}
|
|
29
72
|
|
|
30
|
-
|
|
31
|
-
|
|
73
|
+
const token = readGitlabToken(context.env)
|
|
74
|
+
|
|
75
|
+
if (!token) {
|
|
76
|
+
return next(context)
|
|
32
77
|
}
|
|
33
78
|
|
|
34
79
|
if (context.dryRun) {
|
|
80
|
+
context.logger.debug('[DRY RUN] GitLab releases will not be created.')
|
|
35
81
|
return next(context)
|
|
36
82
|
}
|
|
37
83
|
|
|
@@ -39,23 +85,25 @@ const gitlab = (): ReleasePlugin => ({
|
|
|
39
85
|
(a.package.name || '').localeCompare(b.package.name || '')
|
|
40
86
|
)
|
|
41
87
|
|
|
42
|
-
const token = context.env.GITLAB_TOKEN
|
|
43
|
-
|
|
44
88
|
const repository = context.repository
|
|
45
89
|
|
|
90
|
+
const rollbacks: ReleaseRollback[] = []
|
|
91
|
+
|
|
46
92
|
// Create releases in reverse order
|
|
47
93
|
// so that the root package release is created last,
|
|
48
94
|
// as it is often the most important one.
|
|
49
95
|
for (const plan of plans.toReversed()) {
|
|
50
|
-
await createRelease(context, plan, token, repository)
|
|
96
|
+
const rollback = await createRelease(context, plan, token, repository)
|
|
97
|
+
|
|
98
|
+
if (rollback) {
|
|
99
|
+
rollbacks.push(rollback)
|
|
100
|
+
}
|
|
51
101
|
}
|
|
52
102
|
|
|
53
103
|
try {
|
|
54
104
|
return await next(context)
|
|
55
105
|
} catch (err) {
|
|
56
|
-
await Promise.allSettled(
|
|
57
|
-
plans.map((plan) => deleteRelease(plan, token, repository))
|
|
58
|
-
)
|
|
106
|
+
await Promise.allSettled(rollbacks.map((rollback) => rollback()))
|
|
59
107
|
|
|
60
108
|
throw err
|
|
61
109
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
|
|
4
4
|
export * from './createRelease'
|
|
5
5
|
export * from './deleteRelease'
|
|
6
|
+
export * from './fetchRelease'
|
|
6
7
|
export * from './gitlab'
|
|
7
8
|
export { default } from './gitlab'
|
|
8
9
|
export * from './isRepositoryPublic'
|
|
10
|
+
export * from './restoreRelease'
|
|
11
|
+
export * from './verifyToken'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Check whether an error represents a GitLab API "not found" response.
|
|
6
|
+
*
|
|
7
|
+
* GitBeaker exposes the HTTP status of a failed request on the `cause`
|
|
8
|
+
* property of the thrown error.
|
|
9
|
+
*
|
|
10
|
+
* @param err Error thrown by a GitBeaker API call
|
|
11
|
+
* @returns True when the error represents a 404 response
|
|
12
|
+
*/
|
|
13
|
+
export function isNotFoundError(err: unknown): boolean {
|
|
14
|
+
const status = (err as { cause?: { response?: { status?: number } } })?.cause
|
|
15
|
+
?.response?.status
|
|
16
|
+
|
|
17
|
+
return status === 404
|
|
18
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import { Gitlab } from '@gitbeaker/rest'
|
|
5
|
+
import type { GitRepository, ReleasePlan } from '@reelout/core'
|
|
6
|
+
import type { GitLabRelease } from './fetchRelease'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Restore an overwritten GitLab release to its original content.
|
|
10
|
+
*
|
|
11
|
+
* Reapplies the captured original name and description via the update
|
|
12
|
+
* endpoint. Skips the restore when the plan does not contain a valid tag
|
|
13
|
+
* name.
|
|
14
|
+
*
|
|
15
|
+
* @param plan Release plan containing the tag associated with the release to restore
|
|
16
|
+
* @param original Original release content captured before the overwrite
|
|
17
|
+
* @param token GitLab authentication token
|
|
18
|
+
* @param repository Target repository
|
|
19
|
+
* @returns Promise that resolves when the release is restored
|
|
20
|
+
*/
|
|
21
|
+
export async function restoreRelease(
|
|
22
|
+
plan: ReleasePlan,
|
|
23
|
+
original: GitLabRelease,
|
|
24
|
+
token: string,
|
|
25
|
+
repository: GitRepository
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
if (!plan.release?.tag?.name) {
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const api = new Gitlab({ token })
|
|
32
|
+
|
|
33
|
+
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)
|
|
34
|
+
|
|
35
|
+
await api.ProjectReleases.edit(projectId, plan.release.tag.name, {
|
|
36
|
+
name: original.name,
|
|
37
|
+
description: original.description,
|
|
38
|
+
})
|
|
39
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
// Copyright (c) 2026 Fai
|
|
3
|
+
|
|
4
|
+
import { Gitlab } from '@gitbeaker/rest'
|
|
5
|
+
import type { GitRepository } from '@reelout/core'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Verify that a GitLab token can access the project it releases to.
|
|
9
|
+
*
|
|
10
|
+
* Performs the token pre-flight for the GitLab plugin by reading the target
|
|
11
|
+
* project, so a provided-but-invalid or expired token fails during the
|
|
12
|
+
* configure stage, before any release is created. The project is read
|
|
13
|
+
* instead of the current user because tokens scoped to a project or a CI
|
|
14
|
+
* job are not always permitted to read the current user while still being
|
|
15
|
+
* able to create releases for the project. The check runs identically
|
|
16
|
+
* under dry-run.
|
|
17
|
+
*
|
|
18
|
+
* @param token GitLab authentication token
|
|
19
|
+
* @param repository Target repository
|
|
20
|
+
* @returns A promise that resolves when GitLab accepts the token
|
|
21
|
+
* @throws If reading the project fails, carrying the original error as the cause
|
|
22
|
+
*/
|
|
23
|
+
export async function verifyToken(
|
|
24
|
+
token: string,
|
|
25
|
+
repository: GitRepository
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
const api = new Gitlab({ token })
|
|
28
|
+
|
|
29
|
+
const projectId = encodeURIComponent(`${repository.owner}/${repository.repo}`)
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
await api.Projects.show(projectId)
|
|
33
|
+
} catch (err) {
|
|
34
|
+
throw new Error(
|
|
35
|
+
'The GITLAB_TOKEN environment variable could not be verified.',
|
|
36
|
+
{ cause: err }
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
}
|