@charles7c/git-forge 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/index.cjs +4873 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1265 -0
- package/dist/index.d.ts +1265 -0
- package/dist/index.js +4803 -0
- package/dist/index.js.map +1 -0
- package/package.json +48 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,4803 @@
|
|
|
1
|
+
import { Buffer } from 'buffer';
|
|
2
|
+
|
|
3
|
+
// src/adapters/issues.ts
|
|
4
|
+
function selectIssueRows(http, response, isPullRequest) {
|
|
5
|
+
const raw = response.json;
|
|
6
|
+
if (!Array.isArray(raw)) {
|
|
7
|
+
throw http.invalidResponse(response, "The provider Issue list response must be an array.");
|
|
8
|
+
}
|
|
9
|
+
const rows = raw;
|
|
10
|
+
const states = http.provider === "gitee" ? GITEE_ISSUE_STATES : DEFAULT_ISSUE_STATES;
|
|
11
|
+
for (const row of rows) {
|
|
12
|
+
if (isPullRequest?.(row)) continue;
|
|
13
|
+
const record = row;
|
|
14
|
+
const number = record?.number;
|
|
15
|
+
if (!(typeof number === "string" && number.trim().length > 0) && !(typeof number === "number" && Number.isSafeInteger(number) && number > 0)) {
|
|
16
|
+
throw http.invalidResponse(response, "A provider Issue list entry has no valid number.");
|
|
17
|
+
}
|
|
18
|
+
if (typeof record?.state !== "string" || !states.includes(record.state)) {
|
|
19
|
+
throw http.invalidResponse(response, "A provider Issue list entry has an unknown state.");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return rows;
|
|
23
|
+
}
|
|
24
|
+
var DEFAULT_ISSUE_STATES = ["open", "closed"];
|
|
25
|
+
var GITEE_ISSUE_STATES = [...DEFAULT_ISSUE_STATES, "progressing", "rejected"];
|
|
26
|
+
|
|
27
|
+
// src/core/redact.ts
|
|
28
|
+
var REDACTED = "***";
|
|
29
|
+
var SENSITIVE_QUERY_KEYS = ["access_token", "token"];
|
|
30
|
+
var SECRET_ASSIGNMENT = /\b(access_token|token|authorization)(\s*[:=]\s*)(bearer\s+)?([^\s&"',;]+)/gi;
|
|
31
|
+
function maskAssignment(_match, key, separator, scheme) {
|
|
32
|
+
return `${key}${separator}${scheme ?? ""}${REDACTED}`;
|
|
33
|
+
}
|
|
34
|
+
function redactUrl(url) {
|
|
35
|
+
if (typeof url !== "string" || url.length === 0) {
|
|
36
|
+
return url;
|
|
37
|
+
}
|
|
38
|
+
let redacted = url.replace(
|
|
39
|
+
/([?&](?:access_token|token)=)([^&#\s]*)/gi,
|
|
40
|
+
(_match, prefix) => `${prefix}${REDACTED}`
|
|
41
|
+
);
|
|
42
|
+
redacted = redacted.replace(SECRET_ASSIGNMENT, maskAssignment);
|
|
43
|
+
return redacted;
|
|
44
|
+
}
|
|
45
|
+
function scrubSecrets(text, secrets = []) {
|
|
46
|
+
if (typeof text !== "string" || text.length === 0) {
|
|
47
|
+
return text;
|
|
48
|
+
}
|
|
49
|
+
let scrubbed = text;
|
|
50
|
+
const known = secrets.filter((secret) => typeof secret === "string" && secret.length > 0).sort((a, b) => b.length - a.length);
|
|
51
|
+
for (const secret of known) {
|
|
52
|
+
scrubbed = scrubbed.split(secret).join(REDACTED);
|
|
53
|
+
}
|
|
54
|
+
scrubbed = scrubbed.replace(SECRET_ASSIGNMENT, maskAssignment);
|
|
55
|
+
return scrubbed;
|
|
56
|
+
}
|
|
57
|
+
function isSensitiveKey(key) {
|
|
58
|
+
return SENSITIVE_QUERY_KEYS.includes(key.toLowerCase());
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/core/errors.ts
|
|
62
|
+
var ERROR_CODES = [
|
|
63
|
+
"UNSUPPORTED_ON_PROVIDER",
|
|
64
|
+
"AUTH_MISSING",
|
|
65
|
+
"AUTH_INVALID",
|
|
66
|
+
"NOT_FOUND",
|
|
67
|
+
"RATE_LIMITED",
|
|
68
|
+
"PROVIDER_ERROR",
|
|
69
|
+
"INVALID_ARGUMENT",
|
|
70
|
+
"NETWORK_ERROR",
|
|
71
|
+
"READONLY_BLOCKED",
|
|
72
|
+
"PAGINATION_LIMIT"
|
|
73
|
+
];
|
|
74
|
+
var DEFAULT_RETRYABLE = {
|
|
75
|
+
UNSUPPORTED_ON_PROVIDER: false,
|
|
76
|
+
AUTH_MISSING: false,
|
|
77
|
+
AUTH_INVALID: false,
|
|
78
|
+
NOT_FOUND: false,
|
|
79
|
+
RATE_LIMITED: true,
|
|
80
|
+
PROVIDER_ERROR: false,
|
|
81
|
+
INVALID_ARGUMENT: false,
|
|
82
|
+
NETWORK_ERROR: true,
|
|
83
|
+
READONLY_BLOCKED: false,
|
|
84
|
+
PAGINATION_LIMIT: false
|
|
85
|
+
};
|
|
86
|
+
function defaultRetryable(code) {
|
|
87
|
+
return DEFAULT_RETRYABLE[code];
|
|
88
|
+
}
|
|
89
|
+
var GitForgeError = class _GitForgeError extends Error {
|
|
90
|
+
code;
|
|
91
|
+
provider;
|
|
92
|
+
status;
|
|
93
|
+
requestId;
|
|
94
|
+
capability;
|
|
95
|
+
retryable;
|
|
96
|
+
cause;
|
|
97
|
+
constructor(init) {
|
|
98
|
+
super(truncate(scrubSecrets(init.message, init.secrets), 1024));
|
|
99
|
+
this.name = "GitForgeError";
|
|
100
|
+
this.code = init.code;
|
|
101
|
+
this.provider = init.provider;
|
|
102
|
+
this.status = init.status;
|
|
103
|
+
this.requestId = init.requestId === void 0 ? void 0 : truncate(scrubSecrets(init.requestId, init.secrets), 128);
|
|
104
|
+
this.capability = init.capability;
|
|
105
|
+
this.retryable = init.retryable ?? DEFAULT_RETRYABLE[init.code];
|
|
106
|
+
this.cause = init.cause === void 0 ? void 0 : "Underlying failure (details omitted)";
|
|
107
|
+
Object.setPrototypeOf(this, _GitForgeError.prototype);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Serializes the error without headers, token values or the cause chain.
|
|
111
|
+
*
|
|
112
|
+
* Optional fields are omitted rather than set to `undefined`, so a JSON
|
|
113
|
+
* round-trip cannot reintroduce a key that was never known.
|
|
114
|
+
*/
|
|
115
|
+
toJSON() {
|
|
116
|
+
const json = {
|
|
117
|
+
code: this.code,
|
|
118
|
+
message: this.message,
|
|
119
|
+
retryable: this.retryable
|
|
120
|
+
};
|
|
121
|
+
if (this.provider !== void 0) {
|
|
122
|
+
json.provider = this.provider;
|
|
123
|
+
}
|
|
124
|
+
if (this.status !== void 0) {
|
|
125
|
+
json.status = this.status;
|
|
126
|
+
}
|
|
127
|
+
if (this.requestId !== void 0) {
|
|
128
|
+
json.requestId = this.requestId;
|
|
129
|
+
}
|
|
130
|
+
if (this.capability !== void 0) {
|
|
131
|
+
json.capability = this.capability;
|
|
132
|
+
}
|
|
133
|
+
return json;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
function isGitForgeError(value) {
|
|
137
|
+
return value instanceof GitForgeError;
|
|
138
|
+
}
|
|
139
|
+
var REQUEST_ID_HEADER_KEYS = ["x-github-request-id", "x-request-id"];
|
|
140
|
+
function extractRequestId(headers, body) {
|
|
141
|
+
if (headers !== void 0) {
|
|
142
|
+
for (const key of REQUEST_ID_HEADER_KEYS) {
|
|
143
|
+
const value = readHeader(headers, key);
|
|
144
|
+
if (value !== void 0 && value.length > 0) {
|
|
145
|
+
return value;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (body !== null && typeof body === "object" && "trace_id" in body) {
|
|
150
|
+
const traceId = body.trace_id;
|
|
151
|
+
if (typeof traceId === "string" && traceId.length > 0) {
|
|
152
|
+
return traceId;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return void 0;
|
|
156
|
+
}
|
|
157
|
+
function readHeader(headers, key) {
|
|
158
|
+
if (typeof headers.get === "function") {
|
|
159
|
+
const value = headers.get(key);
|
|
160
|
+
return value === null ? void 0 : value;
|
|
161
|
+
}
|
|
162
|
+
const record = headers;
|
|
163
|
+
return record[key] ?? record[key.toLowerCase()];
|
|
164
|
+
}
|
|
165
|
+
function extractUpstreamMessage(body, maxLength = 200, secrets) {
|
|
166
|
+
if (body === null || typeof body !== "object") {
|
|
167
|
+
return void 0;
|
|
168
|
+
}
|
|
169
|
+
const record = body;
|
|
170
|
+
for (const key of ["message", "error_message", "error", "errors"]) {
|
|
171
|
+
const value = record[key];
|
|
172
|
+
if (typeof value === "string" && value.length > 0) {
|
|
173
|
+
return truncate(scrubSecrets(value, secrets), maxLength);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return void 0;
|
|
177
|
+
}
|
|
178
|
+
function truncate(value, maxLength) {
|
|
179
|
+
return value.length <= maxLength ? value : `${value.slice(0, maxLength)}\u2026`;
|
|
180
|
+
}
|
|
181
|
+
function isRateLimitResponse(status, headers) {
|
|
182
|
+
if (status === 429) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
if (status !== 403 || headers === void 0) {
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
const remaining = readHeader(headers, "x-ratelimit-remaining");
|
|
189
|
+
return remaining !== void 0 && remaining.trim() === "0";
|
|
190
|
+
}
|
|
191
|
+
function describeProvider(provider) {
|
|
192
|
+
return provider === void 0 ? "the provider" : `provider=${provider}`;
|
|
193
|
+
}
|
|
194
|
+
function errorFromResponse(params) {
|
|
195
|
+
const { provider, status, headers, body, context, secrets } = params;
|
|
196
|
+
const code = mapStatusToCode(status, headers);
|
|
197
|
+
const requestId = extractRequestId(headers, body);
|
|
198
|
+
const upstream = extractUpstreamMessage(body, 200, secrets);
|
|
199
|
+
const detail = upstream === void 0 ? "" : `: ${upstream}`;
|
|
200
|
+
const message = `${context} failed with HTTP ${status} (${describeProvider(provider)})${detail}`;
|
|
201
|
+
return new GitForgeError({
|
|
202
|
+
code,
|
|
203
|
+
message,
|
|
204
|
+
provider,
|
|
205
|
+
status,
|
|
206
|
+
requestId,
|
|
207
|
+
retryable: retryableForStatus(code, status),
|
|
208
|
+
secrets
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
function mapStatusToCode(status, headers) {
|
|
212
|
+
if (status === 401) {
|
|
213
|
+
return "AUTH_INVALID";
|
|
214
|
+
}
|
|
215
|
+
if (isRateLimitResponse(status, headers)) {
|
|
216
|
+
return "RATE_LIMITED";
|
|
217
|
+
}
|
|
218
|
+
if (status === 403) {
|
|
219
|
+
return "AUTH_INVALID";
|
|
220
|
+
}
|
|
221
|
+
if (status === 404) {
|
|
222
|
+
return "NOT_FOUND";
|
|
223
|
+
}
|
|
224
|
+
if (status === 400 || status === 422) {
|
|
225
|
+
return "INVALID_ARGUMENT";
|
|
226
|
+
}
|
|
227
|
+
return "PROVIDER_ERROR";
|
|
228
|
+
}
|
|
229
|
+
function retryableForStatus(code, status) {
|
|
230
|
+
if (code === "RATE_LIMITED") {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
if (code === "PROVIDER_ERROR") {
|
|
234
|
+
return status >= 500;
|
|
235
|
+
}
|
|
236
|
+
if (code === "NETWORK_ERROR") {
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
return DEFAULT_RETRYABLE[code];
|
|
240
|
+
}
|
|
241
|
+
function errorFromNetwork(params) {
|
|
242
|
+
const { provider, context, cause, secrets, timeoutMs } = params;
|
|
243
|
+
const reason = describeNetworkCause(cause, timeoutMs);
|
|
244
|
+
return new GitForgeError({
|
|
245
|
+
code: "NETWORK_ERROR",
|
|
246
|
+
status: params.response?.status,
|
|
247
|
+
requestId: extractRequestId(params.response?.headers),
|
|
248
|
+
message: `${context} failed: ${reason} (${describeProvider(provider)})`,
|
|
249
|
+
provider,
|
|
250
|
+
retryable: params.retryable ?? true,
|
|
251
|
+
cause,
|
|
252
|
+
secrets
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
function describeNetworkCause(cause, timeoutMs) {
|
|
256
|
+
if (timeoutMs !== void 0) {
|
|
257
|
+
const name = cause instanceof Error ? cause.name : "";
|
|
258
|
+
if (name === "TimeoutError" || name === "AbortError") {
|
|
259
|
+
return `request timed out after ${timeoutMs}ms`;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (cause instanceof Error && cause.message.length > 0) {
|
|
263
|
+
return `network error (${cause.name})`;
|
|
264
|
+
}
|
|
265
|
+
return "network error";
|
|
266
|
+
}
|
|
267
|
+
function unsupportedCapabilityError(provider, capability) {
|
|
268
|
+
return new GitForgeError({
|
|
269
|
+
code: "UNSUPPORTED_ON_PROVIDER",
|
|
270
|
+
message: `${String(capability)} is not supported on provider=${provider}`,
|
|
271
|
+
provider,
|
|
272
|
+
capability
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
function authMissingError(provider, envVar) {
|
|
276
|
+
return new GitForgeError({
|
|
277
|
+
code: "AUTH_MISSING",
|
|
278
|
+
message: `No token configured for provider=${provider}. Set ${envVar} and retry.`,
|
|
279
|
+
provider
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
function readonlyBlockedError(provider, capability, method) {
|
|
283
|
+
return new GitForgeError({
|
|
284
|
+
code: "READONLY_BLOCKED",
|
|
285
|
+
message: `${method} is a write operation (${capability}) and was blocked because the client is read-only. Construct the client without \`readonly\` to allow writes.`,
|
|
286
|
+
provider,
|
|
287
|
+
capability
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
function invalidArgumentError(message, provider, capability) {
|
|
291
|
+
return new GitForgeError({ code: "INVALID_ARGUMENT", message, provider, capability });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// src/core/paginate.ts
|
|
295
|
+
var DEFAULT_PER_PAGE = 100;
|
|
296
|
+
var MAX_PER_PAGE = 100;
|
|
297
|
+
var DEFAULT_MAX_PAGES = 10;
|
|
298
|
+
var MAX_PAGES_LIMIT = 100;
|
|
299
|
+
var LINK_ENTRY = /<([^>]+)>\s*;\s*rel\s*=\s*['"]?([^'";,]+?)['"]?\s*(?:,|$)/g;
|
|
300
|
+
function parseLinkHeader(header) {
|
|
301
|
+
const result = {};
|
|
302
|
+
if (header === null || header === void 0) {
|
|
303
|
+
return result;
|
|
304
|
+
}
|
|
305
|
+
LINK_ENTRY.lastIndex = 0;
|
|
306
|
+
let match = LINK_ENTRY.exec(header);
|
|
307
|
+
while (match !== null) {
|
|
308
|
+
const url = match[1];
|
|
309
|
+
const rel = match[2];
|
|
310
|
+
if (url !== void 0 && rel !== void 0) {
|
|
311
|
+
for (const token of rel.toLowerCase().split(/\s+/)) {
|
|
312
|
+
if (token.length > 0 && result[token] === void 0) {
|
|
313
|
+
result[token] = url;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
match = LINK_ENTRY.exec(header);
|
|
318
|
+
}
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
function positivePageNumber(raw) {
|
|
322
|
+
if (raw === void 0 || !/^\d+$/.test(raw)) return null;
|
|
323
|
+
const value = Number(raw);
|
|
324
|
+
return Number.isSafeInteger(value) && value > 0 ? value : null;
|
|
325
|
+
}
|
|
326
|
+
function strictLinkHint(header, page) {
|
|
327
|
+
if (!header?.trim()) return void 0;
|
|
328
|
+
const link = /<([^<>\s]+)>[ \t]*/y;
|
|
329
|
+
const param = /;[ \t]*([\w!#$%&'*+.^`|~-]+)(?:[ \t]*=[ \t]*(?:"((?:[^"\\]|\\.)*)"|'((?:[^'\\]|\\.)*)'|([\w!#$%&*+.^`|~-][\w!#$%&'*+.^`|~-]*)))?[ \t]*/y;
|
|
330
|
+
const separator = /(?:,[ \t]*)+/y;
|
|
331
|
+
const field = header.trim();
|
|
332
|
+
let cursor = separator.test(field) ? separator.lastIndex : 0;
|
|
333
|
+
if (cursor === field.length) return void 0;
|
|
334
|
+
let terminal = true;
|
|
335
|
+
while (cursor < field.length) {
|
|
336
|
+
link.lastIndex = cursor;
|
|
337
|
+
const target = link.exec(field)?.[1];
|
|
338
|
+
if (target === void 0) return void 0;
|
|
339
|
+
cursor = link.lastIndex;
|
|
340
|
+
let relations = [];
|
|
341
|
+
let anchored = false;
|
|
342
|
+
while (field[cursor] === ";") {
|
|
343
|
+
param.lastIndex = cursor;
|
|
344
|
+
const attribute = param.exec(field);
|
|
345
|
+
const name = attribute?.[1]?.toLowerCase();
|
|
346
|
+
if (name === void 0) return void 0;
|
|
347
|
+
cursor = param.lastIndex;
|
|
348
|
+
if (name === "anchor") anchored = true;
|
|
349
|
+
if (name === "rel" && relations.length === 0) {
|
|
350
|
+
const value = attribute?.[2] ?? attribute?.[3] ?? attribute?.[4];
|
|
351
|
+
if (!value?.trim()) return void 0;
|
|
352
|
+
relations = value.trim().toLowerCase().split(/\s+/);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (cursor < field.length) {
|
|
356
|
+
separator.lastIndex = cursor;
|
|
357
|
+
if (!separator.test(field)) return void 0;
|
|
358
|
+
cursor = separator.lastIndex;
|
|
359
|
+
}
|
|
360
|
+
if (!anchored && relations.includes("next")) return true;
|
|
361
|
+
if (!anchored && relations.includes("last")) {
|
|
362
|
+
const query = /^[^?#]*\?([^#]*)/.exec(target)?.[1];
|
|
363
|
+
const pages = new URLSearchParams(query).getAll("page");
|
|
364
|
+
const last = pages.length === 1 ? positivePageNumber(pages[0]) : null;
|
|
365
|
+
if (last !== null && last > page) return true;
|
|
366
|
+
if (last !== page) terminal = false;
|
|
367
|
+
} else if (anchored || relations.length === 0 || !relations.every(isBackwardRelation)) {
|
|
368
|
+
terminal = false;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return terminal ? false : void 0;
|
|
372
|
+
}
|
|
373
|
+
function isBackwardRelation(relation) {
|
|
374
|
+
return relation === "prev" || relation === "first";
|
|
375
|
+
}
|
|
376
|
+
function resolveNextPage(signals, strict = false) {
|
|
377
|
+
const { link, totalPage, page, itemCount, perPage } = signals;
|
|
378
|
+
if (strict) {
|
|
379
|
+
const linkHint = strictLinkHint(link, page);
|
|
380
|
+
if (linkHint === true) return true;
|
|
381
|
+
if (totalPage !== null && Number.isSafeInteger(totalPage) && totalPage > 0) {
|
|
382
|
+
return page < totalPage;
|
|
383
|
+
}
|
|
384
|
+
if (linkHint === false) return false;
|
|
385
|
+
} else {
|
|
386
|
+
if (parseLinkHeader(link).next !== void 0) return true;
|
|
387
|
+
if (totalPage !== null && Number.isFinite(totalPage)) return page < totalPage;
|
|
388
|
+
}
|
|
389
|
+
return itemCount >= perPage && itemCount > 0;
|
|
390
|
+
}
|
|
391
|
+
function readTotalPage(headers) {
|
|
392
|
+
const raw = readHeader2(headers, "total_page") ?? readHeader2(headers, "x-total-pages");
|
|
393
|
+
if (raw === void 0) {
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
const value = Number(raw.trim());
|
|
397
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
398
|
+
return null;
|
|
399
|
+
}
|
|
400
|
+
return value;
|
|
401
|
+
}
|
|
402
|
+
function readStrictTotalPage(headers) {
|
|
403
|
+
const totalPage = positivePageNumber(readHeader2(headers, "total_page")?.trim());
|
|
404
|
+
const alias = positivePageNumber(readHeader2(headers, "x-total-pages")?.trim());
|
|
405
|
+
return totalPage === null || alias === null ? totalPage ?? alias : Math.max(totalPage, alias);
|
|
406
|
+
}
|
|
407
|
+
function readTotalCount(headers) {
|
|
408
|
+
const raw = readHeader2(headers, "total_count") ?? readHeader2(headers, "x-total-count");
|
|
409
|
+
if (raw === void 0) {
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
const value = Number(raw.trim());
|
|
413
|
+
return Number.isInteger(value) && value >= 0 ? value : null;
|
|
414
|
+
}
|
|
415
|
+
function readHeader2(headers, key) {
|
|
416
|
+
if (headers === void 0) {
|
|
417
|
+
return void 0;
|
|
418
|
+
}
|
|
419
|
+
if (typeof headers.get === "function") {
|
|
420
|
+
const value = headers.get(key);
|
|
421
|
+
return value === null ? void 0 : value;
|
|
422
|
+
}
|
|
423
|
+
const record = headers;
|
|
424
|
+
return record[key] ?? record[key.toLowerCase()];
|
|
425
|
+
}
|
|
426
|
+
function dedupeBy(items, keyOf) {
|
|
427
|
+
const seen = /* @__PURE__ */ new Set();
|
|
428
|
+
const out = [];
|
|
429
|
+
for (const item of items) {
|
|
430
|
+
const key = keyOf(item);
|
|
431
|
+
if (seen.has(key)) {
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
seen.add(key);
|
|
435
|
+
out.push(item);
|
|
436
|
+
}
|
|
437
|
+
return out;
|
|
438
|
+
}
|
|
439
|
+
function normalizePerPage(value) {
|
|
440
|
+
if (value === void 0 || !Number.isFinite(value)) {
|
|
441
|
+
return DEFAULT_PER_PAGE;
|
|
442
|
+
}
|
|
443
|
+
return Math.min(Math.max(Math.trunc(value), 1), MAX_PER_PAGE);
|
|
444
|
+
}
|
|
445
|
+
function normalizeMaxPages(value) {
|
|
446
|
+
if (value === void 0 || !Number.isFinite(value)) {
|
|
447
|
+
return DEFAULT_MAX_PAGES;
|
|
448
|
+
}
|
|
449
|
+
return Math.min(Math.max(Math.trunc(value), 1), MAX_PAGES_LIMIT);
|
|
450
|
+
}
|
|
451
|
+
async function collectPages(http, spec, options) {
|
|
452
|
+
const perPage = normalizePerPage(options.perPage);
|
|
453
|
+
const maxPages = normalizeMaxPages(options.maxPages);
|
|
454
|
+
const strict = options.requireComplete === true;
|
|
455
|
+
const collected = [];
|
|
456
|
+
const baseQuery = { ...spec.query, ...options.query };
|
|
457
|
+
for (let page = 1; page <= maxPages; page += 1) {
|
|
458
|
+
const response = await http.request({
|
|
459
|
+
...spec,
|
|
460
|
+
query: {
|
|
461
|
+
...baseQuery,
|
|
462
|
+
per_page: perPage,
|
|
463
|
+
page
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
const items = options.selectItems(response.json, { page, response });
|
|
467
|
+
collected.push(...items);
|
|
468
|
+
const hasNext = resolveNextPage(
|
|
469
|
+
{
|
|
470
|
+
link: readHeader2(response.headers, "link") ?? null,
|
|
471
|
+
totalPage: strict ? readStrictTotalPage(response.headers) : readTotalPage(response.headers),
|
|
472
|
+
page,
|
|
473
|
+
itemCount: items.length,
|
|
474
|
+
perPage
|
|
475
|
+
},
|
|
476
|
+
strict
|
|
477
|
+
);
|
|
478
|
+
if (!hasNext) {
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
481
|
+
if (strict && page === maxPages) {
|
|
482
|
+
throw new GitForgeError({
|
|
483
|
+
code: "PAGINATION_LIMIT",
|
|
484
|
+
provider: http.provider,
|
|
485
|
+
message: `The page budget (maxPages=${maxPages}) ended while more pages were indicated.`
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return dedupeBy(collected, options.keyOf);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// src/adapters/pulls.ts
|
|
493
|
+
function decimalPullNumber(value) {
|
|
494
|
+
const n = typeof value === "string" && value.length > 0 && !/\D/.test(value) ? Number(value) : value;
|
|
495
|
+
return typeof n === "number" && Number.isSafeInteger(n) && n > 0 ? n : void 0;
|
|
496
|
+
}
|
|
497
|
+
function requirePullNumber(http, response, raw, expected) {
|
|
498
|
+
const record = raw !== null && typeof raw === "object" && !Array.isArray(raw) ? raw : {};
|
|
499
|
+
const iid = http.provider === "atomgit" ? record.iid : void 0;
|
|
500
|
+
const n = decimalPullNumber(record.number === void 0 ? iid : record.number);
|
|
501
|
+
if (n === void 0 || iid !== void 0 && decimalPullNumber(iid) !== n) {
|
|
502
|
+
throw http.invalidResponse(response, "Pull request is missing a valid, unambiguous number");
|
|
503
|
+
}
|
|
504
|
+
if (expected !== void 0 && n !== expected) {
|
|
505
|
+
throw http.invalidResponse(response, "Detail returned a different pull request number");
|
|
506
|
+
}
|
|
507
|
+
return n;
|
|
508
|
+
}
|
|
509
|
+
function pullFromResponse(map, ctx, params, response, options = {}) {
|
|
510
|
+
const raw = "raw" in options ? options.raw : response.json;
|
|
511
|
+
const number = requirePullNumber(ctx.http, response, raw, options.expectedNumber);
|
|
512
|
+
const base2 = raw?.base;
|
|
513
|
+
if (typeof base2?.ref !== "string" || !base2.ref.trim() || typeof base2.sha !== "string" || !base2.sha.trim()) {
|
|
514
|
+
throw ctx.http.invalidResponse(
|
|
515
|
+
response,
|
|
516
|
+
"Pull request response is missing its required base ref or SHA"
|
|
517
|
+
);
|
|
518
|
+
}
|
|
519
|
+
return map(params.owner, params.repo, raw, { ref: base2.ref, sha: base2.sha }, number);
|
|
520
|
+
}
|
|
521
|
+
async function collectPullRequests(ctx, params, map, options) {
|
|
522
|
+
const collected = /* @__PURE__ */ new Map();
|
|
523
|
+
for (const state of options.states) {
|
|
524
|
+
const rows = await collectPages(
|
|
525
|
+
ctx.http,
|
|
526
|
+
{
|
|
527
|
+
method: "GET",
|
|
528
|
+
path: "/repos/{owner}/{repo}/pulls",
|
|
529
|
+
pathParams: { owner: params.owner, repo: params.repo },
|
|
530
|
+
headers: options.headers,
|
|
531
|
+
query: { ...options.query, state }
|
|
532
|
+
},
|
|
533
|
+
{
|
|
534
|
+
maxPages: params.maxPages,
|
|
535
|
+
selectItems: (raw, { response }) => {
|
|
536
|
+
if (!Array.isArray(raw))
|
|
537
|
+
throw ctx.http.invalidResponse(response, "Expected a pull request list");
|
|
538
|
+
return raw.map((item) => ({
|
|
539
|
+
pull: pullFromResponse(map, ctx, params, response, { raw: item }),
|
|
540
|
+
matches: options.matches?.(item) ?? true
|
|
541
|
+
}));
|
|
542
|
+
},
|
|
543
|
+
keyOf: (row) => String(row.pull.number)
|
|
544
|
+
}
|
|
545
|
+
);
|
|
546
|
+
for (const { pull, matches } of rows) {
|
|
547
|
+
if (matches && (!collected.has(pull.number) || pull.merged)) collected.set(pull.number, pull);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
return [...collected.values()];
|
|
551
|
+
}
|
|
552
|
+
function contentsBase64(content, encoding = "utf8", provider) {
|
|
553
|
+
if (encoding === "utf8") return Buffer.from(content, "utf8").toString("base64");
|
|
554
|
+
const compact = content.replace(/\s+/g, "");
|
|
555
|
+
const canonical = Buffer.from(compact, "base64").toString("base64");
|
|
556
|
+
if (encoding !== "base64" || compact !== canonical && compact !== canonical.replace(/=+$/, "")) {
|
|
557
|
+
throw invalidArgumentError("Invalid `content`: expected base64", provider);
|
|
558
|
+
}
|
|
559
|
+
return canonical;
|
|
560
|
+
}
|
|
561
|
+
function confirmedSha(http, response, value) {
|
|
562
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
563
|
+
throw http.invalidResponse(response, "The provider response did not confirm a Git object SHA");
|
|
564
|
+
}
|
|
565
|
+
return value;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
// src/core/issue-number.ts
|
|
569
|
+
function issueNumberFields(number) {
|
|
570
|
+
if (/^\d+$/.test(number)) {
|
|
571
|
+
const numeric = Number(number);
|
|
572
|
+
if (Number.isSafeInteger(numeric)) {
|
|
573
|
+
return { number, number_as_number: numeric };
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
return { number };
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// src/adapters/github/map.ts
|
|
580
|
+
var PROVIDER = "github";
|
|
581
|
+
var PUSH_ACTIONS = ["create", "update", "delete"];
|
|
582
|
+
function asRecord(value) {
|
|
583
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
584
|
+
}
|
|
585
|
+
function asArray(value) {
|
|
586
|
+
return Array.isArray(value) ? value : [];
|
|
587
|
+
}
|
|
588
|
+
function str(value) {
|
|
589
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
590
|
+
}
|
|
591
|
+
function num(value) {
|
|
592
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
593
|
+
}
|
|
594
|
+
function listKey(value) {
|
|
595
|
+
if (typeof value === "string") {
|
|
596
|
+
return value;
|
|
597
|
+
}
|
|
598
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
599
|
+
return String(value);
|
|
600
|
+
}
|
|
601
|
+
return "";
|
|
602
|
+
}
|
|
603
|
+
function bool(value) {
|
|
604
|
+
return typeof value === "boolean" ? value : void 0;
|
|
605
|
+
}
|
|
606
|
+
function normalizeLabels(raw) {
|
|
607
|
+
const out = [];
|
|
608
|
+
for (const entry of asArray(raw)) {
|
|
609
|
+
if (typeof entry === "string" && entry.length > 0) {
|
|
610
|
+
out.push(entry);
|
|
611
|
+
continue;
|
|
612
|
+
}
|
|
613
|
+
const name = str(asRecord(entry).name);
|
|
614
|
+
if (name !== void 0) {
|
|
615
|
+
out.push(name);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
return out;
|
|
619
|
+
}
|
|
620
|
+
function mapUser(raw) {
|
|
621
|
+
const record = asRecord(raw);
|
|
622
|
+
const login = str(record.login) ?? str(record.name);
|
|
623
|
+
if (login === void 0) {
|
|
624
|
+
return void 0;
|
|
625
|
+
}
|
|
626
|
+
const user = { login };
|
|
627
|
+
const name = str(record.name);
|
|
628
|
+
const avatar = str(record.avatar_url);
|
|
629
|
+
if (name !== void 0) {
|
|
630
|
+
user.name = name;
|
|
631
|
+
}
|
|
632
|
+
if (avatar !== void 0) {
|
|
633
|
+
user.avatar_url = avatar;
|
|
634
|
+
}
|
|
635
|
+
return user;
|
|
636
|
+
}
|
|
637
|
+
function mapUsers(raw) {
|
|
638
|
+
const users = asArray(raw).map((entry) => mapUser(entry)).filter((entry) => entry !== void 0);
|
|
639
|
+
return users.length > 0 ? users : void 0;
|
|
640
|
+
}
|
|
641
|
+
function mapIssueState(raw) {
|
|
642
|
+
return raw === "open" ? "open" : "closed";
|
|
643
|
+
}
|
|
644
|
+
function mapPullRequestState(raw) {
|
|
645
|
+
return raw === "open" ? "open" : "closed";
|
|
646
|
+
}
|
|
647
|
+
function isPullRequestEntry(raw) {
|
|
648
|
+
const marker = asRecord(asRecord(raw).pull_request);
|
|
649
|
+
const url = str(marker.url);
|
|
650
|
+
return url !== void 0 && /^https?:\/\//i.test(url) && URL.canParse(url);
|
|
651
|
+
}
|
|
652
|
+
function mapIssue(owner, repo, raw) {
|
|
653
|
+
const record = asRecord(raw);
|
|
654
|
+
const issue = {
|
|
655
|
+
provider: PROVIDER,
|
|
656
|
+
owner,
|
|
657
|
+
repo,
|
|
658
|
+
...issueNumberFields(listKey(record.number)),
|
|
659
|
+
title: str(record.title) ?? "",
|
|
660
|
+
state: mapIssueState(record.state),
|
|
661
|
+
html_url: str(record.html_url) ?? "",
|
|
662
|
+
labels: normalizeLabels(record.labels),
|
|
663
|
+
created_at: str(record.created_at) ?? "",
|
|
664
|
+
updated_at: str(record.updated_at) ?? ""
|
|
665
|
+
};
|
|
666
|
+
const body = str(record.body);
|
|
667
|
+
if (body !== void 0) {
|
|
668
|
+
issue.body = body;
|
|
669
|
+
}
|
|
670
|
+
const user = mapUser(record.user);
|
|
671
|
+
if (user !== void 0) {
|
|
672
|
+
issue.user = user;
|
|
673
|
+
}
|
|
674
|
+
const assignees = mapUsers(record.assignees);
|
|
675
|
+
if (assignees !== void 0) {
|
|
676
|
+
issue.assignees = assignees;
|
|
677
|
+
}
|
|
678
|
+
const comments = num(record.comments);
|
|
679
|
+
if (comments !== void 0) {
|
|
680
|
+
issue.comments_count = comments;
|
|
681
|
+
}
|
|
682
|
+
const closedAt = str(record.closed_at);
|
|
683
|
+
if (closedAt !== void 0) {
|
|
684
|
+
issue.closed_at = closedAt;
|
|
685
|
+
}
|
|
686
|
+
return issue;
|
|
687
|
+
}
|
|
688
|
+
function mapRef(raw) {
|
|
689
|
+
const record = asRecord(raw);
|
|
690
|
+
const ref = str(record.ref);
|
|
691
|
+
const sha = str(record.sha);
|
|
692
|
+
return ref !== void 0 && sha !== void 0 ? { ref, sha } : void 0;
|
|
693
|
+
}
|
|
694
|
+
function mapPullRequest(owner, repo, raw, base2, number) {
|
|
695
|
+
const record = asRecord(raw);
|
|
696
|
+
const merged = bool(record.merged) ?? str(record.merged_at) !== void 0;
|
|
697
|
+
const draft = {
|
|
698
|
+
base: base2,
|
|
699
|
+
provider: PROVIDER,
|
|
700
|
+
owner,
|
|
701
|
+
repo,
|
|
702
|
+
number,
|
|
703
|
+
title: str(record.title) ?? "",
|
|
704
|
+
state: merged ? "closed" : mapPullRequestState(record.state),
|
|
705
|
+
merged,
|
|
706
|
+
html_url: str(record.html_url) ?? "",
|
|
707
|
+
created_at: str(record.created_at) ?? "",
|
|
708
|
+
updated_at: str(record.updated_at) ?? ""
|
|
709
|
+
};
|
|
710
|
+
const head = mapRef(record.head);
|
|
711
|
+
if (head !== void 0) {
|
|
712
|
+
draft.head = head;
|
|
713
|
+
}
|
|
714
|
+
const mergedAt = str(record.merged_at);
|
|
715
|
+
if (mergedAt !== void 0) {
|
|
716
|
+
draft.merged_at = mergedAt;
|
|
717
|
+
}
|
|
718
|
+
const body = str(record.body);
|
|
719
|
+
if (body !== void 0) {
|
|
720
|
+
draft.body = body;
|
|
721
|
+
}
|
|
722
|
+
const isDraft = bool(record.draft);
|
|
723
|
+
if (isDraft !== void 0) {
|
|
724
|
+
draft.draft = isDraft;
|
|
725
|
+
}
|
|
726
|
+
const mergeable = bool(record.mergeable);
|
|
727
|
+
if (mergeable !== void 0) {
|
|
728
|
+
draft.mergeable = mergeable;
|
|
729
|
+
}
|
|
730
|
+
const user = mapUser(record.user);
|
|
731
|
+
if (user !== void 0) {
|
|
732
|
+
draft.user = user;
|
|
733
|
+
}
|
|
734
|
+
for (const key of ["additions", "deletions", "changed_files"]) {
|
|
735
|
+
const value = num(record[key]);
|
|
736
|
+
if (value !== void 0) {
|
|
737
|
+
draft[key] = value;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
return draft;
|
|
741
|
+
}
|
|
742
|
+
function mapRepository(owner, repo, raw) {
|
|
743
|
+
const record = asRecord(raw);
|
|
744
|
+
const repository = {
|
|
745
|
+
provider: PROVIDER,
|
|
746
|
+
owner: str(asRecord(record.owner).login) ?? owner,
|
|
747
|
+
repo,
|
|
748
|
+
name: str(record.name) ?? repo,
|
|
749
|
+
html_url: str(record.html_url) ?? "",
|
|
750
|
+
private: bool(record.private) === true,
|
|
751
|
+
default_branch: str(record.default_branch) ?? ""
|
|
752
|
+
};
|
|
753
|
+
const fullName = str(record.full_name);
|
|
754
|
+
if (fullName !== void 0) {
|
|
755
|
+
repository.full_name = fullName;
|
|
756
|
+
}
|
|
757
|
+
const description = str(record.description);
|
|
758
|
+
if (description !== void 0) {
|
|
759
|
+
repository.description = description;
|
|
760
|
+
}
|
|
761
|
+
const fork = bool(record.fork);
|
|
762
|
+
if (fork !== void 0) {
|
|
763
|
+
repository.fork = fork;
|
|
764
|
+
}
|
|
765
|
+
const language = str(record.language);
|
|
766
|
+
if (language !== void 0) {
|
|
767
|
+
repository.language = language;
|
|
768
|
+
}
|
|
769
|
+
const createdAt = str(record.created_at);
|
|
770
|
+
if (createdAt !== void 0) {
|
|
771
|
+
repository.created_at = createdAt;
|
|
772
|
+
}
|
|
773
|
+
const updatedAt = str(record.updated_at);
|
|
774
|
+
if (updatedAt !== void 0) {
|
|
775
|
+
repository.updated_at = updatedAt;
|
|
776
|
+
}
|
|
777
|
+
return repository;
|
|
778
|
+
}
|
|
779
|
+
function mapFileContent(owner, repo, raw) {
|
|
780
|
+
const record = asRecord(raw);
|
|
781
|
+
const content = {
|
|
782
|
+
provider: PROVIDER,
|
|
783
|
+
owner,
|
|
784
|
+
repo,
|
|
785
|
+
path: str(record.path) ?? "",
|
|
786
|
+
content: typeof record.content === "string" ? record.content : "",
|
|
787
|
+
encoding: str(record.encoding) ?? "base64"
|
|
788
|
+
};
|
|
789
|
+
const sha = str(record.sha);
|
|
790
|
+
if (sha !== void 0) {
|
|
791
|
+
content.sha = sha;
|
|
792
|
+
}
|
|
793
|
+
const size = num(record.size);
|
|
794
|
+
if (size !== void 0) {
|
|
795
|
+
content.size = size;
|
|
796
|
+
}
|
|
797
|
+
const htmlUrl = str(record.html_url);
|
|
798
|
+
if (htmlUrl !== void 0) {
|
|
799
|
+
content.html_url = htmlUrl;
|
|
800
|
+
}
|
|
801
|
+
const type = str(record.type);
|
|
802
|
+
if (type !== void 0) {
|
|
803
|
+
content.type = type;
|
|
804
|
+
}
|
|
805
|
+
return content;
|
|
806
|
+
}
|
|
807
|
+
function deriveBranchHtmlUrl(webBaseUrl, owner, repo, name) {
|
|
808
|
+
const base2 = webBaseUrl.endsWith("/") ? webBaseUrl.slice(0, -1) : webBaseUrl;
|
|
809
|
+
if (base2.length === 0) {
|
|
810
|
+
return void 0;
|
|
811
|
+
}
|
|
812
|
+
return `${base2}/${owner}/${repo}/tree/${name}`;
|
|
813
|
+
}
|
|
814
|
+
function mapBranch(owner, repo, raw, webBaseUrl) {
|
|
815
|
+
const record = asRecord(raw);
|
|
816
|
+
const name = str(record.name) ?? "";
|
|
817
|
+
const branch = {
|
|
818
|
+
provider: PROVIDER,
|
|
819
|
+
owner,
|
|
820
|
+
repo,
|
|
821
|
+
name,
|
|
822
|
+
commit_sha: str(asRecord(record.commit).sha) ?? ""
|
|
823
|
+
};
|
|
824
|
+
const isProtected = bool(record.protected);
|
|
825
|
+
if (isProtected !== void 0) {
|
|
826
|
+
branch.protected = isProtected;
|
|
827
|
+
}
|
|
828
|
+
const htmlUrl = deriveBranchHtmlUrl(webBaseUrl, owner, repo, name);
|
|
829
|
+
if (htmlUrl !== void 0) {
|
|
830
|
+
branch.html_url = htmlUrl;
|
|
831
|
+
}
|
|
832
|
+
return branch;
|
|
833
|
+
}
|
|
834
|
+
function mapCommitAuthor(raw) {
|
|
835
|
+
const record = asRecord(raw);
|
|
836
|
+
const name = str(record.name);
|
|
837
|
+
if (name === void 0) {
|
|
838
|
+
return void 0;
|
|
839
|
+
}
|
|
840
|
+
return {
|
|
841
|
+
name,
|
|
842
|
+
email: str(record.email) ?? "",
|
|
843
|
+
date: str(record.date) ?? ""
|
|
844
|
+
};
|
|
845
|
+
}
|
|
846
|
+
function mapCommit(owner, repo, raw) {
|
|
847
|
+
const record = asRecord(raw);
|
|
848
|
+
const payload = asRecord(record.commit);
|
|
849
|
+
const commit = {
|
|
850
|
+
provider: PROVIDER,
|
|
851
|
+
owner,
|
|
852
|
+
repo,
|
|
853
|
+
sha: str(record.sha) ?? "",
|
|
854
|
+
message: str(payload.message) ?? "",
|
|
855
|
+
html_url: str(record.html_url) ?? ""
|
|
856
|
+
};
|
|
857
|
+
const author = mapCommitAuthor(payload.author);
|
|
858
|
+
if (author !== void 0) {
|
|
859
|
+
commit.author = author;
|
|
860
|
+
}
|
|
861
|
+
const committer = mapCommitAuthor(payload.committer);
|
|
862
|
+
if (committer !== void 0) {
|
|
863
|
+
commit.committer = committer;
|
|
864
|
+
}
|
|
865
|
+
const parents = asArray(record.parents).map((entry) => str(asRecord(entry).sha)).filter((entry) => entry !== void 0);
|
|
866
|
+
if (parents.length > 0) {
|
|
867
|
+
commit.parents = parents;
|
|
868
|
+
}
|
|
869
|
+
return commit;
|
|
870
|
+
}
|
|
871
|
+
function mapComment(owner, repo, raw, parentUrl) {
|
|
872
|
+
const record = asRecord(raw);
|
|
873
|
+
const comment = {
|
|
874
|
+
provider: PROVIDER,
|
|
875
|
+
owner,
|
|
876
|
+
repo,
|
|
877
|
+
id: num(record.id) ?? Number(str(record.id) ?? 0),
|
|
878
|
+
body: typeof record.body === "string" ? record.body : "",
|
|
879
|
+
created_at: str(record.created_at) ?? ""
|
|
880
|
+
};
|
|
881
|
+
const htmlUrl = str(record.html_url) ?? parentUrl;
|
|
882
|
+
if (htmlUrl !== void 0 && htmlUrl.length > 0) {
|
|
883
|
+
comment.html_url = htmlUrl;
|
|
884
|
+
}
|
|
885
|
+
const updatedAt = str(record.updated_at);
|
|
886
|
+
if (updatedAt !== void 0) {
|
|
887
|
+
comment.updated_at = updatedAt;
|
|
888
|
+
}
|
|
889
|
+
const user = mapUser(record.user);
|
|
890
|
+
if (user !== void 0) {
|
|
891
|
+
comment.user = user;
|
|
892
|
+
}
|
|
893
|
+
return comment;
|
|
894
|
+
}
|
|
895
|
+
function buildFileWriteResult(inputs) {
|
|
896
|
+
const result = {
|
|
897
|
+
provider: PROVIDER,
|
|
898
|
+
owner: inputs.owner,
|
|
899
|
+
repo: inputs.repo,
|
|
900
|
+
commit_sha: inputs.commitSha,
|
|
901
|
+
commit_html_url: inputs.commitHtmlUrl,
|
|
902
|
+
paths: [...inputs.paths],
|
|
903
|
+
branch: inputs.branch ?? ""
|
|
904
|
+
};
|
|
905
|
+
if (inputs.fileHtmlUrls !== void 0) {
|
|
906
|
+
result.file_html_urls = inputs.fileHtmlUrls;
|
|
907
|
+
}
|
|
908
|
+
return result;
|
|
909
|
+
}
|
|
910
|
+
function isShaLike(value) {
|
|
911
|
+
return /^[0-9a-f]{7,40}$/i.test(value);
|
|
912
|
+
}
|
|
913
|
+
var BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
914
|
+
function isValidBase64(value) {
|
|
915
|
+
const compact = value.replace(/\s+/g, "");
|
|
916
|
+
const body = compact.replace(/={0,2}$/, "");
|
|
917
|
+
if (body.length === 0) {
|
|
918
|
+
return false;
|
|
919
|
+
}
|
|
920
|
+
if (body.length % 4 === 1) {
|
|
921
|
+
return false;
|
|
922
|
+
}
|
|
923
|
+
for (const char of body) {
|
|
924
|
+
if (!BASE64_ALPHABET.includes(char)) {
|
|
925
|
+
return false;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
return true;
|
|
929
|
+
}
|
|
930
|
+
function readPushEntries(files) {
|
|
931
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
932
|
+
throw invalidArgumentError(
|
|
933
|
+
"Invalid `files`: pushFiles requires at least one entry (an empty batch would create an empty commit).",
|
|
934
|
+
PROVIDER,
|
|
935
|
+
"files.pushFiles"
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
return files.map((raw, index) => {
|
|
939
|
+
const file = asRecord(raw);
|
|
940
|
+
const path = file.path;
|
|
941
|
+
if (typeof path !== "string" || path.trim().length === 0) {
|
|
942
|
+
throw invalidArgumentError(
|
|
943
|
+
`Invalid \`files[${index}].path\`: expected a non-empty string.`,
|
|
944
|
+
PROVIDER,
|
|
945
|
+
"files.pushFiles"
|
|
946
|
+
);
|
|
947
|
+
}
|
|
948
|
+
const rawAction = file.action;
|
|
949
|
+
if (typeof rawAction !== "string" || !PUSH_ACTIONS.includes(rawAction)) {
|
|
950
|
+
throw invalidArgumentError(
|
|
951
|
+
`Invalid \`files[${index}].action\`: expected one of ${PUSH_ACTIONS.join(" | ")}, received ${describe(rawAction)}. pushFiles does not provide upsert.`,
|
|
952
|
+
PROVIDER,
|
|
953
|
+
"files.pushFiles"
|
|
954
|
+
);
|
|
955
|
+
}
|
|
956
|
+
const action = rawAction;
|
|
957
|
+
const rawEncoding = file.encoding ?? "utf8";
|
|
958
|
+
if (rawEncoding !== "utf8" && rawEncoding !== "base64") {
|
|
959
|
+
throw invalidArgumentError(
|
|
960
|
+
`Invalid \`files[${index}].encoding\`: expected utf8 | base64, received ${describe(rawEncoding)}.`,
|
|
961
|
+
PROVIDER,
|
|
962
|
+
"files.pushFiles"
|
|
963
|
+
);
|
|
964
|
+
}
|
|
965
|
+
const encoding = rawEncoding;
|
|
966
|
+
if (action === "delete") {
|
|
967
|
+
if (file.content !== void 0) {
|
|
968
|
+
throw invalidArgumentError(
|
|
969
|
+
`Invalid \`files[${index}]\`: action 'delete' must not carry content.`,
|
|
970
|
+
PROVIDER,
|
|
971
|
+
"files.pushFiles"
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
return { path, action, encoding };
|
|
975
|
+
}
|
|
976
|
+
const content = file.content;
|
|
977
|
+
if (typeof content !== "string") {
|
|
978
|
+
throw invalidArgumentError(
|
|
979
|
+
`Invalid \`files[${index}].content\`: action '${action}' requires content.`,
|
|
980
|
+
PROVIDER,
|
|
981
|
+
"files.pushFiles"
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
if (encoding === "base64" && !isValidBase64(content)) {
|
|
985
|
+
throw invalidArgumentError(
|
|
986
|
+
`Invalid \`files[${index}].content\`: not a valid base64 payload.`,
|
|
987
|
+
PROVIDER,
|
|
988
|
+
"files.pushFiles"
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
return { path, action, content, encoding };
|
|
992
|
+
});
|
|
993
|
+
}
|
|
994
|
+
function describe(value) {
|
|
995
|
+
if (value === void 0) {
|
|
996
|
+
return "undefined";
|
|
997
|
+
}
|
|
998
|
+
if (typeof value === "string") {
|
|
999
|
+
return value.length === 0 ? "an empty string" : `"${value}"`;
|
|
1000
|
+
}
|
|
1001
|
+
return typeof value;
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
// src/adapters/github/tree.ts
|
|
1005
|
+
function resolveFileModes(http, response, entries) {
|
|
1006
|
+
const tree = asRecord(response.json);
|
|
1007
|
+
if (tree.truncated !== false || !Array.isArray(tree.tree)) {
|
|
1008
|
+
throw http.invalidResponse(response, "Cannot plan file changes without a complete Git tree");
|
|
1009
|
+
}
|
|
1010
|
+
const index = /* @__PURE__ */ new Map();
|
|
1011
|
+
for (const raw of tree.tree) {
|
|
1012
|
+
const item = asRecord(raw);
|
|
1013
|
+
if (typeof item.path !== "string" || !item.path || index.has(item.path)) {
|
|
1014
|
+
throw http.invalidResponse(response, "Ambiguous entries in the parent Git tree");
|
|
1015
|
+
}
|
|
1016
|
+
index.set(item.path, item);
|
|
1017
|
+
}
|
|
1018
|
+
const planned = new Set(entries.map(({ path }) => path));
|
|
1019
|
+
if (planned.size !== entries.length)
|
|
1020
|
+
throw invalidArgumentError("Duplicate paths in file batch", "github");
|
|
1021
|
+
return entries.map(({ path, action }) => {
|
|
1022
|
+
let ancestor = path;
|
|
1023
|
+
while (ancestor.includes("/")) {
|
|
1024
|
+
ancestor = ancestor.slice(0, ancestor.lastIndexOf("/"));
|
|
1025
|
+
const item = index.get(ancestor);
|
|
1026
|
+
if (planned.has(ancestor) || item && item.type !== "tree") {
|
|
1027
|
+
throw invalidArgumentError("A parent path conflicts with a file change", "github");
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
const old = index.get(path);
|
|
1031
|
+
if (action === "create" && !old) return "100644";
|
|
1032
|
+
if (!old || action === "create")
|
|
1033
|
+
throw invalidArgumentError("File existence does not match the requested action", "github");
|
|
1034
|
+
if (old.type !== "blob" || old.mode !== "100644" && old.mode !== "100755") {
|
|
1035
|
+
throw invalidArgumentError("Only regular files can be updated or deleted", "github");
|
|
1036
|
+
}
|
|
1037
|
+
return old.mode;
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
// src/adapters/github/capabilities.ts
|
|
1042
|
+
var capabilities = {
|
|
1043
|
+
"issues.list": "supported",
|
|
1044
|
+
"issues.get": "supported",
|
|
1045
|
+
"issues.create": "supported",
|
|
1046
|
+
"issues.update": "supported",
|
|
1047
|
+
"issues.listComments": "supported",
|
|
1048
|
+
"issues.createComment": "supported",
|
|
1049
|
+
"pulls.list": "supported",
|
|
1050
|
+
"pulls.get": "supported",
|
|
1051
|
+
"pulls.create": "supported",
|
|
1052
|
+
"pulls.merge": "supported",
|
|
1053
|
+
"pulls.listComments": "supported",
|
|
1054
|
+
"pulls.createComment": "supported",
|
|
1055
|
+
"repos.list": "supported",
|
|
1056
|
+
"repos.get": "supported",
|
|
1057
|
+
"repos.create": "supported",
|
|
1058
|
+
"repos.getFileContents": "supported",
|
|
1059
|
+
"files.create": "supported",
|
|
1060
|
+
"files.update": "supported",
|
|
1061
|
+
"files.delete": "supported",
|
|
1062
|
+
"files.pushFiles": "supported",
|
|
1063
|
+
"branches.list": "supported",
|
|
1064
|
+
"branches.create": "supported",
|
|
1065
|
+
"commits.list": "supported",
|
|
1066
|
+
"commits.get": "supported"
|
|
1067
|
+
};
|
|
1068
|
+
var paramCapabilities = {
|
|
1069
|
+
"pulls.list.headFilter": "supported",
|
|
1070
|
+
"pulls.list.milestoneFilter": "supported",
|
|
1071
|
+
"issues.list.milestoneFilter": "supported",
|
|
1072
|
+
"files.pushFiles.atomic": "supported"
|
|
1073
|
+
};
|
|
1074
|
+
|
|
1075
|
+
// src/adapters/github/adapter.ts
|
|
1076
|
+
var GITHUB_HEADERS = {
|
|
1077
|
+
accept: "application/vnd.github+json",
|
|
1078
|
+
"x-github-api-version": "2022-11-28"
|
|
1079
|
+
};
|
|
1080
|
+
var REPO_PATH = "/repos/{owner}/{repo}";
|
|
1081
|
+
function base(owner, repo) {
|
|
1082
|
+
return { pathParams: { owner, repo }, headers: GITHUB_HEADERS };
|
|
1083
|
+
}
|
|
1084
|
+
function joinLabels(labels) {
|
|
1085
|
+
return labels === void 0 || labels.length === 0 ? void 0 : labels.join(",");
|
|
1086
|
+
}
|
|
1087
|
+
function assertCreation(params) {
|
|
1088
|
+
if (params.sha !== void 0) {
|
|
1089
|
+
throw invalidArgumentError(
|
|
1090
|
+
"Invalid `sha`: createFile creates a new file and must not receive a blob sha; use updateFile to replace an existing file.",
|
|
1091
|
+
"github"
|
|
1092
|
+
);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
function assertUpdate(params) {
|
|
1096
|
+
if (params.sha === void 0 || params.sha.length === 0) {
|
|
1097
|
+
throw invalidArgumentError(
|
|
1098
|
+
"Invalid `sha`: updateFile requires the current blob sha of the file being replaced.",
|
|
1099
|
+
"github"
|
|
1100
|
+
);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
function writeResultFromContents(ctx, owner, repo, branch, path, response, keepFileUrl) {
|
|
1104
|
+
const record = asRecord(response.json);
|
|
1105
|
+
const commit = asRecord(record.commit);
|
|
1106
|
+
const contentUrl = keepFileUrl ? str(asRecord(record.content).html_url) : void 0;
|
|
1107
|
+
const result = buildFileWriteResult({
|
|
1108
|
+
owner,
|
|
1109
|
+
repo,
|
|
1110
|
+
branch,
|
|
1111
|
+
paths: [path],
|
|
1112
|
+
commitSha: confirmedSha(ctx.http, response, commit.sha),
|
|
1113
|
+
commitHtmlUrl: str(commit.html_url) ?? ""
|
|
1114
|
+
});
|
|
1115
|
+
if (keepFileUrl) {
|
|
1116
|
+
result.file_html_urls = [contentUrl];
|
|
1117
|
+
}
|
|
1118
|
+
return result;
|
|
1119
|
+
}
|
|
1120
|
+
var GitHubAdapter = class {
|
|
1121
|
+
provider = "github";
|
|
1122
|
+
capabilities = capabilities;
|
|
1123
|
+
paramCapabilities = paramCapabilities;
|
|
1124
|
+
/* ---------------------------------------------------------------------- */
|
|
1125
|
+
/* Issue */
|
|
1126
|
+
/* ---------------------------------------------------------------------- */
|
|
1127
|
+
async listIssues(ctx, params) {
|
|
1128
|
+
const { owner, repo } = params;
|
|
1129
|
+
const entries = await collectPages(
|
|
1130
|
+
ctx.http,
|
|
1131
|
+
{ method: "GET", path: `${REPO_PATH}/issues`, ...base(owner, repo) },
|
|
1132
|
+
{
|
|
1133
|
+
maxPages: params.maxPages,
|
|
1134
|
+
requireComplete: params.requireComplete,
|
|
1135
|
+
query: {
|
|
1136
|
+
state: params.state,
|
|
1137
|
+
labels: joinLabels(params.labels),
|
|
1138
|
+
milestone: params.milestone
|
|
1139
|
+
},
|
|
1140
|
+
selectItems: (_raw, { response }) => selectIssueRows(ctx.http, response, isPullRequestEntry),
|
|
1141
|
+
keyOf: (raw) => listKey(asRecord(raw).number)
|
|
1142
|
+
}
|
|
1143
|
+
);
|
|
1144
|
+
return entries.filter((entry) => !isPullRequestEntry(entry)).map((entry) => mapIssue(owner, repo, entry));
|
|
1145
|
+
}
|
|
1146
|
+
async getIssue(ctx, params) {
|
|
1147
|
+
const response = await ctx.http.request({
|
|
1148
|
+
method: "GET",
|
|
1149
|
+
path: `${REPO_PATH}/issues/{number}`,
|
|
1150
|
+
pathParams: { owner: params.owner, repo: params.repo, number: String(params.number) },
|
|
1151
|
+
headers: GITHUB_HEADERS
|
|
1152
|
+
});
|
|
1153
|
+
return mapIssue(params.owner, params.repo, response.json);
|
|
1154
|
+
}
|
|
1155
|
+
async createIssue(ctx, params) {
|
|
1156
|
+
const response = await ctx.http.request({
|
|
1157
|
+
method: "POST",
|
|
1158
|
+
path: `${REPO_PATH}/issues`,
|
|
1159
|
+
...base(params.owner, params.repo),
|
|
1160
|
+
body: {
|
|
1161
|
+
title: params.title,
|
|
1162
|
+
body: params.body,
|
|
1163
|
+
labels: params.labels,
|
|
1164
|
+
milestone: params.milestone
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
return mapIssue(params.owner, params.repo, response.json);
|
|
1168
|
+
}
|
|
1169
|
+
async updateIssue(ctx, params) {
|
|
1170
|
+
const response = await ctx.http.request({
|
|
1171
|
+
method: "PATCH",
|
|
1172
|
+
path: `${REPO_PATH}/issues/{number}`,
|
|
1173
|
+
pathParams: { owner: params.owner, repo: params.repo, number: String(params.number) },
|
|
1174
|
+
headers: GITHUB_HEADERS,
|
|
1175
|
+
body: {
|
|
1176
|
+
title: params.title,
|
|
1177
|
+
body: params.body,
|
|
1178
|
+
state: params.state,
|
|
1179
|
+
labels: params.labels,
|
|
1180
|
+
milestone: params.milestone
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
return mapIssue(params.owner, params.repo, response.json);
|
|
1184
|
+
}
|
|
1185
|
+
async listIssueComments(ctx, params) {
|
|
1186
|
+
return this.listComments(ctx, params, `${REPO_PATH}/issues/{number}/comments`);
|
|
1187
|
+
}
|
|
1188
|
+
async createIssueComment(ctx, params) {
|
|
1189
|
+
return this.createComment(ctx, params, `${REPO_PATH}/issues/{number}/comments`);
|
|
1190
|
+
}
|
|
1191
|
+
/* ---------------------------------------------------------------------- */
|
|
1192
|
+
/* Pull request */
|
|
1193
|
+
/* ---------------------------------------------------------------------- */
|
|
1194
|
+
async listPullRequests(ctx, params) {
|
|
1195
|
+
return collectPullRequests(ctx, params, mapPullRequest, {
|
|
1196
|
+
states: [params.state],
|
|
1197
|
+
headers: GITHUB_HEADERS,
|
|
1198
|
+
query: { head: params.head, base: params.base },
|
|
1199
|
+
matches: (entry) => {
|
|
1200
|
+
const record = asRecord(entry);
|
|
1201
|
+
const labels = normalizeLabels(record.labels).map((label) => label.toLowerCase());
|
|
1202
|
+
return (params.milestone === void 0 || asRecord(record.milestone).number === params.milestone) && (params.labels ?? []).every((label) => labels.includes(label.toLowerCase()));
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
async getPullRequest(ctx, params) {
|
|
1207
|
+
const response = await ctx.http.request({
|
|
1208
|
+
method: "GET",
|
|
1209
|
+
path: `${REPO_PATH}/pulls/{number}`,
|
|
1210
|
+
pathParams: { owner: params.owner, repo: params.repo, number: String(params.number) },
|
|
1211
|
+
headers: GITHUB_HEADERS
|
|
1212
|
+
});
|
|
1213
|
+
return pullFromResponse(mapPullRequest, ctx, params, response, {
|
|
1214
|
+
expectedNumber: params.number
|
|
1215
|
+
});
|
|
1216
|
+
}
|
|
1217
|
+
async createPullRequest(ctx, params) {
|
|
1218
|
+
const response = await ctx.http.request({
|
|
1219
|
+
method: "POST",
|
|
1220
|
+
path: `${REPO_PATH}/pulls`,
|
|
1221
|
+
...base(params.owner, params.repo),
|
|
1222
|
+
body: {
|
|
1223
|
+
title: params.title,
|
|
1224
|
+
head: params.head,
|
|
1225
|
+
base: params.base,
|
|
1226
|
+
body: params.body,
|
|
1227
|
+
draft: params.draft
|
|
1228
|
+
}
|
|
1229
|
+
});
|
|
1230
|
+
return pullFromResponse(mapPullRequest, ctx, params, response);
|
|
1231
|
+
}
|
|
1232
|
+
async mergePullRequest(ctx, params) {
|
|
1233
|
+
await ctx.http.request({
|
|
1234
|
+
method: "PUT",
|
|
1235
|
+
path: `${REPO_PATH}/pulls/{number}/merge`,
|
|
1236
|
+
pathParams: { owner: params.owner, repo: params.repo, number: String(params.number) },
|
|
1237
|
+
headers: GITHUB_HEADERS,
|
|
1238
|
+
body: {
|
|
1239
|
+
merge_method: params.merge_method,
|
|
1240
|
+
commit_title: params.commit_title,
|
|
1241
|
+
commit_message: params.commit_message
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
return this.getPullRequest(ctx, params);
|
|
1245
|
+
}
|
|
1246
|
+
async listPullRequestComments(ctx, params) {
|
|
1247
|
+
return this.listComments(ctx, params, `${REPO_PATH}/issues/{number}/comments`);
|
|
1248
|
+
}
|
|
1249
|
+
async createPullRequestComment(ctx, params) {
|
|
1250
|
+
return this.createComment(ctx, params, `${REPO_PATH}/issues/{number}/comments`);
|
|
1251
|
+
}
|
|
1252
|
+
/* ---------------------------------------------------------------------- */
|
|
1253
|
+
/* Repository */
|
|
1254
|
+
/* ---------------------------------------------------------------------- */
|
|
1255
|
+
async listRepositories(ctx, params) {
|
|
1256
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : params.user !== void 0 ? "/users/{user}/repos" : "/user/repos";
|
|
1257
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : params.user !== void 0 ? { user: params.user } : {};
|
|
1258
|
+
const entries = await collectPages(
|
|
1259
|
+
ctx.http,
|
|
1260
|
+
{ method: "GET", path, pathParams, headers: GITHUB_HEADERS },
|
|
1261
|
+
{
|
|
1262
|
+
maxPages: params.maxPages,
|
|
1263
|
+
selectItems: (raw) => asArray(raw),
|
|
1264
|
+
keyOf: (raw) => listKey(asRecord(raw).id ?? asRecord(raw).full_name)
|
|
1265
|
+
}
|
|
1266
|
+
);
|
|
1267
|
+
return entries.map((entry) => {
|
|
1268
|
+
const record = asRecord(entry);
|
|
1269
|
+
const fullName = str(record.full_name) ?? "";
|
|
1270
|
+
const [ownerFromPayload, repoFromPayload] = fullName.split("/");
|
|
1271
|
+
const owner = str(asRecord(record.owner).login) ?? ownerFromPayload ?? "";
|
|
1272
|
+
const repo = str(record.name) ?? repoFromPayload ?? "";
|
|
1273
|
+
return mapRepository(owner, repo, record);
|
|
1274
|
+
});
|
|
1275
|
+
}
|
|
1276
|
+
async getRepository(ctx, params) {
|
|
1277
|
+
const response = await ctx.http.request({
|
|
1278
|
+
method: "GET",
|
|
1279
|
+
path: REPO_PATH,
|
|
1280
|
+
...base(params.owner, params.repo)
|
|
1281
|
+
});
|
|
1282
|
+
const record = asRecord(response.json);
|
|
1283
|
+
const owner = str(asRecord(record.owner).login) ?? params.owner;
|
|
1284
|
+
return mapRepository(owner, params.repo, record);
|
|
1285
|
+
}
|
|
1286
|
+
async createRepository(ctx, params) {
|
|
1287
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : "/user/repos";
|
|
1288
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : {};
|
|
1289
|
+
const response = await ctx.http.request({
|
|
1290
|
+
method: "POST",
|
|
1291
|
+
path,
|
|
1292
|
+
pathParams,
|
|
1293
|
+
headers: GITHUB_HEADERS,
|
|
1294
|
+
body: {
|
|
1295
|
+
name: params.name,
|
|
1296
|
+
description: params.description,
|
|
1297
|
+
private: params.private,
|
|
1298
|
+
auto_init: params.auto_init
|
|
1299
|
+
}
|
|
1300
|
+
});
|
|
1301
|
+
const record = asRecord(response.json);
|
|
1302
|
+
const owner = str(asRecord(record.owner).login) ?? params.org ?? "";
|
|
1303
|
+
const repo = str(record.name) ?? params.name;
|
|
1304
|
+
return mapRepository(owner, repo, record);
|
|
1305
|
+
}
|
|
1306
|
+
async getFileContents(ctx, params) {
|
|
1307
|
+
const response = await ctx.http.request({
|
|
1308
|
+
method: "GET",
|
|
1309
|
+
path: `${REPO_PATH}/contents/{path}`,
|
|
1310
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
1311
|
+
query: { ref: params.ref },
|
|
1312
|
+
headers: GITHUB_HEADERS
|
|
1313
|
+
});
|
|
1314
|
+
if (Array.isArray(response.json)) {
|
|
1315
|
+
throw invalidArgumentError(
|
|
1316
|
+
`Invalid \`path\`: "${params.path}" is a directory. v0.1.0 getFileContents reads a single file only.`,
|
|
1317
|
+
"github"
|
|
1318
|
+
);
|
|
1319
|
+
}
|
|
1320
|
+
return mapFileContent(params.owner, params.repo, response.json);
|
|
1321
|
+
}
|
|
1322
|
+
/* ---------------------------------------------------------------------- */
|
|
1323
|
+
/* Files */
|
|
1324
|
+
/* ---------------------------------------------------------------------- */
|
|
1325
|
+
async createFile(ctx, params) {
|
|
1326
|
+
assertCreation(params);
|
|
1327
|
+
return this.putContents(ctx, params);
|
|
1328
|
+
}
|
|
1329
|
+
async updateFile(ctx, params) {
|
|
1330
|
+
assertUpdate(params);
|
|
1331
|
+
return this.putContents(ctx, params);
|
|
1332
|
+
}
|
|
1333
|
+
async deleteFile(ctx, params) {
|
|
1334
|
+
const response = await ctx.http.request({
|
|
1335
|
+
method: "DELETE",
|
|
1336
|
+
path: `${REPO_PATH}/contents/{path}`,
|
|
1337
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
1338
|
+
headers: GITHUB_HEADERS,
|
|
1339
|
+
body: { message: params.message, sha: params.sha, branch: params.branch }
|
|
1340
|
+
});
|
|
1341
|
+
return writeResultFromContents(
|
|
1342
|
+
ctx,
|
|
1343
|
+
params.owner,
|
|
1344
|
+
params.repo,
|
|
1345
|
+
params.branch,
|
|
1346
|
+
params.path,
|
|
1347
|
+
response,
|
|
1348
|
+
false
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
async pushFiles(ctx, params) {
|
|
1352
|
+
const entries = readPushEntries(params.files);
|
|
1353
|
+
const { owner, repo } = params;
|
|
1354
|
+
const refResponse = await ctx.http.request({
|
|
1355
|
+
method: "GET",
|
|
1356
|
+
path: `${REPO_PATH}/git/ref/heads/{branch}`,
|
|
1357
|
+
pathParams: { owner, repo, branch: params.branch },
|
|
1358
|
+
headers: GITHUB_HEADERS
|
|
1359
|
+
});
|
|
1360
|
+
const commitSha = confirmedSha(
|
|
1361
|
+
ctx.http,
|
|
1362
|
+
refResponse,
|
|
1363
|
+
asRecord(asRecord(refResponse.json).object).sha
|
|
1364
|
+
);
|
|
1365
|
+
const commitResponse = await ctx.http.request({
|
|
1366
|
+
method: "GET",
|
|
1367
|
+
path: `${REPO_PATH}/git/commits/{sha}`,
|
|
1368
|
+
pathParams: { owner, repo, sha: commitSha },
|
|
1369
|
+
headers: GITHUB_HEADERS
|
|
1370
|
+
});
|
|
1371
|
+
const baseTree = confirmedSha(
|
|
1372
|
+
ctx.http,
|
|
1373
|
+
commitResponse,
|
|
1374
|
+
asRecord(asRecord(commitResponse.json).tree).sha
|
|
1375
|
+
);
|
|
1376
|
+
const parentTree = await ctx.http.request({
|
|
1377
|
+
method: "GET",
|
|
1378
|
+
path: `${REPO_PATH}/git/trees/{sha}`,
|
|
1379
|
+
pathParams: { owner, repo, sha: baseTree },
|
|
1380
|
+
headers: GITHUB_HEADERS,
|
|
1381
|
+
query: { recursive: "1" }
|
|
1382
|
+
});
|
|
1383
|
+
const modes = resolveFileModes(ctx.http, parentTree, entries);
|
|
1384
|
+
const tree = [];
|
|
1385
|
+
for (const [index, entry] of entries.entries()) {
|
|
1386
|
+
tree.push(await this.treeEntry(ctx, owner, repo, entry, modes[index]));
|
|
1387
|
+
}
|
|
1388
|
+
const treeResponse = await ctx.http.request({
|
|
1389
|
+
method: "POST",
|
|
1390
|
+
path: `${REPO_PATH}/git/trees`,
|
|
1391
|
+
...base(owner, repo),
|
|
1392
|
+
body: { base_tree: baseTree, tree }
|
|
1393
|
+
});
|
|
1394
|
+
const newTree = confirmedSha(ctx.http, treeResponse, asRecord(treeResponse.json).sha);
|
|
1395
|
+
const newCommitResponse = await ctx.http.request({
|
|
1396
|
+
method: "POST",
|
|
1397
|
+
path: `${REPO_PATH}/git/commits`,
|
|
1398
|
+
...base(owner, repo),
|
|
1399
|
+
body: { message: params.message, tree: newTree, parents: [commitSha] }
|
|
1400
|
+
});
|
|
1401
|
+
const newCommit = asRecord(newCommitResponse.json);
|
|
1402
|
+
const newCommitSha = confirmedSha(ctx.http, newCommitResponse, newCommit.sha);
|
|
1403
|
+
const updated = await ctx.http.request({
|
|
1404
|
+
method: "PATCH",
|
|
1405
|
+
path: `${REPO_PATH}/git/refs/heads/{branch}`,
|
|
1406
|
+
pathParams: { owner, repo, branch: params.branch },
|
|
1407
|
+
headers: GITHUB_HEADERS,
|
|
1408
|
+
body: { sha: newCommitSha, force: false }
|
|
1409
|
+
});
|
|
1410
|
+
if (asRecord(asRecord(updated.json).object).sha !== newCommitSha) {
|
|
1411
|
+
throw ctx.http.invalidResponse(
|
|
1412
|
+
updated,
|
|
1413
|
+
"The provider did not confirm the updated branch tip"
|
|
1414
|
+
);
|
|
1415
|
+
}
|
|
1416
|
+
return buildFileWriteResult({
|
|
1417
|
+
owner,
|
|
1418
|
+
repo,
|
|
1419
|
+
branch: params.branch,
|
|
1420
|
+
paths: entries.map((entry) => entry.path),
|
|
1421
|
+
commitSha: newCommitSha,
|
|
1422
|
+
commitHtmlUrl: str(newCommit.html_url) ?? ""
|
|
1423
|
+
});
|
|
1424
|
+
}
|
|
1425
|
+
/* ---------------------------------------------------------------------- */
|
|
1426
|
+
/* Branch / Commit */
|
|
1427
|
+
/* ---------------------------------------------------------------------- */
|
|
1428
|
+
async listBranches(ctx, params) {
|
|
1429
|
+
const entries = await collectPages(
|
|
1430
|
+
ctx.http,
|
|
1431
|
+
{ method: "GET", path: `${REPO_PATH}/branches`, ...base(params.owner, params.repo) },
|
|
1432
|
+
{
|
|
1433
|
+
maxPages: params.maxPages,
|
|
1434
|
+
selectItems: (raw) => asArray(raw),
|
|
1435
|
+
keyOf: (raw) => listKey(asRecord(raw).name)
|
|
1436
|
+
}
|
|
1437
|
+
);
|
|
1438
|
+
return entries.map((entry) => mapBranch(params.owner, params.repo, entry, ctx.webBaseUrl));
|
|
1439
|
+
}
|
|
1440
|
+
async createBranch(ctx, params) {
|
|
1441
|
+
const sha = isShaLike(params.from) ? params.from : await this.resolveBranchSha(ctx, params.owner, params.repo, params.from);
|
|
1442
|
+
const response = await ctx.http.request({
|
|
1443
|
+
method: "POST",
|
|
1444
|
+
path: `${REPO_PATH}/git/refs`,
|
|
1445
|
+
...base(params.owner, params.repo),
|
|
1446
|
+
body: { ref: `refs/heads/${params.name}`, sha }
|
|
1447
|
+
});
|
|
1448
|
+
const objectSha = confirmedSha(
|
|
1449
|
+
ctx.http,
|
|
1450
|
+
response,
|
|
1451
|
+
asRecord(asRecord(response.json).object).sha
|
|
1452
|
+
);
|
|
1453
|
+
const branch = {
|
|
1454
|
+
provider: "github",
|
|
1455
|
+
owner: params.owner,
|
|
1456
|
+
repo: params.repo,
|
|
1457
|
+
name: params.name,
|
|
1458
|
+
commit_sha: objectSha
|
|
1459
|
+
};
|
|
1460
|
+
const htmlUrl = deriveBranchHtmlUrl(ctx.webBaseUrl, params.owner, params.repo, params.name);
|
|
1461
|
+
if (htmlUrl !== void 0) {
|
|
1462
|
+
branch.html_url = htmlUrl;
|
|
1463
|
+
}
|
|
1464
|
+
return branch;
|
|
1465
|
+
}
|
|
1466
|
+
async listCommits(ctx, params) {
|
|
1467
|
+
const entries = await collectPages(
|
|
1468
|
+
ctx.http,
|
|
1469
|
+
{ method: "GET", path: `${REPO_PATH}/commits`, ...base(params.owner, params.repo) },
|
|
1470
|
+
{
|
|
1471
|
+
maxPages: params.maxPages,
|
|
1472
|
+
query: { sha: params.branch },
|
|
1473
|
+
selectItems: (raw) => asArray(raw),
|
|
1474
|
+
keyOf: (raw) => listKey(asRecord(raw).sha)
|
|
1475
|
+
}
|
|
1476
|
+
);
|
|
1477
|
+
return entries.map((entry) => mapCommit(params.owner, params.repo, entry));
|
|
1478
|
+
}
|
|
1479
|
+
async getCommit(ctx, params) {
|
|
1480
|
+
const response = await ctx.http.request({
|
|
1481
|
+
method: "GET",
|
|
1482
|
+
path: `${REPO_PATH}/commits/{sha}`,
|
|
1483
|
+
pathParams: { owner: params.owner, repo: params.repo, sha: params.sha },
|
|
1484
|
+
headers: GITHUB_HEADERS
|
|
1485
|
+
});
|
|
1486
|
+
return mapCommit(params.owner, params.repo, response.json);
|
|
1487
|
+
}
|
|
1488
|
+
/* ---------------------------------------------------------------------- */
|
|
1489
|
+
/* Internals */
|
|
1490
|
+
/* ---------------------------------------------------------------------- */
|
|
1491
|
+
/** Shared comment listing used by both issue and pull-request domains. */
|
|
1492
|
+
async listComments(ctx, params, path) {
|
|
1493
|
+
const entries = await collectPages(
|
|
1494
|
+
ctx.http,
|
|
1495
|
+
{
|
|
1496
|
+
method: "GET",
|
|
1497
|
+
path,
|
|
1498
|
+
pathParams: {
|
|
1499
|
+
owner: params.owner,
|
|
1500
|
+
repo: params.repo,
|
|
1501
|
+
number: String(params.number)
|
|
1502
|
+
},
|
|
1503
|
+
headers: GITHUB_HEADERS
|
|
1504
|
+
},
|
|
1505
|
+
{
|
|
1506
|
+
maxPages: params.maxPages,
|
|
1507
|
+
selectItems: (raw) => asArray(raw),
|
|
1508
|
+
keyOf: (raw) => listKey(asRecord(raw).id)
|
|
1509
|
+
}
|
|
1510
|
+
);
|
|
1511
|
+
return entries.map((entry) => mapComment(params.owner, params.repo, entry));
|
|
1512
|
+
}
|
|
1513
|
+
/** Shared comment creation used by both comment domains. */
|
|
1514
|
+
async createComment(ctx, params, path) {
|
|
1515
|
+
const response = await ctx.http.request({
|
|
1516
|
+
method: "POST",
|
|
1517
|
+
path,
|
|
1518
|
+
pathParams: {
|
|
1519
|
+
owner: params.owner,
|
|
1520
|
+
repo: params.repo,
|
|
1521
|
+
number: String(params.number)
|
|
1522
|
+
},
|
|
1523
|
+
headers: GITHUB_HEADERS,
|
|
1524
|
+
body: { body: params.body }
|
|
1525
|
+
});
|
|
1526
|
+
return mapComment(params.owner, params.repo, response.json);
|
|
1527
|
+
}
|
|
1528
|
+
/** PUTs a file through the `contents` upsert endpoint. */
|
|
1529
|
+
async putContents(ctx, params) {
|
|
1530
|
+
const response = await ctx.http.request({
|
|
1531
|
+
method: "PUT",
|
|
1532
|
+
path: `${REPO_PATH}/contents/{path}`,
|
|
1533
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
1534
|
+
headers: GITHUB_HEADERS,
|
|
1535
|
+
body: {
|
|
1536
|
+
message: params.message,
|
|
1537
|
+
content: contentsBase64(params.content, params.encoding, "github"),
|
|
1538
|
+
branch: params.branch,
|
|
1539
|
+
sha: params.sha
|
|
1540
|
+
}
|
|
1541
|
+
});
|
|
1542
|
+
return writeResultFromContents(
|
|
1543
|
+
ctx,
|
|
1544
|
+
params.owner,
|
|
1545
|
+
params.repo,
|
|
1546
|
+
params.branch,
|
|
1547
|
+
params.path,
|
|
1548
|
+
response,
|
|
1549
|
+
true
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
/** Builds one tree entry, creating a blob first for base64 payloads. */
|
|
1553
|
+
async treeEntry(ctx, owner, repo, entry, mode) {
|
|
1554
|
+
if (entry.action === "delete") {
|
|
1555
|
+
return { path: entry.path, mode, type: "blob", sha: null };
|
|
1556
|
+
}
|
|
1557
|
+
if (entry.encoding === "utf8") {
|
|
1558
|
+
return {
|
|
1559
|
+
path: entry.path,
|
|
1560
|
+
mode,
|
|
1561
|
+
type: "blob",
|
|
1562
|
+
content: entry.content
|
|
1563
|
+
};
|
|
1564
|
+
}
|
|
1565
|
+
const blobResponse = await ctx.http.request({
|
|
1566
|
+
method: "POST",
|
|
1567
|
+
path: `${REPO_PATH}/git/blobs`,
|
|
1568
|
+
...base(owner, repo),
|
|
1569
|
+
body: { content: entry.content, encoding: "base64" }
|
|
1570
|
+
});
|
|
1571
|
+
const blobSha = confirmedSha(ctx.http, blobResponse, asRecord(blobResponse.json).sha);
|
|
1572
|
+
return { path: entry.path, mode, type: "blob", sha: blobSha };
|
|
1573
|
+
}
|
|
1574
|
+
/** Reads the commit sha a branch currently points at. */
|
|
1575
|
+
async resolveBranchSha(ctx, owner, repo, branch) {
|
|
1576
|
+
const response = await ctx.http.request({
|
|
1577
|
+
method: "GET",
|
|
1578
|
+
path: `${REPO_PATH}/git/ref/heads/{branch}`,
|
|
1579
|
+
pathParams: { owner, repo, branch },
|
|
1580
|
+
headers: GITHUB_HEADERS
|
|
1581
|
+
});
|
|
1582
|
+
const sha = str(asRecord(asRecord(response.json).object).sha);
|
|
1583
|
+
if (sha === void 0) {
|
|
1584
|
+
throw invalidArgumentError(
|
|
1585
|
+
`Cannot resolve "${branch}": the ref response carried no commit sha.`,
|
|
1586
|
+
"github"
|
|
1587
|
+
);
|
|
1588
|
+
}
|
|
1589
|
+
return sha;
|
|
1590
|
+
}
|
|
1591
|
+
};
|
|
1592
|
+
var githubAdapter = new GitHubAdapter();
|
|
1593
|
+
|
|
1594
|
+
// src/adapters/gitee/map.ts
|
|
1595
|
+
var PROVIDER2 = "gitee";
|
|
1596
|
+
var PUSH_ACTIONS2 = ["create", "update", "delete"];
|
|
1597
|
+
function asRecord2(value) {
|
|
1598
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
1599
|
+
}
|
|
1600
|
+
function asArray2(value) {
|
|
1601
|
+
return Array.isArray(value) ? value : [];
|
|
1602
|
+
}
|
|
1603
|
+
function str2(value) {
|
|
1604
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
1605
|
+
}
|
|
1606
|
+
function num2(value) {
|
|
1607
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
1608
|
+
}
|
|
1609
|
+
function listKey2(value) {
|
|
1610
|
+
if (typeof value === "string") {
|
|
1611
|
+
return value;
|
|
1612
|
+
}
|
|
1613
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
1614
|
+
return String(value);
|
|
1615
|
+
}
|
|
1616
|
+
return "";
|
|
1617
|
+
}
|
|
1618
|
+
function bool2(value) {
|
|
1619
|
+
return typeof value === "boolean" ? value : void 0;
|
|
1620
|
+
}
|
|
1621
|
+
function normalizeLabels2(raw) {
|
|
1622
|
+
const out = [];
|
|
1623
|
+
for (const entry of asArray2(raw)) {
|
|
1624
|
+
if (typeof entry === "string" && entry.length > 0) {
|
|
1625
|
+
out.push(entry);
|
|
1626
|
+
continue;
|
|
1627
|
+
}
|
|
1628
|
+
const name = str2(asRecord2(entry).name);
|
|
1629
|
+
if (name !== void 0) {
|
|
1630
|
+
out.push(name);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
return out;
|
|
1634
|
+
}
|
|
1635
|
+
function mapUser2(raw) {
|
|
1636
|
+
const record = asRecord2(raw);
|
|
1637
|
+
const login = str2(record.login);
|
|
1638
|
+
const name = str2(record.name);
|
|
1639
|
+
if (login === void 0 && name === void 0) {
|
|
1640
|
+
return void 0;
|
|
1641
|
+
}
|
|
1642
|
+
const user = { login: login ?? name };
|
|
1643
|
+
if (name !== void 0) {
|
|
1644
|
+
user.name = name;
|
|
1645
|
+
}
|
|
1646
|
+
const avatar = str2(record.avatar_url);
|
|
1647
|
+
if (avatar !== void 0) {
|
|
1648
|
+
user.avatar_url = avatar;
|
|
1649
|
+
}
|
|
1650
|
+
return user;
|
|
1651
|
+
}
|
|
1652
|
+
function mapUsers2(raw) {
|
|
1653
|
+
const users = asArray2(raw).map((entry) => mapUser2(entry)).filter((entry) => entry !== void 0);
|
|
1654
|
+
return users.length > 0 ? users : void 0;
|
|
1655
|
+
}
|
|
1656
|
+
function mapIssueState2(raw) {
|
|
1657
|
+
return raw === "open" || raw === "progressing" ? "open" : "closed";
|
|
1658
|
+
}
|
|
1659
|
+
function mapPullRequestState2(raw) {
|
|
1660
|
+
return raw === "open" ? "open" : "closed";
|
|
1661
|
+
}
|
|
1662
|
+
function joinLabels2(labels) {
|
|
1663
|
+
return labels === void 0 || labels.length === 0 ? void 0 : labels.join(",");
|
|
1664
|
+
}
|
|
1665
|
+
function mapIssue2(owner, repo, raw) {
|
|
1666
|
+
const record = asRecord2(raw);
|
|
1667
|
+
const issue = {
|
|
1668
|
+
provider: PROVIDER2,
|
|
1669
|
+
owner,
|
|
1670
|
+
repo,
|
|
1671
|
+
...issueNumberFields(listKey2(record.number)),
|
|
1672
|
+
title: str2(record.title) ?? "",
|
|
1673
|
+
state: mapIssueState2(record.state),
|
|
1674
|
+
html_url: str2(record.html_url) ?? "",
|
|
1675
|
+
labels: normalizeLabels2(record.labels),
|
|
1676
|
+
created_at: str2(record.created_at) ?? "",
|
|
1677
|
+
updated_at: str2(record.updated_at) ?? ""
|
|
1678
|
+
};
|
|
1679
|
+
const body = str2(record.body);
|
|
1680
|
+
if (body !== void 0) {
|
|
1681
|
+
issue.body = body;
|
|
1682
|
+
}
|
|
1683
|
+
const user = mapUser2(record.user);
|
|
1684
|
+
if (user !== void 0) {
|
|
1685
|
+
issue.user = user;
|
|
1686
|
+
}
|
|
1687
|
+
const assignees = mapUsers2(record.assignees);
|
|
1688
|
+
if (assignees !== void 0) {
|
|
1689
|
+
issue.assignees = assignees;
|
|
1690
|
+
}
|
|
1691
|
+
const comments = num2(record.comments);
|
|
1692
|
+
if (comments !== void 0) {
|
|
1693
|
+
issue.comments_count = comments;
|
|
1694
|
+
}
|
|
1695
|
+
const closedAt = str2(record.finished_at) ?? str2(record.closed_at);
|
|
1696
|
+
if (closedAt !== void 0) {
|
|
1697
|
+
issue.closed_at = closedAt;
|
|
1698
|
+
}
|
|
1699
|
+
return issue;
|
|
1700
|
+
}
|
|
1701
|
+
function mapRef2(raw) {
|
|
1702
|
+
const record = asRecord2(raw);
|
|
1703
|
+
const ref = str2(record.ref);
|
|
1704
|
+
const sha = str2(record.sha);
|
|
1705
|
+
return ref !== void 0 && sha !== void 0 ? { ref, sha } : void 0;
|
|
1706
|
+
}
|
|
1707
|
+
function mapPullRequest2(owner, repo, raw, base2, number) {
|
|
1708
|
+
const record = asRecord2(raw);
|
|
1709
|
+
const mergedAt = str2(record.merged_at);
|
|
1710
|
+
const state = str2(record.state);
|
|
1711
|
+
const merged = mergedAt !== void 0 || state === "merged";
|
|
1712
|
+
const draft = {
|
|
1713
|
+
base: base2,
|
|
1714
|
+
provider: PROVIDER2,
|
|
1715
|
+
owner,
|
|
1716
|
+
repo,
|
|
1717
|
+
number,
|
|
1718
|
+
title: str2(record.title) ?? "",
|
|
1719
|
+
state: merged ? "closed" : mapPullRequestState2(record.state),
|
|
1720
|
+
merged,
|
|
1721
|
+
html_url: str2(record.html_url) ?? "",
|
|
1722
|
+
created_at: str2(record.created_at) ?? "",
|
|
1723
|
+
updated_at: str2(record.updated_at) ?? ""
|
|
1724
|
+
};
|
|
1725
|
+
const head = mapRef2(record.head);
|
|
1726
|
+
if (head !== void 0) {
|
|
1727
|
+
draft.head = head;
|
|
1728
|
+
}
|
|
1729
|
+
if (mergedAt !== void 0) {
|
|
1730
|
+
draft.merged_at = mergedAt;
|
|
1731
|
+
}
|
|
1732
|
+
const body = str2(record.body);
|
|
1733
|
+
if (body !== void 0) {
|
|
1734
|
+
draft.body = body;
|
|
1735
|
+
}
|
|
1736
|
+
const isDraft = bool2(record.draft);
|
|
1737
|
+
if (isDraft !== void 0) {
|
|
1738
|
+
draft.draft = isDraft;
|
|
1739
|
+
}
|
|
1740
|
+
const mergeable = bool2(record.mergeable);
|
|
1741
|
+
if (mergeable !== void 0) {
|
|
1742
|
+
draft.mergeable = mergeable;
|
|
1743
|
+
}
|
|
1744
|
+
const user = mapUser2(record.user);
|
|
1745
|
+
if (user !== void 0) {
|
|
1746
|
+
draft.user = user;
|
|
1747
|
+
}
|
|
1748
|
+
for (const key of ["additions", "deletions", "changed_files"]) {
|
|
1749
|
+
const value = num2(record[key]);
|
|
1750
|
+
if (value !== void 0) {
|
|
1751
|
+
draft[key] = value;
|
|
1752
|
+
}
|
|
1753
|
+
}
|
|
1754
|
+
return draft;
|
|
1755
|
+
}
|
|
1756
|
+
function stripGitSuffix(url) {
|
|
1757
|
+
return url.replace(/\.git$/, "");
|
|
1758
|
+
}
|
|
1759
|
+
function mapRepository2(owner, repo, raw) {
|
|
1760
|
+
const record = asRecord2(raw);
|
|
1761
|
+
const rawHtmlUrl = str2(record.html_url);
|
|
1762
|
+
const repository = {
|
|
1763
|
+
provider: PROVIDER2,
|
|
1764
|
+
owner: str2(asRecord2(record.namespace).path) ?? owner,
|
|
1765
|
+
repo,
|
|
1766
|
+
name: str2(record.name) ?? repo,
|
|
1767
|
+
html_url: rawHtmlUrl === void 0 ? "" : stripGitSuffix(rawHtmlUrl),
|
|
1768
|
+
private: bool2(record.private) === true,
|
|
1769
|
+
default_branch: str2(record.default_branch) ?? ""
|
|
1770
|
+
};
|
|
1771
|
+
const fullName = str2(record.full_name);
|
|
1772
|
+
if (fullName !== void 0) {
|
|
1773
|
+
repository.full_name = fullName;
|
|
1774
|
+
}
|
|
1775
|
+
const description = str2(record.description);
|
|
1776
|
+
if (description !== void 0) {
|
|
1777
|
+
repository.description = description;
|
|
1778
|
+
}
|
|
1779
|
+
const fork = bool2(record.fork);
|
|
1780
|
+
if (fork !== void 0) {
|
|
1781
|
+
repository.fork = fork;
|
|
1782
|
+
}
|
|
1783
|
+
const language = str2(record.language);
|
|
1784
|
+
if (language !== void 0) {
|
|
1785
|
+
repository.language = language;
|
|
1786
|
+
}
|
|
1787
|
+
const createdAt = str2(record.created_at);
|
|
1788
|
+
if (createdAt !== void 0) {
|
|
1789
|
+
repository.created_at = createdAt;
|
|
1790
|
+
}
|
|
1791
|
+
const updatedAt = str2(record.updated_at);
|
|
1792
|
+
if (updatedAt !== void 0) {
|
|
1793
|
+
repository.updated_at = updatedAt;
|
|
1794
|
+
}
|
|
1795
|
+
return repository;
|
|
1796
|
+
}
|
|
1797
|
+
function mapFileContent2(owner, repo, raw) {
|
|
1798
|
+
const record = asRecord2(raw);
|
|
1799
|
+
const content = {
|
|
1800
|
+
provider: PROVIDER2,
|
|
1801
|
+
owner,
|
|
1802
|
+
repo,
|
|
1803
|
+
path: str2(record.path) ?? "",
|
|
1804
|
+
content: typeof record.content === "string" ? record.content : "",
|
|
1805
|
+
encoding: str2(record.encoding) ?? "base64"
|
|
1806
|
+
};
|
|
1807
|
+
const sha = str2(record.sha);
|
|
1808
|
+
if (sha !== void 0) {
|
|
1809
|
+
content.sha = sha;
|
|
1810
|
+
}
|
|
1811
|
+
const size = num2(record.size);
|
|
1812
|
+
if (size !== void 0) {
|
|
1813
|
+
content.size = size;
|
|
1814
|
+
}
|
|
1815
|
+
const htmlUrl = str2(record.html_url);
|
|
1816
|
+
if (htmlUrl !== void 0) {
|
|
1817
|
+
content.html_url = htmlUrl;
|
|
1818
|
+
}
|
|
1819
|
+
const type = str2(record.type);
|
|
1820
|
+
if (type !== void 0) {
|
|
1821
|
+
content.type = type;
|
|
1822
|
+
}
|
|
1823
|
+
return content;
|
|
1824
|
+
}
|
|
1825
|
+
function deriveBranchHtmlUrl2(webBaseUrl, owner, repo, name) {
|
|
1826
|
+
const base2 = webBaseUrl.endsWith("/") ? webBaseUrl.slice(0, -1) : webBaseUrl;
|
|
1827
|
+
if (base2.length === 0) {
|
|
1828
|
+
return void 0;
|
|
1829
|
+
}
|
|
1830
|
+
return `${base2}/${owner}/${repo}/tree/${name}`;
|
|
1831
|
+
}
|
|
1832
|
+
function mapBranch2(owner, repo, raw, webBaseUrl) {
|
|
1833
|
+
const record = asRecord2(raw);
|
|
1834
|
+
const name = str2(record.name) ?? "";
|
|
1835
|
+
const branch = {
|
|
1836
|
+
provider: PROVIDER2,
|
|
1837
|
+
owner,
|
|
1838
|
+
repo,
|
|
1839
|
+
name,
|
|
1840
|
+
commit_sha: str2(asRecord2(record.commit).sha) ?? ""
|
|
1841
|
+
};
|
|
1842
|
+
const isProtected = bool2(record.protected);
|
|
1843
|
+
if (isProtected !== void 0) {
|
|
1844
|
+
branch.protected = isProtected;
|
|
1845
|
+
}
|
|
1846
|
+
const htmlUrl = deriveBranchHtmlUrl2(webBaseUrl, owner, repo, name);
|
|
1847
|
+
if (htmlUrl !== void 0) {
|
|
1848
|
+
branch.html_url = htmlUrl;
|
|
1849
|
+
}
|
|
1850
|
+
return branch;
|
|
1851
|
+
}
|
|
1852
|
+
function mapCommitAuthor2(raw) {
|
|
1853
|
+
const record = asRecord2(raw);
|
|
1854
|
+
const name = str2(record.name);
|
|
1855
|
+
if (name === void 0) {
|
|
1856
|
+
return void 0;
|
|
1857
|
+
}
|
|
1858
|
+
return {
|
|
1859
|
+
name,
|
|
1860
|
+
email: str2(record.email) ?? "",
|
|
1861
|
+
date: str2(record.date) ?? ""
|
|
1862
|
+
};
|
|
1863
|
+
}
|
|
1864
|
+
function mapCommit2(owner, repo, raw) {
|
|
1865
|
+
const record = asRecord2(raw);
|
|
1866
|
+
const payload = asRecord2(record.commit);
|
|
1867
|
+
const commit = {
|
|
1868
|
+
provider: PROVIDER2,
|
|
1869
|
+
owner,
|
|
1870
|
+
repo,
|
|
1871
|
+
sha: str2(record.sha) ?? "",
|
|
1872
|
+
message: str2(payload.message) ?? "",
|
|
1873
|
+
html_url: str2(record.html_url) ?? ""
|
|
1874
|
+
};
|
|
1875
|
+
const author = mapCommitAuthor2(payload.author);
|
|
1876
|
+
if (author !== void 0) {
|
|
1877
|
+
commit.author = author;
|
|
1878
|
+
}
|
|
1879
|
+
const committer = mapCommitAuthor2(payload.committer);
|
|
1880
|
+
if (committer !== void 0) {
|
|
1881
|
+
commit.committer = committer;
|
|
1882
|
+
}
|
|
1883
|
+
const parents = asArray2(record.parents).map((entry) => str2(asRecord2(entry).sha)).filter((entry) => entry !== void 0);
|
|
1884
|
+
if (parents.length > 0) {
|
|
1885
|
+
commit.parents = parents;
|
|
1886
|
+
}
|
|
1887
|
+
return commit;
|
|
1888
|
+
}
|
|
1889
|
+
function mapComment2(owner, repo, raw, parentUrl) {
|
|
1890
|
+
const record = asRecord2(raw);
|
|
1891
|
+
const comment = {
|
|
1892
|
+
provider: PROVIDER2,
|
|
1893
|
+
owner,
|
|
1894
|
+
repo,
|
|
1895
|
+
id: num2(record.id) ?? Number(str2(record.id) ?? 0),
|
|
1896
|
+
body: typeof record.body === "string" ? record.body : "",
|
|
1897
|
+
created_at: str2(record.created_at) ?? ""
|
|
1898
|
+
};
|
|
1899
|
+
const htmlUrl = str2(record.html_url) ?? parentUrl;
|
|
1900
|
+
if (htmlUrl !== void 0 && htmlUrl.length > 0) {
|
|
1901
|
+
comment.html_url = htmlUrl;
|
|
1902
|
+
}
|
|
1903
|
+
const updatedAt = str2(record.updated_at);
|
|
1904
|
+
if (updatedAt !== void 0) {
|
|
1905
|
+
comment.updated_at = updatedAt;
|
|
1906
|
+
}
|
|
1907
|
+
const user = mapUser2(record.user);
|
|
1908
|
+
if (user !== void 0) {
|
|
1909
|
+
comment.user = user;
|
|
1910
|
+
}
|
|
1911
|
+
return comment;
|
|
1912
|
+
}
|
|
1913
|
+
function buildFileWriteResult2(inputs) {
|
|
1914
|
+
const result = {
|
|
1915
|
+
provider: PROVIDER2,
|
|
1916
|
+
owner: inputs.owner,
|
|
1917
|
+
repo: inputs.repo,
|
|
1918
|
+
commit_sha: inputs.commitSha,
|
|
1919
|
+
commit_html_url: inputs.commitHtmlUrl,
|
|
1920
|
+
paths: [...inputs.paths],
|
|
1921
|
+
branch: inputs.branch ?? ""
|
|
1922
|
+
};
|
|
1923
|
+
if (inputs.fileHtmlUrls !== void 0) {
|
|
1924
|
+
result.file_html_urls = inputs.fileHtmlUrls;
|
|
1925
|
+
}
|
|
1926
|
+
return result;
|
|
1927
|
+
}
|
|
1928
|
+
function deriveParentHtmlUrl(webBaseUrl, owner, repo, kind, number) {
|
|
1929
|
+
const base2 = webBaseUrl.endsWith("/") ? webBaseUrl.slice(0, -1) : webBaseUrl;
|
|
1930
|
+
if (base2.length === 0) {
|
|
1931
|
+
return void 0;
|
|
1932
|
+
}
|
|
1933
|
+
return `${base2}/${owner}/${repo}/${kind}/${String(number)}`;
|
|
1934
|
+
}
|
|
1935
|
+
var BASE64_ALPHABET2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
1936
|
+
function isValidBase642(value) {
|
|
1937
|
+
const compact = value.replace(/\s+/g, "");
|
|
1938
|
+
const body = compact.replace(/={0,2}$/, "");
|
|
1939
|
+
if (body.length === 0 || body.length % 4 === 1) {
|
|
1940
|
+
return false;
|
|
1941
|
+
}
|
|
1942
|
+
for (const char of body) {
|
|
1943
|
+
if (!BASE64_ALPHABET2.includes(char)) {
|
|
1944
|
+
return false;
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
return true;
|
|
1948
|
+
}
|
|
1949
|
+
function readPushEntries2(files) {
|
|
1950
|
+
if (!Array.isArray(files) || files.length === 0) {
|
|
1951
|
+
throw invalidArgumentError(
|
|
1952
|
+
"Invalid `files`: pushFiles requires at least one entry (an empty batch would create an empty commit).",
|
|
1953
|
+
PROVIDER2,
|
|
1954
|
+
"files.pushFiles"
|
|
1955
|
+
);
|
|
1956
|
+
}
|
|
1957
|
+
return files.map((raw, index) => {
|
|
1958
|
+
const file = asRecord2(raw);
|
|
1959
|
+
const path = file.path;
|
|
1960
|
+
if (typeof path !== "string" || path.trim().length === 0) {
|
|
1961
|
+
throw invalidArgumentError(
|
|
1962
|
+
`Invalid \`files[${index}].path\`: expected a non-empty string.`,
|
|
1963
|
+
PROVIDER2,
|
|
1964
|
+
"files.pushFiles"
|
|
1965
|
+
);
|
|
1966
|
+
}
|
|
1967
|
+
const rawAction = file.action;
|
|
1968
|
+
if (typeof rawAction !== "string" || !PUSH_ACTIONS2.includes(rawAction)) {
|
|
1969
|
+
throw invalidArgumentError(
|
|
1970
|
+
`Invalid \`files[${index}].action\`: expected one of ${PUSH_ACTIONS2.join(" | ")}, received ${describe2(rawAction)}. pushFiles does not provide upsert.`,
|
|
1971
|
+
PROVIDER2,
|
|
1972
|
+
"files.pushFiles"
|
|
1973
|
+
);
|
|
1974
|
+
}
|
|
1975
|
+
const action = rawAction;
|
|
1976
|
+
const rawEncoding = file.encoding ?? "utf8";
|
|
1977
|
+
if (rawEncoding !== "utf8" && rawEncoding !== "base64") {
|
|
1978
|
+
throw invalidArgumentError(
|
|
1979
|
+
`Invalid \`files[${index}].encoding\`: expected utf8 | base64, received ${describe2(rawEncoding)}.`,
|
|
1980
|
+
PROVIDER2,
|
|
1981
|
+
"files.pushFiles"
|
|
1982
|
+
);
|
|
1983
|
+
}
|
|
1984
|
+
const encoding = rawEncoding;
|
|
1985
|
+
if (action === "delete") {
|
|
1986
|
+
if (file.content !== void 0) {
|
|
1987
|
+
throw invalidArgumentError(
|
|
1988
|
+
`Invalid \`files[${index}]\`: action 'delete' must not carry content.`,
|
|
1989
|
+
PROVIDER2,
|
|
1990
|
+
"files.pushFiles"
|
|
1991
|
+
);
|
|
1992
|
+
}
|
|
1993
|
+
return { path, action, encoding };
|
|
1994
|
+
}
|
|
1995
|
+
const content = file.content;
|
|
1996
|
+
if (typeof content !== "string") {
|
|
1997
|
+
throw invalidArgumentError(
|
|
1998
|
+
`Invalid \`files[${index}].content\`: action '${action}' requires content.`,
|
|
1999
|
+
PROVIDER2,
|
|
2000
|
+
"files.pushFiles"
|
|
2001
|
+
);
|
|
2002
|
+
}
|
|
2003
|
+
if (encoding === "base64" && !isValidBase642(content)) {
|
|
2004
|
+
throw invalidArgumentError(
|
|
2005
|
+
`Invalid \`files[${index}].content\`: not a valid base64 payload.`,
|
|
2006
|
+
PROVIDER2,
|
|
2007
|
+
"files.pushFiles"
|
|
2008
|
+
);
|
|
2009
|
+
}
|
|
2010
|
+
return { path, action, content, encoding };
|
|
2011
|
+
});
|
|
2012
|
+
}
|
|
2013
|
+
function toGiteeEncoding(encoding) {
|
|
2014
|
+
return encoding === "base64" ? "base64" : "text";
|
|
2015
|
+
}
|
|
2016
|
+
function describe2(value) {
|
|
2017
|
+
if (value === void 0) {
|
|
2018
|
+
return "undefined";
|
|
2019
|
+
}
|
|
2020
|
+
if (typeof value === "string") {
|
|
2021
|
+
return value.length === 0 ? "an empty string" : `"${value}"`;
|
|
2022
|
+
}
|
|
2023
|
+
return typeof value;
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
// src/adapters/gitee/capabilities.ts
|
|
2027
|
+
var capabilities2 = {
|
|
2028
|
+
"issues.list": "supported",
|
|
2029
|
+
"issues.get": "supported",
|
|
2030
|
+
"issues.create": "supported",
|
|
2031
|
+
"issues.update": "supported",
|
|
2032
|
+
"issues.listComments": "supported",
|
|
2033
|
+
"issues.createComment": "supported",
|
|
2034
|
+
"pulls.list": "supported",
|
|
2035
|
+
"pulls.get": "supported",
|
|
2036
|
+
"pulls.create": "supported",
|
|
2037
|
+
"pulls.merge": "supported",
|
|
2038
|
+
"pulls.listComments": "supported",
|
|
2039
|
+
"pulls.createComment": "supported",
|
|
2040
|
+
"repos.list": "supported",
|
|
2041
|
+
"repos.get": "supported",
|
|
2042
|
+
"repos.create": "supported",
|
|
2043
|
+
"repos.getFileContents": "supported",
|
|
2044
|
+
"files.create": "supported",
|
|
2045
|
+
"files.update": "supported",
|
|
2046
|
+
"files.delete": "supported",
|
|
2047
|
+
"files.pushFiles": "supported",
|
|
2048
|
+
"branches.list": "supported",
|
|
2049
|
+
"branches.create": "supported",
|
|
2050
|
+
"commits.list": "supported",
|
|
2051
|
+
"commits.get": "supported"
|
|
2052
|
+
};
|
|
2053
|
+
var paramCapabilities2 = {
|
|
2054
|
+
"pulls.list.headFilter": "supported",
|
|
2055
|
+
"pulls.list.milestoneFilter": "supported",
|
|
2056
|
+
"issues.list.milestoneFilter": "supported",
|
|
2057
|
+
"files.pushFiles.atomic": "supported"
|
|
2058
|
+
};
|
|
2059
|
+
|
|
2060
|
+
// src/adapters/gitee/adapter.ts
|
|
2061
|
+
var OPEN_STATES = ["open", "progressing"];
|
|
2062
|
+
var CLOSED_STATES = ["closed", "rejected"];
|
|
2063
|
+
var REPO_PATH2 = "/repos/{owner}/{repo}";
|
|
2064
|
+
function repoParams(owner, repo) {
|
|
2065
|
+
return { owner, repo };
|
|
2066
|
+
}
|
|
2067
|
+
function assertCreation2(params) {
|
|
2068
|
+
if (params.sha !== void 0) {
|
|
2069
|
+
throw invalidArgumentError(
|
|
2070
|
+
"Invalid `sha`: createFile creates a new file and must not receive a blob sha; use updateFile to replace an existing file.",
|
|
2071
|
+
"gitee"
|
|
2072
|
+
);
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
function assertUpdate2(params) {
|
|
2076
|
+
if (params.sha === void 0 || params.sha.length === 0) {
|
|
2077
|
+
throw invalidArgumentError(
|
|
2078
|
+
"Invalid `sha`: updateFile requires the current blob sha of the file being replaced.",
|
|
2079
|
+
"gitee"
|
|
2080
|
+
);
|
|
2081
|
+
}
|
|
2082
|
+
}
|
|
2083
|
+
function assertIssueMilestoneFilter(milestone) {
|
|
2084
|
+
if (milestone !== void 0) {
|
|
2085
|
+
throw invalidArgumentError(
|
|
2086
|
+
"Invalid `milestone`: Gitee filters issues by milestone title, not by number. Use listPullRequests for a numeric milestone filter, or filter labels instead.",
|
|
2087
|
+
"gitee"
|
|
2088
|
+
);
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
var GiteeAdapter = class {
|
|
2092
|
+
provider = "gitee";
|
|
2093
|
+
capabilities = capabilities2;
|
|
2094
|
+
paramCapabilities = paramCapabilities2;
|
|
2095
|
+
/* ---------------------------------------------------------------------- */
|
|
2096
|
+
/* Issue */
|
|
2097
|
+
/* ---------------------------------------------------------------------- */
|
|
2098
|
+
/**
|
|
2099
|
+
* Lists issues, fanning out across the platform's single-value states.
|
|
2100
|
+
*
|
|
2101
|
+
* Gitee's `open` excludes `progressing` and its `closed` excludes `rejected`,
|
|
2102
|
+
* so a single request would silently return a subset. An omitted state
|
|
2103
|
+
* follows GitHub's documented default (`open`) and therefore fans out too,
|
|
2104
|
+
* keeping "no state given" equivalent across providers.
|
|
2105
|
+
*
|
|
2106
|
+
* `maxPages` applies per platform state, so a fan-out can issue up to
|
|
2107
|
+
* `2 * maxPages` requests.
|
|
2108
|
+
*/
|
|
2109
|
+
async listIssues(ctx, params) {
|
|
2110
|
+
assertIssueMilestoneFilter(params.milestone);
|
|
2111
|
+
const states = resolveIssueStates(params.state);
|
|
2112
|
+
const collected = [];
|
|
2113
|
+
for (const state of states) {
|
|
2114
|
+
const entries = await this.collectIssues(ctx, params, state);
|
|
2115
|
+
collected.push(...entries);
|
|
2116
|
+
}
|
|
2117
|
+
const merged = dedupeBy(collected, (raw) => listKey2(asRecord2(raw).number));
|
|
2118
|
+
return merged.map((raw) => mapIssue2(params.owner, params.repo, raw));
|
|
2119
|
+
}
|
|
2120
|
+
async getIssue(ctx, params) {
|
|
2121
|
+
const response = await ctx.http.request({
|
|
2122
|
+
method: "GET",
|
|
2123
|
+
path: `${REPO_PATH2}/issues/{number}`,
|
|
2124
|
+
pathParams: { ...repoParams(params.owner, params.repo), number: String(params.number) }
|
|
2125
|
+
});
|
|
2126
|
+
return mapIssue2(params.owner, params.repo, response.json);
|
|
2127
|
+
}
|
|
2128
|
+
async createIssue(ctx, params) {
|
|
2129
|
+
const response = await ctx.http.request({
|
|
2130
|
+
method: "POST",
|
|
2131
|
+
path: "/repos/{owner}/issues",
|
|
2132
|
+
pathParams: { owner: params.owner },
|
|
2133
|
+
body: {
|
|
2134
|
+
repo: params.repo,
|
|
2135
|
+
title: params.title,
|
|
2136
|
+
body: params.body,
|
|
2137
|
+
labels: joinLabels2(params.labels),
|
|
2138
|
+
milestone: params.milestone
|
|
2139
|
+
}
|
|
2140
|
+
});
|
|
2141
|
+
return mapIssue2(params.owner, params.repo, response.json);
|
|
2142
|
+
}
|
|
2143
|
+
async updateIssue(ctx, params) {
|
|
2144
|
+
const response = await ctx.http.request({
|
|
2145
|
+
method: "PATCH",
|
|
2146
|
+
path: "/repos/{owner}/issues/{number}",
|
|
2147
|
+
pathParams: { owner: params.owner, number: String(params.number) },
|
|
2148
|
+
body: {
|
|
2149
|
+
repo: params.repo,
|
|
2150
|
+
title: params.title,
|
|
2151
|
+
body: params.body,
|
|
2152
|
+
state: params.state,
|
|
2153
|
+
labels: joinLabels2(params.labels),
|
|
2154
|
+
milestone: params.milestone
|
|
2155
|
+
}
|
|
2156
|
+
});
|
|
2157
|
+
return mapIssue2(params.owner, params.repo, response.json);
|
|
2158
|
+
}
|
|
2159
|
+
async listIssueComments(ctx, params) {
|
|
2160
|
+
const parentUrl = deriveParentHtmlUrl(
|
|
2161
|
+
ctx.webBaseUrl,
|
|
2162
|
+
params.owner,
|
|
2163
|
+
params.repo,
|
|
2164
|
+
"issues",
|
|
2165
|
+
params.number
|
|
2166
|
+
);
|
|
2167
|
+
return this.listComments(ctx, params, `${REPO_PATH2}/issues/{number}/comments`, parentUrl);
|
|
2168
|
+
}
|
|
2169
|
+
async createIssueComment(ctx, params) {
|
|
2170
|
+
const parentUrl = deriveParentHtmlUrl(
|
|
2171
|
+
ctx.webBaseUrl,
|
|
2172
|
+
params.owner,
|
|
2173
|
+
params.repo,
|
|
2174
|
+
"issues",
|
|
2175
|
+
params.number
|
|
2176
|
+
);
|
|
2177
|
+
return this.createComment(ctx, params, `${REPO_PATH2}/issues/{number}/comments`, parentUrl);
|
|
2178
|
+
}
|
|
2179
|
+
/* ---------------------------------------------------------------------- */
|
|
2180
|
+
/* Pull request */
|
|
2181
|
+
/* ---------------------------------------------------------------------- */
|
|
2182
|
+
async listPullRequests(ctx, params) {
|
|
2183
|
+
return collectPullRequests(ctx, params, mapPullRequest2, {
|
|
2184
|
+
states: params.state === "closed" ? ["closed", "merged"] : [params.state],
|
|
2185
|
+
query: {
|
|
2186
|
+
head: params.head,
|
|
2187
|
+
base: params.base,
|
|
2188
|
+
labels: joinLabels2(params.labels),
|
|
2189
|
+
milestone_number: params.milestone
|
|
2190
|
+
}
|
|
2191
|
+
});
|
|
2192
|
+
}
|
|
2193
|
+
async getPullRequest(ctx, params) {
|
|
2194
|
+
const response = await ctx.http.request({
|
|
2195
|
+
method: "GET",
|
|
2196
|
+
path: `${REPO_PATH2}/pulls/{number}`,
|
|
2197
|
+
pathParams: { ...repoParams(params.owner, params.repo), number: String(params.number) }
|
|
2198
|
+
});
|
|
2199
|
+
return pullFromResponse(mapPullRequest2, ctx, params, response, {
|
|
2200
|
+
expectedNumber: params.number
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
async createPullRequest(ctx, params) {
|
|
2204
|
+
const response = await ctx.http.request({
|
|
2205
|
+
method: "POST",
|
|
2206
|
+
path: `${REPO_PATH2}/pulls`,
|
|
2207
|
+
pathParams: repoParams(params.owner, params.repo),
|
|
2208
|
+
body: {
|
|
2209
|
+
title: params.title,
|
|
2210
|
+
head: params.head,
|
|
2211
|
+
base: params.base,
|
|
2212
|
+
body: params.body,
|
|
2213
|
+
draft: params.draft
|
|
2214
|
+
}
|
|
2215
|
+
});
|
|
2216
|
+
return pullFromResponse(mapPullRequest2, ctx, params, response);
|
|
2217
|
+
}
|
|
2218
|
+
async mergePullRequest(ctx, params) {
|
|
2219
|
+
await ctx.http.request({
|
|
2220
|
+
method: "PUT",
|
|
2221
|
+
path: `${REPO_PATH2}/pulls/{number}/merge`,
|
|
2222
|
+
pathParams: { ...repoParams(params.owner, params.repo), number: String(params.number) },
|
|
2223
|
+
body: {
|
|
2224
|
+
merge_method: params.merge_method,
|
|
2225
|
+
title: params.commit_title,
|
|
2226
|
+
description: params.commit_message
|
|
2227
|
+
}
|
|
2228
|
+
});
|
|
2229
|
+
return this.getPullRequest(ctx, params);
|
|
2230
|
+
}
|
|
2231
|
+
async listPullRequestComments(ctx, params) {
|
|
2232
|
+
const parentUrl = deriveParentHtmlUrl(
|
|
2233
|
+
ctx.webBaseUrl,
|
|
2234
|
+
params.owner,
|
|
2235
|
+
params.repo,
|
|
2236
|
+
"pulls",
|
|
2237
|
+
params.number
|
|
2238
|
+
);
|
|
2239
|
+
return this.listComments(ctx, params, `${REPO_PATH2}/pulls/{number}/comments`, parentUrl);
|
|
2240
|
+
}
|
|
2241
|
+
async createPullRequestComment(ctx, params) {
|
|
2242
|
+
const parentUrl = deriveParentHtmlUrl(
|
|
2243
|
+
ctx.webBaseUrl,
|
|
2244
|
+
params.owner,
|
|
2245
|
+
params.repo,
|
|
2246
|
+
"pulls",
|
|
2247
|
+
params.number
|
|
2248
|
+
);
|
|
2249
|
+
return this.createComment(ctx, params, `${REPO_PATH2}/pulls/{number}/comments`, parentUrl);
|
|
2250
|
+
}
|
|
2251
|
+
/* ---------------------------------------------------------------------- */
|
|
2252
|
+
/* Repository */
|
|
2253
|
+
/* ---------------------------------------------------------------------- */
|
|
2254
|
+
async listRepositories(ctx, params) {
|
|
2255
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : params.user !== void 0 ? "/users/{user}/repos" : "/user/repos";
|
|
2256
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : params.user !== void 0 ? { user: params.user } : {};
|
|
2257
|
+
const entries = await collectPages(
|
|
2258
|
+
ctx.http,
|
|
2259
|
+
{ method: "GET", path, pathParams },
|
|
2260
|
+
{
|
|
2261
|
+
maxPages: params.maxPages,
|
|
2262
|
+
selectItems: (raw) => asArray2(raw),
|
|
2263
|
+
keyOf: (raw) => listKey2(asRecord2(raw).id ?? asRecord2(raw).full_name)
|
|
2264
|
+
}
|
|
2265
|
+
);
|
|
2266
|
+
return entries.map((entry) => {
|
|
2267
|
+
const record = asRecord2(entry);
|
|
2268
|
+
const owner = str2(asRecord2(record.namespace).path) ?? params.org ?? params.user ?? "";
|
|
2269
|
+
const repo = str2(record.name) ?? str2(record.path) ?? "";
|
|
2270
|
+
return mapRepository2(owner, repo, record);
|
|
2271
|
+
});
|
|
2272
|
+
}
|
|
2273
|
+
async getRepository(ctx, params) {
|
|
2274
|
+
const response = await ctx.http.request({
|
|
2275
|
+
method: "GET",
|
|
2276
|
+
path: REPO_PATH2,
|
|
2277
|
+
pathParams: repoParams(params.owner, params.repo)
|
|
2278
|
+
});
|
|
2279
|
+
return mapRepository2(params.owner, params.repo, response.json);
|
|
2280
|
+
}
|
|
2281
|
+
async createRepository(ctx, params) {
|
|
2282
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : "/user/repos";
|
|
2283
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : {};
|
|
2284
|
+
const response = await ctx.http.request({
|
|
2285
|
+
method: "POST",
|
|
2286
|
+
path,
|
|
2287
|
+
pathParams,
|
|
2288
|
+
body: {
|
|
2289
|
+
name: params.name,
|
|
2290
|
+
description: params.description,
|
|
2291
|
+
private: params.private,
|
|
2292
|
+
auto_init: params.auto_init
|
|
2293
|
+
}
|
|
2294
|
+
});
|
|
2295
|
+
const record = asRecord2(response.json);
|
|
2296
|
+
const owner = str2(asRecord2(record.namespace).path) ?? params.org ?? "";
|
|
2297
|
+
const repo = str2(record.name) ?? params.name;
|
|
2298
|
+
return mapRepository2(owner, repo, record);
|
|
2299
|
+
}
|
|
2300
|
+
async getFileContents(ctx, params) {
|
|
2301
|
+
const response = await ctx.http.request({
|
|
2302
|
+
method: "GET",
|
|
2303
|
+
path: `${REPO_PATH2}/contents/{path}`,
|
|
2304
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
2305
|
+
query: { ref: params.ref }
|
|
2306
|
+
});
|
|
2307
|
+
if (Array.isArray(response.json)) {
|
|
2308
|
+
throw invalidArgumentError(
|
|
2309
|
+
`Invalid \`path\`: "${params.path}" is a directory or does not exist (Gitee answers 200 with an array). v0.1.0 getFileContents reads a single file only.`,
|
|
2310
|
+
"gitee"
|
|
2311
|
+
);
|
|
2312
|
+
}
|
|
2313
|
+
return mapFileContent2(params.owner, params.repo, response.json);
|
|
2314
|
+
}
|
|
2315
|
+
/* ---------------------------------------------------------------------- */
|
|
2316
|
+
/* Files */
|
|
2317
|
+
/* ---------------------------------------------------------------------- */
|
|
2318
|
+
async createFile(ctx, params) {
|
|
2319
|
+
assertCreation2(params);
|
|
2320
|
+
return this.writeContents(ctx, params, "POST");
|
|
2321
|
+
}
|
|
2322
|
+
async updateFile(ctx, params) {
|
|
2323
|
+
assertUpdate2(params);
|
|
2324
|
+
return this.writeContents(ctx, params, "PUT");
|
|
2325
|
+
}
|
|
2326
|
+
async deleteFile(ctx, params) {
|
|
2327
|
+
const response = await ctx.http.request({
|
|
2328
|
+
method: "DELETE",
|
|
2329
|
+
path: `${REPO_PATH2}/contents/{path}`,
|
|
2330
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
2331
|
+
paramsIn: "query",
|
|
2332
|
+
body: { sha: params.sha, message: params.message, branch: params.branch }
|
|
2333
|
+
});
|
|
2334
|
+
return this.resultFromContents(ctx, params, response, false);
|
|
2335
|
+
}
|
|
2336
|
+
/**
|
|
2337
|
+
* Commits every entry in one request.
|
|
2338
|
+
*
|
|
2339
|
+
* A single `POST /commits` with `actions[]`
|
|
2340
|
+
* produces one commit, so no probe for existence is needed and no GET is
|
|
2341
|
+
* issued. An empty batch is rejected before the request because the platform
|
|
2342
|
+
* would create an empty commit.
|
|
2343
|
+
*/
|
|
2344
|
+
async pushFiles(ctx, params) {
|
|
2345
|
+
const entries = readPushEntries2(params.files);
|
|
2346
|
+
const response = await ctx.http.request({
|
|
2347
|
+
method: "POST",
|
|
2348
|
+
path: `${REPO_PATH2}/commits`,
|
|
2349
|
+
pathParams: repoParams(params.owner, params.repo),
|
|
2350
|
+
body: {
|
|
2351
|
+
branch: params.branch,
|
|
2352
|
+
message: params.message,
|
|
2353
|
+
actions: entries.map((entry) => {
|
|
2354
|
+
const action = { action: entry.action, path: entry.path };
|
|
2355
|
+
if (entry.action !== "delete") {
|
|
2356
|
+
action.content = entry.content;
|
|
2357
|
+
action.encoding = toGiteeEncoding(entry.encoding);
|
|
2358
|
+
}
|
|
2359
|
+
return action;
|
|
2360
|
+
})
|
|
2361
|
+
}
|
|
2362
|
+
});
|
|
2363
|
+
const record = asRecord2(response.json);
|
|
2364
|
+
return buildFileWriteResult2({
|
|
2365
|
+
owner: params.owner,
|
|
2366
|
+
repo: params.repo,
|
|
2367
|
+
branch: params.branch,
|
|
2368
|
+
paths: entries.map((entry) => entry.path),
|
|
2369
|
+
commitSha: confirmedSha(ctx.http, response, record.sha),
|
|
2370
|
+
commitHtmlUrl: str2(record.html_url) ?? ""
|
|
2371
|
+
});
|
|
2372
|
+
}
|
|
2373
|
+
/* ---------------------------------------------------------------------- */
|
|
2374
|
+
/* Branch / Commit */
|
|
2375
|
+
/* ---------------------------------------------------------------------- */
|
|
2376
|
+
async listBranches(ctx, params) {
|
|
2377
|
+
const entries = await collectPages(
|
|
2378
|
+
ctx.http,
|
|
2379
|
+
{
|
|
2380
|
+
method: "GET",
|
|
2381
|
+
path: `${REPO_PATH2}/branches`,
|
|
2382
|
+
pathParams: repoParams(params.owner, params.repo)
|
|
2383
|
+
},
|
|
2384
|
+
{
|
|
2385
|
+
maxPages: params.maxPages,
|
|
2386
|
+
selectItems: (raw) => asArray2(raw),
|
|
2387
|
+
keyOf: (raw) => listKey2(asRecord2(raw).name)
|
|
2388
|
+
}
|
|
2389
|
+
);
|
|
2390
|
+
return entries.map((entry) => mapBranch2(params.owner, params.repo, entry, ctx.webBaseUrl));
|
|
2391
|
+
}
|
|
2392
|
+
async createBranch(ctx, params) {
|
|
2393
|
+
const response = await ctx.http.request({
|
|
2394
|
+
method: "POST",
|
|
2395
|
+
path: `${REPO_PATH2}/branches`,
|
|
2396
|
+
pathParams: repoParams(params.owner, params.repo),
|
|
2397
|
+
body: { refs: params.from, branch_name: params.name }
|
|
2398
|
+
});
|
|
2399
|
+
const record = asRecord2(response.json);
|
|
2400
|
+
const name = str2(record.name) ?? params.name;
|
|
2401
|
+
const branch = {
|
|
2402
|
+
provider: "gitee",
|
|
2403
|
+
owner: params.owner,
|
|
2404
|
+
repo: params.repo,
|
|
2405
|
+
name,
|
|
2406
|
+
commit_sha: confirmedSha(ctx.http, response, asRecord2(record.commit).sha)
|
|
2407
|
+
};
|
|
2408
|
+
const isProtected = record.protected;
|
|
2409
|
+
if (typeof isProtected === "boolean") {
|
|
2410
|
+
branch.protected = isProtected;
|
|
2411
|
+
}
|
|
2412
|
+
const htmlUrl = deriveBranchHtmlUrl2(ctx.webBaseUrl, params.owner, params.repo, name);
|
|
2413
|
+
if (htmlUrl !== void 0) {
|
|
2414
|
+
branch.html_url = htmlUrl;
|
|
2415
|
+
}
|
|
2416
|
+
return branch;
|
|
2417
|
+
}
|
|
2418
|
+
async listCommits(ctx, params) {
|
|
2419
|
+
const entries = await collectPages(
|
|
2420
|
+
ctx.http,
|
|
2421
|
+
{
|
|
2422
|
+
method: "GET",
|
|
2423
|
+
path: `${REPO_PATH2}/commits`,
|
|
2424
|
+
pathParams: repoParams(params.owner, params.repo)
|
|
2425
|
+
},
|
|
2426
|
+
{
|
|
2427
|
+
maxPages: params.maxPages,
|
|
2428
|
+
query: { sha: params.branch },
|
|
2429
|
+
selectItems: (raw) => asArray2(raw),
|
|
2430
|
+
keyOf: (raw) => listKey2(asRecord2(raw).sha)
|
|
2431
|
+
}
|
|
2432
|
+
);
|
|
2433
|
+
return entries.map((entry) => mapCommit2(params.owner, params.repo, entry));
|
|
2434
|
+
}
|
|
2435
|
+
async getCommit(ctx, params) {
|
|
2436
|
+
const response = await ctx.http.request({
|
|
2437
|
+
method: "GET",
|
|
2438
|
+
path: `${REPO_PATH2}/commits/{sha}`,
|
|
2439
|
+
pathParams: { ...repoParams(params.owner, params.repo), sha: params.sha }
|
|
2440
|
+
});
|
|
2441
|
+
return mapCommit2(params.owner, params.repo, response.json);
|
|
2442
|
+
}
|
|
2443
|
+
/* ---------------------------------------------------------------------- */
|
|
2444
|
+
/* Internals */
|
|
2445
|
+
/* ---------------------------------------------------------------------- */
|
|
2446
|
+
/** Collects one platform state's issues, one page at a time. */
|
|
2447
|
+
async collectIssues(ctx, params, state) {
|
|
2448
|
+
return collectPages(
|
|
2449
|
+
ctx.http,
|
|
2450
|
+
{
|
|
2451
|
+
method: "GET",
|
|
2452
|
+
path: `${REPO_PATH2}/issues`,
|
|
2453
|
+
pathParams: repoParams(params.owner, params.repo)
|
|
2454
|
+
},
|
|
2455
|
+
{
|
|
2456
|
+
maxPages: params.maxPages,
|
|
2457
|
+
requireComplete: params.requireComplete,
|
|
2458
|
+
query: { state, labels: joinLabels2(params.labels) },
|
|
2459
|
+
selectItems: (_raw, { response }) => selectIssueRows(ctx.http, response),
|
|
2460
|
+
keyOf: (raw) => listKey2(asRecord2(raw).number)
|
|
2461
|
+
}
|
|
2462
|
+
);
|
|
2463
|
+
}
|
|
2464
|
+
/** Shared comment listing used by both issue and pull-request domains. */
|
|
2465
|
+
async listComments(ctx, params, path, parentUrl) {
|
|
2466
|
+
const entries = await collectPages(
|
|
2467
|
+
ctx.http,
|
|
2468
|
+
{
|
|
2469
|
+
method: "GET",
|
|
2470
|
+
path,
|
|
2471
|
+
pathParams: {
|
|
2472
|
+
...repoParams(params.owner, params.repo),
|
|
2473
|
+
number: String(params.number)
|
|
2474
|
+
}
|
|
2475
|
+
},
|
|
2476
|
+
{
|
|
2477
|
+
maxPages: params.maxPages,
|
|
2478
|
+
selectItems: (raw) => asArray2(raw),
|
|
2479
|
+
keyOf: (raw) => listKey2(asRecord2(raw).id)
|
|
2480
|
+
}
|
|
2481
|
+
);
|
|
2482
|
+
return entries.map((entry) => mapComment2(params.owner, params.repo, entry, parentUrl));
|
|
2483
|
+
}
|
|
2484
|
+
/** Shared comment creation used by both comment domains. */
|
|
2485
|
+
async createComment(ctx, params, path, parentUrl) {
|
|
2486
|
+
const response = await ctx.http.request({
|
|
2487
|
+
method: "POST",
|
|
2488
|
+
path,
|
|
2489
|
+
pathParams: {
|
|
2490
|
+
...repoParams(params.owner, params.repo),
|
|
2491
|
+
number: String(params.number)
|
|
2492
|
+
},
|
|
2493
|
+
body: { body: params.body }
|
|
2494
|
+
});
|
|
2495
|
+
return mapComment2(params.owner, params.repo, response.json, parentUrl);
|
|
2496
|
+
}
|
|
2497
|
+
/** Creates or replaces one file through the `contents` endpoints. */
|
|
2498
|
+
async writeContents(ctx, params, method) {
|
|
2499
|
+
const response = await ctx.http.request({
|
|
2500
|
+
method,
|
|
2501
|
+
path: `${REPO_PATH2}/contents/{path}`,
|
|
2502
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
2503
|
+
body: {
|
|
2504
|
+
content: contentsBase64(params.content, params.encoding, "gitee"),
|
|
2505
|
+
message: params.message,
|
|
2506
|
+
branch: params.branch,
|
|
2507
|
+
sha: params.sha
|
|
2508
|
+
}
|
|
2509
|
+
});
|
|
2510
|
+
return this.resultFromContents(ctx, params, response, true);
|
|
2511
|
+
}
|
|
2512
|
+
/**
|
|
2513
|
+
* Builds a `FileWriteResult` from a single-file write response.
|
|
2514
|
+
*
|
|
2515
|
+
* Gitee's `commit` object has no `html_url` in these responses,
|
|
2516
|
+
* so the commit URL is derived from the web base. That derivation is the
|
|
2517
|
+
* main path here, not a fallback.
|
|
2518
|
+
*/
|
|
2519
|
+
resultFromContents(ctx, params, response, keepFileUrl) {
|
|
2520
|
+
const record = asRecord2(response.json);
|
|
2521
|
+
const commit = asRecord2(record.commit);
|
|
2522
|
+
const sha = confirmedSha(ctx.http, response, commit.sha);
|
|
2523
|
+
const webBase = ctx.webBaseUrl.endsWith("/") ? ctx.webBaseUrl.slice(0, -1) : ctx.webBaseUrl;
|
|
2524
|
+
const derivedUrl = webBase.length > 0 && sha.length > 0 ? `${webBase}/${params.owner}/${params.repo}/commit/${sha}` : "";
|
|
2525
|
+
const fileUrl = keepFileUrl ? str2(asRecord2(record.content).html_url) : void 0;
|
|
2526
|
+
const result = buildFileWriteResult2({
|
|
2527
|
+
owner: params.owner,
|
|
2528
|
+
repo: params.repo,
|
|
2529
|
+
branch: params.branch,
|
|
2530
|
+
paths: [params.path],
|
|
2531
|
+
commitSha: sha,
|
|
2532
|
+
commitHtmlUrl: str2(commit.html_url) ?? derivedUrl
|
|
2533
|
+
});
|
|
2534
|
+
if (keepFileUrl) {
|
|
2535
|
+
result.file_html_urls = [fileUrl];
|
|
2536
|
+
}
|
|
2537
|
+
return result;
|
|
2538
|
+
}
|
|
2539
|
+
};
|
|
2540
|
+
function resolveIssueStates(state) {
|
|
2541
|
+
if (state === "all") {
|
|
2542
|
+
return ["all"];
|
|
2543
|
+
}
|
|
2544
|
+
if (state === "closed") {
|
|
2545
|
+
return CLOSED_STATES;
|
|
2546
|
+
}
|
|
2547
|
+
return OPEN_STATES;
|
|
2548
|
+
}
|
|
2549
|
+
var giteeAdapter = new GiteeAdapter();
|
|
2550
|
+
|
|
2551
|
+
// src/core/types.ts
|
|
2552
|
+
var PROVIDERS = ["github", "gitee", "atomgit"];
|
|
2553
|
+
var CAPABILITY_IDS = [
|
|
2554
|
+
// Issue (6)
|
|
2555
|
+
"issues.list",
|
|
2556
|
+
"issues.get",
|
|
2557
|
+
"issues.create",
|
|
2558
|
+
"issues.update",
|
|
2559
|
+
"issues.listComments",
|
|
2560
|
+
"issues.createComment",
|
|
2561
|
+
// Pull request (6)
|
|
2562
|
+
"pulls.list",
|
|
2563
|
+
"pulls.get",
|
|
2564
|
+
"pulls.create",
|
|
2565
|
+
"pulls.merge",
|
|
2566
|
+
"pulls.listComments",
|
|
2567
|
+
"pulls.createComment",
|
|
2568
|
+
// Repository (4)
|
|
2569
|
+
"repos.list",
|
|
2570
|
+
"repos.get",
|
|
2571
|
+
"repos.create",
|
|
2572
|
+
"repos.getFileContents",
|
|
2573
|
+
// Files (4)
|
|
2574
|
+
"files.create",
|
|
2575
|
+
"files.update",
|
|
2576
|
+
"files.delete",
|
|
2577
|
+
"files.pushFiles",
|
|
2578
|
+
// Branch (2)
|
|
2579
|
+
"branches.list",
|
|
2580
|
+
"branches.create",
|
|
2581
|
+
// Commit (2)
|
|
2582
|
+
"commits.list",
|
|
2583
|
+
"commits.get"
|
|
2584
|
+
];
|
|
2585
|
+
var PARAM_CAPABILITY_IDS = [
|
|
2586
|
+
"pulls.list.headFilter",
|
|
2587
|
+
"pulls.list.milestoneFilter",
|
|
2588
|
+
"issues.list.milestoneFilter",
|
|
2589
|
+
"files.pushFiles.atomic"
|
|
2590
|
+
];
|
|
2591
|
+
|
|
2592
|
+
// src/adapters/types.ts
|
|
2593
|
+
function unsupported(capability, provider) {
|
|
2594
|
+
return (..._args) => {
|
|
2595
|
+
throw unsupportedCapabilityError(provider, capability);
|
|
2596
|
+
};
|
|
2597
|
+
}
|
|
2598
|
+
|
|
2599
|
+
// src/adapters/atomgit/map.ts
|
|
2600
|
+
var PROVIDER3 = "atomgit";
|
|
2601
|
+
function asRecord3(value) {
|
|
2602
|
+
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : {};
|
|
2603
|
+
}
|
|
2604
|
+
function asArray3(value) {
|
|
2605
|
+
return Array.isArray(value) ? value : [];
|
|
2606
|
+
}
|
|
2607
|
+
function str3(value) {
|
|
2608
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
2609
|
+
}
|
|
2610
|
+
function num3(value) {
|
|
2611
|
+
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
|
|
2612
|
+
}
|
|
2613
|
+
function listKey3(value) {
|
|
2614
|
+
if (typeof value === "string") {
|
|
2615
|
+
return value;
|
|
2616
|
+
}
|
|
2617
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
2618
|
+
return String(value);
|
|
2619
|
+
}
|
|
2620
|
+
return "";
|
|
2621
|
+
}
|
|
2622
|
+
function bool3(value) {
|
|
2623
|
+
return typeof value === "boolean" ? value : void 0;
|
|
2624
|
+
}
|
|
2625
|
+
function normalizeLabels3(raw) {
|
|
2626
|
+
const out = [];
|
|
2627
|
+
for (const entry of asArray3(raw)) {
|
|
2628
|
+
if (typeof entry === "string" && entry.length > 0) {
|
|
2629
|
+
out.push(entry);
|
|
2630
|
+
continue;
|
|
2631
|
+
}
|
|
2632
|
+
const name = str3(asRecord3(entry).name);
|
|
2633
|
+
if (name !== void 0) {
|
|
2634
|
+
out.push(name);
|
|
2635
|
+
}
|
|
2636
|
+
}
|
|
2637
|
+
return out;
|
|
2638
|
+
}
|
|
2639
|
+
function mapUser3(raw) {
|
|
2640
|
+
const record = asRecord3(raw);
|
|
2641
|
+
const login = str3(record.login);
|
|
2642
|
+
const name = str3(record.name);
|
|
2643
|
+
if (login === void 0 && name === void 0) {
|
|
2644
|
+
return void 0;
|
|
2645
|
+
}
|
|
2646
|
+
const user = { login: login ?? name };
|
|
2647
|
+
if (name !== void 0) {
|
|
2648
|
+
user.name = name;
|
|
2649
|
+
}
|
|
2650
|
+
const avatar = str3(record.avatar_url);
|
|
2651
|
+
if (avatar !== void 0) {
|
|
2652
|
+
user.avatar_url = avatar;
|
|
2653
|
+
}
|
|
2654
|
+
return user;
|
|
2655
|
+
}
|
|
2656
|
+
function mapUsers3(raw) {
|
|
2657
|
+
const users = asArray3(raw).map((entry) => mapUser3(entry)).filter((entry) => entry !== void 0);
|
|
2658
|
+
return users.length > 0 ? users : void 0;
|
|
2659
|
+
}
|
|
2660
|
+
function mapIssueState3(raw) {
|
|
2661
|
+
return raw === "open" ? "open" : "closed";
|
|
2662
|
+
}
|
|
2663
|
+
function mapPullRequestState3(raw) {
|
|
2664
|
+
return raw === "open" ? "open" : "closed";
|
|
2665
|
+
}
|
|
2666
|
+
function toAtomGitState(state) {
|
|
2667
|
+
return state === "closed" ? "close" : "reopen";
|
|
2668
|
+
}
|
|
2669
|
+
function joinLabels3(labels) {
|
|
2670
|
+
return labels === void 0 || labels.length === 0 ? void 0 : labels.join(",");
|
|
2671
|
+
}
|
|
2672
|
+
function mapIssue3(owner, repo, raw) {
|
|
2673
|
+
const record = asRecord3(raw);
|
|
2674
|
+
const issue = {
|
|
2675
|
+
provider: PROVIDER3,
|
|
2676
|
+
owner,
|
|
2677
|
+
repo,
|
|
2678
|
+
...issueNumberFields(listKey3(record.number)),
|
|
2679
|
+
title: str3(record.title) ?? "",
|
|
2680
|
+
state: mapIssueState3(record.state),
|
|
2681
|
+
html_url: str3(record.html_url) ?? "",
|
|
2682
|
+
labels: normalizeLabels3(record.labels),
|
|
2683
|
+
created_at: str3(record.created_at) ?? "",
|
|
2684
|
+
updated_at: str3(record.updated_at) ?? ""
|
|
2685
|
+
};
|
|
2686
|
+
const body = str3(record.body);
|
|
2687
|
+
if (body !== void 0) {
|
|
2688
|
+
issue.body = body;
|
|
2689
|
+
}
|
|
2690
|
+
const user = mapUser3(record.user);
|
|
2691
|
+
if (user !== void 0) {
|
|
2692
|
+
issue.user = user;
|
|
2693
|
+
}
|
|
2694
|
+
const assignees = mapUsers3(record.assignees);
|
|
2695
|
+
if (assignees !== void 0) {
|
|
2696
|
+
issue.assignees = assignees;
|
|
2697
|
+
}
|
|
2698
|
+
const comments = num3(record.comments);
|
|
2699
|
+
if (comments !== void 0) {
|
|
2700
|
+
issue.comments_count = comments;
|
|
2701
|
+
}
|
|
2702
|
+
const closedAt = str3(record.finished_at) ?? str3(record.closed_at);
|
|
2703
|
+
if (closedAt !== void 0) {
|
|
2704
|
+
issue.closed_at = closedAt;
|
|
2705
|
+
}
|
|
2706
|
+
return issue;
|
|
2707
|
+
}
|
|
2708
|
+
function mapRef3(raw) {
|
|
2709
|
+
const record = asRecord3(raw);
|
|
2710
|
+
const ref = str3(record.ref);
|
|
2711
|
+
const sha = str3(record.sha);
|
|
2712
|
+
return ref !== void 0 && sha !== void 0 ? { ref, sha } : void 0;
|
|
2713
|
+
}
|
|
2714
|
+
function mapPullRequest3(owner, repo, raw, base2, number) {
|
|
2715
|
+
const record = asRecord3(raw);
|
|
2716
|
+
const rawState = str3(record.state);
|
|
2717
|
+
const merged = rawState === "merged" || str3(record.merged_at) !== void 0;
|
|
2718
|
+
const draft = {
|
|
2719
|
+
base: base2,
|
|
2720
|
+
provider: PROVIDER3,
|
|
2721
|
+
owner,
|
|
2722
|
+
repo,
|
|
2723
|
+
number,
|
|
2724
|
+
title: str3(record.title) ?? "",
|
|
2725
|
+
state: merged ? "closed" : mapPullRequestState3(rawState),
|
|
2726
|
+
merged,
|
|
2727
|
+
html_url: str3(record.html_url) ?? "",
|
|
2728
|
+
created_at: str3(record.created_at) ?? "",
|
|
2729
|
+
updated_at: str3(record.updated_at) ?? ""
|
|
2730
|
+
};
|
|
2731
|
+
const head = mapRef3(record.head);
|
|
2732
|
+
if (head !== void 0) {
|
|
2733
|
+
draft.head = head;
|
|
2734
|
+
}
|
|
2735
|
+
const mergedAt = str3(record.merged_at);
|
|
2736
|
+
if (mergedAt !== void 0) {
|
|
2737
|
+
draft.merged_at = mergedAt;
|
|
2738
|
+
}
|
|
2739
|
+
const body = str3(record.body);
|
|
2740
|
+
if (body !== void 0) {
|
|
2741
|
+
draft.body = body;
|
|
2742
|
+
}
|
|
2743
|
+
const isDraft = bool3(record.draft);
|
|
2744
|
+
if (isDraft !== void 0) {
|
|
2745
|
+
draft.draft = isDraft;
|
|
2746
|
+
}
|
|
2747
|
+
const mergeable = bool3(record.mergeable);
|
|
2748
|
+
if (mergeable !== void 0) {
|
|
2749
|
+
draft.mergeable = mergeable;
|
|
2750
|
+
}
|
|
2751
|
+
const user = mapUser3(record.user);
|
|
2752
|
+
if (user !== void 0) {
|
|
2753
|
+
draft.user = user;
|
|
2754
|
+
}
|
|
2755
|
+
const additions = num3(record.additions) ?? num3(record.added_lines);
|
|
2756
|
+
if (additions !== void 0) {
|
|
2757
|
+
draft.additions = additions;
|
|
2758
|
+
}
|
|
2759
|
+
const deletions = num3(record.deletions) ?? num3(record.removed_lines);
|
|
2760
|
+
if (deletions !== void 0) {
|
|
2761
|
+
draft.deletions = deletions;
|
|
2762
|
+
}
|
|
2763
|
+
const changedFiles = num3(record.changed_files);
|
|
2764
|
+
if (changedFiles !== void 0) {
|
|
2765
|
+
draft.changed_files = changedFiles;
|
|
2766
|
+
}
|
|
2767
|
+
return draft;
|
|
2768
|
+
}
|
|
2769
|
+
function mapRepository3(owner, repo, raw) {
|
|
2770
|
+
const record = asRecord3(raw);
|
|
2771
|
+
const htmlUrl = str3(record.html_url) ?? str3(record.web_url) ?? "";
|
|
2772
|
+
const repository = {
|
|
2773
|
+
provider: PROVIDER3,
|
|
2774
|
+
// `namespace.path` is the path-safe owner; `human_name` has spaces.
|
|
2775
|
+
owner: str3(asRecord3(record.namespace).path) ?? owner,
|
|
2776
|
+
repo,
|
|
2777
|
+
name: str3(record.name) ?? repo,
|
|
2778
|
+
html_url: htmlUrl,
|
|
2779
|
+
private: bool3(record.private) === true || bool3(record.public) === false,
|
|
2780
|
+
default_branch: str3(record.default_branch) ?? ""
|
|
2781
|
+
};
|
|
2782
|
+
const fullName = str3(record.full_name);
|
|
2783
|
+
if (fullName !== void 0) {
|
|
2784
|
+
repository.full_name = fullName;
|
|
2785
|
+
}
|
|
2786
|
+
const description = str3(record.description);
|
|
2787
|
+
if (description !== void 0) {
|
|
2788
|
+
repository.description = description;
|
|
2789
|
+
}
|
|
2790
|
+
const fork = bool3(record.fork);
|
|
2791
|
+
if (fork !== void 0) {
|
|
2792
|
+
repository.fork = fork;
|
|
2793
|
+
}
|
|
2794
|
+
const language = str3(record.language);
|
|
2795
|
+
if (language !== void 0) {
|
|
2796
|
+
repository.language = language;
|
|
2797
|
+
}
|
|
2798
|
+
const createdAt = str3(record.created_at);
|
|
2799
|
+
if (createdAt !== void 0) {
|
|
2800
|
+
repository.created_at = createdAt;
|
|
2801
|
+
}
|
|
2802
|
+
const updatedAt = str3(record.updated_at);
|
|
2803
|
+
if (updatedAt !== void 0) {
|
|
2804
|
+
repository.updated_at = updatedAt;
|
|
2805
|
+
}
|
|
2806
|
+
return repository;
|
|
2807
|
+
}
|
|
2808
|
+
function mapFileContent3(owner, repo, raw) {
|
|
2809
|
+
const record = asRecord3(raw);
|
|
2810
|
+
const content = {
|
|
2811
|
+
provider: PROVIDER3,
|
|
2812
|
+
owner,
|
|
2813
|
+
repo,
|
|
2814
|
+
path: str3(record.path) ?? "",
|
|
2815
|
+
content: typeof record.content === "string" ? record.content : "",
|
|
2816
|
+
encoding: str3(record.encoding) ?? "base64"
|
|
2817
|
+
};
|
|
2818
|
+
const sha = str3(record.sha);
|
|
2819
|
+
if (sha !== void 0) {
|
|
2820
|
+
content.sha = sha;
|
|
2821
|
+
}
|
|
2822
|
+
const size = num3(record.size);
|
|
2823
|
+
if (size !== void 0) {
|
|
2824
|
+
content.size = size;
|
|
2825
|
+
}
|
|
2826
|
+
const htmlUrl = str3(record.html_url);
|
|
2827
|
+
if (htmlUrl !== void 0) {
|
|
2828
|
+
content.html_url = htmlUrl;
|
|
2829
|
+
}
|
|
2830
|
+
const type = str3(record.type);
|
|
2831
|
+
if (type !== void 0) {
|
|
2832
|
+
content.type = type;
|
|
2833
|
+
}
|
|
2834
|
+
return content;
|
|
2835
|
+
}
|
|
2836
|
+
function deriveBranchHtmlUrl3(webBaseUrl, owner, repo, name) {
|
|
2837
|
+
const base2 = webBaseUrl.endsWith("/") ? webBaseUrl.slice(0, -1) : webBaseUrl;
|
|
2838
|
+
if (base2.length === 0) {
|
|
2839
|
+
return void 0;
|
|
2840
|
+
}
|
|
2841
|
+
return `${base2}/${owner}/${repo}/tree/${name}`;
|
|
2842
|
+
}
|
|
2843
|
+
function mapBranch3(owner, repo, raw, webBaseUrl) {
|
|
2844
|
+
const record = asRecord3(raw);
|
|
2845
|
+
const name = str3(record.name) ?? "";
|
|
2846
|
+
const branch = {
|
|
2847
|
+
provider: PROVIDER3,
|
|
2848
|
+
owner,
|
|
2849
|
+
repo,
|
|
2850
|
+
name,
|
|
2851
|
+
commit_sha: str3(asRecord3(record.commit).sha) ?? ""
|
|
2852
|
+
};
|
|
2853
|
+
const isProtected = bool3(record.protected);
|
|
2854
|
+
if (isProtected !== void 0) {
|
|
2855
|
+
branch.protected = isProtected;
|
|
2856
|
+
}
|
|
2857
|
+
const htmlUrl = deriveBranchHtmlUrl3(webBaseUrl, owner, repo, name);
|
|
2858
|
+
if (htmlUrl !== void 0) {
|
|
2859
|
+
branch.html_url = htmlUrl;
|
|
2860
|
+
}
|
|
2861
|
+
return branch;
|
|
2862
|
+
}
|
|
2863
|
+
function mapCommitAuthor3(raw) {
|
|
2864
|
+
const record = asRecord3(raw);
|
|
2865
|
+
const name = str3(record.name);
|
|
2866
|
+
if (name === void 0) {
|
|
2867
|
+
return void 0;
|
|
2868
|
+
}
|
|
2869
|
+
return {
|
|
2870
|
+
name,
|
|
2871
|
+
email: str3(record.email) ?? "",
|
|
2872
|
+
date: str3(record.date) ?? ""
|
|
2873
|
+
};
|
|
2874
|
+
}
|
|
2875
|
+
function mapCommit3(owner, repo, raw) {
|
|
2876
|
+
const record = asRecord3(raw);
|
|
2877
|
+
const payload = asRecord3(record.commit);
|
|
2878
|
+
const commit = {
|
|
2879
|
+
provider: PROVIDER3,
|
|
2880
|
+
owner,
|
|
2881
|
+
repo,
|
|
2882
|
+
sha: str3(record.sha) ?? "",
|
|
2883
|
+
message: str3(payload.message) ?? "",
|
|
2884
|
+
// AtomGit uses `/commits/detail/<sha>`; kept verbatim.
|
|
2885
|
+
html_url: str3(record.html_url) ?? ""
|
|
2886
|
+
};
|
|
2887
|
+
const author = mapCommitAuthor3(payload.author);
|
|
2888
|
+
if (author !== void 0) {
|
|
2889
|
+
commit.author = author;
|
|
2890
|
+
}
|
|
2891
|
+
const committer = mapCommitAuthor3(payload.committer);
|
|
2892
|
+
if (committer !== void 0) {
|
|
2893
|
+
commit.committer = committer;
|
|
2894
|
+
}
|
|
2895
|
+
const parents = asArray3(record.parents).map((entry) => str3(asRecord3(entry).sha)).filter((entry) => entry !== void 0);
|
|
2896
|
+
if (parents.length > 0) {
|
|
2897
|
+
commit.parents = parents;
|
|
2898
|
+
}
|
|
2899
|
+
return commit;
|
|
2900
|
+
}
|
|
2901
|
+
function mapComment3(owner, repo, raw, parentUrl) {
|
|
2902
|
+
const record = asRecord3(raw);
|
|
2903
|
+
const comment = {
|
|
2904
|
+
provider: PROVIDER3,
|
|
2905
|
+
owner,
|
|
2906
|
+
repo,
|
|
2907
|
+
id: num3(record.id) ?? Number(str3(record.id) ?? 0),
|
|
2908
|
+
body: typeof record.body === "string" ? record.body : "",
|
|
2909
|
+
created_at: str3(record.created_at) ?? ""
|
|
2910
|
+
};
|
|
2911
|
+
const htmlUrl = str3(record.html_url) ?? parentUrl;
|
|
2912
|
+
if (htmlUrl !== void 0 && htmlUrl.length > 0) {
|
|
2913
|
+
comment.html_url = htmlUrl;
|
|
2914
|
+
}
|
|
2915
|
+
const updatedAt = str3(record.updated_at);
|
|
2916
|
+
if (updatedAt !== void 0) {
|
|
2917
|
+
comment.updated_at = updatedAt;
|
|
2918
|
+
}
|
|
2919
|
+
const user = mapUser3(record.user);
|
|
2920
|
+
if (user !== void 0) {
|
|
2921
|
+
comment.user = user;
|
|
2922
|
+
}
|
|
2923
|
+
return comment;
|
|
2924
|
+
}
|
|
2925
|
+
function deriveParentHtmlUrl2(webBaseUrl, owner, repo, kind, number) {
|
|
2926
|
+
const base2 = webBaseUrl.endsWith("/") ? webBaseUrl.slice(0, -1) : webBaseUrl;
|
|
2927
|
+
if (base2.length === 0) {
|
|
2928
|
+
return void 0;
|
|
2929
|
+
}
|
|
2930
|
+
return `${base2}/${owner}/${repo}/${kind}/${String(number)}`;
|
|
2931
|
+
}
|
|
2932
|
+
function headFilterUnsupported() {
|
|
2933
|
+
throw unsupportedCapabilityError(PROVIDER3, "pulls.list.headFilter");
|
|
2934
|
+
}
|
|
2935
|
+
|
|
2936
|
+
// src/adapters/atomgit/capabilities.ts
|
|
2937
|
+
var capabilities3 = {
|
|
2938
|
+
"issues.list": "supported",
|
|
2939
|
+
"issues.get": "supported",
|
|
2940
|
+
"issues.create": "supported",
|
|
2941
|
+
"issues.update": "supported",
|
|
2942
|
+
"issues.listComments": "supported",
|
|
2943
|
+
"issues.createComment": "supported",
|
|
2944
|
+
"pulls.list": "supported",
|
|
2945
|
+
"pulls.get": "supported",
|
|
2946
|
+
"pulls.create": "supported",
|
|
2947
|
+
"pulls.merge": "supported",
|
|
2948
|
+
"pulls.listComments": "supported",
|
|
2949
|
+
"pulls.createComment": "supported",
|
|
2950
|
+
"repos.list": "supported",
|
|
2951
|
+
"repos.get": "supported",
|
|
2952
|
+
"repos.create": "supported",
|
|
2953
|
+
"repos.getFileContents": "supported",
|
|
2954
|
+
"files.create": "supported",
|
|
2955
|
+
"files.update": "supported",
|
|
2956
|
+
"files.delete": "supported",
|
|
2957
|
+
"files.pushFiles": "unsupported",
|
|
2958
|
+
"branches.list": "supported",
|
|
2959
|
+
"branches.create": "supported",
|
|
2960
|
+
"commits.list": "supported",
|
|
2961
|
+
"commits.get": "supported"
|
|
2962
|
+
};
|
|
2963
|
+
var paramCapabilities3 = {
|
|
2964
|
+
"pulls.list.headFilter": "unsupported",
|
|
2965
|
+
"pulls.list.milestoneFilter": "supported",
|
|
2966
|
+
"issues.list.milestoneFilter": "supported",
|
|
2967
|
+
"files.pushFiles.atomic": "unsupported"
|
|
2968
|
+
};
|
|
2969
|
+
|
|
2970
|
+
// src/adapters/atomgit/adapter.ts
|
|
2971
|
+
var REPO_PATH3 = "/repos/{owner}/{repo}";
|
|
2972
|
+
var HEAD_BACKFILL_PER_PAGE = 100;
|
|
2973
|
+
function repoParams2(owner, repo) {
|
|
2974
|
+
return { owner, repo };
|
|
2975
|
+
}
|
|
2976
|
+
function assertCreation3(params) {
|
|
2977
|
+
if (params.sha !== void 0) {
|
|
2978
|
+
throw invalidArgumentError(
|
|
2979
|
+
"Invalid `sha`: createFile creates a new file and must not receive a blob sha; use updateFile to replace an existing file.",
|
|
2980
|
+
"atomgit"
|
|
2981
|
+
);
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
function assertUpdate3(params) {
|
|
2985
|
+
if (params.sha === void 0 || params.sha.length === 0) {
|
|
2986
|
+
throw invalidArgumentError(
|
|
2987
|
+
"Invalid `sha`: updateFile requires the current blob sha of the file being replaced.",
|
|
2988
|
+
"atomgit"
|
|
2989
|
+
);
|
|
2990
|
+
}
|
|
2991
|
+
}
|
|
2992
|
+
var AtomGitAdapter = class {
|
|
2993
|
+
provider = "atomgit";
|
|
2994
|
+
capabilities = capabilities3;
|
|
2995
|
+
paramCapabilities = paramCapabilities3;
|
|
2996
|
+
/* ---------------------------------------------------------------------- */
|
|
2997
|
+
/* Issue */
|
|
2998
|
+
/* ---------------------------------------------------------------------- */
|
|
2999
|
+
async listIssues(ctx, params) {
|
|
3000
|
+
const entries = await collectPages(
|
|
3001
|
+
ctx.http,
|
|
3002
|
+
{
|
|
3003
|
+
method: "GET",
|
|
3004
|
+
path: `${REPO_PATH3}/issues`,
|
|
3005
|
+
pathParams: repoParams2(params.owner, params.repo)
|
|
3006
|
+
},
|
|
3007
|
+
{
|
|
3008
|
+
maxPages: params.maxPages,
|
|
3009
|
+
requireComplete: params.requireComplete,
|
|
3010
|
+
query: {
|
|
3011
|
+
state: params.state,
|
|
3012
|
+
labels: joinLabels3(params.labels),
|
|
3013
|
+
// AtomGit's issue filter takes the milestone ordinal.
|
|
3014
|
+
milestone: params.milestone
|
|
3015
|
+
},
|
|
3016
|
+
selectItems: (_raw, { response }) => selectIssueRows(ctx.http, response),
|
|
3017
|
+
keyOf: (raw) => listKey3(asRecord3(raw).number)
|
|
3018
|
+
}
|
|
3019
|
+
);
|
|
3020
|
+
return entries.map((entry) => mapIssue3(params.owner, params.repo, entry));
|
|
3021
|
+
}
|
|
3022
|
+
async getIssue(ctx, params) {
|
|
3023
|
+
const response = await ctx.http.request({
|
|
3024
|
+
method: "GET",
|
|
3025
|
+
path: `${REPO_PATH3}/issues/{number}`,
|
|
3026
|
+
pathParams: { ...repoParams2(params.owner, params.repo), number: String(params.number) }
|
|
3027
|
+
});
|
|
3028
|
+
return mapIssue3(params.owner, params.repo, response.json);
|
|
3029
|
+
}
|
|
3030
|
+
async createIssue(ctx, params) {
|
|
3031
|
+
const response = await ctx.http.request({
|
|
3032
|
+
method: "POST",
|
|
3033
|
+
path: "/repos/{owner}/issues",
|
|
3034
|
+
pathParams: { owner: params.owner },
|
|
3035
|
+
body: {
|
|
3036
|
+
repo: params.repo,
|
|
3037
|
+
title: params.title,
|
|
3038
|
+
body: params.body,
|
|
3039
|
+
labels: joinLabels3(params.labels),
|
|
3040
|
+
milestone: params.milestone
|
|
3041
|
+
}
|
|
3042
|
+
});
|
|
3043
|
+
return mapIssue3(params.owner, params.repo, response.json);
|
|
3044
|
+
}
|
|
3045
|
+
/**
|
|
3046
|
+
* Updates an issue.
|
|
3047
|
+
*
|
|
3048
|
+
* AtomGit requires both `repo` and `title` on this endpoint, so a caller that
|
|
3049
|
+
* omits `title` costs one extra read to fill it in. That is the documented
|
|
3050
|
+
* trade-off: sending the update without a title is a 422.
|
|
3051
|
+
*/
|
|
3052
|
+
async updateIssue(ctx, params) {
|
|
3053
|
+
const title = params.title ?? (await this.getIssue(ctx, { ...params, number: params.number })).title;
|
|
3054
|
+
const body = {
|
|
3055
|
+
repo: params.repo,
|
|
3056
|
+
title,
|
|
3057
|
+
body: params.body,
|
|
3058
|
+
labels: joinLabels3(params.labels),
|
|
3059
|
+
milestone: params.milestone
|
|
3060
|
+
};
|
|
3061
|
+
if (params.state !== void 0) {
|
|
3062
|
+
body.state = toAtomGitState(params.state);
|
|
3063
|
+
}
|
|
3064
|
+
const response = await ctx.http.request({
|
|
3065
|
+
method: "PATCH",
|
|
3066
|
+
path: "/repos/{owner}/issues/{number}",
|
|
3067
|
+
pathParams: { owner: params.owner, number: String(params.number) },
|
|
3068
|
+
body
|
|
3069
|
+
});
|
|
3070
|
+
return mapIssue3(params.owner, params.repo, response.json);
|
|
3071
|
+
}
|
|
3072
|
+
async listIssueComments(ctx, params) {
|
|
3073
|
+
const parentUrl = deriveParentHtmlUrl2(
|
|
3074
|
+
ctx.webBaseUrl,
|
|
3075
|
+
params.owner,
|
|
3076
|
+
params.repo,
|
|
3077
|
+
"issues",
|
|
3078
|
+
params.number
|
|
3079
|
+
);
|
|
3080
|
+
return this.listComments(ctx, params, `${REPO_PATH3}/issues/{number}/comments`, parentUrl);
|
|
3081
|
+
}
|
|
3082
|
+
async createIssueComment(ctx, params) {
|
|
3083
|
+
const parentUrl = deriveParentHtmlUrl2(
|
|
3084
|
+
ctx.webBaseUrl,
|
|
3085
|
+
params.owner,
|
|
3086
|
+
params.repo,
|
|
3087
|
+
"issues",
|
|
3088
|
+
params.number
|
|
3089
|
+
);
|
|
3090
|
+
return this.createComment(ctx, params, `${REPO_PATH3}/issues/{number}/comments`, parentUrl);
|
|
3091
|
+
}
|
|
3092
|
+
/* ---------------------------------------------------------------------- */
|
|
3093
|
+
/* Pull request */
|
|
3094
|
+
/* ---------------------------------------------------------------------- */
|
|
3095
|
+
/**
|
|
3096
|
+
* Lists pull requests.
|
|
3097
|
+
*
|
|
3098
|
+
* `head` is rejected before any request: AtomGit has no such filter, and
|
|
3099
|
+
* returning an unfiltered list would let a caller believe it was filtered.
|
|
3100
|
+
*/
|
|
3101
|
+
async listPullRequests(ctx, params) {
|
|
3102
|
+
if (params.head !== void 0) {
|
|
3103
|
+
headFilterUnsupported();
|
|
3104
|
+
}
|
|
3105
|
+
return collectPullRequests(ctx, params, mapPullRequest3, {
|
|
3106
|
+
states: params.state === "closed" ? ["closed", "merged", "locked"] : [params.state],
|
|
3107
|
+
query: {
|
|
3108
|
+
base: params.base,
|
|
3109
|
+
labels: joinLabels3(params.labels),
|
|
3110
|
+
milestone_number: params.milestone
|
|
3111
|
+
}
|
|
3112
|
+
});
|
|
3113
|
+
}
|
|
3114
|
+
/**
|
|
3115
|
+
* Reads one pull request. When detail omits the head, try one list page;
|
|
3116
|
+
* if neither response contains it, leave it absent.
|
|
3117
|
+
*/
|
|
3118
|
+
async getPullRequest(ctx, params) {
|
|
3119
|
+
const response = await ctx.http.request({
|
|
3120
|
+
method: "GET",
|
|
3121
|
+
path: `${REPO_PATH3}/pulls/{number}`,
|
|
3122
|
+
pathParams: { ...repoParams2(params.owner, params.repo), number: String(params.number) }
|
|
3123
|
+
});
|
|
3124
|
+
requirePullNumber(ctx.http, response, response.json, params.number);
|
|
3125
|
+
const detail = asRecord3(response.json);
|
|
3126
|
+
if (mapRef3(detail.head) && mapRef3(detail.base)) {
|
|
3127
|
+
return pullFromResponse(mapPullRequest3, ctx, params, response);
|
|
3128
|
+
}
|
|
3129
|
+
const other = await this.findPullRefs(ctx, params);
|
|
3130
|
+
return pullFromResponse(mapPullRequest3, ctx, params, response, {
|
|
3131
|
+
raw: {
|
|
3132
|
+
...detail,
|
|
3133
|
+
head: mapRef3(detail.head) ?? other?.head,
|
|
3134
|
+
base: mapRef3(detail.base) ?? other?.base
|
|
3135
|
+
}
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
async createPullRequest(ctx, params) {
|
|
3139
|
+
const response = await ctx.http.request({
|
|
3140
|
+
method: "POST",
|
|
3141
|
+
path: `${REPO_PATH3}/pulls`,
|
|
3142
|
+
pathParams: repoParams2(params.owner, params.repo),
|
|
3143
|
+
body: {
|
|
3144
|
+
title: params.title,
|
|
3145
|
+
head: params.head,
|
|
3146
|
+
base: params.base,
|
|
3147
|
+
body: params.body,
|
|
3148
|
+
draft: params.draft
|
|
3149
|
+
}
|
|
3150
|
+
});
|
|
3151
|
+
return pullFromResponse(mapPullRequest3, ctx, params, response);
|
|
3152
|
+
}
|
|
3153
|
+
async mergePullRequest(ctx, params) {
|
|
3154
|
+
await ctx.http.request({
|
|
3155
|
+
method: "PUT",
|
|
3156
|
+
path: `${REPO_PATH3}/pulls/{number}/merge`,
|
|
3157
|
+
pathParams: { ...repoParams2(params.owner, params.repo), number: String(params.number) },
|
|
3158
|
+
body: {
|
|
3159
|
+
merge_method: params.merge_method,
|
|
3160
|
+
title: params.commit_title,
|
|
3161
|
+
description: params.commit_message
|
|
3162
|
+
}
|
|
3163
|
+
});
|
|
3164
|
+
return this.getPullRequest(ctx, params);
|
|
3165
|
+
}
|
|
3166
|
+
async listPullRequestComments(ctx, params) {
|
|
3167
|
+
const parentUrl = deriveParentHtmlUrl2(
|
|
3168
|
+
ctx.webBaseUrl,
|
|
3169
|
+
params.owner,
|
|
3170
|
+
params.repo,
|
|
3171
|
+
"merge_requests",
|
|
3172
|
+
params.number
|
|
3173
|
+
);
|
|
3174
|
+
return this.listComments(ctx, params, `${REPO_PATH3}/pulls/{number}/comments`, parentUrl);
|
|
3175
|
+
}
|
|
3176
|
+
async createPullRequestComment(ctx, params) {
|
|
3177
|
+
const parentUrl = deriveParentHtmlUrl2(
|
|
3178
|
+
ctx.webBaseUrl,
|
|
3179
|
+
params.owner,
|
|
3180
|
+
params.repo,
|
|
3181
|
+
"merge_requests",
|
|
3182
|
+
params.number
|
|
3183
|
+
);
|
|
3184
|
+
return this.createComment(ctx, params, `${REPO_PATH3}/pulls/{number}/comments`, parentUrl);
|
|
3185
|
+
}
|
|
3186
|
+
/* ---------------------------------------------------------------------- */
|
|
3187
|
+
/* Repository */
|
|
3188
|
+
/* ---------------------------------------------------------------------- */
|
|
3189
|
+
async listRepositories(ctx, params) {
|
|
3190
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : params.user !== void 0 ? "/users/{user}/repos" : "/user/repos";
|
|
3191
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : params.user !== void 0 ? { user: params.user } : {};
|
|
3192
|
+
const entries = await collectPages(
|
|
3193
|
+
ctx.http,
|
|
3194
|
+
{ method: "GET", path, pathParams },
|
|
3195
|
+
{
|
|
3196
|
+
maxPages: params.maxPages,
|
|
3197
|
+
selectItems: (raw) => asArray3(raw),
|
|
3198
|
+
keyOf: (raw) => listKey3(asRecord3(raw).id ?? asRecord3(raw).full_name)
|
|
3199
|
+
}
|
|
3200
|
+
);
|
|
3201
|
+
return entries.map((entry) => {
|
|
3202
|
+
const record = asRecord3(entry);
|
|
3203
|
+
const owner = str3(asRecord3(record.namespace).path) ?? params.org ?? params.user ?? "";
|
|
3204
|
+
const repo = str3(record.name) ?? str3(record.path) ?? "";
|
|
3205
|
+
return mapRepository3(owner, repo, record);
|
|
3206
|
+
});
|
|
3207
|
+
}
|
|
3208
|
+
async getRepository(ctx, params) {
|
|
3209
|
+
const response = await ctx.http.request({
|
|
3210
|
+
method: "GET",
|
|
3211
|
+
path: REPO_PATH3,
|
|
3212
|
+
pathParams: repoParams2(params.owner, params.repo)
|
|
3213
|
+
});
|
|
3214
|
+
return mapRepository3(params.owner, params.repo, response.json);
|
|
3215
|
+
}
|
|
3216
|
+
async createRepository(ctx, params) {
|
|
3217
|
+
const path = params.org !== void 0 ? "/orgs/{org}/repos" : "/user/repos";
|
|
3218
|
+
const pathParams = params.org !== void 0 ? { org: params.org } : {};
|
|
3219
|
+
const response = await ctx.http.request({
|
|
3220
|
+
method: "POST",
|
|
3221
|
+
path,
|
|
3222
|
+
pathParams,
|
|
3223
|
+
body: {
|
|
3224
|
+
name: params.name,
|
|
3225
|
+
description: params.description,
|
|
3226
|
+
private: params.private,
|
|
3227
|
+
auto_init: params.auto_init
|
|
3228
|
+
}
|
|
3229
|
+
});
|
|
3230
|
+
const record = asRecord3(response.json);
|
|
3231
|
+
const owner = str3(asRecord3(record.namespace).path) ?? params.org ?? "";
|
|
3232
|
+
const repo = str3(record.name) ?? params.name;
|
|
3233
|
+
return mapRepository3(owner, repo, record);
|
|
3234
|
+
}
|
|
3235
|
+
async getFileContents(ctx, params) {
|
|
3236
|
+
const response = await ctx.http.request({
|
|
3237
|
+
method: "GET",
|
|
3238
|
+
path: `${REPO_PATH3}/contents/{path}`,
|
|
3239
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
3240
|
+
query: { ref: params.ref }
|
|
3241
|
+
});
|
|
3242
|
+
if (Array.isArray(response.json)) {
|
|
3243
|
+
throw invalidArgumentError(
|
|
3244
|
+
`Invalid \`path\`: "${params.path}" is a directory. v0.1.0 getFileContents reads a single file only.`,
|
|
3245
|
+
"atomgit"
|
|
3246
|
+
);
|
|
3247
|
+
}
|
|
3248
|
+
return mapFileContent3(params.owner, params.repo, response.json);
|
|
3249
|
+
}
|
|
3250
|
+
/* ---------------------------------------------------------------------- */
|
|
3251
|
+
/* Files */
|
|
3252
|
+
/* ---------------------------------------------------------------------- */
|
|
3253
|
+
async createFile(ctx, params) {
|
|
3254
|
+
assertCreation3(params);
|
|
3255
|
+
return this.writeContents(ctx, params, "POST");
|
|
3256
|
+
}
|
|
3257
|
+
async updateFile(ctx, params) {
|
|
3258
|
+
assertUpdate3(params);
|
|
3259
|
+
return this.writeContents(ctx, params, "PUT");
|
|
3260
|
+
}
|
|
3261
|
+
async deleteFile(ctx, params) {
|
|
3262
|
+
const response = await ctx.http.request({
|
|
3263
|
+
method: "DELETE",
|
|
3264
|
+
path: `${REPO_PATH3}/contents/{path}`,
|
|
3265
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
3266
|
+
body: { sha: params.sha, message: params.message, branch: params.branch }
|
|
3267
|
+
});
|
|
3268
|
+
const record = asRecord3(response.json);
|
|
3269
|
+
const commit = asRecord3(record.commit);
|
|
3270
|
+
return {
|
|
3271
|
+
provider: "atomgit",
|
|
3272
|
+
owner: params.owner,
|
|
3273
|
+
repo: params.repo,
|
|
3274
|
+
commit_sha: confirmedSha(ctx.http, response, commit.sha),
|
|
3275
|
+
commit_html_url: str3(commit.html_url) ?? "",
|
|
3276
|
+
paths: [params.path],
|
|
3277
|
+
branch: params.branch ?? ""
|
|
3278
|
+
};
|
|
3279
|
+
}
|
|
3280
|
+
/**
|
|
3281
|
+
* AtomGit has no batch commit endpoint, so this capability is not implemented.
|
|
3282
|
+
*
|
|
3283
|
+
* Splitting the batch into N single-file commits would break the contract's
|
|
3284
|
+
* "one commit" promise and could leave a partial, unrecoverable write.
|
|
3285
|
+
*/
|
|
3286
|
+
pushFiles = unsupported("files.pushFiles", "atomgit");
|
|
3287
|
+
/* ---------------------------------------------------------------------- */
|
|
3288
|
+
/* Branch / Commit */
|
|
3289
|
+
/* ---------------------------------------------------------------------- */
|
|
3290
|
+
async listBranches(ctx, params) {
|
|
3291
|
+
const entries = await collectPages(
|
|
3292
|
+
ctx.http,
|
|
3293
|
+
{
|
|
3294
|
+
method: "GET",
|
|
3295
|
+
path: `${REPO_PATH3}/branches`,
|
|
3296
|
+
pathParams: repoParams2(params.owner, params.repo)
|
|
3297
|
+
},
|
|
3298
|
+
{
|
|
3299
|
+
maxPages: params.maxPages,
|
|
3300
|
+
selectItems: (raw) => asArray3(raw),
|
|
3301
|
+
keyOf: (raw) => listKey3(asRecord3(raw).name)
|
|
3302
|
+
}
|
|
3303
|
+
);
|
|
3304
|
+
return entries.map((entry) => mapBranch3(params.owner, params.repo, entry, ctx.webBaseUrl));
|
|
3305
|
+
}
|
|
3306
|
+
async createBranch(ctx, params) {
|
|
3307
|
+
const response = await ctx.http.request({
|
|
3308
|
+
method: "POST",
|
|
3309
|
+
path: `${REPO_PATH3}/branches`,
|
|
3310
|
+
pathParams: repoParams2(params.owner, params.repo),
|
|
3311
|
+
body: { refs: params.from, branch_name: params.name }
|
|
3312
|
+
});
|
|
3313
|
+
const record = asRecord3(response.json);
|
|
3314
|
+
const name = str3(record.name) ?? params.name;
|
|
3315
|
+
const branch = {
|
|
3316
|
+
provider: "atomgit",
|
|
3317
|
+
owner: params.owner,
|
|
3318
|
+
repo: params.repo,
|
|
3319
|
+
name,
|
|
3320
|
+
commit_sha: confirmedSha(ctx.http, response, asRecord3(record.commit).sha)
|
|
3321
|
+
};
|
|
3322
|
+
const isProtected = record.protected;
|
|
3323
|
+
if (typeof isProtected === "boolean") {
|
|
3324
|
+
branch.protected = isProtected;
|
|
3325
|
+
}
|
|
3326
|
+
const htmlUrl = deriveBranchHtmlUrl3(ctx.webBaseUrl, params.owner, params.repo, name);
|
|
3327
|
+
if (htmlUrl !== void 0) {
|
|
3328
|
+
branch.html_url = htmlUrl;
|
|
3329
|
+
}
|
|
3330
|
+
return branch;
|
|
3331
|
+
}
|
|
3332
|
+
async listCommits(ctx, params) {
|
|
3333
|
+
const entries = await collectPages(
|
|
3334
|
+
ctx.http,
|
|
3335
|
+
{
|
|
3336
|
+
method: "GET",
|
|
3337
|
+
path: `${REPO_PATH3}/commits`,
|
|
3338
|
+
pathParams: repoParams2(params.owner, params.repo)
|
|
3339
|
+
},
|
|
3340
|
+
{
|
|
3341
|
+
maxPages: params.maxPages,
|
|
3342
|
+
query: { sha: params.branch },
|
|
3343
|
+
selectItems: (raw) => asArray3(raw),
|
|
3344
|
+
keyOf: (raw) => listKey3(asRecord3(raw).sha)
|
|
3345
|
+
}
|
|
3346
|
+
);
|
|
3347
|
+
return entries.map((entry) => mapCommit3(params.owner, params.repo, entry));
|
|
3348
|
+
}
|
|
3349
|
+
async getCommit(ctx, params) {
|
|
3350
|
+
const response = await ctx.http.request({
|
|
3351
|
+
method: "GET",
|
|
3352
|
+
path: `${REPO_PATH3}/commits/{sha}`,
|
|
3353
|
+
pathParams: { ...repoParams2(params.owner, params.repo), sha: params.sha }
|
|
3354
|
+
});
|
|
3355
|
+
return mapCommit3(params.owner, params.repo, response.json);
|
|
3356
|
+
}
|
|
3357
|
+
/* ---------------------------------------------------------------------- */
|
|
3358
|
+
/* Internals */
|
|
3359
|
+
/* ---------------------------------------------------------------------- */
|
|
3360
|
+
/** Look for refs on one bounded list page; only a verified native identity may supply them. */
|
|
3361
|
+
async findPullRefs(ctx, params) {
|
|
3362
|
+
const response = await ctx.http.request({
|
|
3363
|
+
method: "GET",
|
|
3364
|
+
path: `${REPO_PATH3}/pulls`,
|
|
3365
|
+
pathParams: repoParams2(params.owner, params.repo),
|
|
3366
|
+
query: { state: "all", per_page: HEAD_BACKFILL_PER_PAGE, page: 1 }
|
|
3367
|
+
});
|
|
3368
|
+
if (!Array.isArray(response.json))
|
|
3369
|
+
throw ctx.http.invalidResponse(response, "Expected a pull request list");
|
|
3370
|
+
for (const raw of response.json) {
|
|
3371
|
+
if (requirePullNumber(ctx.http, response, raw) === params.number) {
|
|
3372
|
+
const row = asRecord3(raw);
|
|
3373
|
+
return { head: mapRef3(row.head), base: mapRef3(row.base) };
|
|
3374
|
+
}
|
|
3375
|
+
}
|
|
3376
|
+
return void 0;
|
|
3377
|
+
}
|
|
3378
|
+
/** Shared comment listing used by both issue and pull-request domains. */
|
|
3379
|
+
async listComments(ctx, params, path, parentUrl) {
|
|
3380
|
+
const entries = await collectPages(
|
|
3381
|
+
ctx.http,
|
|
3382
|
+
{
|
|
3383
|
+
method: "GET",
|
|
3384
|
+
path,
|
|
3385
|
+
pathParams: {
|
|
3386
|
+
...repoParams2(params.owner, params.repo),
|
|
3387
|
+
number: String(params.number)
|
|
3388
|
+
}
|
|
3389
|
+
},
|
|
3390
|
+
{
|
|
3391
|
+
maxPages: params.maxPages,
|
|
3392
|
+
selectItems: (raw) => asArray3(raw),
|
|
3393
|
+
keyOf: (raw) => listKey3(asRecord3(raw).id)
|
|
3394
|
+
}
|
|
3395
|
+
);
|
|
3396
|
+
return entries.map((entry) => mapComment3(params.owner, params.repo, entry, parentUrl));
|
|
3397
|
+
}
|
|
3398
|
+
/** Shared comment creation used by both comment domains. */
|
|
3399
|
+
async createComment(ctx, params, path, parentUrl) {
|
|
3400
|
+
const response = await ctx.http.request({
|
|
3401
|
+
method: "POST",
|
|
3402
|
+
path,
|
|
3403
|
+
pathParams: {
|
|
3404
|
+
...repoParams2(params.owner, params.repo),
|
|
3405
|
+
number: String(params.number)
|
|
3406
|
+
},
|
|
3407
|
+
body: { body: params.body }
|
|
3408
|
+
});
|
|
3409
|
+
return mapComment3(params.owner, params.repo, response.json, parentUrl);
|
|
3410
|
+
}
|
|
3411
|
+
/** Creates or replaces one file through the `contents` endpoints. */
|
|
3412
|
+
async writeContents(ctx, params, method) {
|
|
3413
|
+
const response = await ctx.http.request({
|
|
3414
|
+
method,
|
|
3415
|
+
path: `${REPO_PATH3}/contents/{path}`,
|
|
3416
|
+
pathParams: { owner: params.owner, repo: params.repo, path: params.path },
|
|
3417
|
+
body: {
|
|
3418
|
+
content: contentsBase64(params.content, params.encoding, "atomgit"),
|
|
3419
|
+
message: params.message,
|
|
3420
|
+
branch: params.branch,
|
|
3421
|
+
sha: params.sha
|
|
3422
|
+
}
|
|
3423
|
+
});
|
|
3424
|
+
const record = asRecord3(response.json);
|
|
3425
|
+
const commit = asRecord3(record.commit);
|
|
3426
|
+
const result = {
|
|
3427
|
+
provider: "atomgit",
|
|
3428
|
+
owner: params.owner,
|
|
3429
|
+
repo: params.repo,
|
|
3430
|
+
commit_sha: confirmedSha(ctx.http, response, commit.sha),
|
|
3431
|
+
// AtomGit's commit objects do carry `html_url`.
|
|
3432
|
+
commit_html_url: str3(commit.html_url) ?? "",
|
|
3433
|
+
paths: [params.path],
|
|
3434
|
+
branch: params.branch ?? ""
|
|
3435
|
+
};
|
|
3436
|
+
const fileUrl = str3(asRecord3(record.content).html_url);
|
|
3437
|
+
result.file_html_urls = [fileUrl];
|
|
3438
|
+
return result;
|
|
3439
|
+
}
|
|
3440
|
+
};
|
|
3441
|
+
var atomgitAdapter = new AtomGitAdapter();
|
|
3442
|
+
|
|
3443
|
+
// src/adapters/index.ts
|
|
3444
|
+
var ADAPTERS = {
|
|
3445
|
+
github: githubAdapter,
|
|
3446
|
+
gitee: giteeAdapter,
|
|
3447
|
+
atomgit: atomgitAdapter
|
|
3448
|
+
};
|
|
3449
|
+
function getAdapter(provider) {
|
|
3450
|
+
return ADAPTERS[provider];
|
|
3451
|
+
}
|
|
3452
|
+
|
|
3453
|
+
// src/core/bulk.ts
|
|
3454
|
+
var DEFAULT_BULK_CONCURRENCY = 4;
|
|
3455
|
+
async function bulk(items, run, options = {}) {
|
|
3456
|
+
if (!Array.isArray(items)) {
|
|
3457
|
+
throw invalidArgumentError("Invalid `items`: expected an array");
|
|
3458
|
+
}
|
|
3459
|
+
if (typeof run !== "function") {
|
|
3460
|
+
throw invalidArgumentError("Invalid `run`: expected a function");
|
|
3461
|
+
}
|
|
3462
|
+
const concurrency = normalizeConcurrency(options.concurrency);
|
|
3463
|
+
if (items.length === 0) {
|
|
3464
|
+
return [];
|
|
3465
|
+
}
|
|
3466
|
+
const results = new Array(items.length);
|
|
3467
|
+
let nextIndex = 0;
|
|
3468
|
+
const claimNext = () => {
|
|
3469
|
+
if (nextIndex >= items.length) {
|
|
3470
|
+
return void 0;
|
|
3471
|
+
}
|
|
3472
|
+
const index = nextIndex;
|
|
3473
|
+
nextIndex += 1;
|
|
3474
|
+
return index;
|
|
3475
|
+
};
|
|
3476
|
+
const processItem = async (item, index) => {
|
|
3477
|
+
try {
|
|
3478
|
+
const value = await run(item, index);
|
|
3479
|
+
results[index] = { ok: true, value, index };
|
|
3480
|
+
} catch (error) {
|
|
3481
|
+
results[index] = { ok: false, error: toErrorJson(error), index };
|
|
3482
|
+
}
|
|
3483
|
+
};
|
|
3484
|
+
const worker = async () => {
|
|
3485
|
+
for (; ; ) {
|
|
3486
|
+
const index = claimNext();
|
|
3487
|
+
if (index === void 0) {
|
|
3488
|
+
return;
|
|
3489
|
+
}
|
|
3490
|
+
const item = items[index];
|
|
3491
|
+
await processItem(item, index);
|
|
3492
|
+
}
|
|
3493
|
+
};
|
|
3494
|
+
const workers = Array.from({ length: Math.min(concurrency, items.length) }, () => worker());
|
|
3495
|
+
await Promise.all(workers);
|
|
3496
|
+
return results;
|
|
3497
|
+
}
|
|
3498
|
+
function normalizeConcurrency(value) {
|
|
3499
|
+
if (value === void 0) {
|
|
3500
|
+
return DEFAULT_BULK_CONCURRENCY;
|
|
3501
|
+
}
|
|
3502
|
+
if (!Number.isFinite(value) || value < 1) {
|
|
3503
|
+
throw invalidArgumentError("Invalid `concurrency`: expected a positive integer");
|
|
3504
|
+
}
|
|
3505
|
+
return Math.trunc(value);
|
|
3506
|
+
}
|
|
3507
|
+
function toErrorJson(error) {
|
|
3508
|
+
if (isGitForgeError(error)) {
|
|
3509
|
+
return error.toJSON();
|
|
3510
|
+
}
|
|
3511
|
+
const detail = error instanceof Error ? error.name : typeof error;
|
|
3512
|
+
return new GitForgeError({
|
|
3513
|
+
code: "PROVIDER_ERROR",
|
|
3514
|
+
message: `Unexpected failure (${detail})`
|
|
3515
|
+
}).toJSON();
|
|
3516
|
+
}
|
|
3517
|
+
function partitionBulk(results) {
|
|
3518
|
+
const succeeded = [];
|
|
3519
|
+
const failed = [];
|
|
3520
|
+
for (const result of results) {
|
|
3521
|
+
if (result.ok) {
|
|
3522
|
+
succeeded.push(result.value);
|
|
3523
|
+
} else {
|
|
3524
|
+
failed.push(result.error);
|
|
3525
|
+
}
|
|
3526
|
+
}
|
|
3527
|
+
return { succeeded, failed };
|
|
3528
|
+
}
|
|
3529
|
+
function allSucceeded(results) {
|
|
3530
|
+
return results.every((result) => result.ok);
|
|
3531
|
+
}
|
|
3532
|
+
|
|
3533
|
+
// src/core/validate.ts
|
|
3534
|
+
var WRITE_CAPABILITIES = /* @__PURE__ */ new Set([
|
|
3535
|
+
"issues.create",
|
|
3536
|
+
"issues.update",
|
|
3537
|
+
"issues.createComment",
|
|
3538
|
+
"pulls.create",
|
|
3539
|
+
"pulls.merge",
|
|
3540
|
+
"pulls.createComment",
|
|
3541
|
+
"repos.create",
|
|
3542
|
+
"files.create",
|
|
3543
|
+
"files.update",
|
|
3544
|
+
"files.delete",
|
|
3545
|
+
"files.pushFiles",
|
|
3546
|
+
"branches.create"
|
|
3547
|
+
]);
|
|
3548
|
+
function isWriteCapability(capability) {
|
|
3549
|
+
return WRITE_CAPABILITIES.has(capability);
|
|
3550
|
+
}
|
|
3551
|
+
function invalid(field, reason, provider) {
|
|
3552
|
+
throw invalidArgumentError(`Invalid \`${field}\`: ${reason}`, provider);
|
|
3553
|
+
}
|
|
3554
|
+
function assertProvider(value) {
|
|
3555
|
+
if (typeof value !== "string" || !PROVIDERS.includes(value)) {
|
|
3556
|
+
invalid("provider", `expected one of ${PROVIDERS.join(" | ")}, received ${describe3(value)}`);
|
|
3557
|
+
}
|
|
3558
|
+
}
|
|
3559
|
+
function assertNonEmpty(field, value) {
|
|
3560
|
+
if (typeof value !== "string") {
|
|
3561
|
+
invalid(field, `expected a string, received ${describe3(value)}`);
|
|
3562
|
+
}
|
|
3563
|
+
if (value.trim().length === 0) {
|
|
3564
|
+
invalid(field, "must not be empty");
|
|
3565
|
+
}
|
|
3566
|
+
}
|
|
3567
|
+
function assertOptionalNonEmpty(field, value) {
|
|
3568
|
+
if (value === void 0) {
|
|
3569
|
+
return;
|
|
3570
|
+
}
|
|
3571
|
+
assertNonEmpty(field, value);
|
|
3572
|
+
}
|
|
3573
|
+
function assertOneOf(field, value, allowed) {
|
|
3574
|
+
if (typeof value !== "string" || !allowed.includes(value)) {
|
|
3575
|
+
invalid(field, `expected one of ${allowed.join(" | ")}, received ${describe3(value)}`);
|
|
3576
|
+
}
|
|
3577
|
+
}
|
|
3578
|
+
function assertNativeId(field, value) {
|
|
3579
|
+
if (typeof value === "string") {
|
|
3580
|
+
assertPathSegment(field, value);
|
|
3581
|
+
return;
|
|
3582
|
+
}
|
|
3583
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
3584
|
+
invalid(field, `expected a string or a finite number, received ${describe3(value)}`);
|
|
3585
|
+
}
|
|
3586
|
+
}
|
|
3587
|
+
function assertPositiveInt(field, value) {
|
|
3588
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value <= 0) {
|
|
3589
|
+
invalid(field, `expected a positive integer, received ${describe3(value)}`);
|
|
3590
|
+
}
|
|
3591
|
+
}
|
|
3592
|
+
function assertOptionalPositiveInt(field, value) {
|
|
3593
|
+
if (value === void 0) {
|
|
3594
|
+
return;
|
|
3595
|
+
}
|
|
3596
|
+
assertPositiveInt(field, value);
|
|
3597
|
+
}
|
|
3598
|
+
function assertRepoRef(value) {
|
|
3599
|
+
if (value === null || typeof value !== "object") {
|
|
3600
|
+
invalid("owner/repo", `expected an object with owner and repo, received ${describe3(value)}`);
|
|
3601
|
+
}
|
|
3602
|
+
const ref = value;
|
|
3603
|
+
assertPathSegment("owner", ref.owner);
|
|
3604
|
+
assertPathSegment("repo", ref.repo);
|
|
3605
|
+
}
|
|
3606
|
+
function assertStringArray(field, value) {
|
|
3607
|
+
if (value === void 0) {
|
|
3608
|
+
return;
|
|
3609
|
+
}
|
|
3610
|
+
if (!Array.isArray(value)) {
|
|
3611
|
+
invalid(field, `expected an array of strings, received ${describe3(value)}`);
|
|
3612
|
+
}
|
|
3613
|
+
value.forEach((entry, index) => {
|
|
3614
|
+
if (typeof entry !== "string") {
|
|
3615
|
+
invalid(field, `entry ${index} must be a string, received ${describe3(entry)}`);
|
|
3616
|
+
}
|
|
3617
|
+
if (entry.trim().length === 0) {
|
|
3618
|
+
invalid(field, `entry ${index} must not be empty`);
|
|
3619
|
+
}
|
|
3620
|
+
});
|
|
3621
|
+
}
|
|
3622
|
+
function assertOptionalString(field, value) {
|
|
3623
|
+
if (value === void 0) {
|
|
3624
|
+
return;
|
|
3625
|
+
}
|
|
3626
|
+
if (typeof value !== "string") {
|
|
3627
|
+
invalid(field, `expected a string, received ${describe3(value)}`);
|
|
3628
|
+
}
|
|
3629
|
+
}
|
|
3630
|
+
function assertOptionalBoolean(field, value) {
|
|
3631
|
+
if (value === void 0) {
|
|
3632
|
+
return;
|
|
3633
|
+
}
|
|
3634
|
+
if (typeof value !== "boolean") {
|
|
3635
|
+
invalid(field, `expected a boolean, received ${describe3(value)}`);
|
|
3636
|
+
}
|
|
3637
|
+
}
|
|
3638
|
+
function assertOptionalOneOf(field, value, allowed) {
|
|
3639
|
+
if (value === void 0) {
|
|
3640
|
+
return;
|
|
3641
|
+
}
|
|
3642
|
+
assertOneOf(field, value, allowed);
|
|
3643
|
+
}
|
|
3644
|
+
function assertNonEmptyStringArray(field, value) {
|
|
3645
|
+
if (!Array.isArray(value)) {
|
|
3646
|
+
invalid(field, `expected a non-empty array, received ${describe3(value)}`);
|
|
3647
|
+
}
|
|
3648
|
+
if (value.length === 0) {
|
|
3649
|
+
invalid(field, "must contain at least one entry");
|
|
3650
|
+
}
|
|
3651
|
+
assertStringArray(field, value);
|
|
3652
|
+
}
|
|
3653
|
+
function assertPathSegment(field, value) {
|
|
3654
|
+
assertNonEmpty(field, value);
|
|
3655
|
+
if (unsafeSegment(value) || /[/\\?#]/.test(value)) {
|
|
3656
|
+
invalid(field, "must not contain path separators or query characters");
|
|
3657
|
+
}
|
|
3658
|
+
}
|
|
3659
|
+
function assertOptionalPathSegment(field, value) {
|
|
3660
|
+
if (value === void 0) {
|
|
3661
|
+
return;
|
|
3662
|
+
}
|
|
3663
|
+
assertPathSegment(field, value);
|
|
3664
|
+
}
|
|
3665
|
+
function unsafeSegment(value) {
|
|
3666
|
+
return value === "" || value === "." || value === ".." || /%(?:2e|2f|5c|25)/i.test(value) || Array.from(value).some((c) => c.charCodeAt(0) < 32 || c.charCodeAt(0) === 127);
|
|
3667
|
+
}
|
|
3668
|
+
function assertFilePath(field, value) {
|
|
3669
|
+
assertNonEmpty(field, value);
|
|
3670
|
+
const segments = value.split("/");
|
|
3671
|
+
if (segments.some(unsafeSegment) || value.includes("\\")) {
|
|
3672
|
+
invalid(field, 'must be a relative path and must not contain ".." segments');
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
function assertOptionalFilePath(field, value) {
|
|
3676
|
+
if (value === void 0) {
|
|
3677
|
+
return;
|
|
3678
|
+
}
|
|
3679
|
+
assertFilePath(field, value);
|
|
3680
|
+
}
|
|
3681
|
+
function assertMaxPages(value) {
|
|
3682
|
+
if (value === void 0) {
|
|
3683
|
+
return;
|
|
3684
|
+
}
|
|
3685
|
+
assertPositiveInt("maxPages", value);
|
|
3686
|
+
if (value > MAX_PAGES_LIMIT) {
|
|
3687
|
+
invalid("maxPages", `must be <= ${MAX_PAGES_LIMIT}`);
|
|
3688
|
+
}
|
|
3689
|
+
}
|
|
3690
|
+
function assertParamsObject(value, method) {
|
|
3691
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
3692
|
+
invalid(`${method} parameters`, `expected an object, received ${describe3(value)}`);
|
|
3693
|
+
}
|
|
3694
|
+
}
|
|
3695
|
+
function assertCapabilityTableKeys(table) {
|
|
3696
|
+
const expected = new Set(CAPABILITY_IDS);
|
|
3697
|
+
const actual = Object.keys(table);
|
|
3698
|
+
const missing = [...expected].filter((key) => !actual.includes(key));
|
|
3699
|
+
const extra = actual.filter((key) => !expected.has(key));
|
|
3700
|
+
if (missing.length > 0 || extra.length > 0) {
|
|
3701
|
+
throw invalidArgumentError(
|
|
3702
|
+
`Capability table must match CAPABILITY_IDS exactly (missing: [${missing.join(", ")}], unexpected: [${extra.join(", ")}])`
|
|
3703
|
+
);
|
|
3704
|
+
}
|
|
3705
|
+
}
|
|
3706
|
+
var PUSH_FILE_ACTIONS = ["create", "update", "delete"];
|
|
3707
|
+
var PUSH_FILE_ENCODINGS = ["utf8", "base64"];
|
|
3708
|
+
var BASE64_ALPHABET3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
3709
|
+
function isValidBase64Payload(value) {
|
|
3710
|
+
const compact = value.replace(/\s+/g, "");
|
|
3711
|
+
const body = compact.replace(/={0,2}$/, "");
|
|
3712
|
+
if (body.length === 0 || body.length % 4 === 1) {
|
|
3713
|
+
return false;
|
|
3714
|
+
}
|
|
3715
|
+
for (const char of body) {
|
|
3716
|
+
if (!BASE64_ALPHABET3.includes(char)) {
|
|
3717
|
+
return false;
|
|
3718
|
+
}
|
|
3719
|
+
}
|
|
3720
|
+
return true;
|
|
3721
|
+
}
|
|
3722
|
+
function assertPushFilesParams(params) {
|
|
3723
|
+
assertParamsObject(params, "pushFiles");
|
|
3724
|
+
const bag = params;
|
|
3725
|
+
assertNonEmpty("branch", bag.branch);
|
|
3726
|
+
assertNonEmpty("message", bag.message);
|
|
3727
|
+
const files = bag.files;
|
|
3728
|
+
if (!Array.isArray(files)) {
|
|
3729
|
+
invalid("files", `expected an array of file entries, received ${describe3(files)}`);
|
|
3730
|
+
}
|
|
3731
|
+
if (files.length === 0) {
|
|
3732
|
+
invalid(
|
|
3733
|
+
"files",
|
|
3734
|
+
"pushFiles requires at least one entry (an empty batch would create an empty commit)"
|
|
3735
|
+
);
|
|
3736
|
+
}
|
|
3737
|
+
files.forEach((raw, index) => {
|
|
3738
|
+
const field = `files[${index}]`;
|
|
3739
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
3740
|
+
invalid(field, `expected an object, received ${describe3(raw)}`);
|
|
3741
|
+
}
|
|
3742
|
+
const entry = raw;
|
|
3743
|
+
assertFilePath(`${field}.path`, entry.path);
|
|
3744
|
+
assertOneOf(`${field}.action`, entry.action, PUSH_FILE_ACTIONS);
|
|
3745
|
+
const encoding = entry.encoding ?? "utf8";
|
|
3746
|
+
assertOneOf(`${field}.encoding`, encoding, PUSH_FILE_ENCODINGS);
|
|
3747
|
+
if (entry.action === "delete") {
|
|
3748
|
+
if (entry.content !== void 0) {
|
|
3749
|
+
invalid(field, `action 'delete' must not carry content`);
|
|
3750
|
+
}
|
|
3751
|
+
return;
|
|
3752
|
+
}
|
|
3753
|
+
if (typeof entry.content !== "string") {
|
|
3754
|
+
invalid(`${field}.content`, `action '${entry.action}' requires content`);
|
|
3755
|
+
}
|
|
3756
|
+
if (encoding === "base64" && !isValidBase64Payload(entry.content)) {
|
|
3757
|
+
invalid(`${field}.content`, "not a valid base64 payload");
|
|
3758
|
+
}
|
|
3759
|
+
});
|
|
3760
|
+
}
|
|
3761
|
+
function describe3(value) {
|
|
3762
|
+
if (value === null) {
|
|
3763
|
+
return "null";
|
|
3764
|
+
}
|
|
3765
|
+
if (value === void 0) {
|
|
3766
|
+
return "undefined";
|
|
3767
|
+
}
|
|
3768
|
+
if (typeof value === "string") {
|
|
3769
|
+
return value.length === 0 ? "an empty string" : `"${truncate2(value)}"`;
|
|
3770
|
+
}
|
|
3771
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
3772
|
+
return String(value);
|
|
3773
|
+
}
|
|
3774
|
+
if (Array.isArray(value)) {
|
|
3775
|
+
return `an array of length ${value.length}`;
|
|
3776
|
+
}
|
|
3777
|
+
return typeof value;
|
|
3778
|
+
}
|
|
3779
|
+
function truncate2(value) {
|
|
3780
|
+
return value.length <= 60 ? value : `${value.slice(0, 60)}\u2026`;
|
|
3781
|
+
}
|
|
3782
|
+
|
|
3783
|
+
// src/core/retry.ts
|
|
3784
|
+
var DEFAULT_RETRY_POLICY = {
|
|
3785
|
+
maxRetries: 3,
|
|
3786
|
+
baseMs: 500,
|
|
3787
|
+
factor: 2,
|
|
3788
|
+
maxDelayMs: 8e3,
|
|
3789
|
+
writes: false,
|
|
3790
|
+
maxRetryAfterMs: 6e4
|
|
3791
|
+
};
|
|
3792
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "OPTIONS"]);
|
|
3793
|
+
function isIdempotentMethod(method) {
|
|
3794
|
+
return IDEMPOTENT_METHODS.has(method.toUpperCase());
|
|
3795
|
+
}
|
|
3796
|
+
function resolveRetryPolicy(overrides) {
|
|
3797
|
+
if (overrides === void 0) {
|
|
3798
|
+
return { ...DEFAULT_RETRY_POLICY };
|
|
3799
|
+
}
|
|
3800
|
+
return {
|
|
3801
|
+
maxRetries: overrides.maxRetries ?? DEFAULT_RETRY_POLICY.maxRetries,
|
|
3802
|
+
baseMs: overrides.baseMs ?? DEFAULT_RETRY_POLICY.baseMs,
|
|
3803
|
+
factor: overrides.factor ?? DEFAULT_RETRY_POLICY.factor,
|
|
3804
|
+
maxDelayMs: overrides.maxDelayMs ?? DEFAULT_RETRY_POLICY.maxDelayMs,
|
|
3805
|
+
writes: overrides.writes ?? DEFAULT_RETRY_POLICY.writes,
|
|
3806
|
+
maxRetryAfterMs: overrides.maxRetryAfterMs ?? DEFAULT_RETRY_POLICY.maxRetryAfterMs
|
|
3807
|
+
};
|
|
3808
|
+
}
|
|
3809
|
+
function computeDelay(attempt, policy, random, retryAfterMs) {
|
|
3810
|
+
if (retryAfterMs !== void 0) return retryAfterMs;
|
|
3811
|
+
const ceiling = Math.min(
|
|
3812
|
+
policy.baseMs * policy.factor ** Math.max(attempt - 1, 0),
|
|
3813
|
+
policy.maxDelayMs
|
|
3814
|
+
);
|
|
3815
|
+
return Math.floor(random() * ceiling);
|
|
3816
|
+
}
|
|
3817
|
+
function parseRetryAfter(value, maxMs) {
|
|
3818
|
+
if (value === null || value === void 0) {
|
|
3819
|
+
return void 0;
|
|
3820
|
+
}
|
|
3821
|
+
const trimmed = value.trim();
|
|
3822
|
+
if (trimmed.length === 0) {
|
|
3823
|
+
return void 0;
|
|
3824
|
+
}
|
|
3825
|
+
if (/^\d+$/.test(trimmed)) {
|
|
3826
|
+
const ms2 = Number(trimmed) * 1e3;
|
|
3827
|
+
return maxMs === void 0 ? ms2 : Math.min(ms2, maxMs);
|
|
3828
|
+
}
|
|
3829
|
+
const imfDate = /^[A-Za-z]{3}, \d{2} [A-Za-z]{3} \d{4} \d{2}:\d{2}:\d{2} GMT$/;
|
|
3830
|
+
const rfc850 = /^[A-Za-z]+, \d{2}-[A-Za-z]{3}-\d{2} \d{2}:\d{2}:\d{2} GMT$/;
|
|
3831
|
+
const asctime = /^[A-Za-z]{3} [A-Za-z]{3} [ \d]\d \d{2}:\d{2}:\d{2} \d{4}$/;
|
|
3832
|
+
if (!imfDate.test(trimmed) && !rfc850.test(trimmed) && !asctime.test(trimmed)) return void 0;
|
|
3833
|
+
const date = Date.parse(trimmed.endsWith(" GMT") ? trimmed : `${trimmed} GMT`);
|
|
3834
|
+
if (Number.isNaN(date)) {
|
|
3835
|
+
return void 0;
|
|
3836
|
+
}
|
|
3837
|
+
const delta = date - Date.now();
|
|
3838
|
+
const ms = delta <= 0 ? 0 : delta;
|
|
3839
|
+
return maxMs === void 0 ? ms : Math.min(ms, maxMs);
|
|
3840
|
+
}
|
|
3841
|
+
function shouldRetry(input) {
|
|
3842
|
+
const { attempt, method, code, status, retryAfterMs, policy } = input;
|
|
3843
|
+
const maxServerWait = Math.min(policy.maxRetryAfterMs, 2 ** 31 - 1);
|
|
3844
|
+
if (retryAfterMs !== void 0 && !(retryAfterMs <= maxServerWait)) return false;
|
|
3845
|
+
if (attempt > policy.maxRetries) {
|
|
3846
|
+
return false;
|
|
3847
|
+
}
|
|
3848
|
+
if (!isIdempotentMethod(method) && !policy.writes) {
|
|
3849
|
+
return false;
|
|
3850
|
+
}
|
|
3851
|
+
if (code === "NETWORK_ERROR" || code === "RATE_LIMITED") {
|
|
3852
|
+
return true;
|
|
3853
|
+
}
|
|
3854
|
+
if (code === "PROVIDER_ERROR") {
|
|
3855
|
+
return status !== void 0 ? retryableForStatus(code, status) : defaultRetryable(code);
|
|
3856
|
+
}
|
|
3857
|
+
return false;
|
|
3858
|
+
}
|
|
3859
|
+
function retryAfterFromHeaders(headers, maxMs) {
|
|
3860
|
+
if (headers === void 0) {
|
|
3861
|
+
return void 0;
|
|
3862
|
+
}
|
|
3863
|
+
const retryAfter = parseRetryAfter(readHeader3(headers, "retry-after"), maxMs);
|
|
3864
|
+
if (retryAfter !== void 0) {
|
|
3865
|
+
return retryAfter;
|
|
3866
|
+
}
|
|
3867
|
+
if (readHeader3(headers, "x-ratelimit-remaining")?.trim() !== "0") return void 0;
|
|
3868
|
+
const reset = readHeader3(headers, "x-ratelimit-reset");
|
|
3869
|
+
if (reset !== void 0 && /^\d+$/.test(reset.trim())) {
|
|
3870
|
+
const delta = Number(reset.trim()) * 1e3 - Date.now();
|
|
3871
|
+
if (delta > 0) {
|
|
3872
|
+
return maxMs === void 0 ? delta : Math.min(delta, maxMs);
|
|
3873
|
+
}
|
|
3874
|
+
}
|
|
3875
|
+
return void 0;
|
|
3876
|
+
}
|
|
3877
|
+
function readHeader3(headers, key) {
|
|
3878
|
+
if (typeof headers.get === "function") {
|
|
3879
|
+
const value = headers.get(key);
|
|
3880
|
+
return value === null ? void 0 : value;
|
|
3881
|
+
}
|
|
3882
|
+
const record = headers;
|
|
3883
|
+
return record[key] ?? record[key.toLowerCase()];
|
|
3884
|
+
}
|
|
3885
|
+
function isRetryableStatus(status, headers) {
|
|
3886
|
+
if (status === 429) {
|
|
3887
|
+
return true;
|
|
3888
|
+
}
|
|
3889
|
+
if (status >= 500) {
|
|
3890
|
+
return true;
|
|
3891
|
+
}
|
|
3892
|
+
return isRateLimitResponse(status, headers);
|
|
3893
|
+
}
|
|
3894
|
+
|
|
3895
|
+
// src/core/http.ts
|
|
3896
|
+
var DEFAULT_TIMEOUT_MS = 3e4;
|
|
3897
|
+
var DEFAULT_USER_AGENT = "git-forge/0.1.0";
|
|
3898
|
+
var HttpClient = class {
|
|
3899
|
+
#base;
|
|
3900
|
+
get baseUrl() {
|
|
3901
|
+
return stripTrailingSlash(this.#base.href);
|
|
3902
|
+
}
|
|
3903
|
+
timeoutMs;
|
|
3904
|
+
provider;
|
|
3905
|
+
auth;
|
|
3906
|
+
token;
|
|
3907
|
+
defaultHeaders;
|
|
3908
|
+
fetchImpl;
|
|
3909
|
+
sleep;
|
|
3910
|
+
random;
|
|
3911
|
+
policy;
|
|
3912
|
+
userAgent;
|
|
3913
|
+
constructor(options) {
|
|
3914
|
+
try {
|
|
3915
|
+
this.#base = new URL(options.baseUrl);
|
|
3916
|
+
} catch {
|
|
3917
|
+
throw invalidArgumentError("Invalid baseUrl: expected an absolute URL", options.provider);
|
|
3918
|
+
}
|
|
3919
|
+
const { protocol, hostname, username, password, hash } = this.#base;
|
|
3920
|
+
const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(hostname);
|
|
3921
|
+
if (protocol !== "https:" && !(protocol === "http:" && loopback) || username || password || hash) {
|
|
3922
|
+
throw invalidArgumentError(
|
|
3923
|
+
"Invalid baseUrl: expected HTTPS or loopback without userinfo or fragment",
|
|
3924
|
+
options.provider
|
|
3925
|
+
);
|
|
3926
|
+
}
|
|
3927
|
+
this.provider = options.provider;
|
|
3928
|
+
this.timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
3929
|
+
this.auth = options.auth;
|
|
3930
|
+
this.token = options.token;
|
|
3931
|
+
this.defaultHeaders = options.defaultHeaders ?? {};
|
|
3932
|
+
const injectedFetch = options.fetch;
|
|
3933
|
+
this.fetchImpl = injectedFetch ?? ((input, init) => globalThis.fetch(input, init));
|
|
3934
|
+
this.sleep = options.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
3935
|
+
this.random = options.random ?? Math.random;
|
|
3936
|
+
this.policy = resolveRetryPolicy(options.retry);
|
|
3937
|
+
this.userAgent = options.userAgent ?? DEFAULT_USER_AGENT;
|
|
3938
|
+
}
|
|
3939
|
+
/** Whether a token was configured on this client. */
|
|
3940
|
+
get hasToken() {
|
|
3941
|
+
return this.token !== void 0 && this.token.length > 0;
|
|
3942
|
+
}
|
|
3943
|
+
/**
|
|
3944
|
+
* Renders a request for diagnostics.
|
|
3945
|
+
*
|
|
3946
|
+
* Query parameters are shown, path parameters are *not* substituted: the
|
|
3947
|
+
* template is what belongs in a message, both because a concrete path can
|
|
3948
|
+
* carry identifying data and because a caller needs the template to find the
|
|
3949
|
+
* endpoint in the platform docs. Under query-style auth the parameter is
|
|
3950
|
+
* present but redacted.
|
|
3951
|
+
*/
|
|
3952
|
+
describe(spec) {
|
|
3953
|
+
const params = new URLSearchParams();
|
|
3954
|
+
if (spec.query !== void 0) {
|
|
3955
|
+
for (const [key, value] of Object.entries(spec.query)) {
|
|
3956
|
+
if (value !== void 0) {
|
|
3957
|
+
params.set(key, stringifyScalar(value));
|
|
3958
|
+
}
|
|
3959
|
+
}
|
|
3960
|
+
}
|
|
3961
|
+
if (spec.paramsIn === "query" && spec.body !== void 0 && typeof spec.body === "object") {
|
|
3962
|
+
for (const [key, value] of Object.entries(spec.body)) {
|
|
3963
|
+
if (value !== void 0 && value !== null) {
|
|
3964
|
+
params.set(key, stringifyScalar(value));
|
|
3965
|
+
}
|
|
3966
|
+
}
|
|
3967
|
+
}
|
|
3968
|
+
if (this.auth?.style === "query" && this.token !== void 0 && this.token.length > 0) {
|
|
3969
|
+
params.set(this.auth.param ?? "access_token", REDACTED);
|
|
3970
|
+
}
|
|
3971
|
+
const query = params.toString();
|
|
3972
|
+
const suffix = query.length > 0 ? `?${query}` : "";
|
|
3973
|
+
return scrubSecrets(
|
|
3974
|
+
redactUrl(
|
|
3975
|
+
`${this.#base.origin}${stripTrailingSlash(this.#base.pathname)}${spec.path}${suffix}`
|
|
3976
|
+
),
|
|
3977
|
+
this.secretValues()
|
|
3978
|
+
).slice(0, 768);
|
|
3979
|
+
}
|
|
3980
|
+
/**
|
|
3981
|
+
* Performs one logical request, retrying per policy.
|
|
3982
|
+
*
|
|
3983
|
+
* @throws GitForgeError always — a raw `Error` never escapes this method.
|
|
3984
|
+
*/
|
|
3985
|
+
async request(spec) {
|
|
3986
|
+
const maxAttempts = this.policy.maxRetries + 1;
|
|
3987
|
+
let lastError;
|
|
3988
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
3989
|
+
const outcome = await this.attempt(spec);
|
|
3990
|
+
if (outcome.kind === "response") {
|
|
3991
|
+
return outcome.response;
|
|
3992
|
+
}
|
|
3993
|
+
const { error, retryAfterMs } = outcome;
|
|
3994
|
+
lastError = error;
|
|
3995
|
+
const retryable = shouldRetry({
|
|
3996
|
+
attempt,
|
|
3997
|
+
method: spec.method,
|
|
3998
|
+
code: error.code,
|
|
3999
|
+
status: error.status,
|
|
4000
|
+
policy: this.policy,
|
|
4001
|
+
retryAfterMs
|
|
4002
|
+
});
|
|
4003
|
+
if (!retryable) {
|
|
4004
|
+
throw error;
|
|
4005
|
+
}
|
|
4006
|
+
const delay = computeDelay(attempt, this.policy, this.random, retryAfterMs);
|
|
4007
|
+
await this.sleep(delay);
|
|
4008
|
+
}
|
|
4009
|
+
throw lastError ?? new Error("request failed without an error");
|
|
4010
|
+
}
|
|
4011
|
+
/** Performs exactly one attempt and reports its outcome without retrying. */
|
|
4012
|
+
async attempt(spec) {
|
|
4013
|
+
const url = this.buildUrl(spec);
|
|
4014
|
+
const headers = this.buildHeaders(spec);
|
|
4015
|
+
const body = this.buildBody(spec);
|
|
4016
|
+
const context = this.describe(spec);
|
|
4017
|
+
const secrets = this.secretValues();
|
|
4018
|
+
const signal = AbortSignal.timeout(this.timeoutMs);
|
|
4019
|
+
let response;
|
|
4020
|
+
let text;
|
|
4021
|
+
try {
|
|
4022
|
+
response = await this.fetchImpl(url, {
|
|
4023
|
+
method: spec.method,
|
|
4024
|
+
headers,
|
|
4025
|
+
body: body ?? null,
|
|
4026
|
+
signal,
|
|
4027
|
+
redirect: "error"
|
|
4028
|
+
});
|
|
4029
|
+
text = await response.text();
|
|
4030
|
+
} catch (cause) {
|
|
4031
|
+
const unconfirmedWrite = spec.method !== "GET" && response?.ok === true;
|
|
4032
|
+
const error = errorFromNetwork({
|
|
4033
|
+
provider: this.provider,
|
|
4034
|
+
context,
|
|
4035
|
+
cause,
|
|
4036
|
+
secrets,
|
|
4037
|
+
response,
|
|
4038
|
+
timeoutMs: this.timeoutMs,
|
|
4039
|
+
retryable: !unconfirmedWrite
|
|
4040
|
+
});
|
|
4041
|
+
if (unconfirmedWrite) throw error;
|
|
4042
|
+
return { kind: "failure", error, retryAfterMs: response && this.serverDelay(response) };
|
|
4043
|
+
}
|
|
4044
|
+
let json;
|
|
4045
|
+
if (text.length > 0) {
|
|
4046
|
+
try {
|
|
4047
|
+
json = JSON.parse(text);
|
|
4048
|
+
} catch {
|
|
4049
|
+
if (response.ok) throw this.invalidResponse(response, "Expected a JSON response");
|
|
4050
|
+
}
|
|
4051
|
+
}
|
|
4052
|
+
if (!text.length && response.ok && response.status !== 204 && response.status !== 205) {
|
|
4053
|
+
throw this.invalidResponse(response, "Expected a JSON response");
|
|
4054
|
+
}
|
|
4055
|
+
if (!response.ok) {
|
|
4056
|
+
const error = errorFromResponse({
|
|
4057
|
+
provider: this.provider,
|
|
4058
|
+
status: response.status,
|
|
4059
|
+
headers: response.headers,
|
|
4060
|
+
body: json,
|
|
4061
|
+
context,
|
|
4062
|
+
secrets
|
|
4063
|
+
});
|
|
4064
|
+
return { kind: "failure", error, retryAfterMs: this.serverDelay(response) };
|
|
4065
|
+
}
|
|
4066
|
+
return {
|
|
4067
|
+
kind: "response",
|
|
4068
|
+
response: { status: response.status, headers: response.headers, json }
|
|
4069
|
+
};
|
|
4070
|
+
}
|
|
4071
|
+
/** Honor delay headers even if reading the response body failed. */
|
|
4072
|
+
serverDelay(response) {
|
|
4073
|
+
return this.provider === "github" && isRateLimitResponse(response.status, response.headers) ? retryAfterFromHeaders(response.headers) : parseRetryAfter(response.headers.get("retry-after"));
|
|
4074
|
+
}
|
|
4075
|
+
/**
|
|
4076
|
+
* Builds the concrete URL, including the token when query-style auth is on.
|
|
4077
|
+
*
|
|
4078
|
+
* `describe()` redacts the result, so the token exists only inside this
|
|
4079
|
+
* function's return value and never reaches a message on its own.
|
|
4080
|
+
*/
|
|
4081
|
+
buildUrl(spec) {
|
|
4082
|
+
let path;
|
|
4083
|
+
try {
|
|
4084
|
+
path = substitutePath(spec.path, spec.pathParams);
|
|
4085
|
+
} catch {
|
|
4086
|
+
throw invalidArgumentError("Invalid request path or path parameter", this.provider);
|
|
4087
|
+
}
|
|
4088
|
+
const url = new URL(this.#base.href);
|
|
4089
|
+
const prefix = stripTrailingSlash(this.#base.pathname);
|
|
4090
|
+
url.pathname = prefix + path;
|
|
4091
|
+
if (url.origin !== this.#base.origin || !url.pathname.startsWith(`${prefix}/`) || url.pathname !== prefix + path) {
|
|
4092
|
+
throw invalidArgumentError("Invalid request path for the configured API base", this.provider);
|
|
4093
|
+
}
|
|
4094
|
+
const queryParams = {
|
|
4095
|
+
...spec.query ?? {}
|
|
4096
|
+
};
|
|
4097
|
+
if (spec.paramsIn === "query" && spec.body !== void 0 && typeof spec.body === "object") {
|
|
4098
|
+
for (const [key, value] of Object.entries(spec.body)) {
|
|
4099
|
+
if (value !== void 0 && value !== null) {
|
|
4100
|
+
queryParams[key] = value;
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
}
|
|
4104
|
+
for (const [key, value] of Object.entries(queryParams)) {
|
|
4105
|
+
if (value !== void 0) {
|
|
4106
|
+
url.searchParams.set(key, String(value));
|
|
4107
|
+
}
|
|
4108
|
+
}
|
|
4109
|
+
if (this.auth?.style === "query" && this.token !== void 0 && this.token.length > 0) {
|
|
4110
|
+
url.searchParams.set(this.auth.param ?? "access_token", this.token);
|
|
4111
|
+
}
|
|
4112
|
+
return url.toString();
|
|
4113
|
+
}
|
|
4114
|
+
/** Assembles request headers, injecting auth when bearer-style. */
|
|
4115
|
+
buildHeaders(spec) {
|
|
4116
|
+
const headers = {
|
|
4117
|
+
"user-agent": this.userAgent,
|
|
4118
|
+
...lowercaseKeys(this.defaultHeaders)
|
|
4119
|
+
};
|
|
4120
|
+
if (this.auth?.style !== "query" && this.token !== void 0 && this.token.length > 0) {
|
|
4121
|
+
headers.authorization = `Bearer ${this.token}`;
|
|
4122
|
+
}
|
|
4123
|
+
const hasBody = spec.body !== void 0 && spec.paramsIn !== "query";
|
|
4124
|
+
if (hasBody && headers["content-type"] === void 0) {
|
|
4125
|
+
headers["content-type"] = spec.paramsIn === "form" ? "application/x-www-form-urlencoded" : "application/json";
|
|
4126
|
+
}
|
|
4127
|
+
if (spec.headers !== void 0) {
|
|
4128
|
+
Object.assign(headers, lowercaseKeys(spec.headers));
|
|
4129
|
+
}
|
|
4130
|
+
return headers;
|
|
4131
|
+
}
|
|
4132
|
+
/** Encodes the body per `paramsIn`. */
|
|
4133
|
+
buildBody(spec) {
|
|
4134
|
+
if (spec.body === void 0 || spec.paramsIn === "query") {
|
|
4135
|
+
return void 0;
|
|
4136
|
+
}
|
|
4137
|
+
if (spec.paramsIn === "form") {
|
|
4138
|
+
const params = new URLSearchParams();
|
|
4139
|
+
for (const [key, value] of Object.entries(spec.body)) {
|
|
4140
|
+
if (value === void 0 || value === null) {
|
|
4141
|
+
continue;
|
|
4142
|
+
}
|
|
4143
|
+
params.set(key, encodeFormValue(value));
|
|
4144
|
+
}
|
|
4145
|
+
return params.toString();
|
|
4146
|
+
}
|
|
4147
|
+
return JSON.stringify(spec.body);
|
|
4148
|
+
}
|
|
4149
|
+
/** Builds a non-replayable error when success cannot be confirmed; uses header metadata only. */
|
|
4150
|
+
invalidResponse(response, message) {
|
|
4151
|
+
return new GitForgeError({
|
|
4152
|
+
code: "PROVIDER_ERROR",
|
|
4153
|
+
provider: this.provider,
|
|
4154
|
+
status: response.status,
|
|
4155
|
+
requestId: extractRequestId(response.headers),
|
|
4156
|
+
message,
|
|
4157
|
+
secrets: this.secretValues()
|
|
4158
|
+
});
|
|
4159
|
+
}
|
|
4160
|
+
/** Known token values, for scrubbing error messages. */
|
|
4161
|
+
secretValues() {
|
|
4162
|
+
if (!this.token) return [];
|
|
4163
|
+
return [
|
|
4164
|
+
this.token,
|
|
4165
|
+
encodeURIComponent(this.token),
|
|
4166
|
+
new URLSearchParams([["v", this.token]]).toString().slice(2)
|
|
4167
|
+
];
|
|
4168
|
+
}
|
|
4169
|
+
};
|
|
4170
|
+
function substitutePath(template, params) {
|
|
4171
|
+
if (!template.startsWith("/") || /[?#\\]/.test(template))
|
|
4172
|
+
throw invalidArgumentError("Expected an absolute API path without query or fragment");
|
|
4173
|
+
if (template === "/") return template;
|
|
4174
|
+
return "/" + template.slice(1).split("/").map((segment) => {
|
|
4175
|
+
const key = /^\{(\w+)\}$/.exec(segment)?.[1];
|
|
4176
|
+
if (key !== void 0) {
|
|
4177
|
+
const value = params?.[key];
|
|
4178
|
+
if (["path", "ref", "branch"].includes(key)) {
|
|
4179
|
+
assertFilePath(key, value);
|
|
4180
|
+
return value.split("/").map(encodeURIComponent).join("/");
|
|
4181
|
+
}
|
|
4182
|
+
assertPathSegment(key, value);
|
|
4183
|
+
return encodeURIComponent(value);
|
|
4184
|
+
}
|
|
4185
|
+
try {
|
|
4186
|
+
assertPathSegment("path", decodeURIComponent(segment));
|
|
4187
|
+
} catch {
|
|
4188
|
+
throw invalidArgumentError("Invalid API path segment");
|
|
4189
|
+
}
|
|
4190
|
+
return segment;
|
|
4191
|
+
}).join("/");
|
|
4192
|
+
}
|
|
4193
|
+
function lowercaseKeys(record) {
|
|
4194
|
+
const out = {};
|
|
4195
|
+
for (const [key, value] of Object.entries(record)) {
|
|
4196
|
+
out[key.toLowerCase()] = value;
|
|
4197
|
+
}
|
|
4198
|
+
return out;
|
|
4199
|
+
}
|
|
4200
|
+
function encodeFormValue(value) {
|
|
4201
|
+
if (Array.isArray(value)) {
|
|
4202
|
+
return value.map((entry) => stringifyScalar(entry)).join(",");
|
|
4203
|
+
}
|
|
4204
|
+
if (typeof value === "object" && value !== null) {
|
|
4205
|
+
return JSON.stringify(value);
|
|
4206
|
+
}
|
|
4207
|
+
return stringifyScalar(value);
|
|
4208
|
+
}
|
|
4209
|
+
function stringifyScalar(value) {
|
|
4210
|
+
if (typeof value === "string") {
|
|
4211
|
+
return value;
|
|
4212
|
+
}
|
|
4213
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
4214
|
+
return String(value);
|
|
4215
|
+
}
|
|
4216
|
+
if (value === null) {
|
|
4217
|
+
return "";
|
|
4218
|
+
}
|
|
4219
|
+
return JSON.stringify(value) ?? "";
|
|
4220
|
+
}
|
|
4221
|
+
function stripTrailingSlash(value) {
|
|
4222
|
+
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
4223
|
+
}
|
|
4224
|
+
|
|
4225
|
+
// src/client.ts
|
|
4226
|
+
var DEFAULT_ENDPOINTS = {
|
|
4227
|
+
github: { api: "https://api.github.com", web: "https://github.com" },
|
|
4228
|
+
gitee: { api: "https://gitee.com/api/v5", web: "https://gitee.com" },
|
|
4229
|
+
atomgit: { api: "https://api.atomgit.com/api/v5", web: "https://atomgit.com" }
|
|
4230
|
+
};
|
|
4231
|
+
var TOKEN_ENV_VARS = {
|
|
4232
|
+
github: "GITHUB_TOKEN",
|
|
4233
|
+
gitee: "GITEE_TOKEN",
|
|
4234
|
+
atomgit: "ATOMGIT_TOKEN"
|
|
4235
|
+
};
|
|
4236
|
+
function assertEndpoint(provider, field, value) {
|
|
4237
|
+
if (value === void 0) {
|
|
4238
|
+
return;
|
|
4239
|
+
}
|
|
4240
|
+
if (typeof value !== "string")
|
|
4241
|
+
throw invalidArgumentError(`Invalid ${provider}.${field}: expected a URL string`, provider);
|
|
4242
|
+
let parsed;
|
|
4243
|
+
try {
|
|
4244
|
+
parsed = new URL(value);
|
|
4245
|
+
} catch {
|
|
4246
|
+
throw invalidArgumentError(
|
|
4247
|
+
`Invalid \`${provider}.${field}\`: expected an absolute URL`,
|
|
4248
|
+
provider
|
|
4249
|
+
);
|
|
4250
|
+
}
|
|
4251
|
+
const isLoopback = parsed.hostname === "localhost" || parsed.hostname === "127.0.0.1" || parsed.hostname === "[::1]" || parsed.hostname === "::1";
|
|
4252
|
+
if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && isLoopback)) {
|
|
4253
|
+
throw invalidArgumentError(
|
|
4254
|
+
`Invalid \`${provider}.${field}\`: must use https: (http: is allowed for localhost only)`,
|
|
4255
|
+
provider
|
|
4256
|
+
);
|
|
4257
|
+
}
|
|
4258
|
+
}
|
|
4259
|
+
function trimSlash(value) {
|
|
4260
|
+
return value.endsWith("/") ? value.slice(0, -1) : value;
|
|
4261
|
+
}
|
|
4262
|
+
function validateListFilters(params) {
|
|
4263
|
+
assertStringArray("labels", params.labels);
|
|
4264
|
+
assertMaxPages(params.maxPages);
|
|
4265
|
+
}
|
|
4266
|
+
function validateListIssues(params) {
|
|
4267
|
+
assertRepoRef(params);
|
|
4268
|
+
assertOptionalBoolean("requireComplete", params.requireComplete);
|
|
4269
|
+
assertOptionalOneOf("state", params.state, ["open", "closed", "all"]);
|
|
4270
|
+
assertOptionalPositiveInt("milestone", params.milestone);
|
|
4271
|
+
validateListFilters(params);
|
|
4272
|
+
}
|
|
4273
|
+
function validateGetIssue(params) {
|
|
4274
|
+
assertRepoRef(params);
|
|
4275
|
+
assertNativeId("number", params.number);
|
|
4276
|
+
}
|
|
4277
|
+
function validateCreateIssue(params) {
|
|
4278
|
+
assertRepoRef(params);
|
|
4279
|
+
assertNonEmpty("title", params.title);
|
|
4280
|
+
assertOptionalString("body", params.body);
|
|
4281
|
+
assertOptionalPositiveInt("milestone", params.milestone);
|
|
4282
|
+
assertStringArray("labels", params.labels);
|
|
4283
|
+
}
|
|
4284
|
+
function validateUpdateIssue(params) {
|
|
4285
|
+
assertRepoRef(params);
|
|
4286
|
+
assertNativeId("number", params.number);
|
|
4287
|
+
assertOptionalNonEmpty("title", params.title);
|
|
4288
|
+
assertOptionalString("body", params.body);
|
|
4289
|
+
assertOptionalOneOf("state", params.state, ["open", "closed"]);
|
|
4290
|
+
assertOptionalPositiveInt("milestone", params.milestone);
|
|
4291
|
+
assertStringArray("labels", params.labels);
|
|
4292
|
+
}
|
|
4293
|
+
function validateListComments(params) {
|
|
4294
|
+
assertRepoRef(params);
|
|
4295
|
+
assertNativeId("number", params.number);
|
|
4296
|
+
assertMaxPages(params.maxPages);
|
|
4297
|
+
}
|
|
4298
|
+
function validateGetPullRequest(params) {
|
|
4299
|
+
assertRepoRef(params);
|
|
4300
|
+
if (!Number.isSafeInteger(params.number) || params.number <= 0) {
|
|
4301
|
+
throw invalidArgumentError("Invalid `number`: expected a positive safe integer");
|
|
4302
|
+
}
|
|
4303
|
+
}
|
|
4304
|
+
function validateListPullRequests(params) {
|
|
4305
|
+
assertRepoRef(params);
|
|
4306
|
+
assertOptionalOneOf("state", params.state, ["open", "closed", "all"]);
|
|
4307
|
+
assertOptionalNonEmpty("head", params.head);
|
|
4308
|
+
assertOptionalNonEmpty("base", params.base);
|
|
4309
|
+
assertOptionalPositiveInt("milestone", params.milestone);
|
|
4310
|
+
validateListFilters(params);
|
|
4311
|
+
}
|
|
4312
|
+
function validateCreatePullRequest(params) {
|
|
4313
|
+
assertRepoRef(params);
|
|
4314
|
+
assertNonEmpty("title", params.title);
|
|
4315
|
+
assertNonEmpty("head", params.head);
|
|
4316
|
+
assertNonEmpty("base", params.base);
|
|
4317
|
+
assertOptionalString("body", params.body);
|
|
4318
|
+
assertOptionalBoolean("draft", params.draft);
|
|
4319
|
+
}
|
|
4320
|
+
function validateCreateComment(params) {
|
|
4321
|
+
assertRepoRef(params);
|
|
4322
|
+
assertNativeId("number", params.number);
|
|
4323
|
+
assertNonEmpty("body", params.body);
|
|
4324
|
+
}
|
|
4325
|
+
function validateMergePullRequest(params) {
|
|
4326
|
+
validateGetPullRequest(params);
|
|
4327
|
+
assertOptionalOneOf("merge_method", params.merge_method, ["merge", "squash", "rebase"]);
|
|
4328
|
+
assertOptionalString("commit_title", params.commit_title);
|
|
4329
|
+
assertOptionalString("commit_message", params.commit_message);
|
|
4330
|
+
}
|
|
4331
|
+
function validateListRepositories(params) {
|
|
4332
|
+
if (params.org !== void 0 && params.user !== void 0) {
|
|
4333
|
+
throw invalidArgumentError("Invalid `org`/`user`: provide at most one of them");
|
|
4334
|
+
}
|
|
4335
|
+
assertOptionalPathSegment("org", params.org);
|
|
4336
|
+
assertOptionalPathSegment("user", params.user);
|
|
4337
|
+
assertMaxPages(params.maxPages);
|
|
4338
|
+
}
|
|
4339
|
+
function validateCreateRepository(params) {
|
|
4340
|
+
assertNonEmpty("name", params.name);
|
|
4341
|
+
assertOptionalString("description", params.description);
|
|
4342
|
+
assertOptionalBoolean("private", params.private);
|
|
4343
|
+
assertOptionalBoolean("auto_init", params.auto_init);
|
|
4344
|
+
assertOptionalPathSegment("org", params.org);
|
|
4345
|
+
}
|
|
4346
|
+
function validateGetFileContents(params) {
|
|
4347
|
+
assertRepoRef(params);
|
|
4348
|
+
assertFilePath("path", params.path);
|
|
4349
|
+
assertOptionalNonEmpty("ref", params.ref);
|
|
4350
|
+
}
|
|
4351
|
+
function validateCreateOrUpdateFile(params) {
|
|
4352
|
+
assertRepoRef(params);
|
|
4353
|
+
assertFilePath("path", params.path);
|
|
4354
|
+
if (typeof params.content !== "string") {
|
|
4355
|
+
throw invalidArgumentError("Invalid `content`: expected a string");
|
|
4356
|
+
}
|
|
4357
|
+
assertNonEmpty("message", params.message);
|
|
4358
|
+
assertOptionalNonEmpty("branch", params.branch);
|
|
4359
|
+
assertOptionalNonEmpty("sha", params.sha);
|
|
4360
|
+
assertOptionalOneOf("encoding", params.encoding, ["base64", "utf8"]);
|
|
4361
|
+
}
|
|
4362
|
+
function validateDeleteFile(params) {
|
|
4363
|
+
assertRepoRef(params);
|
|
4364
|
+
assertFilePath("path", params.path);
|
|
4365
|
+
assertNonEmpty("message", params.message);
|
|
4366
|
+
assertNonEmpty("sha", params.sha);
|
|
4367
|
+
assertOptionalNonEmpty("branch", params.branch);
|
|
4368
|
+
}
|
|
4369
|
+
function validatePushFiles(params) {
|
|
4370
|
+
assertRepoRef(params);
|
|
4371
|
+
assertPushFilesParams(params);
|
|
4372
|
+
}
|
|
4373
|
+
function validateCreateBranch(params) {
|
|
4374
|
+
assertRepoRef(params);
|
|
4375
|
+
assertNonEmpty("name", params.name);
|
|
4376
|
+
assertNonEmpty("from", params.from);
|
|
4377
|
+
}
|
|
4378
|
+
function validateListCommits(params) {
|
|
4379
|
+
assertRepoRef(params);
|
|
4380
|
+
assertOptionalNonEmpty("branch", params.branch);
|
|
4381
|
+
assertMaxPages(params.maxPages);
|
|
4382
|
+
}
|
|
4383
|
+
function validateGetCommit(params) {
|
|
4384
|
+
assertRepoRef(params);
|
|
4385
|
+
assertNonEmpty("sha", params.sha);
|
|
4386
|
+
}
|
|
4387
|
+
var GitClient = class {
|
|
4388
|
+
/** Whether write capabilities are refused. */
|
|
4389
|
+
readonly;
|
|
4390
|
+
providers;
|
|
4391
|
+
constructor(options = {}) {
|
|
4392
|
+
this.readonly = options.readonly === true;
|
|
4393
|
+
this.providers = {
|
|
4394
|
+
github: this.resolve("github", options),
|
|
4395
|
+
gitee: this.resolve("gitee", options),
|
|
4396
|
+
atomgit: this.resolve("atomgit", options)
|
|
4397
|
+
};
|
|
4398
|
+
}
|
|
4399
|
+
/** Providers that have a token configured. Names only — never values. */
|
|
4400
|
+
configuredProviders() {
|
|
4401
|
+
return ["github", "gitee", "atomgit"].filter(
|
|
4402
|
+
(provider) => this.providers[provider].hasToken
|
|
4403
|
+
);
|
|
4404
|
+
}
|
|
4405
|
+
/** Capability table of one provider, for callers that render a matrix. */
|
|
4406
|
+
capabilities(provider) {
|
|
4407
|
+
assertProvider(provider);
|
|
4408
|
+
return this.providers[provider].adapter.capabilities;
|
|
4409
|
+
}
|
|
4410
|
+
/** Parameter-level capability table of one provider. */
|
|
4411
|
+
paramCapabilities(provider) {
|
|
4412
|
+
assertProvider(provider);
|
|
4413
|
+
return this.providers[provider].adapter.paramCapabilities;
|
|
4414
|
+
}
|
|
4415
|
+
/* ---------------------------------------------------------------------- */
|
|
4416
|
+
/* Issue */
|
|
4417
|
+
/* ---------------------------------------------------------------------- */
|
|
4418
|
+
/** Lists issues, aggregating every page. */
|
|
4419
|
+
listIssues(params) {
|
|
4420
|
+
return this.dispatch(
|
|
4421
|
+
"listIssues",
|
|
4422
|
+
"issues.list",
|
|
4423
|
+
params,
|
|
4424
|
+
validateListIssues,
|
|
4425
|
+
(p, ctx) => ctx.adapter.listIssues(ctx.ctx, p)
|
|
4426
|
+
);
|
|
4427
|
+
}
|
|
4428
|
+
/** Reads one issue. */
|
|
4429
|
+
getIssue(params) {
|
|
4430
|
+
return this.dispatch(
|
|
4431
|
+
"getIssue",
|
|
4432
|
+
"issues.get",
|
|
4433
|
+
params,
|
|
4434
|
+
validateGetIssue,
|
|
4435
|
+
(p, ctx) => ctx.adapter.getIssue(ctx.ctx, p)
|
|
4436
|
+
);
|
|
4437
|
+
}
|
|
4438
|
+
/** Creates an issue. */
|
|
4439
|
+
createIssue(params) {
|
|
4440
|
+
return this.dispatch(
|
|
4441
|
+
"createIssue",
|
|
4442
|
+
"issues.create",
|
|
4443
|
+
params,
|
|
4444
|
+
validateCreateIssue,
|
|
4445
|
+
(p, ctx) => ctx.adapter.createIssue(ctx.ctx, p)
|
|
4446
|
+
);
|
|
4447
|
+
}
|
|
4448
|
+
/** Updates an issue, leaving omitted fields untouched. */
|
|
4449
|
+
updateIssue(params) {
|
|
4450
|
+
return this.dispatch(
|
|
4451
|
+
"updateIssue",
|
|
4452
|
+
"issues.update",
|
|
4453
|
+
params,
|
|
4454
|
+
validateUpdateIssue,
|
|
4455
|
+
(p, ctx) => ctx.adapter.updateIssue(ctx.ctx, p)
|
|
4456
|
+
);
|
|
4457
|
+
}
|
|
4458
|
+
/** Lists an issue's comments. */
|
|
4459
|
+
listIssueComments(params) {
|
|
4460
|
+
return this.dispatch(
|
|
4461
|
+
"listIssueComments",
|
|
4462
|
+
"issues.listComments",
|
|
4463
|
+
params,
|
|
4464
|
+
validateListComments,
|
|
4465
|
+
(p, ctx) => ctx.adapter.listIssueComments(ctx.ctx, p)
|
|
4466
|
+
);
|
|
4467
|
+
}
|
|
4468
|
+
/** Adds a comment to an issue. */
|
|
4469
|
+
createIssueComment(params) {
|
|
4470
|
+
return this.dispatch(
|
|
4471
|
+
"createIssueComment",
|
|
4472
|
+
"issues.createComment",
|
|
4473
|
+
params,
|
|
4474
|
+
validateCreateComment,
|
|
4475
|
+
(p, ctx) => ctx.adapter.createIssueComment(ctx.ctx, p)
|
|
4476
|
+
);
|
|
4477
|
+
}
|
|
4478
|
+
/* ---------------------------------------------------------------------- */
|
|
4479
|
+
/* Pull request */
|
|
4480
|
+
/* ---------------------------------------------------------------------- */
|
|
4481
|
+
/**
|
|
4482
|
+
* Lists pull requests.
|
|
4483
|
+
*
|
|
4484
|
+
* The `head` filter goes through the parameter capability gate: AtomGit has no
|
|
4485
|
+
* such filter, and returning an unfiltered list would be undetectable.
|
|
4486
|
+
*/
|
|
4487
|
+
listPullRequests(params) {
|
|
4488
|
+
return this.dispatch(
|
|
4489
|
+
"listPullRequests",
|
|
4490
|
+
"pulls.list",
|
|
4491
|
+
params,
|
|
4492
|
+
validateListPullRequests,
|
|
4493
|
+
(p, ctx) => ctx.adapter.listPullRequests(ctx.ctx, p),
|
|
4494
|
+
(p, ctx) => {
|
|
4495
|
+
if (p.head !== void 0) {
|
|
4496
|
+
this.assertParamCapability(ctx, "pulls.list.headFilter");
|
|
4497
|
+
}
|
|
4498
|
+
if (p.milestone !== void 0) {
|
|
4499
|
+
this.assertParamCapability(ctx, "pulls.list.milestoneFilter");
|
|
4500
|
+
}
|
|
4501
|
+
}
|
|
4502
|
+
);
|
|
4503
|
+
}
|
|
4504
|
+
/** Reads one pull request. */
|
|
4505
|
+
getPullRequest(params) {
|
|
4506
|
+
return this.dispatch(
|
|
4507
|
+
"getPullRequest",
|
|
4508
|
+
"pulls.get",
|
|
4509
|
+
params,
|
|
4510
|
+
validateGetPullRequest,
|
|
4511
|
+
(p, ctx) => ctx.adapter.getPullRequest(ctx.ctx, p)
|
|
4512
|
+
);
|
|
4513
|
+
}
|
|
4514
|
+
/** Opens a pull request. */
|
|
4515
|
+
createPullRequest(params) {
|
|
4516
|
+
return this.dispatch(
|
|
4517
|
+
"createPullRequest",
|
|
4518
|
+
"pulls.create",
|
|
4519
|
+
params,
|
|
4520
|
+
validateCreatePullRequest,
|
|
4521
|
+
(p, ctx) => ctx.adapter.createPullRequest(ctx.ctx, p)
|
|
4522
|
+
);
|
|
4523
|
+
}
|
|
4524
|
+
/** Merges a pull request and returns its post-merge state. */
|
|
4525
|
+
mergePullRequest(params) {
|
|
4526
|
+
return this.dispatch(
|
|
4527
|
+
"mergePullRequest",
|
|
4528
|
+
"pulls.merge",
|
|
4529
|
+
params,
|
|
4530
|
+
validateMergePullRequest,
|
|
4531
|
+
(p, ctx) => ctx.adapter.mergePullRequest(ctx.ctx, p)
|
|
4532
|
+
);
|
|
4533
|
+
}
|
|
4534
|
+
/** Lists a pull request's comments. */
|
|
4535
|
+
listPullRequestComments(params) {
|
|
4536
|
+
return this.dispatch(
|
|
4537
|
+
"listPullRequestComments",
|
|
4538
|
+
"pulls.listComments",
|
|
4539
|
+
params,
|
|
4540
|
+
validateListComments,
|
|
4541
|
+
(p, ctx) => ctx.adapter.listPullRequestComments(ctx.ctx, p)
|
|
4542
|
+
);
|
|
4543
|
+
}
|
|
4544
|
+
/** Adds a comment to a pull request. */
|
|
4545
|
+
createPullRequestComment(params) {
|
|
4546
|
+
return this.dispatch(
|
|
4547
|
+
"createPullRequestComment",
|
|
4548
|
+
"pulls.createComment",
|
|
4549
|
+
params,
|
|
4550
|
+
validateCreateComment,
|
|
4551
|
+
(p, ctx) => ctx.adapter.createPullRequestComment(ctx.ctx, p)
|
|
4552
|
+
);
|
|
4553
|
+
}
|
|
4554
|
+
/* ---------------------------------------------------------------------- */
|
|
4555
|
+
/* Repository */
|
|
4556
|
+
/* ---------------------------------------------------------------------- */
|
|
4557
|
+
/** Lists repositories of an org, a user, or the authenticated account. */
|
|
4558
|
+
listRepositories(params) {
|
|
4559
|
+
return this.dispatch(
|
|
4560
|
+
"listRepositories",
|
|
4561
|
+
"repos.list",
|
|
4562
|
+
params,
|
|
4563
|
+
validateListRepositories,
|
|
4564
|
+
(p, ctx) => ctx.adapter.listRepositories(ctx.ctx, p)
|
|
4565
|
+
);
|
|
4566
|
+
}
|
|
4567
|
+
/** Reads one repository. */
|
|
4568
|
+
getRepository(params) {
|
|
4569
|
+
return this.dispatch(
|
|
4570
|
+
"getRepository",
|
|
4571
|
+
"repos.get",
|
|
4572
|
+
params,
|
|
4573
|
+
assertRepoRef,
|
|
4574
|
+
(p, ctx) => ctx.adapter.getRepository(ctx.ctx, p)
|
|
4575
|
+
);
|
|
4576
|
+
}
|
|
4577
|
+
/** Creates a repository. */
|
|
4578
|
+
createRepository(params) {
|
|
4579
|
+
return this.dispatch(
|
|
4580
|
+
"createRepository",
|
|
4581
|
+
"repos.create",
|
|
4582
|
+
params,
|
|
4583
|
+
validateCreateRepository,
|
|
4584
|
+
(p, ctx) => ctx.adapter.createRepository(ctx.ctx, p)
|
|
4585
|
+
);
|
|
4586
|
+
}
|
|
4587
|
+
/** Reads one file's content. Directory paths are rejected by the adapters. */
|
|
4588
|
+
getFileContents(params) {
|
|
4589
|
+
return this.dispatch(
|
|
4590
|
+
"getFileContents",
|
|
4591
|
+
"repos.getFileContents",
|
|
4592
|
+
params,
|
|
4593
|
+
validateGetFileContents,
|
|
4594
|
+
(p, ctx) => ctx.adapter.getFileContents(ctx.ctx, p)
|
|
4595
|
+
);
|
|
4596
|
+
}
|
|
4597
|
+
/* ---------------------------------------------------------------------- */
|
|
4598
|
+
/* Files */
|
|
4599
|
+
/* ---------------------------------------------------------------------- */
|
|
4600
|
+
/**
|
|
4601
|
+
* Creates or replaces one file.
|
|
4602
|
+
*
|
|
4603
|
+
* `sha` selects the endpoint: absent means create, present means update. The
|
|
4604
|
+
* capability gate follows that choice so a provider that served one but not
|
|
4605
|
+
* the other would still be blocked correctly.
|
|
4606
|
+
*/
|
|
4607
|
+
createOrUpdateFile(params) {
|
|
4608
|
+
return this.dispatch(
|
|
4609
|
+
"createOrUpdateFile",
|
|
4610
|
+
(p) => p.sha === void 0 ? "files.create" : "files.update",
|
|
4611
|
+
params,
|
|
4612
|
+
validateCreateOrUpdateFile,
|
|
4613
|
+
(p, ctx) => p.sha === void 0 ? ctx.adapter.createFile(ctx.ctx, p) : ctx.adapter.updateFile(ctx.ctx, p)
|
|
4614
|
+
);
|
|
4615
|
+
}
|
|
4616
|
+
/** Deletes one file. */
|
|
4617
|
+
deleteFile(params) {
|
|
4618
|
+
return this.dispatch(
|
|
4619
|
+
"deleteFile",
|
|
4620
|
+
"files.delete",
|
|
4621
|
+
params,
|
|
4622
|
+
validateDeleteFile,
|
|
4623
|
+
(p, ctx) => ctx.adapter.deleteFile(ctx.ctx, p)
|
|
4624
|
+
);
|
|
4625
|
+
}
|
|
4626
|
+
/**
|
|
4627
|
+
* Commits every entry of a batch in one commit.
|
|
4628
|
+
*
|
|
4629
|
+
* AtomGit cannot do this and is refused by the capability gate; the batch is
|
|
4630
|
+
* never split into N single-file commits.
|
|
4631
|
+
*/
|
|
4632
|
+
pushFiles(params) {
|
|
4633
|
+
return this.dispatch(
|
|
4634
|
+
"pushFiles",
|
|
4635
|
+
"files.pushFiles",
|
|
4636
|
+
params,
|
|
4637
|
+
validatePushFiles,
|
|
4638
|
+
(p, ctx) => ctx.adapter.pushFiles(ctx.ctx, p)
|
|
4639
|
+
);
|
|
4640
|
+
}
|
|
4641
|
+
/* ---------------------------------------------------------------------- */
|
|
4642
|
+
/* Branch / Commit */
|
|
4643
|
+
/* ---------------------------------------------------------------------- */
|
|
4644
|
+
/** Lists branches. */
|
|
4645
|
+
listBranches(params) {
|
|
4646
|
+
return this.dispatch(
|
|
4647
|
+
"listBranches",
|
|
4648
|
+
"branches.list",
|
|
4649
|
+
params,
|
|
4650
|
+
(p) => {
|
|
4651
|
+
assertRepoRef(p);
|
|
4652
|
+
assertMaxPages(p.maxPages);
|
|
4653
|
+
},
|
|
4654
|
+
(p, ctx) => ctx.adapter.listBranches(ctx.ctx, p)
|
|
4655
|
+
);
|
|
4656
|
+
}
|
|
4657
|
+
/** Creates a branch from a branch name or commit sha. */
|
|
4658
|
+
createBranch(params) {
|
|
4659
|
+
return this.dispatch(
|
|
4660
|
+
"createBranch",
|
|
4661
|
+
"branches.create",
|
|
4662
|
+
params,
|
|
4663
|
+
validateCreateBranch,
|
|
4664
|
+
(p, ctx) => ctx.adapter.createBranch(ctx.ctx, p)
|
|
4665
|
+
);
|
|
4666
|
+
}
|
|
4667
|
+
/** Lists commits. */
|
|
4668
|
+
listCommits(params) {
|
|
4669
|
+
return this.dispatch(
|
|
4670
|
+
"listCommits",
|
|
4671
|
+
"commits.list",
|
|
4672
|
+
params,
|
|
4673
|
+
validateListCommits,
|
|
4674
|
+
(p, ctx) => ctx.adapter.listCommits(ctx.ctx, p)
|
|
4675
|
+
);
|
|
4676
|
+
}
|
|
4677
|
+
/** Reads one commit. */
|
|
4678
|
+
getCommit(params) {
|
|
4679
|
+
return this.dispatch(
|
|
4680
|
+
"getCommit",
|
|
4681
|
+
"commits.get",
|
|
4682
|
+
params,
|
|
4683
|
+
validateGetCommit,
|
|
4684
|
+
(p, ctx) => ctx.adapter.getCommit(ctx.ctx, p)
|
|
4685
|
+
);
|
|
4686
|
+
}
|
|
4687
|
+
/* ---------------------------------------------------------------------- */
|
|
4688
|
+
/* Bulk */
|
|
4689
|
+
/* ---------------------------------------------------------------------- */
|
|
4690
|
+
/**
|
|
4691
|
+
* Runs a task per item with partial-failure isolation.
|
|
4692
|
+
*
|
|
4693
|
+
* Delegates to `core/bulk.ts` so the batching semantics (bounded concurrency,
|
|
4694
|
+
* `{ok, index}` results, never rejecting) are identical to the standalone
|
|
4695
|
+
* helper.
|
|
4696
|
+
*/
|
|
4697
|
+
bulk(items, run, options) {
|
|
4698
|
+
return bulk(items, run, options);
|
|
4699
|
+
}
|
|
4700
|
+
/* ---------------------------------------------------------------------- */
|
|
4701
|
+
/* Internals */
|
|
4702
|
+
/* ---------------------------------------------------------------------- */
|
|
4703
|
+
/** Builds the per-provider bundle consumed by every method. */
|
|
4704
|
+
resolve(provider, options) {
|
|
4705
|
+
const credentials = options[provider] ?? {};
|
|
4706
|
+
const defaults = DEFAULT_ENDPOINTS[provider];
|
|
4707
|
+
assertEndpoint(provider, "baseUrl", credentials.baseUrl);
|
|
4708
|
+
assertEndpoint(provider, "webBaseUrl", credentials.webBaseUrl);
|
|
4709
|
+
const baseUrl = trimSlash(credentials.baseUrl ?? defaults.api);
|
|
4710
|
+
const webBaseUrl = trimSlash(credentials.webBaseUrl ?? defaults.web);
|
|
4711
|
+
const token = credentials.token !== void 0 && credentials.token.length > 0 ? credentials.token : void 0;
|
|
4712
|
+
const http = new HttpClient({
|
|
4713
|
+
baseUrl,
|
|
4714
|
+
provider,
|
|
4715
|
+
// All provider tokens travel in the Authorization header.
|
|
4716
|
+
auth: { style: "bearer" },
|
|
4717
|
+
token,
|
|
4718
|
+
fetch: options.fetch,
|
|
4719
|
+
sleep: options.sleep,
|
|
4720
|
+
random: options.random,
|
|
4721
|
+
timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
4722
|
+
retry: options.retry,
|
|
4723
|
+
userAgent: options.userAgent ?? DEFAULT_USER_AGENT
|
|
4724
|
+
});
|
|
4725
|
+
const adapter = getAdapter(provider);
|
|
4726
|
+
return {
|
|
4727
|
+
provider,
|
|
4728
|
+
adapter,
|
|
4729
|
+
tokenEnvVar: TOKEN_ENV_VARS[provider],
|
|
4730
|
+
hasToken: http.hasToken,
|
|
4731
|
+
ctx: {
|
|
4732
|
+
http,
|
|
4733
|
+
caps: adapter.capabilities,
|
|
4734
|
+
paramCaps: adapter.paramCapabilities,
|
|
4735
|
+
webBaseUrl
|
|
4736
|
+
}
|
|
4737
|
+
};
|
|
4738
|
+
}
|
|
4739
|
+
/** Throws unless a parameter-level capability is supported. */
|
|
4740
|
+
assertParamCapability(ctx, capability) {
|
|
4741
|
+
if (ctx.ctx.paramCaps[capability] !== "supported") {
|
|
4742
|
+
throw unsupportedCapabilityError(ctx.provider, capability);
|
|
4743
|
+
}
|
|
4744
|
+
}
|
|
4745
|
+
/**
|
|
4746
|
+
* Runs the five gates in order, then dispatches to the adapter.
|
|
4747
|
+
*
|
|
4748
|
+
* `async` on purpose: a gate failure must reject the returned promise rather
|
|
4749
|
+
* than throw synchronously, so `await`/`.catch()` behave identically for a
|
|
4750
|
+
* refused call and for a failed request.
|
|
4751
|
+
*
|
|
4752
|
+
* @param method Method name, used in validation and read-only messages.
|
|
4753
|
+
* @param capabilityFor Method-level capability, or a selector evaluated after validation.
|
|
4754
|
+
* @param params Caller payload; `provider` is read from it.
|
|
4755
|
+
* @param validate Parameter validator, run before any other gate.
|
|
4756
|
+
* @param run Adapter call, reached only when every gate passes.
|
|
4757
|
+
* @param paramGate Optional parameter-level capability check.
|
|
4758
|
+
*/
|
|
4759
|
+
async dispatch(method, capabilityFor, params, validate, run, paramGate) {
|
|
4760
|
+
assertParamsObject(params, method);
|
|
4761
|
+
assertProvider(params.provider);
|
|
4762
|
+
try {
|
|
4763
|
+
validate(params);
|
|
4764
|
+
} catch (error) {
|
|
4765
|
+
throw withProvider(error, params.provider);
|
|
4766
|
+
}
|
|
4767
|
+
const capability = typeof capabilityFor === "function" ? capabilityFor(params) : capabilityFor;
|
|
4768
|
+
const ctx = this.providers[params.provider];
|
|
4769
|
+
if (this.readonly && isWriteCapability(capability)) {
|
|
4770
|
+
throw readonlyBlockedError(params.provider, capability, method);
|
|
4771
|
+
}
|
|
4772
|
+
if (!ctx.hasToken) {
|
|
4773
|
+
throw authMissingError(params.provider, ctx.tokenEnvVar);
|
|
4774
|
+
}
|
|
4775
|
+
if (ctx.adapter.capabilities[capability] !== "supported") {
|
|
4776
|
+
throw unsupportedCapabilityError(params.provider, capability);
|
|
4777
|
+
}
|
|
4778
|
+
paramGate?.(params, ctx);
|
|
4779
|
+
return run(params, ctx);
|
|
4780
|
+
}
|
|
4781
|
+
};
|
|
4782
|
+
function withProvider(error, provider) {
|
|
4783
|
+
if (error instanceof GitForgeError && error.provider === void 0) {
|
|
4784
|
+
return new GitForgeError({
|
|
4785
|
+
code: error.code,
|
|
4786
|
+
message: error.message,
|
|
4787
|
+
provider,
|
|
4788
|
+
status: error.status,
|
|
4789
|
+
requestId: error.requestId,
|
|
4790
|
+
capability: error.capability,
|
|
4791
|
+
retryable: error.retryable,
|
|
4792
|
+
cause: error.cause
|
|
4793
|
+
});
|
|
4794
|
+
}
|
|
4795
|
+
return error;
|
|
4796
|
+
}
|
|
4797
|
+
|
|
4798
|
+
// src/index.ts
|
|
4799
|
+
var VERSION = "0.1.0";
|
|
4800
|
+
|
|
4801
|
+
export { CAPABILITY_IDS, DEFAULT_BULK_CONCURRENCY, DEFAULT_MAX_PAGES, DEFAULT_PER_PAGE, DEFAULT_RETRY_POLICY, DEFAULT_TIMEOUT_MS, DEFAULT_USER_AGENT, ERROR_CODES, GitClient, GitForgeError, HttpClient, MAX_PAGES_LIMIT, MAX_PER_PAGE, PARAM_CAPABILITY_IDS, PROVIDERS, REDACTED, VERSION, WRITE_CAPABILITIES, allSucceeded, assertCapabilityTableKeys, assertFilePath, assertMaxPages, assertNativeId, assertNonEmpty, assertNonEmptyStringArray, assertOneOf, assertOptionalFilePath, assertOptionalNonEmpty, assertOptionalPositiveInt, assertParamsObject, assertPathSegment, assertPositiveInt, assertProvider, assertRepoRef, assertStringArray, authMissingError, bulk, collectPages, computeDelay, dedupeBy, defaultRetryable, errorFromNetwork, errorFromResponse, extractRequestId, extractUpstreamMessage, invalidArgumentError, isGitForgeError, isIdempotentMethod, isRateLimitResponse, isRetryableStatus, isSensitiveKey, isWriteCapability, mapStatusToCode, normalizeMaxPages, normalizePerPage, parseLinkHeader, parseRetryAfter, partitionBulk, readTotalCount, readTotalPage, readonlyBlockedError, redactUrl, resolveNextPage, resolveRetryPolicy, retryAfterFromHeaders, retryableForStatus, scrubSecrets, shouldRetry, unsupportedCapabilityError };
|
|
4802
|
+
//# sourceMappingURL=index.js.map
|
|
4803
|
+
//# sourceMappingURL=index.js.map
|