@gitkraken/core-gitlens 0.65.0 → 0.67.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.
Files changed (48) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/plus/integrations/extensionManager.js.map +1 -1
  3. package/dist/plus/integrations/index.d.ts +2 -1
  4. package/dist/plus/integrations/index.js +12 -2
  5. package/dist/plus/integrations/index.js.map +1 -1
  6. package/dist/plus/integrations/integrationService.d.ts +20 -6
  7. package/dist/plus/integrations/integrationService.js +62 -9
  8. package/dist/plus/integrations/integrationService.js.map +1 -1
  9. package/dist/plus/integrations/manager.d.ts +7 -4
  10. package/dist/plus/integrations/models/gitlabMergeRequestWrites.d.ts +3 -9
  11. package/dist/plus/integrations/models/linearIssueWrites.d.ts +101 -0
  12. package/dist/plus/integrations/models/linearIssueWrites.js +0 -0
  13. package/dist/plus/integrations/models/publicIntegration.d.ts +1 -0
  14. package/dist/plus/integrations/providers/azure/azure.js +1 -1
  15. package/dist/plus/integrations/providers/gitlab/mergeRequestPermissions.d.ts +36 -0
  16. package/dist/plus/integrations/providers/gitlab/mergeRequestPermissions.js +36 -0
  17. package/dist/plus/integrations/providers/gitlab/mergeRequestPermissions.js.map +1 -0
  18. package/dist/plus/integrations/providers/gitlab/mergeRequestWrites.d.ts +1 -2
  19. package/dist/plus/integrations/providers/gitlab/mergeRequestWrites.js +93 -73
  20. package/dist/plus/integrations/providers/gitlab/mergeRequestWrites.js.map +1 -1
  21. package/dist/plus/integrations/providers/gitlab/writeTransport.d.ts +7 -1
  22. package/dist/plus/integrations/providers/gitlab/writeTransport.js +21 -2
  23. package/dist/plus/integrations/providers/gitlab/writeTransport.js.map +1 -1
  24. package/dist/plus/integrations/providers/issueSorts.d.ts +1 -0
  25. package/dist/plus/integrations/providers/linear.d.ts +6 -6
  26. package/dist/plus/integrations/providers/linear.js +36 -14
  27. package/dist/plus/integrations/providers/linear.js.map +1 -1
  28. package/dist/plus/integrations/providers/linearIssueWriteTransport.d.ts +27 -0
  29. package/dist/plus/integrations/providers/linearIssueWriteTransport.js +110 -0
  30. package/dist/plus/integrations/providers/linearIssueWriteTransport.js.map +1 -0
  31. package/dist/plus/integrations/providers/linearIssueWrites.d.ts +27 -0
  32. package/dist/plus/integrations/providers/linearIssueWrites.js +310 -0
  33. package/dist/plus/integrations/providers/linearIssueWrites.js.map +1 -0
  34. package/dist/plus/integrations/providers/linearIssuesEtag.d.ts +2 -0
  35. package/dist/plus/integrations/providers/linearIssuesEtag.js +27 -1
  36. package/dist/plus/integrations/providers/linearIssuesEtag.js.map +1 -1
  37. package/dist/plus/integrations/providers/providerApiClient.d.ts +2 -2
  38. package/dist/plus/integrations/providers/providerApiClient.js +5 -5
  39. package/dist/plus/integrations/providers/providerApiClient.js.map +1 -1
  40. package/dist/plus/integrations/providers/providerErrors.d.ts +9 -0
  41. package/dist/plus/integrations/providers/providerErrors.js +1 -1
  42. package/dist/plus/integrations/providers/providerErrors.js.map +1 -1
  43. package/dist/plus/integrations/providers/providersApi.d.ts +1 -0
  44. package/dist/plus/integrations/providers/providersApi.js +8 -1
  45. package/dist/plus/integrations/providers/providersApi.js.map +1 -1
  46. package/dist/plus/integrations/reads/drains.d.ts +1 -0
  47. package/docs/integrations.md +92 -14
  48. package/package.json +1 -1
@@ -1,12 +1,12 @@
1
1
  import { isGitLabDecimalId } from "../../models/gitlabIssueWrites.js";
2
2
  import { isGitBranchName } from "../utils/writeInputs.js";
3
3
  import { GitLabWriteError, gitLabWriteFailure, validateGitLabWriteFields } from "./writeErrors.js";
4
- import { isGitLabProjectRef, isGitLabUrl, restId } from "./writeTransport.js";
4
+ import { gitLabWriteUrlKey, isGitLabProjectRef, isGitLabSha, isGitLabUrl, restId } from "./writeTransport.js";
5
+ import { changedMergeRequestRef, readMergeRequestPermissions } from "./mergeRequestPermissions.js";
5
6
  import * as l10n from "@vscode/l10n";
6
7
  //#region ../plus/integrations/src/providers/gitlab/mergeRequestWrites.ts
7
8
  const mergeRequestGidPrefix = "gid://gitlab/MergeRequest/";
8
9
  const ownerAccessLevel = 50;
9
- const shaPattern = /^[\da-f]{40}(?:[\da-f]{24})?$/i;
10
10
  const draftTitlePattern = /^\s*(?:\[draft\]|\(draft\)|draft:)/i;
11
11
  const timestampPattern = /^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(?:\.(\d{1,9}))?(Z|[+-]\d{2}:\d{2})$/;
12
12
  const mergeStatusPattern = /^[a-z][a-z_]{0,63}$/;
@@ -56,23 +56,6 @@ const createPermissionsQuery = `query MergeRequestCreatePermissions($target: ID!
56
56
  target: project(fullPath: $target) { id userPermissions { createMergeRequestIn } }
57
57
  source: project(fullPath: $source) { id userPermissions { createMergeRequestFrom } }
58
58
  }`;
59
- const mergeRequestPermissionsQuery = `query MergeRequestWritePermissions($path: ID!, $iid: String!) {
60
- project(fullPath: $path) {
61
- id
62
- maxAccessLevel { integerValue }
63
- mergeRequest(iid: $iid) {
64
- id
65
- autoMergeEnabled
66
- availableAutoMergeStrategies
67
- squashOnMerge
68
- squashReadOnly
69
- userPermissions { updateMergeRequest canMerge }
70
- }
71
- }
72
- }`;
73
- function isSha(value) {
74
- return typeof value === "string" && shaPattern.test(value);
75
- }
76
59
  function sameSha(actual, expected) {
77
60
  return actual?.toLowerCase() === expected.toLowerCase();
78
61
  }
@@ -94,17 +77,25 @@ function mergeStatusOf(raw) {
94
77
  function isApprovedMerge(mergeRequest, input) {
95
78
  return mergeRequest.sourceProjectId === input.expectedSourceProjectId && mergeRequest.sourceBranch === input.expectedSourceBranch && mergeRequest.targetBranch === input.expectedTargetBranch && sameSha(mergeRequest.sourceSha, input.expectedSourceSha);
96
79
  }
80
+ function reachedApprovedSquash(raw, input) {
81
+ return input.squash ? isGitLabSha(raw.squash_commit_sha) : raw.squash_commit_sha === null;
82
+ }
97
83
  function isApprovedTrainCar(car, project, input) {
98
84
  const queued = car?.merge_request;
99
85
  return restId(queued?.id) === input.expectedMergeRequestId && restId(queued?.iid) === input.iid && restId(queued?.project_id) === project.id && queued?.state === "opened" && car?.target_branch === input.expectedTargetBranch;
100
86
  }
101
- function mergeRequestReceipt(raw, project, iid, expectedId) {
87
+ function mergeRequestReceipt(raw, context, iid, expectedId) {
88
+ const { project } = context;
102
89
  const id = restId(raw?.id);
103
90
  const actualIid = restId(raw?.iid);
104
91
  const targetProjectId = restId(raw?.target_project_id);
105
92
  const state = raw?.state;
106
- if (id == null || actualIid == null || targetProjectId == null || !isGitLabUrl(raw.web_url) || typeof raw.title !== "string" || raw.description !== null && typeof raw.description !== "string" || !states.includes(state) || typeof raw.draft !== "boolean" || typeof raw.source_branch !== "string" || typeof raw.target_branch !== "string" || raw.sha != null && !isSha(raw.sha)) throw new GitLabWriteError("unknown");
93
+ if (id == null || actualIid == null || targetProjectId == null || typeof raw.web_url !== "string" || context.providerId === "gitlab" && !isGitLabUrl(raw.web_url) || typeof raw.title !== "string" || raw.description !== null && typeof raw.description !== "string" || !states.includes(state) || typeof raw.draft !== "boolean" || typeof raw.source_branch !== "string" || typeof raw.target_branch !== "string" || raw.sha != null && !isGitLabSha(raw.sha)) throw new GitLabWriteError("unknown");
107
94
  if (targetProjectId !== project.id || restId(raw.project_id) !== project.id || iid != null && iid !== actualIid || expectedId != null && expectedId !== id) throw new GitLabWriteError("conflict");
95
+ if (context.providerId !== "gitlab") {
96
+ const projectUrl = gitLabWriteUrlKey(project.url);
97
+ if (projectUrl == null || gitLabWriteUrlKey(raw.web_url) !== `${projectUrl}/-/merge_requests/${actualIid}`) throw new GitLabWriteError("conflict");
98
+ }
108
99
  const sourceProjectId = restId(raw.source_project_id);
109
100
  return {
110
101
  id,
@@ -128,6 +119,15 @@ function allowed(value) {
128
119
  reason: "forbidden"
129
120
  };
130
121
  }
122
+ function selfManagedSquash(project, squashOnMerge) {
123
+ switch (project.squash_option) {
124
+ case "always": return squashOnMerge ? "required" : void 0;
125
+ case "never": return squashOnMerge ? void 0 : "unavailable";
126
+ case "default_on":
127
+ case "default_off": return squashOnMerge ? "required" : "optional";
128
+ default: return;
129
+ }
130
+ }
131
131
  function transition(permission, available) {
132
132
  return permission.allowed && !available ? {
133
133
  allowed: false,
@@ -153,7 +153,7 @@ function mergeFailure({ kind: _kind, ...refusal }, dispatched) {
153
153
  };
154
154
  }
155
155
  function isValidMerge(input) {
156
- return typeof input.squash === "boolean" && mergeExecutions.includes(input.expectedExecution) && isMergeMethod(input.expectedMergeMethod) && typeof input.expectedRemovesSourceBranch === "boolean" && isGitLabDecimalId(input.expectedSourceProjectId) && isGitBranchName(input.expectedSourceBranch) && isGitBranchName(input.expectedTargetBranch) && isSha(input.expectedSourceSha) && isSha(input.expectedTargetSha);
156
+ return typeof input.squash === "boolean" && mergeExecutions.includes(input.expectedExecution) && isMergeMethod(input.expectedMergeMethod) && typeof input.expectedRemovesSourceBranch === "boolean" && isGitLabDecimalId(input.expectedSourceProjectId) && isGitBranchName(input.expectedSourceBranch) && isGitBranchName(input.expectedTargetBranch) && isGitLabSha(input.expectedSourceSha) && isGitLabSha(input.expectedTargetSha);
157
157
  }
158
158
  function validateFields(action, title, description) {
159
159
  return validateGitLabWriteFields(action, title, description, l10n.t("GitLab quick actions are unavailable in merge request descriptions."));
@@ -215,7 +215,7 @@ var GitLabMergeRequestWriter = class {
215
215
  if (input.action === "create") return this.create(input);
216
216
  const { action } = input;
217
217
  if (action !== "edit" && action !== "close" && action !== "reopen" && action !== "merge") return gitLabWriteFailure("invalid-input");
218
- if (!isGitLabDecimalId(input.expectedMergeRequestId)) return gitLabWriteFailure("invalid-input");
218
+ if (!isGitLabDecimalId(input.iid) || !isGitLabDecimalId(input.expectedMergeRequestId)) return gitLabWriteFailure("invalid-input");
219
219
  return this.writeExisting(input);
220
220
  }
221
221
  async delete(input) {
@@ -235,7 +235,10 @@ var GitLabMergeRequestWriter = class {
235
235
  ok: true,
236
236
  status: "deleted",
237
237
  mergeRequest
238
- }), void 0, { "If-Unmodified-Since": unmodifiedSince });
238
+ }), this.transport.selfManaged ? (response) => response.status === 405 ? {
239
+ ok: false,
240
+ reason: "unsupported"
241
+ } : void 0 : void 0, { "If-Unmodified-Since": unmodifiedSince });
239
242
  }
240
243
  async create(input) {
241
244
  const { title, description, sourceBranch, targetBranch, sourceProjectId, draft } = input;
@@ -250,9 +253,9 @@ var GitLabMergeRequestWriter = class {
250
253
  if (moved != null) return moved;
251
254
  if (!capability.allowed) return gitLabWriteFailure(capability.reason);
252
255
  if (sourceProject.id === project.id && sourceBranch === targetBranch) return gitLabWriteFailure("invalid-input");
253
- if (await this.readBranchTip(sourceProject, sourceBranch) == null) return gitLabWriteFailure("not-found", { sourceBranch: l10n.t("Branch '{0}' not found", sourceBranch) });
254
- if (await this.readBranchTip(project, targetBranch) == null) return gitLabWriteFailure("not-found", { targetBranch: l10n.t("Branch '{0}' not found", targetBranch) });
255
- const existing = await this.findOpenMergeRequest(project, sourceProject, sourceBranch, targetBranch);
256
+ if (await this.transport.readBranchTip(sourceProject, sourceBranch) == null) return gitLabWriteFailure("not-found", { sourceBranch: l10n.t("Branch '{0}' not found", sourceBranch) });
257
+ if (await this.transport.readBranchTip(project, targetBranch) == null) return gitLabWriteFailure("not-found", { targetBranch: l10n.t("Branch '{0}' not found", targetBranch) });
258
+ const existing = await this.findOpenMergeRequest(context, sourceProject, sourceBranch, targetBranch);
256
259
  if (existing != null) return {
257
260
  ...gitLabWriteFailure("conflict"),
258
261
  mergeRequest: existing
@@ -264,7 +267,7 @@ var GitLabMergeRequestWriter = class {
264
267
  title: draft === true && !marked ? `Draft: ${title}` : title,
265
268
  description
266
269
  }, 201, async (response) => {
267
- const created = mergeRequestReceipt(await response.json(), project);
270
+ const created = mergeRequestReceipt(await response.json(), context);
268
271
  return created.state === "opened" && created.sourceProjectId === sourceProject.id && created.sourceBranch === sourceBranch && created.targetBranch === targetBranch ? {
269
272
  ...context,
270
273
  ok: true,
@@ -283,10 +286,15 @@ var GitLabMergeRequestWriter = class {
283
286
  const invalid = validateFields(action, input.title, input.description);
284
287
  if (invalid != null) return invalid;
285
288
  } else if (action === "merge" && !isValidMerge(input)) return gitLabWriteFailure("invalid-input");
289
+ const selfManagedMerge = action === "merge" && this.transport.selfManaged ? input : void 0;
290
+ if (selfManagedMerge != null) {
291
+ const stale = await this.checkLiveMergeRefs(selfManagedMerge);
292
+ if (stale != null) return stale;
293
+ }
286
294
  const read = await this.readMergeRequest({
287
295
  ...input,
288
296
  project: input.projectId
289
- });
297
+ }, selfManagedMerge?.expectedTargetSha);
290
298
  const { context, mergeRequest, capabilities, lockVersion } = read;
291
299
  const moved = movedProject(input, context.project);
292
300
  if (moved != null) return moved;
@@ -306,7 +314,7 @@ var GitLabMergeRequestWriter = class {
306
314
  }
307
315
  const { project } = context;
308
316
  return this.transport.mutate("PUT", this.mergeRequestPath(project, mergeRequest), fields, 200, async (response) => {
309
- const updated = mergeRequestReceipt(await response.json(), project, mergeRequest.iid, mergeRequest.id);
317
+ const updated = mergeRequestReceipt(await response.json(), context, mergeRequest.iid, mergeRequest.id);
310
318
  if (action === "edit") return {
311
319
  ...context,
312
320
  ok: true,
@@ -326,6 +334,25 @@ var GitLabMergeRequestWriter = class {
326
334
  };
327
335
  });
328
336
  }
337
+ async checkLiveMergeRefs(input) {
338
+ const account = await this.transport.readAccount();
339
+ const target = await this.transport.readProject(input.projectId);
340
+ const moved = movedProject(input, target.project);
341
+ if (moved != null) return moved;
342
+ const unavailable = this.getWriteRefusal(account, target.raw);
343
+ if (unavailable != null) return gitLabWriteFailure(unavailable.reason);
344
+ const source = input.expectedSourceProjectId === target.project.id ? target.project : (await this.transport.readProject(input.expectedSourceProjectId)).project;
345
+ if (!sameSha(await this.transport.readBranchTip(source, input.expectedSourceBranch), input.expectedSourceSha)) return {
346
+ ok: false,
347
+ reason: "source-changed",
348
+ mutationDispatched: false
349
+ };
350
+ if (!sameSha(await this.transport.readBranchTip(target.project, input.expectedTargetBranch), input.expectedTargetSha)) return {
351
+ ok: false,
352
+ reason: "target-changed",
353
+ mutationDispatched: false
354
+ };
355
+ }
329
356
  merge(input, { context, mergeRequest, capabilities }) {
330
357
  const { merge } = capabilities;
331
358
  if (merge.kind === "unavailable") return mergeFailure(merge, false);
@@ -365,9 +392,13 @@ var GitLabMergeRequestWriter = class {
365
392
  };
366
393
  }, refusal);
367
394
  }
368
- return this.transport.mutate("PUT", `${this.mergeRequestPath(project, mergeRequest)}/merge`, body, 200, async (response) => {
369
- const merged = mergeRequestReceipt(await response.json(), project, mergeRequest.iid, mergeRequest.id);
370
- return merged.state === "merged" && isApprovedMerge(merged, input) ? {
395
+ return this.transport.mutate("PUT", `${this.mergeRequestPath(project, mergeRequest)}/merge`, this.transport.selfManaged ? {
396
+ ...body,
397
+ should_remove_source_branch: input.expectedRemovesSourceBranch
398
+ } : body, 200, async (response) => {
399
+ const raw = await response.json();
400
+ const merged = mergeRequestReceipt(raw, context, mergeRequest.iid, mergeRequest.id);
401
+ return merged.state === "merged" && isApprovedMerge(merged, input) && (!this.transport.selfManaged || reachedApprovedSquash(raw, input)) ? {
371
402
  ...context,
372
403
  ok: true,
373
404
  status: "merged",
@@ -393,10 +424,12 @@ var GitLabMergeRequestWriter = class {
393
424
  if (!refused && (execution === "train" || status !== 422)) return void 0;
394
425
  let current;
395
426
  let mergeStatus;
427
+ let matchedSquash = false;
396
428
  try {
397
429
  const raw = await this.transport.read(this.mergeRequestPath(context.project, input));
398
- current = mergeRequestReceipt(raw, context.project, input.iid, input.expectedMergeRequestId);
430
+ current = mergeRequestReceipt(raw, context, input.iid, input.expectedMergeRequestId);
399
431
  mergeStatus = mergeStatusOf(raw);
432
+ matchedSquash = !this.transport.selfManaged || reachedApprovedSquash(raw, input);
400
433
  } catch {
401
434
  current = void 0;
402
435
  }
@@ -406,13 +439,13 @@ var GitLabMergeRequestWriter = class {
406
439
  reason: "unknown",
407
440
  applied: "maybe"
408
441
  };
409
- if (current.state === "merged") return {
442
+ if (current.state === "merged" && matchedSquash) return {
410
443
  ...context,
411
444
  ok: true,
412
445
  status: "merged",
413
446
  mergeRequest: current
414
447
  };
415
- if (current.state !== "opened") return {
448
+ if (current.state !== "opened" || this.transport.selfManaged && input.squash) return {
416
449
  ok: false,
417
450
  reason: "unknown",
418
451
  applied: "maybe"
@@ -440,22 +473,22 @@ var GitLabMergeRequestWriter = class {
440
473
  capability = allowed(into && from);
441
474
  }
442
475
  return {
443
- context: this.getContext(account, target.project),
476
+ context: this.transport.context(account, target.project),
444
477
  sourceProject: source.project,
445
478
  defaultBranch: target.raw.default_branch || void 0,
446
479
  capability
447
480
  };
448
481
  }
449
- async readMergeRequest(options) {
482
+ async readMergeRequest(options, checkedTargetSha) {
450
483
  const { project, iid, expectedMergeRequestId } = options;
451
484
  if (!isGitLabProjectRef(project) || !isGitLabDecimalId(iid) || expectedMergeRequestId !== void 0 && !isGitLabDecimalId(expectedMergeRequestId)) throw new GitLabWriteError("invalid-input");
452
485
  const account = await this.transport.readAccount();
453
486
  const target = await this.transport.readProject(project);
454
- const context = this.getContext(account, target.project);
487
+ const context = this.transport.context(account, target.project);
455
488
  const raw = await this.transport.read(this.mergeRequestPath(target.project, { iid }));
456
- let mergeRequest = mergeRequestReceipt(raw, target.project, iid, expectedMergeRequestId);
489
+ let mergeRequest = mergeRequestReceipt(raw, context, iid, expectedMergeRequestId);
457
490
  if (mergeRequest.state === "opened") {
458
- const targetSha = await this.readBranchTip(target.project, mergeRequest.targetBranch);
491
+ const targetSha = checkedTargetSha ?? await this.transport.readBranchTip(target.project, mergeRequest.targetBranch);
459
492
  if (targetSha != null) mergeRequest = {
460
493
  ...mergeRequest,
461
494
  targetSha
@@ -465,15 +498,10 @@ var GitLabMergeRequestWriter = class {
465
498
  return {
466
499
  context,
467
500
  mergeRequest,
468
- capabilities: await this.getCapabilities(account, target, raw, mergeRequest),
501
+ capabilities: await this.getCapabilities(account, target, raw, mergeRequest, checkedTargetSha != null),
469
502
  lockVersion: typeof lockVersion === "number" && Number.isSafeInteger(lockVersion) && lockVersion >= 0 ? lockVersion : void 0
470
503
  };
471
504
  }
472
- getContext(account, project) {
473
- const context = this.transport.context(account, project);
474
- if (context.providerId !== "gitlab") throw new GitLabWriteError("unsupported");
475
- return context;
476
- }
477
505
  getWriteRefusal(account, project) {
478
506
  if (!account.canWrite) return {
479
507
  allowed: false,
@@ -484,7 +512,7 @@ var GitLabMergeRequestWriter = class {
484
512
  reason: "provider-policy"
485
513
  };
486
514
  }
487
- async getCapabilities(account, target, raw, mergeRequest) {
515
+ async getCapabilities(account, target, raw, mergeRequest, checkRefs) {
488
516
  const mergeStatus = mergeStatusOf(raw);
489
517
  const unavailable = this.getWriteRefusal(account, target.raw);
490
518
  if (unavailable != null) return {
@@ -494,29 +522,31 @@ var GitLabMergeRequestWriter = class {
494
522
  delete: unavailable,
495
523
  merge: unavailableMerge(unavailable.reason, mergeStatus)
496
524
  };
497
- const { project } = await this.transport.readGraphQL(mergeRequestPermissionsQuery, {
498
- path: target.project.fullPath,
499
- iid: mergeRequest.iid
500
- }, { project: target.project });
525
+ const { project } = await readMergeRequestPermissions(this.transport, target.project, mergeRequest.iid, checkRefs && mergeRequest.state === "opened");
501
526
  const permissions = project?.mergeRequest;
502
527
  if (permissions?.id !== `${mergeRequestGidPrefix}${mergeRequest.id}`) throw new GitLabWriteError("conflict");
503
528
  const { updateMergeRequest, canMerge } = permissions.userPermissions ?? {};
504
529
  const { autoMergeEnabled, availableAutoMergeStrategies: strategies, squashOnMerge, squashReadOnly } = permissions;
505
530
  const accessLevel = project?.maxAccessLevel?.integerValue;
506
- if (typeof updateMergeRequest !== "boolean" || typeof canMerge !== "boolean" || typeof autoMergeEnabled !== "boolean" || typeof squashOnMerge !== "boolean" || typeof squashReadOnly !== "boolean" || typeof accessLevel !== "number" || strategies != null && !Array.isArray(strategies)) throw new GitLabWriteError("unknown");
531
+ if (typeof updateMergeRequest !== "boolean" || typeof canMerge !== "boolean" || typeof autoMergeEnabled !== "boolean" || typeof squashOnMerge !== "boolean" || typeof squashReadOnly !== "boolean" && !(this.transport.selfManaged && squashReadOnly === void 0) || typeof accessLevel !== "number" || strategies != null && !Array.isArray(strategies)) throw new GitLabWriteError("unknown");
507
532
  const { state } = mergeRequest;
508
533
  const update = allowed(updateMergeRequest);
534
+ const changed = checkRefs && state === "opened" ? changedMergeRequestRef(permissions, mergeRequest) : void 0;
535
+ const squash = squashReadOnly === void 0 ? selfManagedSquash(target.raw, squashOnMerge) : squashOnMerge ? "required" : squashReadOnly ? "unavailable" : "optional";
509
536
  return {
510
537
  edit: transition(update, state === "opened" || state === "closed"),
511
538
  close: transition(update, state === "opened"),
512
539
  reopen: transition(update, state === "closed"),
513
- delete: allowed(accessLevel >= ownerAccessLevel),
514
- merge: this.getMergeCapability(target.raw, raw, mergeRequest, {
540
+ delete: this.transport.selfManaged && accessLevel >= ownerAccessLevel && deleteFence(mergeRequest.updatedAt) == null ? {
541
+ allowed: false,
542
+ reason: "unsupported"
543
+ } : allowed(accessLevel >= ownerAccessLevel),
544
+ merge: changed == null ? this.getMergeCapability(target.raw, raw, mergeRequest, {
515
545
  canMerge,
516
546
  autoMergeEnabled,
517
547
  strategies: strategies ?? [],
518
- squash: squashOnMerge ? "required" : squashReadOnly ? "unavailable" : "optional"
519
- })
548
+ squash
549
+ }) : unavailableMerge(changed, mergeStatus)
520
550
  };
521
551
  }
522
552
  getMergeCapability(project, raw, mergeRequest, policy) {
@@ -528,10 +558,12 @@ var GitLabMergeRequestWriter = class {
528
558
  if (policy.autoMergeEnabled) return unavailableMerge("policy-blocked", mergeStatus, "auto-merge");
529
559
  if (mergeRequest.sourceSha == null) return unavailableMerge("policy-blocked", mergeStatus, "checking");
530
560
  if (mergeRequest.targetSha == null) return blockedMerge(mergeStatus, "other");
531
- const hasTrain = project.merge_trains_enabled === true || policy.strategies.includes("merge_train") || policy.strategies.includes("add_to_merge_train_when_checks_pass");
561
+ const hasTrain = project.merge_trains_enabled === true || policy.strategies.includes("merge_train") || policy.strategies.includes("add_to_merge_train_when_checks_pass") || this.transport.selfManaged && policy.strategies.includes("add_to_merge_train_when_pipeline_succeeds");
532
562
  if (hasTrain) {
563
+ if (this.transport.selfManaged) return unavailableMerge("unsupported", mergeStatus, "merge-train");
533
564
  if (!policy.strategies.includes("merge_train")) return blockedMerge(mergeStatus, "merge-train");
534
565
  } else if (mergeStatus !== "mergeable") return blockedMerge(mergeStatus, "other");
566
+ if (policy.squash == null) return unavailableMerge("unsupported", mergeStatus);
535
567
  return {
536
568
  kind: "available",
537
569
  execution: hasTrain ? "train" : "merge",
@@ -540,29 +572,17 @@ var GitLabMergeRequestWriter = class {
540
572
  removesSourceBranch: raw.should_remove_source_branch === true || raw.force_remove_source_branch === true
541
573
  };
542
574
  }
543
- async findOpenMergeRequest(project, sourceProject, sourceBranch, targetBranch) {
575
+ async findOpenMergeRequest(context, sourceProject, sourceBranch, targetBranch) {
544
576
  const query = new URLSearchParams({
545
577
  state: "opened",
546
578
  source_branch: sourceBranch,
547
579
  target_branch: targetBranch,
548
580
  per_page: "100"
549
581
  });
550
- const found = await this.transport.read(`/api/v4/projects/${project.id}/merge_requests?${query.toString()}`);
582
+ const found = await this.transport.read(`/api/v4/projects/${context.project.id}/merge_requests?${query.toString()}`);
551
583
  if (!Array.isArray(found)) throw new GitLabWriteError("unknown");
552
584
  const existing = found.find((raw) => restId(raw?.source_project_id) === sourceProject.id && raw.source_branch === sourceBranch && raw.target_branch === targetBranch && raw.state === "opened");
553
- return existing && mergeRequestReceipt(existing, project);
554
- }
555
- async readBranchTip(project, branch) {
556
- let raw;
557
- try {
558
- raw = await this.transport.read(`/api/v4/projects/${project.id}/repository/branches/${encodeURIComponent(branch)}`);
559
- } catch (ex) {
560
- if (ex instanceof GitLabWriteError && ex.reason === "not-found") return void 0;
561
- throw ex;
562
- }
563
- const sha = raw?.commit?.id;
564
- if (raw?.name !== branch || !isSha(sha)) throw new GitLabWriteError("unknown");
565
- return sha;
585
+ return existing && mergeRequestReceipt(existing, context);
566
586
  }
567
587
  mergeRequestPath(project, mergeRequest) {
568
588
  return `/api/v4/projects/${project.id}/merge_requests/${mergeRequest.iid}`;