@bakers-build/addon-forge 0.20.0 → 0.21.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.
@@ -30,6 +30,17 @@ export interface ForgeMerge {
30
30
  readonly subject: string;
31
31
  readonly body: string;
32
32
  }
33
+ /**
34
+ * Where a ticket whose origin is an issue with its own fork works: the fork's
35
+ * git remote and branch, and the fork project the MR comes from.
36
+ */
37
+ export interface ForkTarget {
38
+ readonly auth: GitAuth;
39
+ /** The fork branch the ticket branch maps to. */
40
+ readonly branch: string;
41
+ readonly projectId: number;
42
+ readonly path: string;
43
+ }
33
44
  export interface IssueLabel {
34
45
  readonly name: string;
35
46
  /** False for a label inherited from a group. */
@@ -41,12 +52,19 @@ export interface ForgeBackend {
41
52
  secrets(): readonly string[];
42
53
  /** The configured repository URL and the header that authenticates git. */
43
54
  gitAuth(): Promise<GitAuth>;
44
- findOpenPr(head: string): Promise<ForgePr | undefined>;
55
+ /**
56
+ * The issue fork for this origin; undefined when the backend has no fork
57
+ * mode or the origin is not one of its issues. Refuses a missing fork.
58
+ */
59
+ forkTarget?(issueUrl: string, title: string): Promise<ForkTarget | undefined>;
60
+ /** With `fork`, only an MR from that fork. */
61
+ findOpenPr(head: string, fork?: ForkTarget): Promise<ForgePr | undefined>;
45
62
  createPr(input: {
46
63
  head: string;
47
64
  base: string;
48
65
  title: string;
49
66
  body: string;
67
+ fork?: ForkTarget;
50
68
  }): Promise<ForgePr>;
51
69
  /** Also retargets the PR to `base`, which may have moved since it opened. */
52
70
  updatePr(pr: ForgePr, input: {
@@ -61,14 +79,15 @@ export interface ForgeBackend {
61
79
  */
62
80
  checks(ref: string, base: string, opts?: {
63
81
  allRuns?: boolean;
82
+ fork?: ForkTarget;
64
83
  }): Promise<ForgeCheck[]>;
65
84
  /** A conflict or a not yet computed mergeability is a reason. */
66
85
  mergeBlocker(pr: ForgePr): Promise<string | undefined>;
67
86
  /** Merges with every option sent explicitly; deletes the source when asked. */
68
87
  merge(pr: ForgePr, options: ForgeMerge): Promise<void>;
69
88
  isMerged(pr: ForgePr): Promise<boolean>;
70
- /** The [tail] of a job's log; refuses a job outside the repository. */
71
- jobLog(jobId: number, maxBytes: number): Promise<LogTail>;
89
+ /** The [tail] of a job's log; refuses a job outside the repository (or `fork`). */
90
+ jobLog(jobId: number, maxBytes: number, fork?: ForkTarget): Promise<LogTail>;
72
91
  /**
73
92
  * The origin issue's operations, by its web URL; a backend refuses a URL
74
93
  * outside its host before any request. Labels include inherited ones.
package/dist/src/git.d.ts CHANGED
@@ -12,20 +12,22 @@ export interface PushLease {
12
12
  /** The SHA of a local branch of the clone, if it exists. */
13
13
  export declare function localSha(worktree: string, branch: string): Promise<string | undefined>;
14
14
  /**
15
- * Fetch `branches` into the clone's `refs/remotes/origin/<name>`, no tags.
16
- * Branches the remote lacks are skipped. Changes nothing remote.
15
+ * Fetch `branches` into the clone's `refs/remotes/origin/<name>`, no tags;
16
+ * `as[remote]` names the tracking ref (and the result) of a remote branch
17
+ * whose local name differs. Branches the remote lacks are skipped. Changes
18
+ * nothing remote.
17
19
  */
18
- export declare function fetchBranches(worktree: string, auth: GitAuth, branches: readonly string[]): Promise<{
20
+ export declare function fetchBranches(worktree: string, auth: GitAuth, branches: readonly string[], as?: Readonly<Record<string, string>>): Promise<{
19
21
  name: string;
20
22
  sha: string;
21
23
  }[]>;
22
24
  /**
23
- * Push the clone's `refs/heads/<branch>` to the remote's branch of the same
24
- * name. Without a lease only a fast-forward lands; with one, the remote must
25
+ * Push the clone's `refs/heads/<branch>` to the remote's `remoteBranch`
26
+ * (default: the same name). Without a lease only a fast-forward lands; with one, the remote must
25
27
  * still be at the expected SHA (absent remote-tracking ref: no branch).
26
28
  * Returns the pushed SHA and records it as refs/remotes/origin/<branch>.
27
29
  */
28
- export declare function pushBranch(worktree: string, auth: GitAuth, branch: string, lease: PushLease): Promise<string>;
30
+ export declare function pushBranch(worktree: string, auth: GitAuth, branch: string, lease: PushLease, remoteBranch?: string): Promise<string>;
29
31
  export interface BranchUpdate {
30
32
  /** The branch tip before the update. */
31
33
  previous: string;
package/dist/src/git.js CHANGED
@@ -117,10 +117,12 @@ function parseLsRemote(out) {
117
117
  return heads;
118
118
  }
119
119
  /**
120
- * Fetch `branches` into the clone's `refs/remotes/origin/<name>`, no tags.
121
- * Branches the remote lacks are skipped. Changes nothing remote.
120
+ * Fetch `branches` into the clone's `refs/remotes/origin/<name>`, no tags;
121
+ * `as[remote]` names the tracking ref (and the result) of a remote branch
122
+ * whose local name differs. Branches the remote lacks are skipped. Changes
123
+ * nothing remote.
122
124
  */
123
- export async function fetchBranches(worktree, auth, branches) {
125
+ export async function fetchBranches(worktree, auth, branches, as = {}) {
124
126
  return withScratch(worktree, auth, async ({ common, env, remote }) => {
125
127
  const heads = parseLsRemote(await remote([
126
128
  'ls-remote',
@@ -142,10 +144,10 @@ export async function fetchBranches(worktree, auth, branches) {
142
144
  '--no-write-fetch-head',
143
145
  '-q',
144
146
  auth.url,
145
- ...wanted.map((b) => `+refs/heads/${b}:refs/remotes/origin/${b}`),
147
+ ...wanted.map((b) => `+refs/heads/${b}:refs/remotes/origin/${as[b] ?? b}`),
146
148
  ]);
147
149
  const out = [];
148
- for (const name of wanted) {
150
+ for (const name of wanted.map((b) => as[b] ?? b)) {
149
151
  // What the fetch wrote, not ls-remote's answer: the remote may have moved since.
150
152
  const sha = await remote(['rev-parse', `refs/remotes/origin/${name}`]);
151
153
  await inClone(common, env, 'update-ref', `refs/remotes/origin/${name}`, sha);
@@ -155,12 +157,12 @@ export async function fetchBranches(worktree, auth, branches) {
155
157
  });
156
158
  }
157
159
  /**
158
- * Push the clone's `refs/heads/<branch>` to the remote's branch of the same
159
- * name. Without a lease only a fast-forward lands; with one, the remote must
160
+ * Push the clone's `refs/heads/<branch>` to the remote's `remoteBranch`
161
+ * (default: the same name). Without a lease only a fast-forward lands; with one, the remote must
160
162
  * still be at the expected SHA (absent remote-tracking ref: no branch).
161
163
  * Returns the pushed SHA and records it as refs/remotes/origin/<branch>.
162
164
  */
163
- export async function pushBranch(worktree, auth, branch, lease) {
165
+ export async function pushBranch(worktree, auth, branch, lease, remoteBranch = branch) {
164
166
  return withScratch(worktree, auth, async ({ common, env, remote }) => {
165
167
  const sha = await readRef(common, env, `refs/heads/${branch}`);
166
168
  if (!sha)
@@ -170,9 +172,9 @@ export async function pushBranch(worktree, auth, branch, lease) {
170
172
  const expected = lease.expectedSha ??
171
173
  (await readRef(common, env, `refs/remotes/origin/${branch}`)) ??
172
174
  '';
173
- args.push(`--force-with-lease=refs/heads/${branch}:${expected}`);
175
+ args.push(`--force-with-lease=refs/heads/${remoteBranch}:${expected}`);
174
176
  }
175
- await remote([...args, auth.url, `${sha}:refs/heads/${branch}`]);
177
+ await remote([...args, auth.url, `${sha}:refs/heads/${remoteBranch}`]);
176
178
  await inClone(common, env, 'update-ref', `refs/remotes/origin/${branch}`, sha);
177
179
  return sha;
178
180
  });
package/dist/src/tools.js CHANGED
@@ -97,6 +97,25 @@ async function requestReviewers(req, b, pr) {
97
97
  ],
98
98
  };
99
99
  }
100
+ /** The issue fork the run's ticket works in, when its origin has one. */
101
+ const forkOf = (req, b) => req.ticket?.issueUrl && b.forkTarget
102
+ ? b.forkTarget(req.ticket.issueUrl, req.ticket.title ?? '')
103
+ : Promise.resolve(undefined);
104
+ /** The ticket branch maps to the fork's issue branch; its variants keep their names. */
105
+ const remoteName = (branch, run, fork) => fork && branch === run.branchName ? fork.branch : branch;
106
+ /** Someone else may have started the issue branch; the run continues it. */
107
+ const adoptHint = (run, fork) => `origin/${run.branchName} is the issue branch ${fork.branch} of ${fork.path}; when your branch lacks it, run git merge origin/${run.branchName} to continue it`;
108
+ /** Fetch `branches` from upstream, and the ticket branch from the fork in fork mode. */
109
+ async function fetchAll(dir, b, fork, run, branches) {
110
+ if (!fork)
111
+ return fetchBranches(dir, await b.gitAuth(), branches);
112
+ return [
113
+ ...(await fetchBranches(dir, await b.gitAuth(), branches.filter((x) => x !== run.branchName))),
114
+ ...(await fetchBranches(dir, fork.auth, [fork.branch], {
115
+ [fork.branch]: run.branchName,
116
+ })),
117
+ ];
118
+ }
100
119
  const errorText = (err) => err instanceof Error ? err.message : String(err);
101
120
  function ownedBranch(req, run) {
102
121
  const branch = req.args.branch ?? run.branchName;
@@ -163,11 +182,15 @@ export function forgeTools(deps) {
163
182
  const b = await backend();
164
183
  // A run clone has no Git on the host: the parent repository is fetched.
165
184
  const clone = loadRunClone(req.worktreePath);
166
- const refs = await fetchBranches(clone?.parentGitDir ?? req.worktreePath, await b.gitAuth(), [run.baseBranch, run.branchName, ...extra]);
185
+ const fork = await forkOf(req, b);
186
+ const refs = await fetchAll(clone?.parentGitDir ?? req.worktreePath, b, fork, run, [run.baseBranch, run.branchName, ...extra]);
167
187
  // The run sees what the parent got, written without Git in its directory.
168
188
  if (clone)
169
189
  refreshRunTarget(clone, refs.map((ref) => ref.name));
170
- return json({ fetched: refs });
190
+ return json({
191
+ fetched: refs,
192
+ ...(fork ? { next: adoptHint(run, fork) } : {}),
193
+ });
171
194
  },
172
195
  forge_push: async (req) => {
173
196
  const run = runOf(req);
@@ -177,11 +200,18 @@ export function forgeTools(deps) {
177
200
  // Only checked commits leave the run: transfer first, push from the parent.
178
201
  if (clone && branch === clone.branch)
179
202
  transferRun(clone);
180
- const sha = await pushBranch(clone?.parentGitDir ?? req.worktreePath, await b.gitAuth(), branch, {
203
+ const fork = await forkOf(req, b);
204
+ const sha = await pushBranch(clone?.parentGitDir ?? req.worktreePath, fork?.auth ?? (await b.gitAuth()), branch, {
181
205
  forceWithLease: req.args.force_with_lease === true,
182
206
  ...(typeof req.args.expected_sha === 'string'
183
207
  ? { expectedSha: req.args.expected_sha }
184
208
  : {}),
209
+ }, remoteName(branch, run, fork)).catch((err) => {
210
+ if (!fork)
211
+ throw err;
212
+ const why = errorText(err);
213
+ const denied = /\b403\b|denied|not allowed|authentication failed/i.test(why);
214
+ throw new Error(`pushing to issue fork ${fork.path} failed (${why})${denied ? '; without access, use "Get push access" on the issue page, then run again' : ''}`);
185
215
  });
186
216
  if (clone)
187
217
  refreshRunTarget(clone, [branch]);
@@ -195,7 +225,9 @@ export function forgeTools(deps) {
195
225
  if (loaded.restartPending)
196
226
  throw new Error(restartRefusal(loaded.restartPending));
197
227
  const target = await currentBase(req, run.branchName, run);
198
- const auth = await (await backend()).gitAuth();
228
+ const b = await backend();
229
+ const auth = await b.gitAuth();
230
+ const fork = await forkOf(req, b);
199
231
  const tip = runCloneTip(loaded);
200
232
  // Run commits first, so the merge keeps them; a run behind its branch keeps its tip.
201
233
  const clone = {
@@ -203,13 +235,20 @@ export function forgeTools(deps) {
203
235
  expected: transferIsolatedCommit(loaded, tip),
204
236
  };
205
237
  saveRunClone(clone);
206
- await fetchBranches(clone.parentGitDir, auth, [target, run.branchName]);
207
- refreshRunTarget(clone, [target]);
238
+ await fetchAll(clone.parentGitDir, b, fork, run, [
239
+ target,
240
+ run.branchName,
241
+ ]);
242
+ refreshRunTarget(clone, fork ? [target, run.branchName] : [target]);
208
243
  const u = await updateBranch(clone.parentGitDir, auth, {
209
244
  branch: run.branchName,
210
245
  target,
211
246
  expected: clone.expected,
212
247
  workspace: { path: clone.workspace, clean: tip },
248
+ }).catch((err) => {
249
+ if (!fork || !/pushed elsewhere/.test(errorText(err)))
250
+ throw err;
251
+ throw new Error(`${errorText(err)}. ${adoptHint(run, fork)}`);
213
252
  });
214
253
  const protectedPaths = u.resulting === tip
215
254
  ? []
@@ -258,14 +297,17 @@ export function forgeTools(deps) {
258
297
  }
259
298
  const base = await currentBase(req, branch, run);
260
299
  const b = await backend();
261
- const open = await b.findOpenPr(branch);
300
+ const fork = await forkOf(req, b);
301
+ const head = remoteName(branch, run, fork);
302
+ const open = await b.findOpenPr(head, fork);
262
303
  const pr = open
263
304
  ? await b.updatePr(open, { title, body, base })
264
305
  : await b.createPr({
265
- head: branch,
306
+ head,
266
307
  base,
267
308
  title,
268
309
  body,
310
+ ...(fork ? { fork } : {}),
269
311
  });
270
312
  return json({
271
313
  url: pr.url,
@@ -280,6 +322,7 @@ export function forgeTools(deps) {
280
322
  forge_checks: async (req) => {
281
323
  const run = runOf(req);
282
324
  const b = await backend();
325
+ const fork = await forkOf(req, b);
283
326
  let ref;
284
327
  let base = run.targetBranch ?? run.baseBranch;
285
328
  if (typeof req.args.ref === 'string' && req.args.branch === undefined) {
@@ -287,12 +330,14 @@ export function forgeTools(deps) {
287
330
  }
288
331
  else {
289
332
  const branch = ownedBranch(req, run);
290
- const pr = await b.findOpenPr(branch);
291
- ref = pr?.headSha ?? branch;
333
+ const head = remoteName(branch, run, fork);
334
+ const pr = await b.findOpenPr(head, fork);
335
+ ref = pr?.headSha ?? head;
292
336
  base = pr?.base ?? derivedBase(branch, run);
293
337
  }
294
338
  const checks = await b.checks(ref, base, {
295
339
  allRuns: req.args.all_runs === true,
340
+ ...(fork ? { fork } : {}),
296
341
  });
297
342
  return json({ ref, base, overall: overall(checks), checks });
298
343
  },
@@ -301,6 +346,8 @@ export function forgeTools(deps) {
301
346
  const branch = ownedBranch(req, run);
302
347
  const base = await currentBase(req, branch, run);
303
348
  const b = await backend();
349
+ if (await forkOf(req, b))
350
+ throw new Error('upstream maintainers merge drupal.org MRs; bakers stops at the open MR');
304
351
  const pr = await b.findOpenPr(branch);
305
352
  if (!pr)
306
353
  throw new Error(`no open PR/MR from '${branch}'; open it with forge_pr_upsert`);
@@ -348,7 +395,8 @@ export function forgeTools(deps) {
348
395
  const maxBytes = typeof asked === 'number' && asked >= 1
349
396
  ? Math.min(Math.floor(asked), LOG_MAX_BYTES)
350
397
  : LOG_DEFAULT_BYTES;
351
- const log = await (await backend()).jobLog(jobId, maxBytes);
398
+ const b = await backend();
399
+ const log = await b.jobLog(jobId, maxBytes, await forkOf(req, b));
352
400
  return json({
353
401
  job_id: jobId,
354
402
  bytes: log.bytes,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakers-build/addon-forge",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "bakers.build forge tools: a sandboxed run fetches, pushes its own branches, opens its PR/MR, reads checks and merges through the conductor, which holds the token. Backends implement the ForgeBackend contract (./backend).",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,11 +23,11 @@
23
23
  "directory": "bakers-cli/addons/forge"
24
24
  },
25
25
  "peerDependencies": {
26
- "@bakers-build/conductor": "^0.20.0",
27
- "@bakers-build/core": "^0.20.0"
26
+ "@bakers-build/conductor": "^0.21.0",
27
+ "@bakers-build/core": "^0.21.0"
28
28
  },
29
29
  "bakersBuild": {
30
- "commit": "f0dfe478bb1378684622216adaa97571657e3e00",
30
+ "commit": "a3a7417bfbbd14a550213883dbaea762909095ae",
31
31
  "channel": "latest"
32
32
  }
33
33
  }