@gitkraken/core-gitlens 0.55.0 → 0.56.1
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/CHANGELOG.md +20 -1
- package/dist/git/cache.js +3 -0
- package/dist/git/cache.js.map +1 -1
- package/dist/git-cli/providers/pausedOperations.d.ts +2 -0
- package/dist/git-cli/providers/pausedOperations.js +104 -40
- package/dist/git-cli/providers/pausedOperations.js.map +1 -1
- package/dist/plus/integrations/index.d.ts +2 -1
- package/dist/plus/integrations/index.js.map +1 -1
- package/dist/plus/integrations/integrationService.d.ts +9 -0
- package/dist/plus/integrations/integrationService.js +75 -3
- package/dist/plus/integrations/integrationService.js.map +1 -1
- package/dist/plus/integrations/manager.d.ts +10 -0
- package/dist/plus/integrations/models/bitbucketPullRequestWrites.d.ts +217 -0
- package/dist/plus/integrations/models/bitbucketPullRequestWrites.js +0 -0
- package/dist/plus/integrations/models/integration.d.ts +2 -1
- package/dist/plus/integrations/models/integration.js +6 -0
- package/dist/plus/integrations/models/integration.js.map +1 -1
- package/dist/plus/integrations/models/publicIntegration.d.ts +0 -1
- package/dist/plus/integrations/providers/azure/writeIdentity.d.ts +0 -3
- package/dist/plus/integrations/providers/azure/writeIdentity.js +4 -12
- package/dist/plus/integrations/providers/azure/writeIdentity.js.map +1 -1
- package/dist/plus/integrations/providers/azureDevOps.d.ts +0 -2
- package/dist/plus/integrations/providers/azureDevOps.js +1 -7
- package/dist/plus/integrations/providers/azureDevOps.js.map +1 -1
- package/dist/plus/integrations/providers/azurePullRequestWrites.js +24 -26
- package/dist/plus/integrations/providers/azurePullRequestWrites.js.map +1 -1
- package/dist/plus/integrations/providers/azureWorkItemWriteErrors.js +4 -3
- package/dist/plus/integrations/providers/azureWorkItemWriteErrors.js.map +1 -1
- package/dist/plus/integrations/providers/azureWorkItemWrites.js +22 -21
- package/dist/plus/integrations/providers/azureWorkItemWrites.js.map +1 -1
- package/dist/plus/integrations/providers/bitbucket.d.ts +4 -0
- package/dist/plus/integrations/providers/bitbucket.js +8 -0
- package/dist/plus/integrations/providers/bitbucket.js.map +1 -1
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteErrors.d.ts +13 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteErrors.js +88 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteErrors.js.map +1 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteLedger.d.ts +14 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteLedger.js +36 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWriteLedger.js.map +1 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWrites.d.ts +47 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWrites.js +796 -0
- package/dist/plus/integrations/providers/bitbucketPullRequestWrites.js.map +1 -0
- package/dist/plus/integrations/providers/issueSorts.d.ts +0 -1
- package/dist/plus/integrations/providers/providersApi.d.ts +4 -0
- package/dist/plus/integrations/providers/providersApi.js +5 -0
- package/dist/plus/integrations/providers/providersApi.js.map +1 -1
- package/dist/plus/integrations/providers/utils/writeInputs.d.ts +7 -0
- package/dist/plus/integrations/providers/utils/writeInputs.js +17 -0
- package/dist/plus/integrations/providers/utils/writeInputs.js.map +1 -0
- package/dist/plus/integrations/reads/drains.d.ts +0 -1
- package/docs/integrations.md +155 -0
- package/package.json +1 -1
|
@@ -0,0 +1,796 @@
|
|
|
1
|
+
import { uuid } from "../../../utils/crypto.js";
|
|
2
|
+
import { getResponseHeader } from "../errors.js";
|
|
3
|
+
import { isCommitSha, isPositiveInt32, isWriteIdentifier, sameIdentifier } from "./utils/writeInputs.js";
|
|
4
|
+
import { BitbucketPullRequestWriteError, getBitbucketMergeRefusal, toBitbucketPullRequestWriteFailure } from "./bitbucketPullRequestWriteErrors.js";
|
|
5
|
+
//#region ../plus/integrations/src/providers/bitbucketPullRequestWrites.ts
|
|
6
|
+
const apiUrl = "https://api.bitbucket.org/2.0";
|
|
7
|
+
const webUrl = "https://bitbucket.org";
|
|
8
|
+
const mergeStrategies = [
|
|
9
|
+
"merge_commit",
|
|
10
|
+
"squash",
|
|
11
|
+
"fast_forward",
|
|
12
|
+
"squash_fast_forward",
|
|
13
|
+
"rebase_fast_forward",
|
|
14
|
+
"rebase_merge"
|
|
15
|
+
];
|
|
16
|
+
const queueStrategies = [
|
|
17
|
+
"merge_commit",
|
|
18
|
+
"squash",
|
|
19
|
+
"rebase_merge"
|
|
20
|
+
];
|
|
21
|
+
const uuidPattern = /^\{[\da-f]{8}-(?:[\da-f]{4}-){3}[\da-f]{12}\}$/i;
|
|
22
|
+
const slugPattern = /^[\w.-]+$/;
|
|
23
|
+
const mergeTaskIdPattern = /^(?!\.{1,2}$)[\w.:-]{1,256}$/;
|
|
24
|
+
function isBitbucketUuid(value) {
|
|
25
|
+
return typeof value === "string" && uuidPattern.test(value);
|
|
26
|
+
}
|
|
27
|
+
function isSlugOrUuid(value) {
|
|
28
|
+
return isBitbucketUuid(value) || typeof value === "string" && slugPattern.test(value) && !/^\.\.?$/.test(value);
|
|
29
|
+
}
|
|
30
|
+
function isBranchName(name) {
|
|
31
|
+
return typeof name === "string" && name.length > 0 && !name.startsWith("refs/") && !name.startsWith("-") && ![...name].some((character) => character <= " " || "~^:?*[\\\"".includes(character)) && !name.includes("..") && !name.includes("@{") && name !== "@" && !name.endsWith(".") && name.split("/").every((part) => part.length > 0 && !part.startsWith(".") && !part.endsWith(".lock"));
|
|
32
|
+
}
|
|
33
|
+
function isTitle(title) {
|
|
34
|
+
return typeof title === "string" && title.trim().length > 0;
|
|
35
|
+
}
|
|
36
|
+
function seg(value) {
|
|
37
|
+
return encodeURIComponent(value);
|
|
38
|
+
}
|
|
39
|
+
function isAttemptPullRequest(pullRequest, sourceBranch, targetBranch) {
|
|
40
|
+
return !pullRequest.isCrossRepository && pullRequest.sourceBranch === sourceBranch && (targetBranch === void 0 || pullRequest.targetBranch === targetBranch);
|
|
41
|
+
}
|
|
42
|
+
function getCreateKey(repositoryId, sourceBranch) {
|
|
43
|
+
return `create\n${repositoryId.toLowerCase()}\n${sourceBranch}`;
|
|
44
|
+
}
|
|
45
|
+
function getMergeKey(repositoryId, number) {
|
|
46
|
+
return `merge\n${repositoryId.toLowerCase()}\n${number}`;
|
|
47
|
+
}
|
|
48
|
+
function isBitbucketMergeTaskId(value) {
|
|
49
|
+
return typeof value === "string" && mergeTaskIdPattern.test(value);
|
|
50
|
+
}
|
|
51
|
+
function isBitbucketPullRequestMergeStatusRead(options) {
|
|
52
|
+
return (options.providerId === void 0 || options.providerId === "bitbucket") && isWriteIdentifier(options.connectionId) && isBitbucketUuid(options.workspaceId) && isBitbucketUuid(options.repositoryId) && isBitbucketUuid(options.expectedAccountId) && isPositiveInt32(options.number) && isBitbucketMergeTaskId(options.mergeTaskId) && isBranchName(options.expectedSourceBranch) && isBranchName(options.expectedTargetBranch) && options.expectedSourceBranch !== options.expectedTargetBranch && isCommitSha(options.expectedSourceSha);
|
|
53
|
+
}
|
|
54
|
+
function isBitbucketPullRequestScope(options) {
|
|
55
|
+
return (options.providerId === void 0 || options.providerId === "bitbucket") && isWriteIdentifier(options.connectionId) && isSlugOrUuid(options.workspace) && isSlugOrUuid(options.repository) && (options.expectedAccountId === void 0 || isBitbucketUuid(options.expectedAccountId));
|
|
56
|
+
}
|
|
57
|
+
function isBitbucketPullRequestWrite(options) {
|
|
58
|
+
if (options.providerId !== void 0 && options.providerId !== "bitbucket" || !isWriteIdentifier(options.connectionId) || !isBitbucketUuid(options.workspaceId) || !isBitbucketUuid(options.repositoryId) || !isBitbucketUuid(options.expectedAccountId)) return false;
|
|
59
|
+
if (options.action === "create") return isBranchName(options.sourceBranch) && isBranchName(options.targetBranch) && options.sourceBranch !== options.targetBranch && isTitle(options.title) && (options.description === void 0 || typeof options.description === "string");
|
|
60
|
+
if (!isPositiveInt32(options.number)) return false;
|
|
61
|
+
switch (options.action) {
|
|
62
|
+
case "edit": return (options.title !== void 0 || options.description !== void 0) && (options.title === void 0 || isTitle(options.title)) && (options.description === void 0 || typeof options.description === "string");
|
|
63
|
+
case "decline": return true;
|
|
64
|
+
case "merge": return mergeStrategies.includes(options.strategy) && isBranchName(options.expectedSourceBranch) && isBranchName(options.expectedTargetBranch) && options.expectedSourceBranch !== options.expectedTargetBranch && isCommitSha(options.expectedSourceSha) && isCommitSha(options.expectedTargetSha);
|
|
65
|
+
default: return false;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function isBitbucketPullRequestWriteRelease(options) {
|
|
69
|
+
return (options.providerId === void 0 || options.providerId === "bitbucket") && isWriteIdentifier(options.connectionId) && isBitbucketUuid(options.workspaceId) && isBitbucketUuid(options.repositoryId) && isBitbucketUuid(options.expectedAccountId) && isWriteIdentifier(options.attemptId) && (options.action === "create" ? isBranchName(options.sourceBranch) : options.action === "merge" && isPositiveInt32(options.number) && (options.mergeTaskId === void 0 || isBitbucketMergeTaskId(options.mergeTaskId)));
|
|
70
|
+
}
|
|
71
|
+
function toStrategies(value) {
|
|
72
|
+
return Array.isArray(value) ? mergeStrategies.filter((strategy) => value.includes(strategy)) : [];
|
|
73
|
+
}
|
|
74
|
+
function getBlockingCheckFailure(checks) {
|
|
75
|
+
const blocking = new Set(checks.filter((check) => check.blocking === true).map((check) => check.type));
|
|
76
|
+
if (blocking.size === 0) return void 0;
|
|
77
|
+
if (blocking.has("pullrequest_state_check")) return "conflict";
|
|
78
|
+
if (blocking.has("current_user_permission_check")) return "forbidden";
|
|
79
|
+
if (blocking.has("git_mergeability_check")) return "conflict";
|
|
80
|
+
return "policy-blocked";
|
|
81
|
+
}
|
|
82
|
+
function getMergeStateFailure(pullRequest) {
|
|
83
|
+
if (pullRequest.state !== "open" || pullRequest.queued) return "conflict";
|
|
84
|
+
if (pullRequest.isCrossRepository) return "unsupported";
|
|
85
|
+
if (pullRequest.draft) return "policy-blocked";
|
|
86
|
+
}
|
|
87
|
+
function getMergeRefFailure(options, read) {
|
|
88
|
+
const { pullRequest, sourceHash } = read;
|
|
89
|
+
if (pullRequest.sourceBranch !== options.expectedSourceBranch) return "source-changed";
|
|
90
|
+
if (pullRequest.targetBranch !== options.expectedTargetBranch) return "target-changed";
|
|
91
|
+
if (sourceHash == null || !options.expectedSourceSha.toLowerCase().startsWith(sourceHash)) return "source-changed";
|
|
92
|
+
}
|
|
93
|
+
var BitbucketPullRequestWriter = class BitbucketPullRequestWriter {
|
|
94
|
+
request;
|
|
95
|
+
headers;
|
|
96
|
+
accountId;
|
|
97
|
+
repository;
|
|
98
|
+
defaultBranch;
|
|
99
|
+
isSessionCurrent;
|
|
100
|
+
ledger;
|
|
101
|
+
static async connect(request, accessToken, options, isSessionCurrent, ledger) {
|
|
102
|
+
const headers = { Authorization: `Bearer ${accessToken}` };
|
|
103
|
+
const { body: user } = await request({
|
|
104
|
+
url: `${apiUrl}/user`,
|
|
105
|
+
headers,
|
|
106
|
+
redirect: "error"
|
|
107
|
+
});
|
|
108
|
+
const accountId = user?.uuid;
|
|
109
|
+
if (!isBitbucketUuid(accountId) || options.expectedAccountId !== void 0 && !sameIdentifier(accountId, options.expectedAccountId)) throw new BitbucketPullRequestWriteError("no-connection");
|
|
110
|
+
const { body: repo } = await request({
|
|
111
|
+
url: `${apiUrl}/repositories/${seg(options.workspace)}/${seg(options.repository)}`,
|
|
112
|
+
headers,
|
|
113
|
+
redirect: "error"
|
|
114
|
+
});
|
|
115
|
+
const workspace = repo?.workspace;
|
|
116
|
+
if (!isBitbucketUuid(repo?.uuid) || !isBitbucketUuid(workspace?.uuid) || !isSlugOrUuid(repo.slug) || !isSlugOrUuid(workspace.slug) || typeof repo.full_name !== "string" || !sameIdentifier(isBitbucketUuid(options.workspace) ? workspace.uuid : workspace.slug, options.workspace) || !sameIdentifier(isBitbucketUuid(options.repository) ? repo.uuid : repo.slug, options.repository)) throw new BitbucketPullRequestWriteError("not-found");
|
|
117
|
+
const mainBranch = repo.mainbranch?.name;
|
|
118
|
+
return new BitbucketPullRequestWriter(request, headers, accountId, {
|
|
119
|
+
workspaceId: workspace.uuid,
|
|
120
|
+
workspaceSlug: workspace.slug,
|
|
121
|
+
repositoryId: repo.uuid,
|
|
122
|
+
repositorySlug: repo.slug,
|
|
123
|
+
fullName: repo.full_name
|
|
124
|
+
}, isBranchName(mainBranch) ? mainBranch : void 0, isSessionCurrent, ledger);
|
|
125
|
+
}
|
|
126
|
+
repoUrl;
|
|
127
|
+
constructor(request, headers, accountId, repository, defaultBranch, isSessionCurrent, ledger) {
|
|
128
|
+
this.request = request;
|
|
129
|
+
this.headers = headers;
|
|
130
|
+
this.accountId = accountId;
|
|
131
|
+
this.repository = repository;
|
|
132
|
+
this.defaultBranch = defaultBranch;
|
|
133
|
+
this.isSessionCurrent = isSessionCurrent;
|
|
134
|
+
this.ledger = ledger;
|
|
135
|
+
this.repoUrl = `${apiUrl}/repositories/${seg(repository.workspaceId)}/${seg(repository.repositoryId)}`;
|
|
136
|
+
}
|
|
137
|
+
getCreateCapability() {
|
|
138
|
+
return {
|
|
139
|
+
ok: true,
|
|
140
|
+
accountId: this.accountId,
|
|
141
|
+
repository: this.repository,
|
|
142
|
+
...this.defaultBranch !== void 0 && { defaultBranch: this.defaultBranch }
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
async resolveTarget(number) {
|
|
146
|
+
if (!isPositiveInt32(number)) return {
|
|
147
|
+
ok: false,
|
|
148
|
+
reason: "invalid-input"
|
|
149
|
+
};
|
|
150
|
+
const read = await this.readPullRequest(number);
|
|
151
|
+
const open = read.pullRequest.state === "open";
|
|
152
|
+
const key = getMergeKey(this.repository.repositoryId, number);
|
|
153
|
+
if (!open) this.ledger.settle(key);
|
|
154
|
+
const target = await this.getMergeTarget(read);
|
|
155
|
+
const { pullRequest } = target;
|
|
156
|
+
const unsettled = open ? this.ledger.get(key) : void 0;
|
|
157
|
+
let merge = target.merge;
|
|
158
|
+
if (unsettled != null) merge = merge.kind === "available" ? {
|
|
159
|
+
kind: "unavailable",
|
|
160
|
+
reason: "outcome-unknown",
|
|
161
|
+
unsettled
|
|
162
|
+
} : {
|
|
163
|
+
...merge,
|
|
164
|
+
unsettled
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
ok: true,
|
|
168
|
+
accountId: this.accountId,
|
|
169
|
+
pullRequest,
|
|
170
|
+
capabilities: {
|
|
171
|
+
edit: open,
|
|
172
|
+
decline: open,
|
|
173
|
+
reopen: false,
|
|
174
|
+
merge
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
async write(options) {
|
|
179
|
+
if (options.action === "create") {
|
|
180
|
+
const key = getCreateKey(this.repository.repositoryId, options.sourceBranch);
|
|
181
|
+
return this.ledger.run(key, () => this.create(options, key));
|
|
182
|
+
}
|
|
183
|
+
if (options.action === "merge") {
|
|
184
|
+
const key = getMergeKey(this.repository.repositoryId, options.number);
|
|
185
|
+
return this.ledger.run(key, () => this.merge(options, key));
|
|
186
|
+
}
|
|
187
|
+
const current = await this.readPullRequest(options.number);
|
|
188
|
+
switch (options.action) {
|
|
189
|
+
case "edit":
|
|
190
|
+
if (current.pullRequest.state !== "open") return {
|
|
191
|
+
ok: false,
|
|
192
|
+
reason: "conflict"
|
|
193
|
+
};
|
|
194
|
+
return this.send(options, "PUT", this.pullRequestUrl(options.number), {
|
|
195
|
+
title: options.title,
|
|
196
|
+
description: options.description
|
|
197
|
+
});
|
|
198
|
+
case "decline":
|
|
199
|
+
if (current.pullRequest.state !== "open") return {
|
|
200
|
+
ok: false,
|
|
201
|
+
reason: "conflict"
|
|
202
|
+
};
|
|
203
|
+
return this.send(options, "POST", `${this.pullRequestUrl(options.number)}/decline`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
async create(options, key) {
|
|
207
|
+
const [source, target] = await Promise.all([this.readBranch(options.sourceBranch), this.readBranch(options.targetBranch)]);
|
|
208
|
+
if (source == null || target == null) return {
|
|
209
|
+
ok: false,
|
|
210
|
+
reason: "not-found"
|
|
211
|
+
};
|
|
212
|
+
const preflight = await this.readCreateEvidence(key, options.sourceBranch);
|
|
213
|
+
if (preflight.open.length) return {
|
|
214
|
+
ok: false,
|
|
215
|
+
reason: "conflict"
|
|
216
|
+
};
|
|
217
|
+
if (preflight.unsettled != null) return {
|
|
218
|
+
ok: false,
|
|
219
|
+
reason: "outcome-unknown",
|
|
220
|
+
unsettled: preflight.unsettled
|
|
221
|
+
};
|
|
222
|
+
const { serverTime } = preflight;
|
|
223
|
+
const dispatchedAt = Date.now();
|
|
224
|
+
let pullRequestNumber;
|
|
225
|
+
const result = await this.send(options, "POST", `${this.repoUrl}/pullrequests`, {
|
|
226
|
+
title: options.title,
|
|
227
|
+
description: options.description,
|
|
228
|
+
source: { branch: { name: options.sourceBranch } },
|
|
229
|
+
destination: { branch: { name: options.targetBranch } }
|
|
230
|
+
}, {
|
|
231
|
+
createdNotBefore: serverTime,
|
|
232
|
+
onReceipt: (pullRequest) => {
|
|
233
|
+
if (!pullRequest.isCrossRepository && pullRequest.sourceBranch === options.sourceBranch) pullRequestNumber = pullRequest.number;
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
return this.recordUnsettled(key, result, {
|
|
237
|
+
attemptId: uuid(),
|
|
238
|
+
accountId: this.accountId,
|
|
239
|
+
dispatchedAt,
|
|
240
|
+
action: "create",
|
|
241
|
+
sourceBranch: options.sourceBranch,
|
|
242
|
+
targetBranch: options.targetBranch,
|
|
243
|
+
...pullRequestNumber !== void 0 && { pullRequestNumber }
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
async readCreateEvidence(key, branch) {
|
|
247
|
+
const entry = this.ledger.get(key);
|
|
248
|
+
const receipt = entry?.action === "create" ? entry.pullRequestNumber : void 0;
|
|
249
|
+
const open = [];
|
|
250
|
+
const serverTime = await this.scanPullRequests(this.getOpenPullRequestsFromQuery(branch), (pr) => {
|
|
251
|
+
if (!sameIdentifier(pr?.source?.repository?.uuid, this.repository.repositoryId)) return false;
|
|
252
|
+
if (pr.state === "MERGED" || pr.state === "DECLINED" || pr.state === "SUPERSEDED") return false;
|
|
253
|
+
open.push(isPositiveInt32(pr.id) ? pr.id : NaN);
|
|
254
|
+
return receipt !== void 0 ? pr.id === receipt : open.length > 1;
|
|
255
|
+
});
|
|
256
|
+
const landed = receipt !== void 0 && open.includes(receipt) ? receipt : void 0;
|
|
257
|
+
const closed = !open.length && receipt !== void 0 ? await this.readClosed(receipt) : void 0;
|
|
258
|
+
if (entry == null || landed !== void 0 || closed != null) {
|
|
259
|
+
this.ledger.settle(key);
|
|
260
|
+
return {
|
|
261
|
+
open,
|
|
262
|
+
serverTime,
|
|
263
|
+
landed,
|
|
264
|
+
closed
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
return {
|
|
268
|
+
open,
|
|
269
|
+
serverTime,
|
|
270
|
+
unsettled: entry
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
refuseRelease(reason, held) {
|
|
274
|
+
return sameIdentifier(held.accountId, this.accountId) ? {
|
|
275
|
+
ok: false,
|
|
276
|
+
reason,
|
|
277
|
+
unsettled: held
|
|
278
|
+
} : {
|
|
279
|
+
ok: false,
|
|
280
|
+
reason
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
async readShownPullRequest(number, sourceBranch, targetBranch) {
|
|
284
|
+
if (number === void 0) return void 0;
|
|
285
|
+
const pullRequest = await this.readPullRequest(number).then((read) => read.pullRequest, () => void 0);
|
|
286
|
+
return pullRequest?.state === "open" && isAttemptPullRequest(pullRequest, sourceBranch, targetBranch) ? pullRequest : void 0;
|
|
287
|
+
}
|
|
288
|
+
async readClosed(number) {
|
|
289
|
+
const pullRequest = await this.readPullRequest(number).then((read) => read.pullRequest, (ex) => {
|
|
290
|
+
if (toBitbucketPullRequestWriteFailure(ex).reason === "not-found") return void 0;
|
|
291
|
+
throw ex;
|
|
292
|
+
});
|
|
293
|
+
return pullRequest != null && pullRequest.state !== "open" ? pullRequest : void 0;
|
|
294
|
+
}
|
|
295
|
+
recordUnsettled(key, result, write) {
|
|
296
|
+
if (result.ok ? result.status !== "pending" : result.reason !== "outcome-unknown") return result;
|
|
297
|
+
this.ledger.record(key, write);
|
|
298
|
+
const unsettled = this.ledger.get(key);
|
|
299
|
+
return result.ok ? {
|
|
300
|
+
...result,
|
|
301
|
+
status: "pending",
|
|
302
|
+
unsettled
|
|
303
|
+
} : {
|
|
304
|
+
...result,
|
|
305
|
+
unsettled
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
async merge(options, key) {
|
|
309
|
+
const current = await this.readPullRequest(options.number);
|
|
310
|
+
const held = this.ledger.get(key);
|
|
311
|
+
if (held != null) {
|
|
312
|
+
if (current.pullRequest.state === "open") return {
|
|
313
|
+
ok: false,
|
|
314
|
+
reason: "outcome-unknown",
|
|
315
|
+
unsettled: held
|
|
316
|
+
};
|
|
317
|
+
this.ledger.settle(key);
|
|
318
|
+
}
|
|
319
|
+
const failure = getMergeStateFailure(current.pullRequest) ?? getMergeRefFailure(options, current);
|
|
320
|
+
if (failure != null) return {
|
|
321
|
+
ok: false,
|
|
322
|
+
reason: failure
|
|
323
|
+
};
|
|
324
|
+
const checks = await this.readMergeabilityChecks(options.number);
|
|
325
|
+
const [latest, source, target] = await Promise.all([
|
|
326
|
+
this.readPullRequest(options.number),
|
|
327
|
+
this.readBranch(options.expectedSourceBranch),
|
|
328
|
+
this.readBranch(options.expectedTargetBranch)
|
|
329
|
+
]);
|
|
330
|
+
const latestFailure = getMergeStateFailure(latest.pullRequest) ?? getMergeRefFailure(options, latest);
|
|
331
|
+
if (latestFailure != null) return {
|
|
332
|
+
ok: false,
|
|
333
|
+
reason: latestFailure
|
|
334
|
+
};
|
|
335
|
+
if (!sameIdentifier(source?.sha, options.expectedSourceSha)) return {
|
|
336
|
+
ok: false,
|
|
337
|
+
reason: "source-changed"
|
|
338
|
+
};
|
|
339
|
+
if (target == null || !sameIdentifier(target.sha, options.expectedTargetSha)) return {
|
|
340
|
+
ok: false,
|
|
341
|
+
reason: "target-changed"
|
|
342
|
+
};
|
|
343
|
+
if (!getAllowedStrategies(target, checks).includes(options.strategy)) return {
|
|
344
|
+
ok: false,
|
|
345
|
+
reason: "policy-blocked"
|
|
346
|
+
};
|
|
347
|
+
const blocked = getBlockingCheckFailure(checks);
|
|
348
|
+
if (blocked != null) return {
|
|
349
|
+
ok: false,
|
|
350
|
+
reason: blocked
|
|
351
|
+
};
|
|
352
|
+
const dispatchedAt = Date.now();
|
|
353
|
+
const result = await this.send(options, "POST", `${this.pullRequestUrl(options.number)}/merge?async=false`, {
|
|
354
|
+
type: "pullrequest",
|
|
355
|
+
merge_strategy: options.strategy
|
|
356
|
+
}, { pending: latest.pullRequest });
|
|
357
|
+
const mergeTaskId = result.ok && result.status === "pending" ? result.mergeTaskId : void 0;
|
|
358
|
+
return this.recordUnsettled(key, result, {
|
|
359
|
+
attemptId: uuid(),
|
|
360
|
+
accountId: this.accountId,
|
|
361
|
+
dispatchedAt,
|
|
362
|
+
action: "merge",
|
|
363
|
+
number: options.number,
|
|
364
|
+
...mergeTaskId !== void 0 && { mergeTaskId }
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
release(options) {
|
|
368
|
+
if (options.action === "create") {
|
|
369
|
+
const key = getCreateKey(this.repository.repositoryId, options.sourceBranch);
|
|
370
|
+
return this.ledger.run(key, async () => {
|
|
371
|
+
const held = this.ledger.get(key);
|
|
372
|
+
if (held != null && held.attemptId !== options.attemptId) return this.refuseRelease("conflict", held);
|
|
373
|
+
const matches = held != null;
|
|
374
|
+
const target = held?.action === "create" ? held.targetBranch : void 0;
|
|
375
|
+
const evidence = await this.readCreateEvidence(key, options.sourceBranch);
|
|
376
|
+
const only = evidence.open.length === 1 && isPositiveInt32(evidence.open[0]) ? evidence.open[0] : void 0;
|
|
377
|
+
const shown = evidence.landed ?? only;
|
|
378
|
+
if (evidence.open.length && evidence.landed === void 0) return {
|
|
379
|
+
ok: false,
|
|
380
|
+
reason: "conflict",
|
|
381
|
+
pullRequest: await this.readShownPullRequest(shown, options.sourceBranch, target)
|
|
382
|
+
};
|
|
383
|
+
const released = matches && evidence.unsettled == null || this.ledger.release(key, options.attemptId);
|
|
384
|
+
if (evidence.landed !== void 0) return {
|
|
385
|
+
ok: true,
|
|
386
|
+
released,
|
|
387
|
+
observed: "pull-request-open",
|
|
388
|
+
pullRequest: await this.readShownPullRequest(shown, options.sourceBranch, target)
|
|
389
|
+
};
|
|
390
|
+
if (evidence.closed != null && matches) return {
|
|
391
|
+
ok: true,
|
|
392
|
+
released,
|
|
393
|
+
observed: "pull-request-closed",
|
|
394
|
+
pullRequest: isAttemptPullRequest(evidence.closed, options.sourceBranch, target) ? evidence.closed : void 0
|
|
395
|
+
};
|
|
396
|
+
return {
|
|
397
|
+
ok: true,
|
|
398
|
+
released,
|
|
399
|
+
observed: "no-open-pull-request"
|
|
400
|
+
};
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
const key = getMergeKey(this.repository.repositoryId, options.number);
|
|
404
|
+
return this.ledger.run(key, async () => {
|
|
405
|
+
const held = this.ledger.get(key);
|
|
406
|
+
const heldTaskId = held?.action === "merge" ? held.mergeTaskId : void 0;
|
|
407
|
+
if (held != null && heldTaskId != null && options.mergeTaskId != null && options.mergeTaskId !== heldTaskId) return this.refuseRelease("invalid-input", held);
|
|
408
|
+
if (held != null && held.attemptId !== options.attemptId) return this.refuseRelease("conflict", held);
|
|
409
|
+
const matches = held != null;
|
|
410
|
+
const { pullRequest } = await this.readPullRequest(options.number);
|
|
411
|
+
if (pullRequest.state !== "open") {
|
|
412
|
+
this.ledger.settle(key);
|
|
413
|
+
return {
|
|
414
|
+
ok: true,
|
|
415
|
+
released: matches,
|
|
416
|
+
observed: "pull-request-closed",
|
|
417
|
+
pullRequest
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
if (pullRequest.queued) return {
|
|
421
|
+
ok: false,
|
|
422
|
+
reason: "conflict"
|
|
423
|
+
};
|
|
424
|
+
const mergeTaskId = heldTaskId ?? options.mergeTaskId;
|
|
425
|
+
if (mergeTaskId != null) {
|
|
426
|
+
const task = await this.readMergeTaskForRelease(options.number, mergeTaskId);
|
|
427
|
+
if (task !== "expired") return {
|
|
428
|
+
ok: false,
|
|
429
|
+
reason: task
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
ok: true,
|
|
434
|
+
released: this.ledger.release(key, options.attemptId),
|
|
435
|
+
observed: "pull-request-open",
|
|
436
|
+
pullRequest
|
|
437
|
+
};
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
async readMergeTaskForRelease(number, mergeTaskId) {
|
|
441
|
+
const key = getMergeKey(this.repository.repositoryId, number);
|
|
442
|
+
let task;
|
|
443
|
+
try {
|
|
444
|
+
({body: task} = await this.request({
|
|
445
|
+
url: `${this.pullRequestUrl(number)}/merge/task-status/${seg(mergeTaskId)}`,
|
|
446
|
+
headers: this.headers,
|
|
447
|
+
redirect: "error"
|
|
448
|
+
}));
|
|
449
|
+
} catch (ex) {
|
|
450
|
+
const refusal = getBitbucketMergeRefusal(ex);
|
|
451
|
+
if (refusal == null) throw ex;
|
|
452
|
+
if (refusal === "invalid-input") throw new BitbucketPullRequestWriteError(refusal);
|
|
453
|
+
if (refusal !== "conflict" && refusal !== "policy-blocked") return "outcome-unknown";
|
|
454
|
+
this.ledger.settleMergeTask(key, mergeTaskId);
|
|
455
|
+
return "conflict";
|
|
456
|
+
}
|
|
457
|
+
if (task == null || typeof task !== "object") return "outcome-unknown";
|
|
458
|
+
if (task.task_status === "SUCCESS") {
|
|
459
|
+
this.ledger.settleMergeTask(key, mergeTaskId);
|
|
460
|
+
return "conflict";
|
|
461
|
+
}
|
|
462
|
+
if (task.task_status !== "PENDING") return "outcome-unknown";
|
|
463
|
+
if (task.queued_pullrequest == null) return "expired";
|
|
464
|
+
return this.isQueuedPullRequest(task.queued_pullrequest, number) ? "conflict" : "outcome-unknown";
|
|
465
|
+
}
|
|
466
|
+
async readMergeStatus(options) {
|
|
467
|
+
const key = getMergeKey(this.repository.repositoryId, options.number);
|
|
468
|
+
let task;
|
|
469
|
+
let refusal;
|
|
470
|
+
try {
|
|
471
|
+
({body: task} = await this.request({
|
|
472
|
+
url: `${this.pullRequestUrl(options.number)}/merge/task-status/${seg(options.mergeTaskId)}`,
|
|
473
|
+
headers: this.headers,
|
|
474
|
+
redirect: "error"
|
|
475
|
+
}));
|
|
476
|
+
} catch (ex) {
|
|
477
|
+
refusal = getBitbucketMergeRefusal(ex);
|
|
478
|
+
if (refusal == null) throw ex;
|
|
479
|
+
}
|
|
480
|
+
if (refusal === "invalid-input") return {
|
|
481
|
+
ok: false,
|
|
482
|
+
reason: refusal
|
|
483
|
+
};
|
|
484
|
+
if (refusal === "conflict" || refusal === "policy-blocked") {
|
|
485
|
+
this.ledger.settleMergeTask(key, options.mergeTaskId);
|
|
486
|
+
const snapshot = await this.readPullRequest(options.number).catch(() => void 0);
|
|
487
|
+
if (snapshot != null && snapshot.pullRequest.state !== "open") this.ledger.settle(key);
|
|
488
|
+
return {
|
|
489
|
+
ok: true,
|
|
490
|
+
status: "failed",
|
|
491
|
+
reason: refusal,
|
|
492
|
+
...snapshot != null && { pullRequest: snapshot.pullRequest }
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
if (refusal != null) return {
|
|
496
|
+
ok: false,
|
|
497
|
+
reason: refusal
|
|
498
|
+
};
|
|
499
|
+
if (task?.task_status === "SUCCESS") {
|
|
500
|
+
const merged = this.getApprovedMergeResult(task.merge_result, options);
|
|
501
|
+
if (merged != null) {
|
|
502
|
+
this.ledger.settleMergeTask(key, options.mergeTaskId);
|
|
503
|
+
return {
|
|
504
|
+
ok: true,
|
|
505
|
+
status: "merged",
|
|
506
|
+
pullRequest: merged
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
const { pullRequest } = await this.readPullRequest(options.number);
|
|
511
|
+
if (pullRequest.state !== "open") this.ledger.settle(key);
|
|
512
|
+
return task?.task_status === "PENDING" && this.isQueuedPullRequest(task.queued_pullrequest, options.number) ? {
|
|
513
|
+
ok: true,
|
|
514
|
+
status: "pending",
|
|
515
|
+
pullRequest
|
|
516
|
+
} : {
|
|
517
|
+
ok: false,
|
|
518
|
+
reason: "outcome-unknown"
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
getApprovedMergeResult(raw, options) {
|
|
522
|
+
if (raw == null) return void 0;
|
|
523
|
+
let read;
|
|
524
|
+
try {
|
|
525
|
+
read = this.toPullRequestRead(raw, options.number);
|
|
526
|
+
} catch {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
const { pullRequest } = read;
|
|
530
|
+
return pullRequest.state === "merged" && pullRequest.sourceBranch === options.expectedSourceBranch && pullRequest.targetBranch === options.expectedTargetBranch && isApprovedSource(options.expectedSourceSha, read) ? pullRequest : void 0;
|
|
531
|
+
}
|
|
532
|
+
isQueuedPullRequest(queued, number) {
|
|
533
|
+
const pullRequest = queued?.pullrequest;
|
|
534
|
+
const repositoryId = pullRequest?.destination?.repository?.uuid;
|
|
535
|
+
return queued?.type === "queued_pullrequest" && pullRequest?.id === number && (repositoryId == null || sameIdentifier(repositoryId, this.repository.repositoryId));
|
|
536
|
+
}
|
|
537
|
+
async getMergeTarget(read) {
|
|
538
|
+
const { pullRequest } = read;
|
|
539
|
+
const stateFailure = getMergeStateFailure(pullRequest);
|
|
540
|
+
if (stateFailure != null) return {
|
|
541
|
+
merge: {
|
|
542
|
+
kind: "unavailable",
|
|
543
|
+
reason: stateFailure
|
|
544
|
+
},
|
|
545
|
+
pullRequest
|
|
546
|
+
};
|
|
547
|
+
try {
|
|
548
|
+
const [source, target] = await Promise.all([this.readBranch(pullRequest.sourceBranch), this.readBranch(pullRequest.targetBranch)]);
|
|
549
|
+
const snapshot = {
|
|
550
|
+
...pullRequest,
|
|
551
|
+
...source != null && { sourceSha: source.sha },
|
|
552
|
+
...target != null && { targetSha: target.sha }
|
|
553
|
+
};
|
|
554
|
+
if (source == null || read.sourceHash == null || !source.sha.toLowerCase().startsWith(read.sourceHash)) return {
|
|
555
|
+
merge: {
|
|
556
|
+
kind: "unavailable",
|
|
557
|
+
reason: "source-changed"
|
|
558
|
+
},
|
|
559
|
+
pullRequest: snapshot
|
|
560
|
+
};
|
|
561
|
+
if (target == null) return {
|
|
562
|
+
merge: {
|
|
563
|
+
kind: "unavailable",
|
|
564
|
+
reason: "target-changed"
|
|
565
|
+
},
|
|
566
|
+
pullRequest: snapshot
|
|
567
|
+
};
|
|
568
|
+
const checks = await this.readMergeabilityChecks(pullRequest.number);
|
|
569
|
+
const strategies = getAllowedStrategies(target, checks);
|
|
570
|
+
const blocked = strategies.length ? getBlockingCheckFailure(checks) : "policy-blocked";
|
|
571
|
+
if (blocked != null) return {
|
|
572
|
+
merge: {
|
|
573
|
+
kind: "unavailable",
|
|
574
|
+
reason: blocked
|
|
575
|
+
},
|
|
576
|
+
pullRequest: snapshot
|
|
577
|
+
};
|
|
578
|
+
const defaultStrategy = strategies.find((strategy) => strategy === target.defaultStrategy);
|
|
579
|
+
return {
|
|
580
|
+
merge: {
|
|
581
|
+
kind: "available",
|
|
582
|
+
strategies,
|
|
583
|
+
...defaultStrategy !== void 0 && { defaultStrategy }
|
|
584
|
+
},
|
|
585
|
+
pullRequest: snapshot
|
|
586
|
+
};
|
|
587
|
+
} catch (ex) {
|
|
588
|
+
return {
|
|
589
|
+
merge: {
|
|
590
|
+
kind: "unavailable",
|
|
591
|
+
reason: toBitbucketPullRequestWriteFailure(ex).reason
|
|
592
|
+
},
|
|
593
|
+
pullRequest
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
pullRequestUrl(number) {
|
|
598
|
+
return `${this.repoUrl}/pullrequests/${number}`;
|
|
599
|
+
}
|
|
600
|
+
async readPullRequest(number) {
|
|
601
|
+
const { body: raw } = await this.request({
|
|
602
|
+
url: this.pullRequestUrl(number),
|
|
603
|
+
headers: this.headers,
|
|
604
|
+
redirect: "error"
|
|
605
|
+
});
|
|
606
|
+
return this.toPullRequestRead(raw, number);
|
|
607
|
+
}
|
|
608
|
+
toPullRequestRead(raw, number) {
|
|
609
|
+
if (!sameIdentifier(raw?.destination?.repository?.uuid, this.repository.repositoryId)) throw new BitbucketPullRequestWriteError("not-found");
|
|
610
|
+
const { id, title, description, source, destination } = raw;
|
|
611
|
+
const state = typeof raw.state === "string" ? raw.state.toLowerCase() : void 0;
|
|
612
|
+
const sourceBranch = source?.branch?.name;
|
|
613
|
+
const targetBranch = destination?.branch?.name;
|
|
614
|
+
const sourceHash = source?.commit?.hash;
|
|
615
|
+
if (!isPositiveInt32(id) || number !== void 0 && id !== number || typeof title !== "string" || description != null && typeof description !== "string" || state !== "open" && state !== "merged" && state !== "declined" && state !== "superseded" || typeof sourceBranch !== "string" || sourceBranch.length === 0 || typeof targetBranch !== "string" || targetBranch.length === 0) throw new BitbucketPullRequestWriteError("unknown");
|
|
616
|
+
const sourceRepository = source?.repository?.uuid;
|
|
617
|
+
return {
|
|
618
|
+
sourceHash: typeof sourceHash === "string" && /^[\da-f]{7,40}$/i.test(sourceHash) ? sourceHash.toLowerCase() : void 0,
|
|
619
|
+
pullRequest: {
|
|
620
|
+
repository: this.repository,
|
|
621
|
+
number: id,
|
|
622
|
+
url: `${webUrl}/${seg(this.repository.workspaceSlug)}/${seg(this.repository.repositorySlug)}/pull-requests/${id}`,
|
|
623
|
+
title,
|
|
624
|
+
description: description ?? "",
|
|
625
|
+
state,
|
|
626
|
+
draft: raw.draft === true,
|
|
627
|
+
queued: raw.queued === true,
|
|
628
|
+
isCrossRepository: !sameIdentifier(sourceRepository, this.repository.repositoryId),
|
|
629
|
+
sourceBranch,
|
|
630
|
+
targetBranch
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
}
|
|
634
|
+
async readBranch(name) {
|
|
635
|
+
let branch;
|
|
636
|
+
try {
|
|
637
|
+
({body: branch} = await this.request({
|
|
638
|
+
url: `${this.repoUrl}/refs/branches/${seg(name)}`,
|
|
639
|
+
headers: this.headers,
|
|
640
|
+
redirect: "error"
|
|
641
|
+
}));
|
|
642
|
+
} catch (ex) {
|
|
643
|
+
if (toBitbucketPullRequestWriteFailure(ex).reason === "not-found") return void 0;
|
|
644
|
+
throw ex;
|
|
645
|
+
}
|
|
646
|
+
const sha = branch?.target?.hash;
|
|
647
|
+
if (branch?.name !== name || !isCommitSha(sha)) throw new BitbucketPullRequestWriteError("unknown");
|
|
648
|
+
return {
|
|
649
|
+
sha,
|
|
650
|
+
strategies: toStrategies(branch.merge_strategies),
|
|
651
|
+
defaultStrategy: typeof branch.default_merge_strategy === "string" ? branch.default_merge_strategy : void 0
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
async readMergeabilityChecks(number) {
|
|
655
|
+
const { body } = await this.request({
|
|
656
|
+
url: `${this.pullRequestUrl(number)}/mergeability/checks`,
|
|
657
|
+
headers: this.headers,
|
|
658
|
+
redirect: "error"
|
|
659
|
+
});
|
|
660
|
+
const values = body?.values;
|
|
661
|
+
if (!Array.isArray(values) || values.some((check) => check == null || typeof check !== "object")) throw new BitbucketPullRequestWriteError("unknown");
|
|
662
|
+
return values;
|
|
663
|
+
}
|
|
664
|
+
getOpenPullRequestsFromQuery(branch) {
|
|
665
|
+
const escapedBranch = branch.replace(/\\/g, "\\\\").replace(/"/g, "\\\"");
|
|
666
|
+
return new URLSearchParams({
|
|
667
|
+
q: `source.branch.name="${escapedBranch}" AND state="OPEN"`,
|
|
668
|
+
fields: "next,values.id,values.state,values.source.repository.uuid",
|
|
669
|
+
pagelen: "50"
|
|
670
|
+
});
|
|
671
|
+
}
|
|
672
|
+
async scanPullRequests(query, visit) {
|
|
673
|
+
let url = `${this.repoUrl}/pullrequests?${query.toString()}`;
|
|
674
|
+
const seen = /* @__PURE__ */ new Set();
|
|
675
|
+
let serverTime;
|
|
676
|
+
while (true) {
|
|
677
|
+
seen.add(url);
|
|
678
|
+
const response = await this.request({
|
|
679
|
+
url,
|
|
680
|
+
headers: this.headers,
|
|
681
|
+
redirect: "error"
|
|
682
|
+
});
|
|
683
|
+
serverTime ??= getServerTime(response.headers);
|
|
684
|
+
const { values, next } = response.body ?? {};
|
|
685
|
+
if (!Array.isArray(values)) throw new BitbucketPullRequestWriteError("unknown");
|
|
686
|
+
if (values.some((pr) => visit(pr))) return serverTime;
|
|
687
|
+
if (next == null) return serverTime;
|
|
688
|
+
if (typeof next !== "string" || !next.startsWith(`${apiUrl}/`) || seen.has(next)) throw new BitbucketPullRequestWriteError("unknown");
|
|
689
|
+
url = next;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
getMergeTaskId(number, headers) {
|
|
693
|
+
const location = getResponseHeader(headers, "location");
|
|
694
|
+
if (typeof location !== "string" || !URL.canParse(location, apiUrl)) return void 0;
|
|
695
|
+
const url = new URL(location, apiUrl);
|
|
696
|
+
const match = /^\/2\.0\/repositories\/([^/]+)\/([^/]+)\/pullrequests\/(\d+)\/merge\/task-status\/([^/]+)$/.exec(url.pathname);
|
|
697
|
+
if (url.origin !== new URL(apiUrl).origin || url.search !== "" || match == null) return void 0;
|
|
698
|
+
const [, workspace, repository, id, taskId] = match.map((part) => safeDecode(part));
|
|
699
|
+
const { workspaceId, workspaceSlug, repositoryId, repositorySlug } = this.repository;
|
|
700
|
+
return Number(id) === number && (sameIdentifier(workspace, workspaceId) || sameIdentifier(workspace, workspaceSlug)) && (sameIdentifier(repository, repositoryId) || sameIdentifier(repository, repositorySlug)) && isBitbucketMergeTaskId(taskId) ? taskId : void 0;
|
|
701
|
+
}
|
|
702
|
+
async send(options, method, url, body, context) {
|
|
703
|
+
const pending = context?.pending;
|
|
704
|
+
if (!await this.isSessionCurrent()) return {
|
|
705
|
+
ok: false,
|
|
706
|
+
reason: "no-connection"
|
|
707
|
+
};
|
|
708
|
+
let response;
|
|
709
|
+
try {
|
|
710
|
+
response = await this.request({
|
|
711
|
+
url,
|
|
712
|
+
method,
|
|
713
|
+
headers: body == null ? this.headers : {
|
|
714
|
+
...this.headers,
|
|
715
|
+
"Content-Type": "application/json"
|
|
716
|
+
},
|
|
717
|
+
body: body == null ? void 0 : JSON.stringify(body),
|
|
718
|
+
redirect: "error"
|
|
719
|
+
});
|
|
720
|
+
} catch (ex) {
|
|
721
|
+
return toBitbucketPullRequestWriteFailure(ex, { action: options.action });
|
|
722
|
+
}
|
|
723
|
+
if (options.action === "merge" && response.status === 202 && pending != null) {
|
|
724
|
+
const mergeTaskId = this.getMergeTaskId(options.number, response.headers);
|
|
725
|
+
return {
|
|
726
|
+
ok: true,
|
|
727
|
+
status: "pending",
|
|
728
|
+
pullRequest: pending,
|
|
729
|
+
...mergeTaskId !== void 0 && { mergeTaskId }
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
let read;
|
|
733
|
+
try {
|
|
734
|
+
read = this.toPullRequestRead(response.body, options.action === "create" ? void 0 : options.number);
|
|
735
|
+
} catch {
|
|
736
|
+
return {
|
|
737
|
+
ok: false,
|
|
738
|
+
reason: "outcome-unknown"
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
context?.onReceipt?.(read.pullRequest);
|
|
742
|
+
const status = getReceiptStatus(options, response, read, this.accountId, context?.createdNotBefore);
|
|
743
|
+
return status === void 0 ? {
|
|
744
|
+
ok: false,
|
|
745
|
+
reason: "outcome-unknown"
|
|
746
|
+
} : {
|
|
747
|
+
ok: true,
|
|
748
|
+
status,
|
|
749
|
+
pullRequest: read.pullRequest
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
function getAllowedStrategies(target, checks) {
|
|
754
|
+
return checks.some((check) => check.type === "merge_queue_check" && check.required === true) ? target.strategies.filter((strategy) => queueStrategies.includes(strategy)) : target.strategies;
|
|
755
|
+
}
|
|
756
|
+
function safeDecode(value) {
|
|
757
|
+
try {
|
|
758
|
+
return decodeURIComponent(value);
|
|
759
|
+
} catch {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
function isApprovedSource(expectedSourceSha, read) {
|
|
764
|
+
return read.sourceHash != null && expectedSourceSha.toLowerCase().startsWith(read.sourceHash);
|
|
765
|
+
}
|
|
766
|
+
function sameText(actual, expected) {
|
|
767
|
+
return actual.replaceAll("\r\n", "\n").trim() === expected.replaceAll("\r\n", "\n").trim();
|
|
768
|
+
}
|
|
769
|
+
function getServerTime(headers) {
|
|
770
|
+
const date = getResponseHeader(headers, "date");
|
|
771
|
+
if (typeof date !== "string") return void 0;
|
|
772
|
+
const time = Date.parse(date);
|
|
773
|
+
return Number.isNaN(time) ? void 0 : Math.floor(time / 1e3) * 1e3;
|
|
774
|
+
}
|
|
775
|
+
function isCreatedSince(raw, accountId, notBefore) {
|
|
776
|
+
if (notBefore == null || !sameIdentifier(raw.author?.uuid, accountId)) return false;
|
|
777
|
+
if (typeof raw.created_on !== "string") return false;
|
|
778
|
+
const createdOn = Date.parse(raw.created_on);
|
|
779
|
+
return !Number.isNaN(createdOn) && createdOn >= notBefore;
|
|
780
|
+
}
|
|
781
|
+
function getReceiptStatus(options, response, read, accountId, createdNotBefore) {
|
|
782
|
+
const { pullRequest } = read;
|
|
783
|
+
switch (options.action) {
|
|
784
|
+
case "create": return response.status === 201 && isCreatedSince(response.body, accountId, createdNotBefore) && pullRequest.state === "open" && !pullRequest.isCrossRepository && pullRequest.sourceBranch === options.sourceBranch && pullRequest.targetBranch === options.targetBranch && sameText(pullRequest.title, options.title) && (options.description === void 0 || sameText(pullRequest.description, options.description)) ? "created" : void 0;
|
|
785
|
+
case "edit": return pullRequest.state === "open" && (options.title === void 0 || sameText(pullRequest.title, options.title)) && (options.description === void 0 || sameText(pullRequest.description, options.description)) ? "updated" : void 0;
|
|
786
|
+
case "decline": return pullRequest.state === "declined" ? "declined" : void 0;
|
|
787
|
+
case "merge":
|
|
788
|
+
if (pullRequest.sourceBranch !== options.expectedSourceBranch || pullRequest.targetBranch !== options.expectedTargetBranch) return;
|
|
789
|
+
if (pullRequest.state === "merged") return isApprovedSource(options.expectedSourceSha, read) ? "merged" : void 0;
|
|
790
|
+
return pullRequest.state === "open" && pullRequest.queued ? "pending" : void 0;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
//#endregion
|
|
794
|
+
export { BitbucketPullRequestWriter, isBitbucketPullRequestMergeStatusRead, isBitbucketPullRequestScope, isBitbucketPullRequestWrite, isBitbucketPullRequestWriteRelease };
|
|
795
|
+
|
|
796
|
+
//# sourceMappingURL=bitbucketPullRequestWrites.js.map
|