issue-timeline 0.0.0-stage → 1.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONTRIBUTING.md +67 -0
- package/LICENSE +21 -0
- package/README.md +150 -2
- package/package.json +45 -4
- package/server/src/ci.js +159 -0
- package/server/src/ci.test.js +62 -0
- package/server/src/commits.js +196 -0
- package/server/src/derive.js +91 -0
- package/server/src/github.js +65 -0
- package/server/src/github.test.js +48 -0
- package/server/src/gitlab.js +417 -0
- package/server/src/gitlab.test.js +42 -0
- package/server/src/index.js +288 -0
- package/server/src/links.js +38 -0
- package/server/src/links.test.js +39 -0
- package/server/src/model.js +127 -0
- package/server/src/store.js +47 -0
- package/server/src/sync.js +98 -0
- package/server/src/vocabulary.js +42 -0
- package/server/src/vocabulary.json +94 -0
- package/server/src/vocabulary.test.js +98 -0
- package/web/dist/assets/index-Dgupvkuw.css +1 -0
- package/web/dist/assets/index-jzJaFP5Z.js +15 -0
- package/web/dist/index.html +13 -0
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
// GitLab REST adapter. glab supplies the authenticated token; records are normalized to the UI's issue/PR shape.
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { gitlabRunOf } from "./ci.js";
|
|
4
|
+
|
|
5
|
+
const PAGE_SIZE = 100;
|
|
6
|
+
const REQUEST_TIMEOUT_MS = 60_000;
|
|
7
|
+
const MARGIN_MS = 2 * 60 * 1000;
|
|
8
|
+
const authCache = new Map();
|
|
9
|
+
|
|
10
|
+
export function parseGitLabRepo(key) {
|
|
11
|
+
const value = key.startsWith("gitlab:") ? key.slice("gitlab:".length) : key;
|
|
12
|
+
const input = value.includes("://") ? new URL(value) : new URL(`https://gitlab.com/${value}`);
|
|
13
|
+
const host = input.host;
|
|
14
|
+
const project = decodeURIComponent(input.pathname.replace(/^\/+|\/+$/g, ""));
|
|
15
|
+
if (!host || !project || project.split("/").some((s) => !s) || !/^[\w.-]+(?::\d+)?$/.test(host)) throw new Error("Invalid GitLab repository. Use group/project or the full URL.");
|
|
16
|
+
return { host, project, origin: input.origin, apiBase: `${input.protocol}//${host}/api/v4` };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The token for a host: GITLAB_TOKEN_<HOST> (for example GITLAB_TOKEN_GITLAB_MYCOMPANY_COM), then GITLAB_TOKEN, then the
|
|
21
|
+
* glab session. An environment token needs no glab, so it works in containers, CI and on machines without glab.
|
|
22
|
+
*/
|
|
23
|
+
export function tokenFromEnv(host, env = process.env) {
|
|
24
|
+
const specific = `GITLAB_TOKEN_${host.replace(/[^A-Za-z0-9]/g, "_").toUpperCase()}`;
|
|
25
|
+
return env[specific] || (host === "gitlab.com" ? env.GITLAB_TOKEN : undefined) || null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getAuth(host) {
|
|
29
|
+
const cached = authCache.get(host);
|
|
30
|
+
if (cached && cached.expiresAt > Date.now()) return cached;
|
|
31
|
+
const fromEnv = tokenFromEnv(host);
|
|
32
|
+
if (fromEnv) {
|
|
33
|
+
const auth = { token: fromEnv, apiBase: `https://${host}/api/v4`, expiresAt: Date.now() + 20 * 60 * 1000 };
|
|
34
|
+
authCache.set(host, auth);
|
|
35
|
+
return auth;
|
|
36
|
+
}
|
|
37
|
+
const result = spawnSync("glab", ["auth", "status", "--hostname", host, "--show-token"], { encoding: "utf8", timeout: 15_000, maxBuffer: 1024 * 1024 });
|
|
38
|
+
const output = `${result.stdout ?? ""}\n${result.stderr ?? ""}`;
|
|
39
|
+
const token = output.match(/Token found[^:\n]*:\s*(\S+)/i)?.[1];
|
|
40
|
+
const apiBase = output.match(/REST API Endpoint:\s*(https?:\/\/\S+)/i)?.[1]?.replace(/\/+$/, "");
|
|
41
|
+
if (!token || /^\*+$/.test(token)) throw new Error(`Could not read the glab session for ${host}. Run glab auth login --hostname ${host}.`);
|
|
42
|
+
// glab can print the credential even if its optional API health check times out.
|
|
43
|
+
const auth = { token, apiBase: apiBase ?? `https://${host}/api/v4`, expiresAt: Date.now() + 20 * 60 * 1000 };
|
|
44
|
+
authCache.set(host, auth);
|
|
45
|
+
return auth;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function createApi({ token, apiBase, fetchImpl = fetch, log = () => {}, invalidate = () => {} }) {
|
|
49
|
+
let eventCooldownUntil = 0;
|
|
50
|
+
let eventLimitLogged = false;
|
|
51
|
+
return async (path, query = {}) => {
|
|
52
|
+
const eventEndpoint = /\/resource_(?:state|label|milestone)_events(?:\/|$)/.test(path);
|
|
53
|
+
if (eventEndpoint && Date.now() < eventCooldownUntil) return { body: [], next: null };
|
|
54
|
+
const url = new URL(`${apiBase}/${path.replace(/^\//, "")}`);
|
|
55
|
+
for (const [key, value] of Object.entries(query)) if (value !== undefined && value !== null && value !== "") url.searchParams.set(key, String(value));
|
|
56
|
+
for (let attempt = 1; ; attempt++) {
|
|
57
|
+
// A request that never answers would stall the whole sync: give it a deadline, and retry like a server error.
|
|
58
|
+
let response;
|
|
59
|
+
try {
|
|
60
|
+
response = await fetchImpl(url, { headers: { authorization: `Bearer ${token}`, "user-agent": "issue-timeline" }, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) });
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (attempt < 4) { await new Promise((resolve) => setTimeout(resolve, attempt * 2000)); continue; }
|
|
63
|
+
throw new Error(`GitLab request to ${url.pathname} failed after ${attempt} tries: ${e.name}`);
|
|
64
|
+
}
|
|
65
|
+
if (response.status === 429 && eventEndpoint) {
|
|
66
|
+
eventCooldownUntil = Date.now() + (Number(response.headers.get("retry-after")) || 60) * 1000;
|
|
67
|
+
if (!eventLimitLogged) { log("GitLab rate-limited the events endpoints; skipping those events during the cooldown and continuing with issues and merge requests."); eventLimitLogged = true; }
|
|
68
|
+
return { body: [], next: null };
|
|
69
|
+
}
|
|
70
|
+
// Rate limits (429) are waited out with the server's Retry-After, longer than a transient error (5xx).
|
|
71
|
+
if ((response.status === 429 && attempt < 10) || (response.status >= 500 && attempt < 4)) {
|
|
72
|
+
const seconds = Number(response.headers.get("retry-after")) || attempt * 2;
|
|
73
|
+
await new Promise((resolve) => setTimeout(resolve, seconds * 1000));
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
// Before the budget runs out, wait for the window to reset instead of getting refused.
|
|
77
|
+
const remaining = Number(response.headers.get("ratelimit-remaining"));
|
|
78
|
+
const reset = Number(response.headers.get("ratelimit-reset"));
|
|
79
|
+
if (Number.isFinite(remaining) && remaining <= 2 && reset > Date.now() / 1000) {
|
|
80
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(reset - Date.now() / 1000, 120) * 1000));
|
|
81
|
+
}
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
if (response.status === 401) invalidate();
|
|
84
|
+
if (response.status === 404) return null;
|
|
85
|
+
throw new Error(`GitLab ${response.status} at ${url.pathname}: ${(await response.text()).slice(0, 180)}`);
|
|
86
|
+
}
|
|
87
|
+
return { body: await response.json(), next: response.headers.get("x-next-page") };
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function allPages(api, path, query = {}) {
|
|
93
|
+
const out = [];
|
|
94
|
+
let page = 1;
|
|
95
|
+
for (;;) {
|
|
96
|
+
const result = await api(path, { ...query, page, per_page: PAGE_SIZE });
|
|
97
|
+
if (!result) return out;
|
|
98
|
+
if (Array.isArray(result.body)) out.push(...result.body);
|
|
99
|
+
if (!result.next) return out;
|
|
100
|
+
page = Number(result.next);
|
|
101
|
+
if (!Number.isFinite(page) || page < 1) return out;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async function optionalPages(api, path, query = {}) {
|
|
106
|
+
try { return await allPages(api, path, query); } catch (e) {
|
|
107
|
+
if (/GitLab (?:400|403|404|405|429) /.test(String(e.message))) return [];
|
|
108
|
+
throw e;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const user = (u) => u?.username ?? u?.name ?? null;
|
|
113
|
+
const iso = (s) => s ?? null;
|
|
114
|
+
const state = (s) => s === "merged" ? "MERGED" : s === "opened" ? "OPEN" : "CLOSED";
|
|
115
|
+
|
|
116
|
+
function mappedEvents(stateEvents, labelEvents, milestoneEvents = []) {
|
|
117
|
+
const out = [];
|
|
118
|
+
for (const e of stateEvents) {
|
|
119
|
+
const type = e.state === "closed" ? "Closed" : e.state === "merged" ? "Merged" : "Reopened";
|
|
120
|
+
out.push({ type, at: e.created_at, actor: user(e.user), reason: null });
|
|
121
|
+
}
|
|
122
|
+
for (const e of labelEvents) out.push({ type: e.action === "remove" ? "Unlabeled" : "Labeled", at: e.created_at, actor: user(e.user), label: e.label?.name ?? e.label?.title ?? e.label?.name });
|
|
123
|
+
for (const e of milestoneEvents) out.push({ type: e.action === "remove" ? "Demilestoned" : "Milestoned", at: e.created_at, actor: user(e.user), milestone: e.milestone?.title ?? e.milestone?.name });
|
|
124
|
+
return out.filter((e) => e.at).sort((a, b) => Date.parse(a.at) - Date.parse(b.at));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function normalizeNotes(notes) {
|
|
128
|
+
return notes.filter((n) => !n.system && n.body).map((n) => ({ id: String(n.id), author: user(n.author), createdAt: n.created_at, body: n.body }));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function normalizeLabels(labels, colors) {
|
|
132
|
+
return (labels ?? []).map((name) => ({ name: typeof name === "string" ? name : name.name, color: (colors.get(typeof name === "string" ? name : name.name) ?? "b6c5d2").replace(/^#/, "") }));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function common(record, kind, notes, events, colors) {
|
|
136
|
+
const milestone = record.milestone;
|
|
137
|
+
const comments = normalizeNotes(notes);
|
|
138
|
+
return {
|
|
139
|
+
kind,
|
|
140
|
+
id: String(record.id),
|
|
141
|
+
number: record.iid,
|
|
142
|
+
title: record.title,
|
|
143
|
+
body: record.description ?? "",
|
|
144
|
+
url: record.web_url,
|
|
145
|
+
author: user(record.author),
|
|
146
|
+
createdAt: record.created_at,
|
|
147
|
+
updatedAt: record.updated_at,
|
|
148
|
+
closedAt: iso(record.closed_at),
|
|
149
|
+
labels: normalizeLabels(record.labels, colors),
|
|
150
|
+
assignees: (record.assignees ?? (record.assignee ? [record.assignee] : [])).map(user).filter(Boolean),
|
|
151
|
+
milestone: milestone ? { title: milestone.title, dueOn: milestone.due_date ?? null } : null,
|
|
152
|
+
commentsCount: Number(record.user_notes_count ?? comments.length),
|
|
153
|
+
comments,
|
|
154
|
+
events,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function mapLimit(items, limit, fn) {
|
|
159
|
+
const out = new Array(items.length);
|
|
160
|
+
let cursor = 0;
|
|
161
|
+
await Promise.all(Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
162
|
+
for (;;) {
|
|
163
|
+
const i = cursor++;
|
|
164
|
+
if (i >= items.length) return;
|
|
165
|
+
out[i] = await fn(items[i]);
|
|
166
|
+
}
|
|
167
|
+
}));
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* GitLab's issue links into the blocker relation of the UI: "is_blocked_by" makes the other issue a blocker of this one,
|
|
173
|
+
* "blocks" makes it blocked. "relates_to" is not a dependency and is left out.
|
|
174
|
+
*/
|
|
175
|
+
export function gitlabIssueRelations(links) {
|
|
176
|
+
const blockedBy = [], blocking = [];
|
|
177
|
+
for (const l of links ?? []) {
|
|
178
|
+
const n = Number(l.iid);
|
|
179
|
+
if (!Number.isFinite(n)) continue;
|
|
180
|
+
if (l.link_type === "is_blocked_by") blockedBy.push(n);
|
|
181
|
+
else if (l.link_type === "blocks") blocking.push(n);
|
|
182
|
+
}
|
|
183
|
+
return { blockedBy, blocking };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function loadIssue(api, project, record, colors) {
|
|
187
|
+
const base = `projects/${project}/issues/${record.iid}`;
|
|
188
|
+
const [notes, states, labels, milestones, links] = await Promise.all([
|
|
189
|
+
optionalPages(api, `${base}/notes`), optionalPages(api, `${base}/resource_state_events`),
|
|
190
|
+
optionalPages(api, `${base}/resource_label_events`), optionalPages(api, `${base}/resource_milestone_events`),
|
|
191
|
+
optionalPages(api, `${base}/links`),
|
|
192
|
+
]);
|
|
193
|
+
return {
|
|
194
|
+
...common(record, "issue", notes, mappedEvents(states, labels, milestones), colors),
|
|
195
|
+
state: state(record.state), stateReason: null, parent: null, subIssues: [],
|
|
196
|
+
...gitlabIssueRelations(links),
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function loadMergeRequest(api, project, record, colors) {
|
|
201
|
+
const base = `projects/${project}/merge_requests/${record.iid}`;
|
|
202
|
+
const [notes, states, labels, commits, approvals, closes, changes] = await Promise.all([
|
|
203
|
+
optionalPages(api, `${base}/notes`), optionalPages(api, `${base}/resource_state_events`),
|
|
204
|
+
optionalPages(api, `${base}/resource_label_events`), optionalPages(api, `${base}/commits`),
|
|
205
|
+
api(`${base}/approvals`).then((r) => r?.body).catch(() => null),
|
|
206
|
+
optionalPages(api, `${base}/closes_issues`), api(`${base}/changes`).then((r) => r?.body).catch(() => null),
|
|
207
|
+
]);
|
|
208
|
+
const diffList = changes?.changes ?? [];
|
|
209
|
+
const additions = diffList.reduce((n, f) => n + ((f.diff?.match(/^\+(?!\+\+)/gm) ?? []).length), 0);
|
|
210
|
+
const deletions = diffList.reduce((n, f) => n + ((f.diff?.match(/^-(?!--)/gm) ?? []).length), 0);
|
|
211
|
+
const reviews = (approvals?.approved_by ?? []).map((approval) => ({ author: user(approval.user), state: "APPROVED", at: approval.approved_at ?? null, body: "" }));
|
|
212
|
+
const stateEvents = [...states];
|
|
213
|
+
if (record.merged_at && !stateEvents.some((e) => e.state === "merged")) stateEvents.push({ state: "merged", created_at: record.merged_at, user: record.merge_user ?? record.merged_by });
|
|
214
|
+
return {
|
|
215
|
+
...common(record, "pull", notes, mappedEvents(stateEvents, labels), colors),
|
|
216
|
+
state: state(record.state), isDraft: !!(record.draft ?? record.work_in_progress),
|
|
217
|
+
mergedAt: iso(record.merged_at), mergedBy: user(record.merge_user ?? record.merged_by),
|
|
218
|
+
reviewDecision: reviews.length ? "APPROVED" : null,
|
|
219
|
+
base: record.target_branch ?? "", head: record.source_branch ?? "",
|
|
220
|
+
additions, deletions, changedFiles: diffList.length,
|
|
221
|
+
closes: closes.map((issue) => issue.iid).filter(Number.isFinite),
|
|
222
|
+
// The commit this MR left on the target branch (a merge or a squash). It is how a commit of the branch finds its MR.
|
|
223
|
+
mergeCommitSha: record.merge_commit_sha ?? record.squash_commit_sha ?? null,
|
|
224
|
+
commitsCount: commits.length,
|
|
225
|
+
commits: commits.map((c) => ({ oid: c.id, message: c.title ?? c.message, at: c.committed_date ?? c.created_at, author: c.author?.username ?? c.author_name ?? null })),
|
|
226
|
+
reviews,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const HIERARCHY_QUERY = `query ($path: ID!, $after: String) { project(fullPath: $path) { workItems(first: 100, after: $after, state: all) {
|
|
231
|
+
pageInfo { hasNextPage endCursor } nodes { iid workItemType { name } widgets { __typename ... on WorkItemWidgetHierarchy {
|
|
232
|
+
parent { iid title webUrl workItemType { name } } children(first: 100) { nodes { iid } } } } } } } }`;
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The hierarchy of the project's work items (GitLab's issues, tasks and their parents), by iid: `parent` is the item one
|
|
236
|
+
* level up, an issue or an epic of the group (epics are not in the REST issues, so they come from here), and `children`.
|
|
237
|
+
* One GraphQL request per 100 work items. Free tiers have it; a server without work items returns an error.
|
|
238
|
+
*/
|
|
239
|
+
export async function gitlabHierarchy({ repoKey, fetchImpl = fetch }) {
|
|
240
|
+
const { host, project, origin } = parseGitLabRepo(repoKey);
|
|
241
|
+
const { token } = getAuth(host);
|
|
242
|
+
const out = new Map();
|
|
243
|
+
let after = null;
|
|
244
|
+
for (let pages = 0; pages < 500; pages++) {
|
|
245
|
+
const res = await fetchImpl(`${origin}/api/graphql`, {
|
|
246
|
+
method: "POST",
|
|
247
|
+
headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "user-agent": "issue-timeline" },
|
|
248
|
+
body: JSON.stringify({ query: HIERARCHY_QUERY, variables: { path: project, after } }),
|
|
249
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
250
|
+
});
|
|
251
|
+
if (!res.ok) throw new Error(`GitLab GraphQL ${res.status}`);
|
|
252
|
+
const body = await res.json();
|
|
253
|
+
if (body.errors?.length) throw new Error(`GitLab GraphQL: ${body.errors[0].message}`);
|
|
254
|
+
const items = body.data?.project?.workItems;
|
|
255
|
+
if (!items) break;
|
|
256
|
+
for (const n of items.nodes) {
|
|
257
|
+
const hier = (n.widgets ?? []).find((w) => w.__typename === "WorkItemWidgetHierarchy") ?? {};
|
|
258
|
+
const p = hier.parent;
|
|
259
|
+
out.set(Number(n.iid), {
|
|
260
|
+
type: n.workItemType?.name ?? null,
|
|
261
|
+
parent: p ? { iid: Number(p.iid), title: p.title, url: p.webUrl, type: p.workItemType?.name ?? null } : null,
|
|
262
|
+
children: (hier.children?.nodes ?? []).map((c) => Number(c.iid)),
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
if (!items.pageInfo?.hasNextPage) break;
|
|
266
|
+
after = items.pageInfo.endCursor;
|
|
267
|
+
}
|
|
268
|
+
return out;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Writes the hierarchy onto the stored issues: `parent` when the parent is an issue of this project, `parentEpic` when it
|
|
273
|
+
* is an epic (or anything else outside the issues), and `subIssues` for the children that are issues here.
|
|
274
|
+
*/
|
|
275
|
+
export function applyHierarchy(issues, hierarchy) {
|
|
276
|
+
for (const issue of Object.values(issues)) {
|
|
277
|
+
const h = hierarchy.get(Number(issue.number));
|
|
278
|
+
const p = h?.parent ?? null;
|
|
279
|
+
const isIssueHere = p && p.type !== "Epic" && !!issues[p.iid];
|
|
280
|
+
issue.parent = isIssueHere ? p.iid : null;
|
|
281
|
+
issue.parentEpic = p && !isIssueHere ? { iid: p.iid, title: p.title, url: p.url, type: p.type } : null;
|
|
282
|
+
issue.subIssues = (h?.children ?? []).filter((n) => !!issues[n]);
|
|
283
|
+
}
|
|
284
|
+
return issues;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export async function syncGitLab({ repoKey, store, full = false, now = Date.now, log = () => {}, onProgress = () => {} }) {
|
|
288
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
289
|
+
const { token, apiBase } = getAuth(host);
|
|
290
|
+
const api = createApi({ token, apiBase, log, invalidate: () => authCache.delete(host) });
|
|
291
|
+
const encodedProject = encodeURIComponent(project);
|
|
292
|
+
const t0 = now();
|
|
293
|
+
const startedAt = new Date(t0).toISOString();
|
|
294
|
+
const doFull = full || !store.data.meta.fullSyncAt || store.data.meta.repo !== repoKey;
|
|
295
|
+
if (store.data.meta.repo && store.data.meta.repo !== repoKey) { store.data.issues = {}; store.data.pulls = {}; }
|
|
296
|
+
const updatedAfter = doFull ? undefined : new Date(Date.parse(store.data.meta.lastSync) - MARGIN_MS).toISOString();
|
|
297
|
+
onProgress({ phase: "Listing issues and MRs", done: 0, total: null });
|
|
298
|
+
const [rawIssues, rawMRs, labels] = await Promise.all([
|
|
299
|
+
allPages(api, `projects/${encodedProject}/issues`, { order_by: "updated_at", sort: "desc", updated_after: updatedAfter }),
|
|
300
|
+
allPages(api, `projects/${encodedProject}/merge_requests`, { state: "all", order_by: "updated_at", sort: "desc", updated_after: updatedAfter }),
|
|
301
|
+
allPages(api, `projects/${encodedProject}/labels`),
|
|
302
|
+
]);
|
|
303
|
+
const colors = new Map(labels.map((l) => [l.name, l.color ?? "b6c5d2"]));
|
|
304
|
+
const total = rawIssues.length + rawMRs.length;
|
|
305
|
+
let done = 0;
|
|
306
|
+
onProgress({ phase: "Downloading issues and MRs", done, total });
|
|
307
|
+
const issues = await mapLimit(rawIssues, 3, async (r) => {
|
|
308
|
+
const item = await loadIssue(api, encodedProject, r, colors);
|
|
309
|
+
onProgress({ phase: "Downloading issues and MRs", done: ++done, total });
|
|
310
|
+
return item;
|
|
311
|
+
});
|
|
312
|
+
const pulls = await mapLimit(rawMRs, 2, async (r) => {
|
|
313
|
+
const item = await loadMergeRequest(api, encodedProject, r, colors);
|
|
314
|
+
onProgress({ phase: "Downloading issues and MRs", done: ++done, total });
|
|
315
|
+
return item;
|
|
316
|
+
});
|
|
317
|
+
// GitLab's links are not in the issue's own record, and adding one does not always move its updated_at: on an incremental
|
|
318
|
+
// sync the links of the open issues that did not come back are read again, so a new blocker shows up.
|
|
319
|
+
if (!doFull) {
|
|
320
|
+
const came = new Set(rawIssues.map((r) => String(r.iid)));
|
|
321
|
+
const open = Object.values(store.data.issues).filter((i) => i.state === "OPEN" && !came.has(String(i.number)));
|
|
322
|
+
await mapLimit(open, 3, async (i) => {
|
|
323
|
+
const links = await optionalPages(api, `projects/${encodedProject}/issues/${i.number}/links`);
|
|
324
|
+
Object.assign(i, gitlabIssueRelations(links));
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
store.upsert("issues", issues);
|
|
328
|
+
store.upsert("pulls", pulls);
|
|
329
|
+
// Parents, children and epics come from the work items, not from the issue records: read them for every stored issue.
|
|
330
|
+
try {
|
|
331
|
+
applyHierarchy(store.data.issues, await gitlabHierarchy({ repoKey, fetchImpl: undefined }));
|
|
332
|
+
} catch (e) {
|
|
333
|
+
log(`GitLab work items unavailable, hierarchy not updated: ${e.message}`);
|
|
334
|
+
}
|
|
335
|
+
const meta = store.data.meta;
|
|
336
|
+
meta.repo = repoKey;
|
|
337
|
+
meta.lastSync = startedAt;
|
|
338
|
+
if (doFull) meta.fullSyncAt = startedAt;
|
|
339
|
+
meta.totals = { issues: Object.keys(store.data.issues).length, pulls: Object.keys(store.data.pulls).length };
|
|
340
|
+
const entry = { at: startedAt, full: doFull, issues: issues.length, pulls: pulls.length, ms: now() - t0 };
|
|
341
|
+
meta.syncs = [...meta.syncs, entry].slice(-50);
|
|
342
|
+
await store.save();
|
|
343
|
+
log(`GitLab ${doFull ? "full" : "incremental"}: ${entry.issues} issues, ${entry.pulls} merge requests`);
|
|
344
|
+
return entry;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/** One page of the default branch's commits (newest first). `page` is the cursor: the next page number, or null. */
|
|
348
|
+
export async function gitlabCommitPage({ repoKey, page }) {
|
|
349
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
350
|
+
const { token, apiBase } = getAuth(host);
|
|
351
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
352
|
+
const result = await api(`projects/${encodeURIComponent(project)}/repository/commits`, { per_page: PAGE_SIZE, page: page ?? 1 });
|
|
353
|
+
if (!result) return { items: [], next: null };
|
|
354
|
+
return {
|
|
355
|
+
items: result.body.map((c) => ({
|
|
356
|
+
sha: c.id, title: c.title ?? c.message ?? "", author: c.author_name ?? null, at: c.committed_date ?? c.created_at ?? null,
|
|
357
|
+
additions: null, deletions: null,
|
|
358
|
+
})),
|
|
359
|
+
next: result.next ? Number(result.next) : null,
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The info and the files of one commit on GitLab, normalized like the GitHub ones (`patch` is the text diff). */
|
|
364
|
+
export async function gitlabCommitFiles({ repoKey, sha }) {
|
|
365
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
366
|
+
const { token, apiBase } = getAuth(host);
|
|
367
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
368
|
+
const base = `projects/${encodeURIComponent(project)}/repository/commits/${sha}`;
|
|
369
|
+
const commit = await api(base);
|
|
370
|
+
const diffs = await allPages(api, `${base}/diff`);
|
|
371
|
+
const info = commit?.body ? { title: commit.body.title ?? "", author: commit.body.author_name ?? null, at: commit.body.committed_date ?? null } : null;
|
|
372
|
+
const files = diffs.map((d) => ({
|
|
373
|
+
path: d.new_path, oldPath: d.renamed_file ? d.old_path : null,
|
|
374
|
+
status: d.new_file ? "added" : d.deleted_file ? "deleted" : d.renamed_file ? "renamed" : "modified",
|
|
375
|
+
patch: d.diff ?? null, tooLarge: !!d.too_large,
|
|
376
|
+
}));
|
|
377
|
+
return { info, files };
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/** The merge method of a merged merge request on GitLab: squash, merge commit, or rebase (a fast-forward or rebased merge). */
|
|
381
|
+
export async function gitlabMergeMethod({ repoKey, iid }) {
|
|
382
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
383
|
+
const { token, apiBase } = getAuth(host);
|
|
384
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
385
|
+
const result = await api(`projects/${encodeURIComponent(project)}/merge_requests/${iid}`);
|
|
386
|
+
const mr = result?.body;
|
|
387
|
+
if (!mr || mr.state !== "merged") return null;
|
|
388
|
+
if (mr.squash_commit_sha) return "squash";
|
|
389
|
+
if (mr.merge_commit_sha) return "merge commit";
|
|
390
|
+
return "rebase";
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/** Whether the project exists and the glab login can see it (the host answers 404 otherwise). */
|
|
394
|
+
export async function gitlabProjectExists(repoKey) {
|
|
395
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
396
|
+
const { token, apiBase } = getAuth(host);
|
|
397
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
398
|
+
return !!(await api(`projects/${encodeURIComponent(project)}`));
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/** The pipelines of the project updated since `since` (ISO time), newest first, as CI runs. */
|
|
402
|
+
export async function gitlabPipelines({ repoKey, since }) {
|
|
403
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
404
|
+
const { token, apiBase } = getAuth(host);
|
|
405
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
406
|
+
const rows = await allPages(api, `projects/${encodeURIComponent(project)}/pipelines`, { updated_after: since, order_by: "updated_at", sort: "desc" });
|
|
407
|
+
return rows.map(gitlabRunOf);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** The commits that changed the CI definition (.gitlab-ci.yml) since `since`, newest first. */
|
|
411
|
+
export async function gitlabWorkflowChanges({ repoKey, since }) {
|
|
412
|
+
const { host, project } = parseGitLabRepo(repoKey);
|
|
413
|
+
const { token, apiBase } = getAuth(host);
|
|
414
|
+
const api = createApi({ token, apiBase, invalidate: () => authCache.delete(host) });
|
|
415
|
+
const rows = await allPages(api, `projects/${encodeURIComponent(project)}/repository/commits`, { path: ".gitlab-ci.yml", since });
|
|
416
|
+
return rows.map((c) => ({ sha: c.id, title: c.title ?? "", author: c.author_name ?? null, at: c.committed_date ?? c.created_at ?? null, url: c.web_url ?? null }));
|
|
417
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { applyHierarchy, gitlabHierarchy } from "./gitlab.js";
|
|
4
|
+
|
|
5
|
+
const item = (iid, widgets) => ({ iid: String(iid), workItemType: { name: "Issue" }, widgets });
|
|
6
|
+
const hier = (parent, children = []) => ({ __typename: "WorkItemWidgetHierarchy", parent, children: { nodes: children.map((c) => ({ iid: String(c) })) } });
|
|
7
|
+
const epic = { iid: "486", title: "Feature parity", webUrl: "https://gitlab.com/groups/g/-/work_items/486", workItemType: { name: "Epic" } };
|
|
8
|
+
|
|
9
|
+
test("the hierarchy reads two pages of work items, with epic parents and children", async () => {
|
|
10
|
+
process.env.GITLAB_TOKEN = "test-token";
|
|
11
|
+
const pages = [
|
|
12
|
+
{ data: { project: { workItems: { pageInfo: { hasNextPage: true, endCursor: "c1" }, nodes: [item(180, [hier(epic)]), item(5, [hier(null, [6])])] } } } },
|
|
13
|
+
{ data: { project: { workItems: { pageInfo: { hasNextPage: false, endCursor: null }, nodes: [item(6, [hier({ iid: "5", title: "t", webUrl: "u", workItemType: { name: "Issue" } })])] } } } },
|
|
14
|
+
];
|
|
15
|
+
const seen = [];
|
|
16
|
+
const fetchImpl = async (url, opts) => {
|
|
17
|
+
seen.push(JSON.parse(opts.body).variables.after);
|
|
18
|
+
const body = pages.shift();
|
|
19
|
+
return { ok: true, status: 200, json: async () => body };
|
|
20
|
+
};
|
|
21
|
+
const h = await gitlabHierarchy({ repoKey: "gitlab:https://gitlab.com/g/p", fetchImpl });
|
|
22
|
+
assert.deepEqual(seen, [null, "c1"], "the second page is asked with the first page's cursor");
|
|
23
|
+
assert.equal(h.get(180).parent.iid, 486);
|
|
24
|
+
assert.equal(h.get(180).parent.type, "Epic");
|
|
25
|
+
assert.deepEqual(h.get(5).children, [6]);
|
|
26
|
+
assert.equal(h.get(6).parent.iid, 5);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test("applyHierarchy: an issue parent goes to parent, an epic to parentEpic, and children that are issues to subIssues", () => {
|
|
30
|
+
const issues = { 5: { number: 5 }, 6: { number: 6 }, 180: { number: 180 } };
|
|
31
|
+
const h = new Map([
|
|
32
|
+
[180, { type: "Issue", parent: { iid: 486, title: "Feature parity", url: "u", type: "Epic" }, children: [] }],
|
|
33
|
+
[5, { type: "Issue", parent: null, children: [6, 999] }],
|
|
34
|
+
[6, { type: "Issue", parent: { iid: 5, title: "t", url: "u", type: "Issue" }, children: [] }],
|
|
35
|
+
]);
|
|
36
|
+
applyHierarchy(issues, h);
|
|
37
|
+
assert.equal(issues[180].parent, null);
|
|
38
|
+
assert.equal(issues[180].parentEpic.iid, 486);
|
|
39
|
+
assert.equal(issues[6].parent, 5);
|
|
40
|
+
assert.equal(issues[6].parentEpic, null);
|
|
41
|
+
assert.deepEqual(issues[5].subIssues, [6], "a child that is not a stored issue is left out");
|
|
42
|
+
});
|