@bakers-build/addon-forge-gitlab 0.0.0-stage → 0.8.1-next.2

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 keytec GmbH
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,5 @@
1
- # Temporary Holding Version
1
+ # @bakers-build/addon-forge-gitlab
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ bakers.build forge backend for GitLab: a Project Access Token (role Developer, with an expiry date). Implements @bakers-build/addon-forge's ForgeBackend contract.
4
+
5
+ Part of the bakers.build CLI. Install the meta package `@bakers-build/bakers` to get the `bakers` CLI with all addons. Source: https://github.com/bakers-build/bakers-build (bakers-cli/).
@@ -0,0 +1,3 @@
1
+ import { type ForgeBackendFactory } from '@bakers-build/addon-forge/backend';
2
+ declare const factory: ForgeBackendFactory;
3
+ export default factory;
@@ -0,0 +1,132 @@
1
+ // The GitLab forge backend: a Project Access Token (role Developer, with an
2
+ // expiry date) for the one configured project, or, with `auth: 'glab'`, the
3
+ // conductor's own `glab` login. Every merge sends `squash`
4
+ // and `should_remove_source_branch` explicitly, so nothing stored on the MR
5
+ // or the project decides them.
6
+ import { cliToken, requestJson, } from '@bakers-build/addon-forge/backend';
7
+ const prOf = (m) => ({
8
+ number: m.iid,
9
+ url: m.web_url,
10
+ head: m.source_branch,
11
+ base: m.target_branch,
12
+ title: m.title,
13
+ headSha: m.sha,
14
+ });
15
+ function statusOf(status) {
16
+ if (status === 'success' || status === 'skipped')
17
+ return 'success';
18
+ if (status === 'failed' || status === 'canceled')
19
+ return 'failure';
20
+ return 'pending';
21
+ }
22
+ const factory = async (raw, context) => {
23
+ const s = (raw ?? {});
24
+ if (typeof s.project !== 'string' || !s.project.includes('/'))
25
+ throw new Error("project must be the project path, e.g. 'group/repo'");
26
+ const base = (typeof s.url === 'string' ? s.url : 'https://gitlab.com').replace(/\/+$/, '');
27
+ const seen = [];
28
+ let token;
29
+ if (s.auth === 'glab') {
30
+ token = cliToken('glab', ['config', 'get', 'token', '--host', new URL(base).host], seen);
31
+ }
32
+ else if (s.auth !== undefined) {
33
+ throw new Error("auth must be 'glab' or left out for a token");
34
+ }
35
+ else {
36
+ const fixed = typeof s.token === 'string'
37
+ ? s.token
38
+ : typeof s.token_env === 'string'
39
+ ? process.env[s.token_env]
40
+ : undefined;
41
+ if (!fixed)
42
+ throw new Error('token or token_env (naming a set environment variable) must give the Project Access Token');
43
+ seen.push(fixed);
44
+ token = async () => fixed;
45
+ }
46
+ const basic = (t) => Buffer.from(`oauth2:${t}`).toString('base64');
47
+ const doFetch = context.fetch ?? fetch;
48
+ const project = `${base}/api/v4/projects/${encodeURIComponent(s.project)}`;
49
+ const api = async (path, init = {}) => requestJson(doFetch, `${project}${path}`, {
50
+ method: init.method ?? 'GET',
51
+ // Bearer takes access tokens and the OAuth token a `glab` login holds.
52
+ headers: {
53
+ authorization: `Bearer ${await token()}`,
54
+ 'content-type': 'application/json',
55
+ },
56
+ ...(init.body === undefined ? {} : { body: JSON.stringify(init.body) }),
57
+ });
58
+ const mr = async (iid) => (await api(`/merge_requests/${iid}`));
59
+ const q = encodeURIComponent;
60
+ const backend = {
61
+ id: 'gitlab',
62
+ secrets: () => seen.flatMap((t) => [t, basic(t)]),
63
+ gitAuth: async () => ({
64
+ url: `${base}/${s.project}.git`,
65
+ header: `Authorization: Basic ${basic(await token())}`,
66
+ }),
67
+ findOpenPr: async (head) => {
68
+ const list = (await api(`/merge_requests?state=opened&source_branch=${q(head)}`));
69
+ const found = list.find((m) => m.source_branch === head);
70
+ return found && prOf(found);
71
+ },
72
+ createPr: async ({ head, base: target, title, body }) => prOf((await api('/merge_requests', {
73
+ method: 'POST',
74
+ body: {
75
+ source_branch: head,
76
+ target_branch: target,
77
+ title,
78
+ description: body,
79
+ },
80
+ }))),
81
+ updatePr: async (pr, { title, body }) => prOf((await api(`/merge_requests/${pr.number}`, {
82
+ method: 'PUT',
83
+ body: { title, description: body },
84
+ }))),
85
+ openPrsWithBase: async (target) => (await api(`/merge_requests?state=opened&target_branch=${q(target)}&per_page=100`))
86
+ .filter((m) => m.target_branch === target)
87
+ .map(prOf),
88
+ checks: async (ref) => {
89
+ const by = /^[0-9a-f]{40}$/.test(ref) ? `sha=${ref}` : `ref=${q(ref)}`;
90
+ const [pipeline] = (await api(`/pipelines?${by}&order_by=id&sort=desc&per_page=1`));
91
+ if (!pipeline)
92
+ return [{ name: 'pipeline', status: 'missing', required: true }];
93
+ const jobs = (await api(`/pipelines/${pipeline.id}/jobs?per_page=100`));
94
+ return [
95
+ { name: 'pipeline', status: statusOf(pipeline.status), required: true },
96
+ ...jobs.map((j) => ({
97
+ name: j.name,
98
+ status: statusOf(j.status),
99
+ required: !j.allow_failure,
100
+ })),
101
+ ];
102
+ },
103
+ mergeBlocker: async (pr) => {
104
+ const m = await mr(pr.number);
105
+ if (m.has_conflicts)
106
+ return 'merge conflict with its target';
107
+ if (m.detailed_merge_status === 'checking' ||
108
+ m.detailed_merge_status === 'unchecked')
109
+ return 'mergeability is not computed yet; call again shortly';
110
+ return undefined;
111
+ },
112
+ merge: async (pr, o) => {
113
+ if (!o.deleteSource && (await mr(pr.number)).force_remove_source_branch)
114
+ throw new Error(`MR ${pr.url} stores force_remove_source_branch: true, but its source must be kept; the branch owner clears it before the merge`);
115
+ const message = `${o.subject}\n\n${o.body}`;
116
+ await api(`/merge_requests/${pr.number}/merge`, {
117
+ method: 'PUT',
118
+ body: {
119
+ squash: o.method === 'squash',
120
+ should_remove_source_branch: o.deleteSource,
121
+ sha: pr.headSha,
122
+ [o.method === 'squash'
123
+ ? 'squash_commit_message'
124
+ : 'merge_commit_message']: message,
125
+ },
126
+ });
127
+ },
128
+ isMerged: async (pr) => (await mr(pr.number)).state === 'merged',
129
+ };
130
+ return backend;
131
+ };
132
+ export default factory;
@@ -0,0 +1,37 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://bakers.build/schema/addon-forge-gitlab/options.schema.json",
4
+ "title": "@bakers-build/addon-forge-gitlab options",
5
+ "description": "The forge entry's with.backend.with: a Project Access Token, or the conductor's glab login with auth 'glab'.",
6
+ "type": "object",
7
+ "additionalProperties": false,
8
+ "required": [
9
+ "project"
10
+ ],
11
+ "properties": {
12
+ "url": {
13
+ "type": "string",
14
+ "pattern": "^https?://",
15
+ "description": "Default https://gitlab.com."
16
+ },
17
+ "project": {
18
+ "type": "string",
19
+ "pattern": "/",
20
+ "description": "The project path, e.g. group/repo."
21
+ },
22
+ "auth": {
23
+ "const": "glab",
24
+ "description": "Use the conductor's own glab login instead of a token."
25
+ },
26
+ "token": {
27
+ "type": "string",
28
+ "minLength": 1,
29
+ "description": "The Project Access Token; machine config only."
30
+ },
31
+ "token_env": {
32
+ "type": "string",
33
+ "minLength": 1,
34
+ "description": "Names the conductor environment variable that holds the credential."
35
+ }
36
+ }
37
+ }
package/package.json CHANGED
@@ -1,6 +1,31 @@
1
1
  {
2
2
  "name": "@bakers-build/addon-forge-gitlab",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.8.1-next.2",
4
+ "description": "bakers.build forge backend for GitLab: a Project Access Token (role Developer, with an expiry date). Implements @bakers-build/addon-forge's ForgeBackend contract.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "exports": {
8
+ ".": "./dist/src/index.js",
9
+ "./options.schema.json": "./options.schema.json"
10
+ },
11
+ "files": [
12
+ "dist/src",
13
+ "options.schema.json"
14
+ ],
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/bakers-build/bakers-build.git",
21
+ "directory": "bakers-cli/addons/forge-gitlab"
22
+ },
23
+ "peerDependencies": {
24
+ "@bakers-build/addon-forge": "^0.8.1-next.2",
25
+ "@bakers-build/core": "^0.8.1-next.2"
26
+ },
27
+ "bakersBuild": {
28
+ "commit": "0177880667693d1c36c65ffced82d2f3a17cbf2a",
29
+ "channel": "next"
30
+ }
31
+ }