@lbdudc/linea-git 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/README.md +47 -2
- package/dist/access.d.ts +18 -0
- package/dist/access.js +18 -0
- package/dist/access.js.map +1 -0
- package/dist/auth.d.ts +11 -0
- package/dist/auth.js +16 -0
- package/dist/auth.js.map +1 -0
- package/dist/changelog/generate.d.ts +18 -0
- package/dist/changelog/generate.js +166 -0
- package/dist/changelog/generate.js.map +1 -0
- package/dist/changelog/index.d.ts +5 -0
- package/dist/changelog/index.js +3 -0
- package/dist/changelog/index.js.map +1 -0
- package/dist/changelog/parse.d.ts +43 -0
- package/dist/changelog/parse.js +91 -0
- package/dist/changelog/parse.js.map +1 -0
- package/dist/changelog/types.d.ts +85 -0
- package/dist/changelog/types.js +2 -0
- package/dist/changelog/types.js.map +1 -0
- package/dist/checkout/credential.d.ts +14 -0
- package/dist/checkout/credential.js +29 -0
- package/dist/checkout/credential.js.map +1 -0
- package/dist/checkout/git.d.ts +78 -0
- package/dist/checkout/git.js +191 -0
- package/dist/checkout/git.js.map +1 -0
- package/dist/checkout/index.d.ts +3 -0
- package/dist/checkout/index.js +4 -0
- package/dist/checkout/index.js.map +1 -0
- package/dist/checkout/sync.d.ts +156 -0
- package/dist/checkout/sync.js +211 -0
- package/dist/checkout/sync.js.map +1 -0
- package/dist/closing.d.ts +1 -0
- package/dist/closing.js +14 -0
- package/dist/closing.js.map +1 -0
- package/dist/config.d.ts +53 -0
- package/dist/config.js +40 -0
- package/dist/config.js.map +1 -0
- package/dist/deletion.d.ts +19 -0
- package/dist/deletion.js +27 -0
- package/dist/deletion.js.map +1 -0
- package/dist/errors.d.ts +26 -0
- package/dist/errors.js +47 -0
- package/dist/errors.js.map +1 -0
- package/dist/factory.d.ts +20 -0
- package/dist/factory.js +46 -0
- package/dist/factory.js.map +1 -0
- package/dist/github/client.d.ts +44 -0
- package/dist/github/client.js +145 -0
- package/dist/github/client.js.map +1 -0
- package/dist/github/config.d.ts +24 -0
- package/dist/github/config.js +11 -0
- package/dist/github/config.js.map +1 -0
- package/dist/github/provider.d.ts +510 -0
- package/dist/github/provider.js +1009 -0
- package/dist/github/provider.js.map +1 -0
- package/dist/gitlab/client.d.ts +50 -0
- package/dist/gitlab/client.js +228 -0
- package/dist/gitlab/client.js.map +1 -0
- package/dist/gitlab/provider.d.ts +487 -0
- package/dist/gitlab/provider.js +719 -0
- package/dist/gitlab/provider.js.map +1 -0
- package/dist/host.d.ts +27 -0
- package/dist/host.js +45 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +31 -0
- package/dist/index.js.map +1 -0
- package/dist/local/diff.d.ts +3 -0
- package/dist/local/diff.js +43 -0
- package/dist/local/diff.js.map +1 -0
- package/dist/local/git.d.ts +24 -0
- package/dist/local/git.js +51 -0
- package/dist/local/git.js.map +1 -0
- package/dist/local/health.d.ts +36 -0
- package/dist/local/health.js +57 -0
- package/dist/local/health.js.map +1 -0
- package/dist/local/hooks.d.ts +2 -0
- package/dist/local/hooks.js +44 -0
- package/dist/local/hooks.js.map +1 -0
- package/dist/local/http.d.ts +35 -0
- package/dist/local/http.js +160 -0
- package/dist/local/http.js.map +1 -0
- package/dist/local/provider.d.ts +571 -0
- package/dist/local/provider.js +1237 -0
- package/dist/local/provider.js.map +1 -0
- package/dist/local/store.d.ts +136 -0
- package/dist/local/store.js +171 -0
- package/dist/local/store.js.map +1 -0
- package/dist/packages/github-source.d.ts +69 -0
- package/dist/packages/github-source.js +133 -0
- package/dist/packages/github-source.js.map +1 -0
- package/dist/packages/gitlab-source.d.ts +59 -0
- package/dist/packages/gitlab-source.js +214 -0
- package/dist/packages/gitlab-source.js.map +1 -0
- package/dist/packages/index.d.ts +30 -0
- package/dist/packages/index.js +45 -0
- package/dist/packages/index.js.map +1 -0
- package/dist/packages/provider-source.d.ts +34 -0
- package/dist/packages/provider-source.js +57 -0
- package/dist/packages/provider-source.js.map +1 -0
- package/dist/packages/repository-files.d.ts +12 -0
- package/dist/packages/repository-files.js +16 -0
- package/dist/packages/repository-files.js.map +1 -0
- package/dist/provider.d.ts +888 -0
- package/dist/provider.js +29 -0
- package/dist/provider.js.map +1 -0
- package/dist/read-cache.d.ts +5 -0
- package/dist/read-cache.js +101 -0
- package/dist/read-cache.js.map +1 -0
- package/dist/registry/catalog.d.ts +55 -0
- package/dist/registry/catalog.js +143 -0
- package/dist/registry/catalog.js.map +1 -0
- package/dist/registry/kinds.d.ts +21 -0
- package/dist/registry/kinds.js +43 -0
- package/dist/registry/kinds.js.map +1 -0
- package/dist/registry/packages.d.ts +165 -0
- package/dist/registry/packages.js +290 -0
- package/dist/registry/packages.js.map +1 -0
- package/dist/registry/publish.d.ts +35 -0
- package/dist/registry/publish.js +155 -0
- package/dist/registry/publish.js.map +1 -0
- package/dist/registry/release.d.ts +63 -0
- package/dist/registry/release.js +79 -0
- package/dist/registry/release.js.map +1 -0
- package/dist/restoration.d.ts +14 -0
- package/dist/restoration.js +33 -0
- package/dist/restoration.js.map +1 -0
- package/dist/templates.d.ts +8 -0
- package/dist/templates.js +39 -0
- package/dist/templates.js.map +1 -0
- package/dist/testing/contract.d.ts +43 -0
- package/dist/testing/contract.js +713 -0
- package/dist/testing/contract.js.map +1 -0
- package/dist/testing/diff.d.ts +1 -0
- package/dist/testing/diff.js +65 -0
- package/dist/testing/diff.js.map +1 -0
- package/dist/testing/git-http.d.ts +12 -0
- package/dist/testing/git-http.js +308 -0
- package/dist/testing/git-http.js.map +1 -0
- package/dist/testing/github-double.d.ts +151 -0
- package/dist/testing/github-double.js +1129 -0
- package/dist/testing/github-double.js.map +1 -0
- package/dist/testing/gitlab-double.d.ts +161 -0
- package/dist/testing/gitlab-double.js +1554 -0
- package/dist/testing/gitlab-double.js.map +1 -0
- package/dist/testing/index.d.ts +7 -0
- package/dist/testing/index.js +8 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +12 -0
- package/dist/version.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,1129 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import JSZip from 'jszip';
|
|
4
|
+
import { createGitHttp } from './git-http.js';
|
|
5
|
+
import { unifiedDiff } from './diff.js';
|
|
6
|
+
/**
|
|
7
|
+
* An in-memory GitHub: just the part of its REST API that LINEA uses, with real git behaviour behind it (content-addressed blobs and
|
|
8
|
+
* trees, commits with parents, branches and tags as refs, pull requests that merge or conflict). It is what the GitHub provider is tested
|
|
9
|
+
* against, and what `dev/fake-github.js` serves so the whole of LINEA can run on a laptop with `GIT_PROVIDER=github`.
|
|
10
|
+
*
|
|
11
|
+
* The user making every call is `dev` (the token is not looked at). A few things behave as on the real thing on purpose: the
|
|
12
|
+
* `mergeable` of a pull request is `null` the first time it is asked for, a ref update that is not a fast forward is refused (422), a
|
|
13
|
+
* file update without the current sha is refused, and a fault can be injected (rate limits) with `double.inject()`.
|
|
14
|
+
*/
|
|
15
|
+
const sha1 = (text) => crypto.createHash('sha1').update(text).digest('hex');
|
|
16
|
+
const blobSha = (buffer) => crypto.createHash('sha1').update(`blob ${buffer.length}\0`).update(buffer).digest('hex');
|
|
17
|
+
const ROLES = { admin: 50, maintain: 40, write: 30, triage: 20, read: 20 };
|
|
18
|
+
const permissionsOf = (role) => ({
|
|
19
|
+
admin: role === 'admin',
|
|
20
|
+
maintain: role === 'admin' || role === 'maintain',
|
|
21
|
+
push: ['admin', 'maintain', 'write'].includes(role),
|
|
22
|
+
triage: role !== 'read',
|
|
23
|
+
pull: true,
|
|
24
|
+
});
|
|
25
|
+
const NOT_FOUND = { message: 'Not Found' };
|
|
26
|
+
class Repo {
|
|
27
|
+
constructor({ id, name, owner, description = '', topics = [], defaultBranch = 'main', autoInit = true }) {
|
|
28
|
+
this.id = id;
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.owner = owner;
|
|
31
|
+
this.description = description;
|
|
32
|
+
this.topics = topics;
|
|
33
|
+
this.defaultBranch = defaultBranch;
|
|
34
|
+
this.blobs = new Map();
|
|
35
|
+
this.trees = new Map();
|
|
36
|
+
this.commits = new Map();
|
|
37
|
+
this.refs = new Map();
|
|
38
|
+
this.tagObjects = new Map();
|
|
39
|
+
this.pulls = [];
|
|
40
|
+
this.issues = [];
|
|
41
|
+
this.releases = [];
|
|
42
|
+
this.nextRelease = 1;
|
|
43
|
+
this.labels = [];
|
|
44
|
+
this.milestones = [];
|
|
45
|
+
this.hooks = [];
|
|
46
|
+
this.runs = [];
|
|
47
|
+
this.collaborators = [{ login: 'dev', id: 1, role: 'admin' }];
|
|
48
|
+
this.nextPull = 1;
|
|
49
|
+
if (autoInit) {
|
|
50
|
+
const entries = new Map([['README.md', this.putBlob(Buffer.from(`# ${name}\n`))]]);
|
|
51
|
+
this.refs.set(`heads/${defaultBranch}`, this.makeCommit({ tree: this.putTree(entries), parents: [], message: 'Initial commit' }));
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
get fullName() { return `${this.owner.login}/${this.name}`; }
|
|
55
|
+
putBlob(buffer) {
|
|
56
|
+
const sha = blobSha(buffer);
|
|
57
|
+
this.blobs.set(sha, buffer);
|
|
58
|
+
return sha;
|
|
59
|
+
}
|
|
60
|
+
putTree(entries) {
|
|
61
|
+
const sorted = [...entries].sort(([a], [b]) => a.localeCompare(b));
|
|
62
|
+
const sha = sha1(`tree ${JSON.stringify(sorted)}`);
|
|
63
|
+
this.trees.set(sha, new Map(sorted));
|
|
64
|
+
return sha;
|
|
65
|
+
}
|
|
66
|
+
makeCommit({ tree, parents, message, date = new Date().toISOString() }) {
|
|
67
|
+
const sha = sha1(`commit ${tree} ${parents.join(',')} ${message} ${date} ${this.commits.size}`);
|
|
68
|
+
this.commits.set(sha, { sha, tree, parents, message, author: { name: 'Dev User', email: 'dev@example.com', date } });
|
|
69
|
+
return sha;
|
|
70
|
+
}
|
|
71
|
+
/** The commit a name points at: a branch, a tag (possibly annotated), or a sha. */
|
|
72
|
+
resolve(ref) {
|
|
73
|
+
let sha = this.refs.get(`heads/${ref}`) ?? this.refs.get(`tags/${ref}`) ?? (this.commits.has(ref) ? ref : undefined);
|
|
74
|
+
if (sha && this.tagObjects.has(sha))
|
|
75
|
+
sha = this.tagObjects.get(sha).object.sha;
|
|
76
|
+
return sha;
|
|
77
|
+
}
|
|
78
|
+
treeAt(ref) {
|
|
79
|
+
const sha = this.resolve(ref);
|
|
80
|
+
return sha ? this.trees.get(this.commits.get(sha).tree) : undefined;
|
|
81
|
+
}
|
|
82
|
+
ancestors(sha) {
|
|
83
|
+
const seen = new Set();
|
|
84
|
+
const walk = (current) => {
|
|
85
|
+
if (seen.has(current))
|
|
86
|
+
return;
|
|
87
|
+
seen.add(current);
|
|
88
|
+
for (const parent of this.commits.get(current).parents)
|
|
89
|
+
walk(parent);
|
|
90
|
+
};
|
|
91
|
+
walk(sha);
|
|
92
|
+
return seen;
|
|
93
|
+
}
|
|
94
|
+
mergeBase(a, b) {
|
|
95
|
+
const left = this.ancestors(a);
|
|
96
|
+
const queue = [b];
|
|
97
|
+
const seen = new Set();
|
|
98
|
+
while (queue.length) {
|
|
99
|
+
const current = queue.shift();
|
|
100
|
+
if (seen.has(current))
|
|
101
|
+
continue;
|
|
102
|
+
seen.add(current);
|
|
103
|
+
if (left.has(current))
|
|
104
|
+
return current;
|
|
105
|
+
queue.push(...this.commits.get(current).parents);
|
|
106
|
+
}
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
/** Three-way merge of `head` into `base` (both branch names): the merged tree, or `null` when the same file changed on both sides. */
|
|
110
|
+
mergeTrees(base, head) {
|
|
111
|
+
const target = this.resolve(base);
|
|
112
|
+
const source = this.resolve(head);
|
|
113
|
+
const ancestor = this.mergeBase(target, source);
|
|
114
|
+
const t = this.trees.get(this.commits.get(target).tree);
|
|
115
|
+
const s = this.trees.get(this.commits.get(source).tree);
|
|
116
|
+
const b = ancestor ? this.trees.get(this.commits.get(ancestor).tree) : new Map();
|
|
117
|
+
const merged = new Map();
|
|
118
|
+
for (const path of new Set([...t.keys(), ...s.keys(), ...b.keys()])) {
|
|
119
|
+
const [tv, sv, bv] = [t.get(path), s.get(path), b.get(path)];
|
|
120
|
+
let value;
|
|
121
|
+
if (tv === sv)
|
|
122
|
+
value = tv;
|
|
123
|
+
else if (tv === bv)
|
|
124
|
+
value = sv;
|
|
125
|
+
else if (sv === bv)
|
|
126
|
+
value = tv;
|
|
127
|
+
else
|
|
128
|
+
return null;
|
|
129
|
+
if (value !== undefined)
|
|
130
|
+
merged.set(path, value);
|
|
131
|
+
}
|
|
132
|
+
return { entries: merged, target, source };
|
|
133
|
+
}
|
|
134
|
+
mergeable(pull) {
|
|
135
|
+
return this.mergeTrees(pull.base.ref, pull.head.ref) !== null;
|
|
136
|
+
}
|
|
137
|
+
/** Commits `changes` ([{path, sha | null}]) on top of `branch`. */
|
|
138
|
+
commitOn(branch, changes, message) {
|
|
139
|
+
const parent = this.refs.get(`heads/${branch}`);
|
|
140
|
+
const entries = new Map(this.trees.get(this.commits.get(parent).tree));
|
|
141
|
+
for (const { path, sha } of changes) {
|
|
142
|
+
if (sha === null)
|
|
143
|
+
entries.delete(path);
|
|
144
|
+
else
|
|
145
|
+
entries.set(path, sha);
|
|
146
|
+
}
|
|
147
|
+
const sha = this.makeCommit({ tree: this.putTree(entries), parents: [parent], message });
|
|
148
|
+
this.refs.set(`heads/${branch}`, sha);
|
|
149
|
+
return sha;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/** How git over HTTP (git-http.js) reads and writes a repository's commits, branches and tags. */
|
|
153
|
+
const githubAdapter = (repo) => {
|
|
154
|
+
const refsOf = (prefix) => new Map([...repo.refs].filter(([name]) => name.startsWith(prefix)).map(([name, sha]) => [name.slice(prefix.length), repo.tagObjects.get(sha)?.object.sha ?? sha]));
|
|
155
|
+
return {
|
|
156
|
+
defaultBranch: () => repo.defaultBranch,
|
|
157
|
+
commits: () => [...repo.commits.values()].map((c) => ({
|
|
158
|
+
id: c.sha, parents: c.parents, message: c.message, date: c.author.date, authorName: c.author.name, authorEmail: c.author.email,
|
|
159
|
+
files: new Map([...repo.trees.get(c.tree)].map(([path, blob]) => [path, repo.blobs.get(blob)])),
|
|
160
|
+
})),
|
|
161
|
+
heads: () => refsOf('heads/'),
|
|
162
|
+
tags: () => refsOf('tags/'),
|
|
163
|
+
addCommit: ({ id, parents, message, date, authorName, authorEmail, files }) => {
|
|
164
|
+
const entries = new Map([...files].map(([path, content]) => [path, repo.putBlob(content)]));
|
|
165
|
+
repo.commits.set(id, { sha: id, tree: repo.putTree(entries), parents, message, author: { name: authorName, email: authorEmail, date } });
|
|
166
|
+
return id;
|
|
167
|
+
},
|
|
168
|
+
setHead: (name, id) => (id ? repo.refs.set(`heads/${name}`, id) : repo.refs.delete(`heads/${name}`)),
|
|
169
|
+
setTag: (name, id) => (id ? repo.refs.set(`tags/${name}`, id) : repo.refs.delete(`tags/${name}`)),
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
export function createGithubDoubleApp(baseUrl = 'http://github.double') {
|
|
173
|
+
const state = {
|
|
174
|
+
user: { login: 'dev', id: 1, type: 'User' },
|
|
175
|
+
orgs: new Map(),
|
|
176
|
+
repos: new Map(),
|
|
177
|
+
nextRepo: 1000,
|
|
178
|
+
nextAccount: 10,
|
|
179
|
+
faults: [],
|
|
180
|
+
log: [],
|
|
181
|
+
reset() {
|
|
182
|
+
this.orgs.clear();
|
|
183
|
+
this.repos.clear();
|
|
184
|
+
this.nextRepo = 1000;
|
|
185
|
+
this.faults = [];
|
|
186
|
+
this.log = [];
|
|
187
|
+
},
|
|
188
|
+
addOrg(login, role = 'admin') {
|
|
189
|
+
const org = { login, id: this.nextAccount++, type: 'Organization', role };
|
|
190
|
+
this.orgs.set(login, org);
|
|
191
|
+
return org;
|
|
192
|
+
},
|
|
193
|
+
ownerOf(login) {
|
|
194
|
+
if (login === this.user.login)
|
|
195
|
+
return this.user;
|
|
196
|
+
return this.orgs.get(login);
|
|
197
|
+
},
|
|
198
|
+
createRepo({ owner, name, description, topics, defaultBranch, autoInit }) {
|
|
199
|
+
const repo = new Repo({ id: this.nextRepo++, name, owner, description, topics, defaultBranch, autoInit });
|
|
200
|
+
this.repos.set(repo.id, repo);
|
|
201
|
+
return repo;
|
|
202
|
+
},
|
|
203
|
+
find(fullName) {
|
|
204
|
+
return [...this.repos.values()].find((repo) => repo.fullName.toLowerCase() === String(fullName).toLowerCase());
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
const web = (repo) => `${baseUrl}/${repo.fullName}`;
|
|
208
|
+
const repoJson = (repo) => {
|
|
209
|
+
const mine = repo.collaborators.find((c) => c.login === state.user.login)?.role ?? 'read';
|
|
210
|
+
return {
|
|
211
|
+
id: repo.id,
|
|
212
|
+
name: repo.name,
|
|
213
|
+
full_name: repo.fullName,
|
|
214
|
+
owner: { login: repo.owner.login, id: repo.owner.id, type: repo.owner.type },
|
|
215
|
+
description: repo.description,
|
|
216
|
+
html_url: web(repo),
|
|
217
|
+
topics: repo.topics,
|
|
218
|
+
archived: false,
|
|
219
|
+
private: false,
|
|
220
|
+
default_branch: repo.defaultBranch,
|
|
221
|
+
permissions: permissionsOf(mine),
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
const commitJson = (repo, sha) => {
|
|
225
|
+
const commit = repo.commits.get(sha);
|
|
226
|
+
return { sha, html_url: `${web(repo)}/commit/${sha}`, commit: { message: commit.message, author: commit.author, tree: { sha: commit.tree } }, parents: commit.parents.map((p) => ({ sha: p })) };
|
|
227
|
+
};
|
|
228
|
+
const pullJson = (repo, pull, { withMergeable = false } = {}) => {
|
|
229
|
+
const out = {
|
|
230
|
+
number: pull.number,
|
|
231
|
+
title: pull.title,
|
|
232
|
+
state: pull.state,
|
|
233
|
+
merged_at: pull.merged_at,
|
|
234
|
+
html_url: `${web(repo)}/pull/${pull.number}`,
|
|
235
|
+
head: { ...pull.head, sha: repo.resolve(pull.head.ref) },
|
|
236
|
+
base: pull.base,
|
|
237
|
+
body: pull.body ?? '',
|
|
238
|
+
user: userJson(pull.author ?? 'dev'),
|
|
239
|
+
labels: (pull.labels ?? []).map((name) => ({ name, color: repo.labels.find((l) => l.name === name)?.color ?? 'ededed' })),
|
|
240
|
+
requested_reviewers: (pull.reviewers ?? []).map(userJson),
|
|
241
|
+
assignees: [],
|
|
242
|
+
created_at: pull.created_at ?? new Date(0).toISOString(),
|
|
243
|
+
updated_at: pull.updated_at ?? pull.created_at ?? new Date(0).toISOString(),
|
|
244
|
+
comments: (pull.comments ?? []).length,
|
|
245
|
+
review_comments: (pull.reviewComments ?? []).length,
|
|
246
|
+
};
|
|
247
|
+
if (withMergeable) {
|
|
248
|
+
// GitHub works it out in the background: the first ask has no answer yet
|
|
249
|
+
if (!pull.computed) {
|
|
250
|
+
pull.computed = true;
|
|
251
|
+
out.mergeable = null;
|
|
252
|
+
out.mergeable_state = 'unknown';
|
|
253
|
+
}
|
|
254
|
+
else if (pull.state !== 'open') {
|
|
255
|
+
out.mergeable = null;
|
|
256
|
+
out.mergeable_state = 'unknown';
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
const ok = repo.mergeable(pull);
|
|
260
|
+
out.mergeable = ok;
|
|
261
|
+
out.mergeable_state = ok ? 'clean' : 'dirty';
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return out;
|
|
265
|
+
};
|
|
266
|
+
const app = express();
|
|
267
|
+
// `git clone/push` against `<web url>/<owner>/<repo>.git`
|
|
268
|
+
const gitHttp = createGitHttp({ find: (fullName) => state.find(fullName), adapterOf: githubAdapter });
|
|
269
|
+
app.use(gitHttp.router);
|
|
270
|
+
const router = express.Router();
|
|
271
|
+
router.use(express.json({ limit: '50mb' }));
|
|
272
|
+
router.use((req, res, next) => {
|
|
273
|
+
state.log.push({ method: req.method, path: req.path });
|
|
274
|
+
const index = state.faults.findIndex((fault) => !fault.match || fault.match.test(req.path));
|
|
275
|
+
const fault = index < 0 ? undefined : state.faults.splice(index, 1)[0];
|
|
276
|
+
if (!fault)
|
|
277
|
+
return next();
|
|
278
|
+
for (const [name, value] of Object.entries(fault.headers ?? {}))
|
|
279
|
+
res.set(name, value);
|
|
280
|
+
return res.status(fault.status).json(fault.body ?? { message: 'fault' });
|
|
281
|
+
});
|
|
282
|
+
const notFound = (res) => res.status(404).json(NOT_FOUND);
|
|
283
|
+
const repoOr404 = (req, res) => {
|
|
284
|
+
const repo = state.find(`${req.params.owner}/${req.params.repo}`);
|
|
285
|
+
if (!repo)
|
|
286
|
+
notFound(res);
|
|
287
|
+
return repo;
|
|
288
|
+
};
|
|
289
|
+
// ── account ──────────────────────────────────────────────────────────────
|
|
290
|
+
router.get('/user', (_req, res) => res.json({ login: state.user.login, id: state.user.id, avatar_url: '', type: 'User' }));
|
|
291
|
+
router.get('/users/:login', (req, res) => {
|
|
292
|
+
const account = state.ownerOf(req.params.login);
|
|
293
|
+
if (!account)
|
|
294
|
+
return notFound(res);
|
|
295
|
+
res.json({ login: account.login, id: account.id, type: account.type });
|
|
296
|
+
});
|
|
297
|
+
router.get('/user/memberships/orgs', (_req, res) => res.json([...state.orgs.values()].map((org) => ({ state: 'active', role: org.role, organization: { login: org.login, id: org.id } }))));
|
|
298
|
+
router.get('/orgs/:org', (req, res) => {
|
|
299
|
+
const org = state.orgs.get(req.params.org);
|
|
300
|
+
if (!org)
|
|
301
|
+
return notFound(res);
|
|
302
|
+
res.json({ login: org.login, id: org.id, members_can_create_repositories: true });
|
|
303
|
+
});
|
|
304
|
+
router.get('/user/repos', (req, res) => {
|
|
305
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
306
|
+
const page = Number(req.query.page ?? 1);
|
|
307
|
+
const mine = [...state.repos.values()].filter((repo) => repo.collaborators.some((c) => c.login === state.user.login));
|
|
308
|
+
res.json(mine.slice((page - 1) * perPage, page * perPage).map(repoJson));
|
|
309
|
+
});
|
|
310
|
+
router.get('/orgs/:org/repos', (req, res) => {
|
|
311
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
312
|
+
const page = Number(req.query.page ?? 1);
|
|
313
|
+
const all = [...state.repos.values()].filter((repo) => repo.owner.login === req.params.org);
|
|
314
|
+
res.json(all.slice((page - 1) * perPage, page * perPage).map(repoJson));
|
|
315
|
+
});
|
|
316
|
+
// ── repositories ─────────────────────────────────────────────────────────
|
|
317
|
+
const create = (owner, req, res) => {
|
|
318
|
+
const { name, description, auto_init: autoInit } = req.body ?? {};
|
|
319
|
+
if (!name || !/^[A-Za-z0-9._-]+$/.test(name))
|
|
320
|
+
return res.status(422).json({ message: 'Repository creation failed.', errors: [{ message: 'name is invalid' }] });
|
|
321
|
+
if (state.find(`${owner.login}/${name}`)) {
|
|
322
|
+
return res.status(422).json({ message: 'Repository creation failed.', errors: [{ field: 'name', message: 'name already exists on this account' }] });
|
|
323
|
+
}
|
|
324
|
+
const repo = state.createRepo({ owner, name, description, autoInit: autoInit !== false });
|
|
325
|
+
res.status(201).json(repoJson(repo));
|
|
326
|
+
};
|
|
327
|
+
router.post('/user/repos', (req, res) => create(state.user, req, res));
|
|
328
|
+
router.post('/orgs/:org/repos', (req, res) => {
|
|
329
|
+
const org = state.orgs.get(req.params.org);
|
|
330
|
+
if (!org)
|
|
331
|
+
return notFound(res);
|
|
332
|
+
create(org, req, res);
|
|
333
|
+
});
|
|
334
|
+
router.get('/repositories/:id', (req, res) => {
|
|
335
|
+
const repo = state.repos.get(Number(req.params.id));
|
|
336
|
+
if (!repo)
|
|
337
|
+
return notFound(res);
|
|
338
|
+
res.json(repoJson(repo));
|
|
339
|
+
});
|
|
340
|
+
const R = '/repos/:owner/:repo';
|
|
341
|
+
router.get(R, (req, res) => { const repo = repoOr404(req, res); if (repo)
|
|
342
|
+
res.json(repoJson(repo)); });
|
|
343
|
+
router.patch(R, (req, res) => {
|
|
344
|
+
const repo = repoOr404(req, res);
|
|
345
|
+
if (!repo)
|
|
346
|
+
return;
|
|
347
|
+
const { name, default_branch: defaultBranch, description } = req.body ?? {};
|
|
348
|
+
if (name !== undefined && name !== repo.name) {
|
|
349
|
+
if (state.find(`${repo.owner.login}/${name}`))
|
|
350
|
+
return res.status(422).json({ message: 'Repository creation failed.', errors: [{ message: 'name already exists on this account' }] });
|
|
351
|
+
repo.name = name;
|
|
352
|
+
}
|
|
353
|
+
if (defaultBranch !== undefined) {
|
|
354
|
+
if (!repo.refs.has(`heads/${defaultBranch}`))
|
|
355
|
+
return res.status(422).json({ message: 'Validation Failed' });
|
|
356
|
+
repo.defaultBranch = defaultBranch;
|
|
357
|
+
}
|
|
358
|
+
if (description !== undefined)
|
|
359
|
+
repo.description = description;
|
|
360
|
+
res.json(repoJson(repo));
|
|
361
|
+
});
|
|
362
|
+
router.delete(R, (req, res) => {
|
|
363
|
+
const repo = repoOr404(req, res);
|
|
364
|
+
if (!repo)
|
|
365
|
+
return;
|
|
366
|
+
state.repos.delete(repo.id);
|
|
367
|
+
res.status(204).end();
|
|
368
|
+
});
|
|
369
|
+
router.get(`${R}/topics`, (req, res) => { const repo = repoOr404(req, res); if (repo)
|
|
370
|
+
res.json({ names: repo.topics }); });
|
|
371
|
+
router.put(`${R}/topics`, (req, res) => {
|
|
372
|
+
const repo = repoOr404(req, res);
|
|
373
|
+
if (!repo)
|
|
374
|
+
return;
|
|
375
|
+
repo.topics = [...(req.body?.names ?? [])];
|
|
376
|
+
res.json({ names: repo.topics });
|
|
377
|
+
});
|
|
378
|
+
router.get(`${R}/collaborators`, (req, res) => {
|
|
379
|
+
const repo = repoOr404(req, res);
|
|
380
|
+
if (!repo)
|
|
381
|
+
return;
|
|
382
|
+
res.json(repo.collaborators.map((c) => ({ login: c.login, id: c.id, role_name: c.role, permissions: permissionsOf(c.role) })));
|
|
383
|
+
});
|
|
384
|
+
router.get(`${R}/collaborators/:user/permission`, (req, res) => {
|
|
385
|
+
const repo = repoOr404(req, res);
|
|
386
|
+
if (!repo)
|
|
387
|
+
return;
|
|
388
|
+
const found = repo.collaborators.find((c) => c.login === req.params.user);
|
|
389
|
+
if (!found)
|
|
390
|
+
return res.json({ permission: 'none', role_name: 'none', user: { login: req.params.user } });
|
|
391
|
+
res.json({ permission: found.role === 'maintain' || found.role === 'triage' ? 'write' : found.role, role_name: found.role, user: { login: found.login } });
|
|
392
|
+
});
|
|
393
|
+
// ── contents ─────────────────────────────────────────────────────────────
|
|
394
|
+
const wantsRaw = (req) => /raw/.test(req.get('accept') ?? '');
|
|
395
|
+
router.get(`${R}/contents/*`, (req, res) => {
|
|
396
|
+
const repo = repoOr404(req, res);
|
|
397
|
+
if (!repo)
|
|
398
|
+
return;
|
|
399
|
+
const path = req.params[0];
|
|
400
|
+
const tree = repo.treeAt(req.query.ref ?? repo.defaultBranch);
|
|
401
|
+
const sha = tree?.get(path);
|
|
402
|
+
if (!sha)
|
|
403
|
+
return notFound(res);
|
|
404
|
+
const bytes = repo.blobs.get(sha);
|
|
405
|
+
if (wantsRaw(req))
|
|
406
|
+
return res.status(200).type('application/octet-stream').send(bytes);
|
|
407
|
+
res.json({ type: 'file', name: path.split('/').pop(), path, sha, size: bytes.length, encoding: 'base64', content: bytes.toString('base64').replace(/(.{60})/g, '$1\n') });
|
|
408
|
+
});
|
|
409
|
+
const writeContents = (req, res, { remove }) => {
|
|
410
|
+
const repo = repoOr404(req, res);
|
|
411
|
+
if (!repo)
|
|
412
|
+
return;
|
|
413
|
+
const path = req.params[0];
|
|
414
|
+
const branch = req.body?.branch ?? repo.defaultBranch;
|
|
415
|
+
if (!repo.refs.has(`heads/${branch}`))
|
|
416
|
+
return notFound(res);
|
|
417
|
+
const current = repo.treeAt(branch).get(path);
|
|
418
|
+
const given = req.body?.sha;
|
|
419
|
+
if (remove && !current)
|
|
420
|
+
return notFound(res);
|
|
421
|
+
if (current && given === undefined)
|
|
422
|
+
return res.status(422).json({ message: "Invalid request.\n\n\"sha\" wasn't supplied." });
|
|
423
|
+
if (current && given !== current)
|
|
424
|
+
return res.status(409).json({ message: `${path} does not match ${given}` });
|
|
425
|
+
if (!current && given !== undefined && !remove)
|
|
426
|
+
return res.status(422).json({ message: 'Invalid request: sha' });
|
|
427
|
+
const sha = remove ? null : repo.putBlob(Buffer.from(req.body?.content ?? '', 'base64'));
|
|
428
|
+
const commit = repo.commitOn(branch, [{ path, sha }], req.body?.message ?? `Update ${path}`);
|
|
429
|
+
res.status(remove ? 200 : current ? 200 : 201).json({ content: remove ? null : { path, sha }, commit: { sha: commit } });
|
|
430
|
+
};
|
|
431
|
+
router.put(`${R}/contents/*`, (req, res) => writeContents(req, res, { remove: false }));
|
|
432
|
+
router.delete(`${R}/contents/*`, (req, res) => writeContents(req, res, { remove: true }));
|
|
433
|
+
// ── git data ─────────────────────────────────────────────────────────────
|
|
434
|
+
router.post(`${R}/git/blobs`, (req, res) => {
|
|
435
|
+
const repo = repoOr404(req, res);
|
|
436
|
+
if (!repo)
|
|
437
|
+
return;
|
|
438
|
+
const { content = '', encoding = 'utf-8' } = req.body ?? {};
|
|
439
|
+
res.status(201).json({ sha: repo.putBlob(Buffer.from(content, encoding === 'base64' ? 'base64' : 'utf8')) });
|
|
440
|
+
});
|
|
441
|
+
router.get(`${R}/git/blobs/:sha`, (req, res) => {
|
|
442
|
+
const repo = repoOr404(req, res);
|
|
443
|
+
if (!repo)
|
|
444
|
+
return;
|
|
445
|
+
const bytes = repo.blobs.get(req.params.sha);
|
|
446
|
+
if (!bytes)
|
|
447
|
+
return notFound(res);
|
|
448
|
+
res.json({ sha: req.params.sha, encoding: 'base64', content: bytes.toString('base64'), size: bytes.length });
|
|
449
|
+
});
|
|
450
|
+
router.post(`${R}/git/trees`, (req, res) => {
|
|
451
|
+
const repo = repoOr404(req, res);
|
|
452
|
+
if (!repo)
|
|
453
|
+
return;
|
|
454
|
+
const base = req.body?.base_tree ? repo.trees.get(req.body.base_tree) : new Map();
|
|
455
|
+
if (!base)
|
|
456
|
+
return res.status(422).json({ message: 'base_tree not found' });
|
|
457
|
+
const entries = new Map(base);
|
|
458
|
+
for (const item of req.body?.tree ?? []) {
|
|
459
|
+
if (item.sha === null)
|
|
460
|
+
entries.delete(item.path);
|
|
461
|
+
else if (!repo.blobs.has(item.sha))
|
|
462
|
+
return res.status(422).json({ message: `GitRPC::BadObjectState: ${item.path}` });
|
|
463
|
+
else
|
|
464
|
+
entries.set(item.path, item.sha);
|
|
465
|
+
}
|
|
466
|
+
res.status(201).json({ sha: repo.putTree(entries) });
|
|
467
|
+
});
|
|
468
|
+
router.get(`${R}/git/trees/:ref`, (req, res) => {
|
|
469
|
+
const repo = repoOr404(req, res);
|
|
470
|
+
if (!repo)
|
|
471
|
+
return;
|
|
472
|
+
const ref = req.params.ref;
|
|
473
|
+
const tree = repo.trees.get(ref) ?? (repo.commits.has(ref) ? repo.trees.get(repo.commits.get(ref).tree) : undefined) ?? repo.treeAt(ref);
|
|
474
|
+
if (!tree)
|
|
475
|
+
return notFound(res);
|
|
476
|
+
res.json({ sha: ref, truncated: false, tree: [...tree].map(([path, sha]) => ({ path, mode: '100644', type: 'blob', sha })) });
|
|
477
|
+
});
|
|
478
|
+
router.post(`${R}/git/commits`, (req, res) => {
|
|
479
|
+
const repo = repoOr404(req, res);
|
|
480
|
+
if (!repo)
|
|
481
|
+
return;
|
|
482
|
+
const { message, tree, parents = [] } = req.body ?? {};
|
|
483
|
+
if (!repo.trees.has(tree))
|
|
484
|
+
return res.status(422).json({ message: 'Tree SHA does not exist' });
|
|
485
|
+
res.status(201).json({ sha: repo.makeCommit({ tree, parents, message }) });
|
|
486
|
+
});
|
|
487
|
+
router.get(`${R}/git/commits/:sha`, (req, res) => {
|
|
488
|
+
const repo = repoOr404(req, res);
|
|
489
|
+
if (!repo)
|
|
490
|
+
return;
|
|
491
|
+
if (!repo.commits.has(req.params.sha))
|
|
492
|
+
return notFound(res);
|
|
493
|
+
const commit = repo.commits.get(req.params.sha);
|
|
494
|
+
res.json({ sha: commit.sha, message: commit.message, tree: { sha: commit.tree }, parents: commit.parents.map((p) => ({ sha: p })), author: commit.author });
|
|
495
|
+
});
|
|
496
|
+
router.post(`${R}/git/tags`, (req, res) => {
|
|
497
|
+
const repo = repoOr404(req, res);
|
|
498
|
+
if (!repo)
|
|
499
|
+
return;
|
|
500
|
+
const { tag, message, object } = req.body ?? {};
|
|
501
|
+
const sha = sha1(`tag ${tag} ${object} ${message}`);
|
|
502
|
+
repo.tagObjects.set(sha, { sha, tag, message, object: { sha: object, type: 'commit' } });
|
|
503
|
+
res.status(201).json({ sha });
|
|
504
|
+
});
|
|
505
|
+
router.get(`${R}/git/tags/:sha`, (req, res) => {
|
|
506
|
+
const repo = repoOr404(req, res);
|
|
507
|
+
if (!repo)
|
|
508
|
+
return;
|
|
509
|
+
const found = repo.tagObjects.get(req.params.sha);
|
|
510
|
+
if (!found)
|
|
511
|
+
return notFound(res);
|
|
512
|
+
res.json(found);
|
|
513
|
+
});
|
|
514
|
+
router.get(`${R}/git/ref/*`, (req, res) => {
|
|
515
|
+
const repo = repoOr404(req, res);
|
|
516
|
+
if (!repo)
|
|
517
|
+
return;
|
|
518
|
+
const sha = repo.refs.get(req.params[0]);
|
|
519
|
+
if (!sha)
|
|
520
|
+
return notFound(res);
|
|
521
|
+
res.json({ ref: `refs/${req.params[0]}`, object: { sha, type: repo.tagObjects.has(sha) ? 'tag' : 'commit' } });
|
|
522
|
+
});
|
|
523
|
+
router.post(`${R}/git/refs`, (req, res) => {
|
|
524
|
+
const repo = repoOr404(req, res);
|
|
525
|
+
if (!repo)
|
|
526
|
+
return;
|
|
527
|
+
const { ref, sha } = req.body ?? {};
|
|
528
|
+
const name = String(ref).replace(/^refs\//, '');
|
|
529
|
+
if (repo.refs.has(name))
|
|
530
|
+
return res.status(422).json({ message: 'Reference already exists' });
|
|
531
|
+
if (!repo.commits.has(sha) && !repo.tagObjects.has(sha))
|
|
532
|
+
return res.status(422).json({ message: 'Object does not exist' });
|
|
533
|
+
repo.refs.set(name, sha);
|
|
534
|
+
res.status(201).json({ ref, object: { sha } });
|
|
535
|
+
});
|
|
536
|
+
router.patch(`${R}/git/refs/*`, (req, res) => {
|
|
537
|
+
const repo = repoOr404(req, res);
|
|
538
|
+
if (!repo)
|
|
539
|
+
return;
|
|
540
|
+
const name = req.params[0];
|
|
541
|
+
const current = repo.refs.get(name);
|
|
542
|
+
if (!current)
|
|
543
|
+
return notFound(res);
|
|
544
|
+
const { sha, force } = req.body ?? {};
|
|
545
|
+
if (!repo.commits.has(sha))
|
|
546
|
+
return res.status(422).json({ message: 'Object does not exist' });
|
|
547
|
+
if (!force && !repo.ancestors(sha).has(current))
|
|
548
|
+
return res.status(422).json({ message: 'Update is not a fast forward' });
|
|
549
|
+
repo.refs.set(name, sha);
|
|
550
|
+
res.json({ ref: `refs/${name}`, object: { sha } });
|
|
551
|
+
});
|
|
552
|
+
router.delete(`${R}/git/refs/*`, (req, res) => {
|
|
553
|
+
const repo = repoOr404(req, res);
|
|
554
|
+
if (!repo)
|
|
555
|
+
return;
|
|
556
|
+
if (!repo.refs.delete(req.params[0]))
|
|
557
|
+
return res.status(422).json({ message: 'Reference does not exist' });
|
|
558
|
+
res.status(204).end();
|
|
559
|
+
});
|
|
560
|
+
// ── commits, branches, tags ──────────────────────────────────────────────
|
|
561
|
+
router.get(`${R}/commits`, (req, res) => {
|
|
562
|
+
const repo = repoOr404(req, res);
|
|
563
|
+
if (!repo)
|
|
564
|
+
return;
|
|
565
|
+
let sha = repo.resolve(req.query.sha ?? repo.defaultBranch);
|
|
566
|
+
if (!sha)
|
|
567
|
+
return res.status(409).json({ message: 'Git Repository is empty.' });
|
|
568
|
+
const out = [];
|
|
569
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
570
|
+
while (sha && out.length < perPage) {
|
|
571
|
+
out.push(commitJson(repo, sha));
|
|
572
|
+
sha = repo.commits.get(sha).parents[0];
|
|
573
|
+
}
|
|
574
|
+
res.json(out);
|
|
575
|
+
});
|
|
576
|
+
router.get(`${R}/compare/:range`, (req, res) => {
|
|
577
|
+
const repo = repoOr404(req, res);
|
|
578
|
+
if (!repo)
|
|
579
|
+
return;
|
|
580
|
+
const [from, to] = req.params.range.split('...').map((ref) => repo.resolve(decodeURIComponent(ref)));
|
|
581
|
+
if (!from || !to)
|
|
582
|
+
return notFound(res);
|
|
583
|
+
const base = repo.mergeBase(from, to);
|
|
584
|
+
const commits = [];
|
|
585
|
+
for (let sha = to; sha && sha !== base; sha = repo.commits.get(sha).parents[0])
|
|
586
|
+
commits.unshift(commitJson(repo, sha));
|
|
587
|
+
const before = base ? repo.trees.get(repo.commits.get(base).tree) : new Map();
|
|
588
|
+
const after = repo.trees.get(repo.commits.get(to).tree);
|
|
589
|
+
const files = [];
|
|
590
|
+
for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
|
|
591
|
+
const [b, a] = [before.get(path), after.get(path)];
|
|
592
|
+
if (b === a)
|
|
593
|
+
continue;
|
|
594
|
+
const patch = unifiedDiff(b ? repo.blobs.get(b).toString('utf8') : '', a ? repo.blobs.get(a).toString('utf8') : '');
|
|
595
|
+
files.push({
|
|
596
|
+
filename: path, status: !b ? 'added' : !a ? 'removed' : 'modified',
|
|
597
|
+
additions: patch.split('\n').filter((l) => l.startsWith('+')).length, deletions: patch.split('\n').filter((l) => l.startsWith('-')).length, patch,
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
res.json({ commits, files });
|
|
601
|
+
});
|
|
602
|
+
router.get(`${R}/commits/:ref`, (req, res) => {
|
|
603
|
+
const repo = repoOr404(req, res);
|
|
604
|
+
if (!repo)
|
|
605
|
+
return;
|
|
606
|
+
const sha = repo.resolve(req.params.ref);
|
|
607
|
+
if (!sha)
|
|
608
|
+
return notFound(res);
|
|
609
|
+
res.json(commitJson(repo, sha));
|
|
610
|
+
});
|
|
611
|
+
router.get(`${R}/branches`, (req, res) => {
|
|
612
|
+
const repo = repoOr404(req, res);
|
|
613
|
+
if (!repo)
|
|
614
|
+
return;
|
|
615
|
+
res.json([...repo.refs.keys()].filter((name) => name.startsWith('heads/')).map((name) => ({ name: name.slice(6) })));
|
|
616
|
+
});
|
|
617
|
+
router.get(`${R}/branches/:name`, (req, res) => {
|
|
618
|
+
const repo = repoOr404(req, res);
|
|
619
|
+
if (!repo)
|
|
620
|
+
return;
|
|
621
|
+
const sha = repo.refs.get(`heads/${req.params.name}`);
|
|
622
|
+
if (!sha)
|
|
623
|
+
return notFound(res);
|
|
624
|
+
res.json({ name: req.params.name, commit: commitJson(repo, sha) });
|
|
625
|
+
});
|
|
626
|
+
router.post(`${R}/branches/:name/rename`, (req, res) => {
|
|
627
|
+
const repo = repoOr404(req, res);
|
|
628
|
+
if (!repo)
|
|
629
|
+
return;
|
|
630
|
+
const sha = repo.refs.get(`heads/${req.params.name}`);
|
|
631
|
+
if (!sha)
|
|
632
|
+
return notFound(res);
|
|
633
|
+
repo.refs.delete(`heads/${req.params.name}`);
|
|
634
|
+
repo.refs.set(`heads/${req.body.new_name}`, sha);
|
|
635
|
+
if (repo.defaultBranch === req.params.name)
|
|
636
|
+
repo.defaultBranch = req.body.new_name;
|
|
637
|
+
res.status(201).json({ name: req.body.new_name, commit: commitJson(repo, sha) });
|
|
638
|
+
});
|
|
639
|
+
router.get(`${R}/tags`, (req, res) => {
|
|
640
|
+
const repo = repoOr404(req, res);
|
|
641
|
+
if (!repo)
|
|
642
|
+
return;
|
|
643
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
644
|
+
const page = Number(req.query.page ?? 1);
|
|
645
|
+
const tags = [...repo.refs].filter(([name]) => name.startsWith('tags/')).map(([name, sha]) => ({ name: name.slice(5), commit: { sha: repo.tagObjects.get(sha)?.object.sha ?? sha } }));
|
|
646
|
+
res.json(tags.slice((page - 1) * perPage, page * perPage));
|
|
647
|
+
});
|
|
648
|
+
// ── releases ─────────────────────────────────────────────────────────────
|
|
649
|
+
const releaseJson = (repo, release) => ({
|
|
650
|
+
id: release.id,
|
|
651
|
+
tag_name: release.tag_name,
|
|
652
|
+
name: release.name,
|
|
653
|
+
body: release.body,
|
|
654
|
+
prerelease: release.prerelease,
|
|
655
|
+
draft: Boolean(release.draft),
|
|
656
|
+
created_at: release.created_at,
|
|
657
|
+
html_url: `${web(repo)}/releases/tag/${release.tag_name}`,
|
|
658
|
+
});
|
|
659
|
+
router.post(`${R}/releases`, (req, res) => {
|
|
660
|
+
const repo = repoOr404(req, res);
|
|
661
|
+
if (!repo)
|
|
662
|
+
return;
|
|
663
|
+
const { tag_name: tag, name, body, prerelease } = req.body ?? {};
|
|
664
|
+
if (!tag)
|
|
665
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ resource: 'Release', code: 'missing_field', field: 'tag_name' }] });
|
|
666
|
+
if (repo.releases.some((r) => r.tag_name === tag))
|
|
667
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ resource: 'Release', code: 'already_exists', field: 'tag_name' }] });
|
|
668
|
+
// (real GitHub would make the tag from the default branch; the provider never asks for that)
|
|
669
|
+
if (!repo.refs.has(`tags/${tag}`))
|
|
670
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ resource: 'Release', code: 'invalid', field: 'tag_name' }] });
|
|
671
|
+
const release = { id: repo.nextRelease++, tag_name: tag, name: name ?? null, body: body ?? '', prerelease: Boolean(prerelease), created_at: new Date().toISOString() };
|
|
672
|
+
repo.releases.push(release);
|
|
673
|
+
res.status(201).json(releaseJson(repo, release));
|
|
674
|
+
});
|
|
675
|
+
router.get(`${R}/releases`, (req, res) => {
|
|
676
|
+
const repo = repoOr404(req, res);
|
|
677
|
+
if (!repo)
|
|
678
|
+
return;
|
|
679
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
680
|
+
const page = Number(req.query.page ?? 1);
|
|
681
|
+
const all = [...repo.releases].reverse().map((release) => releaseJson(repo, release));
|
|
682
|
+
res.json(all.slice((page - 1) * perPage, page * perPage));
|
|
683
|
+
});
|
|
684
|
+
router.get(`${R}/releases/tags/*`, (req, res) => {
|
|
685
|
+
const repo = repoOr404(req, res);
|
|
686
|
+
if (!repo)
|
|
687
|
+
return;
|
|
688
|
+
const release = repo.releases.find((r) => r.tag_name === req.params[0]);
|
|
689
|
+
if (!release)
|
|
690
|
+
return notFound(res);
|
|
691
|
+
res.json(releaseJson(repo, release));
|
|
692
|
+
});
|
|
693
|
+
router.patch(`${R}/releases/:rid`, (req, res) => {
|
|
694
|
+
const repo = repoOr404(req, res);
|
|
695
|
+
if (!repo)
|
|
696
|
+
return;
|
|
697
|
+
const release = repo.releases.find((r) => r.id === Number(req.params.rid));
|
|
698
|
+
if (!release)
|
|
699
|
+
return notFound(res);
|
|
700
|
+
const { name, body, prerelease } = req.body ?? {};
|
|
701
|
+
if (name !== undefined)
|
|
702
|
+
release.name = name;
|
|
703
|
+
if (body !== undefined)
|
|
704
|
+
release.body = body;
|
|
705
|
+
if (prerelease !== undefined)
|
|
706
|
+
release.prerelease = Boolean(prerelease);
|
|
707
|
+
res.json(releaseJson(repo, release));
|
|
708
|
+
});
|
|
709
|
+
router.delete(`${R}/releases/:rid`, (req, res) => {
|
|
710
|
+
const repo = repoOr404(req, res);
|
|
711
|
+
if (!repo)
|
|
712
|
+
return;
|
|
713
|
+
const index = repo.releases.findIndex((r) => r.id === Number(req.params.rid));
|
|
714
|
+
if (index < 0)
|
|
715
|
+
return notFound(res);
|
|
716
|
+
repo.releases.splice(index, 1);
|
|
717
|
+
res.status(204).send();
|
|
718
|
+
});
|
|
719
|
+
// ── pull requests ────────────────────────────────────────────────────────
|
|
720
|
+
router.post(`${R}/pulls`, (req, res) => {
|
|
721
|
+
const repo = repoOr404(req, res);
|
|
722
|
+
if (!repo)
|
|
723
|
+
return;
|
|
724
|
+
const { title, head, base } = req.body ?? {};
|
|
725
|
+
if (!repo.refs.has(`heads/${head}`) || !repo.refs.has(`heads/${base}`))
|
|
726
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ message: 'branch does not exist' }] });
|
|
727
|
+
if (repo.pulls.some((p) => p.state === 'open' && p.head.ref === head && p.base.ref === base)) {
|
|
728
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ message: `A pull request already exists for ${repo.owner.login}:${head}.` }] });
|
|
729
|
+
}
|
|
730
|
+
const now = new Date().toISOString();
|
|
731
|
+
const pull = {
|
|
732
|
+
number: repo.nextPull++, title, state: 'open', merged_at: null, head: { ref: head }, base: { ref: base },
|
|
733
|
+
body: req.body?.body ?? '', author: 'dev', labels: [], reviewers: [], created_at: now, updated_at: now,
|
|
734
|
+
comments: [], reviewComments: [], reviews: [], nextComment: 1,
|
|
735
|
+
};
|
|
736
|
+
repo.pulls.push(pull);
|
|
737
|
+
res.status(201).json(pullJson(repo, pull));
|
|
738
|
+
});
|
|
739
|
+
router.get(`${R}/pulls`, (req, res) => {
|
|
740
|
+
const repo = repoOr404(req, res);
|
|
741
|
+
if (!repo)
|
|
742
|
+
return;
|
|
743
|
+
const { state = 'open', base, head } = req.query;
|
|
744
|
+
const found = repo.pulls.filter((p) => (state === 'all' || (state === 'open' ? p.state === 'open' : p.state === 'closed')) && (!base || p.base.ref === base) && (!head || p.head.ref === String(head).split(':').pop()));
|
|
745
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
746
|
+
const page = Number(req.query.page ?? 1);
|
|
747
|
+
res.json(found.sort((a, b) => b.number - a.number).slice((page - 1) * perPage, page * perPage).map((p) => pullJson(repo, p)));
|
|
748
|
+
});
|
|
749
|
+
const pullOr404 = (req, res) => {
|
|
750
|
+
const repo = repoOr404(req, res);
|
|
751
|
+
if (!repo)
|
|
752
|
+
return [];
|
|
753
|
+
const pull = repo.pulls.find((p) => p.number === Number(req.params.n));
|
|
754
|
+
if (!pull) {
|
|
755
|
+
notFound(res);
|
|
756
|
+
return [];
|
|
757
|
+
}
|
|
758
|
+
return [repo, pull];
|
|
759
|
+
};
|
|
760
|
+
router.get(`${R}/pulls/:n`, (req, res) => {
|
|
761
|
+
const [repo, pull] = pullOr404(req, res);
|
|
762
|
+
if (pull)
|
|
763
|
+
res.json(pullJson(repo, pull, { withMergeable: true }));
|
|
764
|
+
});
|
|
765
|
+
router.patch(`${R}/pulls/:n`, (req, res) => {
|
|
766
|
+
const [repo, pull] = pullOr404(req, res);
|
|
767
|
+
if (!pull)
|
|
768
|
+
return;
|
|
769
|
+
if (req.body?.title !== undefined)
|
|
770
|
+
pull.title = req.body.title;
|
|
771
|
+
if (req.body?.body !== undefined)
|
|
772
|
+
pull.body = req.body.body;
|
|
773
|
+
if (req.body?.state !== undefined)
|
|
774
|
+
pull.state = req.body.state;
|
|
775
|
+
pull.updated_at = new Date().toISOString();
|
|
776
|
+
res.json(pullJson(repo, pull));
|
|
777
|
+
});
|
|
778
|
+
router.put(`${R}/pulls/:n/merge`, (req, res) => {
|
|
779
|
+
const [repo, pull] = pullOr404(req, res);
|
|
780
|
+
if (!pull)
|
|
781
|
+
return;
|
|
782
|
+
if (pull.state !== 'open')
|
|
783
|
+
return res.status(405).json({ message: 'Pull Request is not mergeable' });
|
|
784
|
+
const merged = repo.mergeTrees(pull.base.ref, pull.head.ref);
|
|
785
|
+
if (!merged)
|
|
786
|
+
return res.status(405).json({ message: 'Pull Request is not mergeable' });
|
|
787
|
+
const squash = req.body?.merge_method === 'squash';
|
|
788
|
+
const fastForward = repo.ancestors(merged.source).has(merged.target) && !squash;
|
|
789
|
+
// a squash leaves one commit whose only parent is the target
|
|
790
|
+
const sha = fastForward
|
|
791
|
+
? merged.source
|
|
792
|
+
: repo.makeCommit({ tree: repo.putTree(merged.entries), parents: squash ? [merged.target] : [merged.target, merged.source], message: req.body?.commit_message ?? `${squash ? 'Squash' : 'Merge'} pull request #${pull.number}` });
|
|
793
|
+
repo.refs.set(`heads/${pull.base.ref}`, sha);
|
|
794
|
+
pull.state = 'closed';
|
|
795
|
+
pull.merged_at = new Date().toISOString();
|
|
796
|
+
pull.updated_at = pull.merged_at;
|
|
797
|
+
res.json({ merged: true, sha, message: 'Pull Request successfully merged' });
|
|
798
|
+
});
|
|
799
|
+
// ── hooks, actions, archives ─────────────────────────────────────────────
|
|
800
|
+
router.get(`${R}/hooks`, (req, res) => { const repo = repoOr404(req, res); if (repo)
|
|
801
|
+
res.json(repo.hooks); });
|
|
802
|
+
router.post(`${R}/hooks`, (req, res) => {
|
|
803
|
+
const repo = repoOr404(req, res);
|
|
804
|
+
if (!repo)
|
|
805
|
+
return;
|
|
806
|
+
const hook = { id: repo.hooks.length + 1, name: 'web', active: true, events: req.body?.events ?? [], config: req.body?.config ?? {} };
|
|
807
|
+
repo.hooks.push(hook);
|
|
808
|
+
res.status(201).json(hook);
|
|
809
|
+
});
|
|
810
|
+
router.get(`${R}/actions/runs`, (req, res) => {
|
|
811
|
+
const repo = repoOr404(req, res);
|
|
812
|
+
if (!repo)
|
|
813
|
+
return;
|
|
814
|
+
const runs = repo.runs.filter((run) => !req.query.branch || run.head_branch === req.query.branch).slice(0, Number(req.query.per_page ?? 30));
|
|
815
|
+
res.json({ total_count: runs.length, workflow_runs: runs });
|
|
816
|
+
});
|
|
817
|
+
router.get(`${R}/zipball/:ref`, (req, res) => {
|
|
818
|
+
const repo = repoOr404(req, res);
|
|
819
|
+
if (!repo)
|
|
820
|
+
return;
|
|
821
|
+
if (!repo.resolve(req.params.ref))
|
|
822
|
+
return notFound(res);
|
|
823
|
+
res.redirect(302, `${baseUrl}/__codeload/${repo.id}/${encodeURIComponent(req.params.ref)}`);
|
|
824
|
+
});
|
|
825
|
+
app.get('/__codeload/:id/:ref', async (req, res) => {
|
|
826
|
+
const repo = state.repos.get(Number(req.params.id));
|
|
827
|
+
const tree = repo?.treeAt(req.params.ref);
|
|
828
|
+
if (!tree)
|
|
829
|
+
return notFound(res);
|
|
830
|
+
const root = `${repo.owner.login}-${repo.name}-${repo.resolve(req.params.ref).slice(0, 7)}`;
|
|
831
|
+
const zip = new JSZip();
|
|
832
|
+
for (const [path, sha] of tree)
|
|
833
|
+
zip.file(`${root}/${path}`, repo.blobs.get(sha));
|
|
834
|
+
res.status(200).type('application/zip').send(await zip.generateAsync({ type: 'nodebuffer' }));
|
|
835
|
+
});
|
|
836
|
+
// ── issues, labels, milestones ───────────────────────────────────────────
|
|
837
|
+
// (an issue and a pull request share the numbers of a repository, as on GitHub)
|
|
838
|
+
const userJson = (login) => ({ login, id: 1000 + login.length, type: 'User' });
|
|
839
|
+
const issueJson = (repo, issue) => ({
|
|
840
|
+
number: issue.number,
|
|
841
|
+
title: issue.title,
|
|
842
|
+
body: issue.body,
|
|
843
|
+
state: issue.state,
|
|
844
|
+
user: userJson(issue.author),
|
|
845
|
+
assignees: issue.assignees.map(userJson),
|
|
846
|
+
labels: issue.labels.map((name) => ({ name, color: repo.labels.find((l) => l.name === name)?.color ?? 'ededed' })),
|
|
847
|
+
milestone: issue.milestone ? { number: issue.milestone.number, title: issue.milestone.title } : null,
|
|
848
|
+
created_at: issue.created_at,
|
|
849
|
+
updated_at: issue.updated_at,
|
|
850
|
+
closed_at: issue.closed_at,
|
|
851
|
+
html_url: `${web(repo)}/issues/${issue.number}`,
|
|
852
|
+
comments: issue.comments.length,
|
|
853
|
+
});
|
|
854
|
+
const issueOr404 = (req, res) => {
|
|
855
|
+
const repo = repoOr404(req, res);
|
|
856
|
+
if (!repo)
|
|
857
|
+
return [];
|
|
858
|
+
const issue = repo.issues.find((i) => i.number === Number(req.params.n));
|
|
859
|
+
// a pull request answers on the path of an issue as well
|
|
860
|
+
const pull = repo.pulls.find((p) => p.number === Number(req.params.n));
|
|
861
|
+
if (!issue && !pull) {
|
|
862
|
+
notFound(res);
|
|
863
|
+
return [];
|
|
864
|
+
}
|
|
865
|
+
return [repo, issue ?? { pullRequest: pull }];
|
|
866
|
+
};
|
|
867
|
+
const filterIssues = (repo, { state = 'open', creator, assignee, labels, milestone, text } = {}) => repo.issues
|
|
868
|
+
.filter((i) => (state === 'all' || i.state === state)
|
|
869
|
+
&& (!creator || i.author === creator)
|
|
870
|
+
&& (!assignee || i.assignees.includes(assignee))
|
|
871
|
+
&& (!labels || String(labels).split(',').every((l) => i.labels.includes(l)))
|
|
872
|
+
&& (!milestone || i.milestone?.number === Number(milestone))
|
|
873
|
+
&& (!text || `${i.title} ${i.body}`.toLowerCase().includes(text.toLowerCase())))
|
|
874
|
+
.sort((a, b) => b.number - a.number);
|
|
875
|
+
const paged = (req, list) => {
|
|
876
|
+
const perPage = Number(req.query.per_page ?? 30);
|
|
877
|
+
const page = Number(req.query.page ?? 1);
|
|
878
|
+
return list.slice((page - 1) * perPage, page * perPage);
|
|
879
|
+
};
|
|
880
|
+
router.get(`${R}/labels`, (req, res) => { const repo = repoOr404(req, res); if (repo)
|
|
881
|
+
res.json(paged(req, repo.labels)); });
|
|
882
|
+
router.post(`${R}/labels`, (req, res) => {
|
|
883
|
+
const repo = repoOr404(req, res);
|
|
884
|
+
if (!repo)
|
|
885
|
+
return;
|
|
886
|
+
const label = { name: req.body?.name, color: req.body?.color ?? 'ededed' };
|
|
887
|
+
repo.labels.push(label);
|
|
888
|
+
res.status(201).json(label);
|
|
889
|
+
});
|
|
890
|
+
router.get(`${R}/milestones`, (req, res) => { const repo = repoOr404(req, res); if (repo)
|
|
891
|
+
res.json(paged(req, repo.milestones)); });
|
|
892
|
+
router.post(`${R}/milestones`, (req, res) => {
|
|
893
|
+
const repo = repoOr404(req, res);
|
|
894
|
+
if (!repo)
|
|
895
|
+
return;
|
|
896
|
+
const milestone = { number: repo.milestones.length + 1, title: req.body?.title, state: 'open', due_on: null };
|
|
897
|
+
repo.milestones.push(milestone);
|
|
898
|
+
res.status(201).json(milestone);
|
|
899
|
+
});
|
|
900
|
+
router.post(`${R}/issues`, (req, res) => {
|
|
901
|
+
const repo = repoOr404(req, res);
|
|
902
|
+
if (!repo)
|
|
903
|
+
return;
|
|
904
|
+
const { title, body, labels, assignees, milestone } = req.body ?? {};
|
|
905
|
+
if (!title)
|
|
906
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ message: 'title is missing' }] });
|
|
907
|
+
const now = new Date().toISOString();
|
|
908
|
+
const issue = {
|
|
909
|
+
number: repo.nextPull++, title, body: body ?? '', state: 'open', author: 'dev', created_at: now, updated_at: now, closed_at: null,
|
|
910
|
+
labels: labels ?? [], assignees: assignees ?? [], milestone: repo.milestones.find((m) => m.number === milestone) ?? null, comments: [],
|
|
911
|
+
};
|
|
912
|
+
repo.issues.push(issue);
|
|
913
|
+
res.status(201).json(issueJson(repo, issue));
|
|
914
|
+
});
|
|
915
|
+
router.get(`${R}/issues`, (req, res) => {
|
|
916
|
+
const repo = repoOr404(req, res);
|
|
917
|
+
if (!repo)
|
|
918
|
+
return;
|
|
919
|
+
res.json(paged(req, filterIssues(repo, req.query)).map((i) => issueJson(repo, i)));
|
|
920
|
+
});
|
|
921
|
+
router.get(`${R}/issues/:n`, (req, res) => {
|
|
922
|
+
const [repo, issue] = issueOr404(req, res);
|
|
923
|
+
if (!issue)
|
|
924
|
+
return;
|
|
925
|
+
if (issue.pullRequest)
|
|
926
|
+
return res.json({ ...pullJson(repo, issue.pullRequest), pull_request: {} });
|
|
927
|
+
res.json(issueJson(repo, issue));
|
|
928
|
+
});
|
|
929
|
+
router.patch(`${R}/issues/:n`, (req, res) => {
|
|
930
|
+
const [repo, issue] = issueOr404(req, res);
|
|
931
|
+
if (!issue || issue.pullRequest)
|
|
932
|
+
return;
|
|
933
|
+
const body = req.body ?? {};
|
|
934
|
+
if (body.title !== undefined)
|
|
935
|
+
issue.title = body.title;
|
|
936
|
+
if (body.body !== undefined)
|
|
937
|
+
issue.body = body.body;
|
|
938
|
+
if (body.labels !== undefined)
|
|
939
|
+
issue.labels = body.labels;
|
|
940
|
+
if (body.assignees !== undefined)
|
|
941
|
+
issue.assignees = body.assignees;
|
|
942
|
+
if (body.milestone !== undefined)
|
|
943
|
+
issue.milestone = repo.milestones.find((m) => m.number === body.milestone) ?? null;
|
|
944
|
+
if (body.state !== undefined && body.state !== issue.state) {
|
|
945
|
+
issue.state = body.state;
|
|
946
|
+
issue.closed_at = body.state === 'closed' ? new Date().toISOString() : null;
|
|
947
|
+
}
|
|
948
|
+
issue.updated_at = new Date().toISOString();
|
|
949
|
+
res.json(issueJson(repo, issue));
|
|
950
|
+
});
|
|
951
|
+
router.get(`${R}/issues/:n/comments`, (req, res) => {
|
|
952
|
+
const [, issue] = issueOr404(req, res);
|
|
953
|
+
if (issue)
|
|
954
|
+
res.json(paged(req, (issue.pullRequest ? issue.pullRequest.comments : issue.comments).map((c) => ({ ...c, user: userJson(c.author) }))));
|
|
955
|
+
});
|
|
956
|
+
router.post(`${R}/issues/:n/comments`, (req, res) => {
|
|
957
|
+
const [, issue] = issueOr404(req, res);
|
|
958
|
+
if (!issue)
|
|
959
|
+
return;
|
|
960
|
+
const list = issue.pullRequest ? issue.pullRequest.comments : issue.comments;
|
|
961
|
+
const comment = { id: Math.floor(Math.random() * 1e9), body: req.body?.body ?? '', author: 'dev', created_at: new Date().toISOString(), updated_at: new Date().toISOString() };
|
|
962
|
+
list.push(comment);
|
|
963
|
+
res.status(201).json({ ...comment, user: userJson('dev') });
|
|
964
|
+
});
|
|
965
|
+
// labels of an issue or a pull request: they live on the issue of that number
|
|
966
|
+
const labelled = (req, res) => {
|
|
967
|
+
const [repo, item] = issueOr404(req, res);
|
|
968
|
+
if (!item)
|
|
969
|
+
return [];
|
|
970
|
+
return [repo, item.pullRequest ?? item];
|
|
971
|
+
};
|
|
972
|
+
const labelsJson = (repo, item) => (item.labels ?? []).map((name) => ({ name, color: repo.labels.find((l) => l.name === name)?.color ?? 'ededed' }));
|
|
973
|
+
router.post(`${R}/issues/:n/labels`, (req, res) => {
|
|
974
|
+
const [repo, item] = labelled(req, res);
|
|
975
|
+
if (!item)
|
|
976
|
+
return;
|
|
977
|
+
item.labels = [...new Set([...(item.labels ?? []), ...(req.body?.labels ?? [])])];
|
|
978
|
+
res.json(labelsJson(repo, item));
|
|
979
|
+
});
|
|
980
|
+
router.put(`${R}/issues/:n/labels`, (req, res) => {
|
|
981
|
+
const [repo, item] = labelled(req, res);
|
|
982
|
+
if (!item)
|
|
983
|
+
return;
|
|
984
|
+
item.labels = req.body?.labels ?? [];
|
|
985
|
+
res.json(labelsJson(repo, item));
|
|
986
|
+
});
|
|
987
|
+
// the code of a pull request, line by line, and what people said about it
|
|
988
|
+
router.get(`${R}/pulls/:n/files`, (req, res) => {
|
|
989
|
+
const [repo, pull] = pullOr404(req, res);
|
|
990
|
+
if (!pull)
|
|
991
|
+
return;
|
|
992
|
+
const target = repo.resolve(pull.base.ref);
|
|
993
|
+
const source = repo.resolve(pull.head.ref);
|
|
994
|
+
const base = repo.mergeBase(target, source);
|
|
995
|
+
const before = base ? repo.trees.get(repo.commits.get(base).tree) : new Map();
|
|
996
|
+
const after = repo.trees.get(repo.commits.get(source).tree);
|
|
997
|
+
const files = [];
|
|
998
|
+
for (const path of [...new Set([...before.keys(), ...after.keys()])].sort()) {
|
|
999
|
+
const [b, a] = [before.get(path), after.get(path)];
|
|
1000
|
+
if (b === a)
|
|
1001
|
+
continue;
|
|
1002
|
+
const patch = unifiedDiff(b ? repo.blobs.get(b).toString('utf8') : '', a ? repo.blobs.get(a).toString('utf8') : '');
|
|
1003
|
+
files.push({
|
|
1004
|
+
filename: path, status: !b ? 'added' : !a ? 'removed' : 'modified',
|
|
1005
|
+
additions: patch.split('\n').filter((l) => l.startsWith('+')).length, deletions: patch.split('\n').filter((l) => l.startsWith('-')).length, patch,
|
|
1006
|
+
});
|
|
1007
|
+
}
|
|
1008
|
+
res.json(paged(req, files));
|
|
1009
|
+
});
|
|
1010
|
+
router.get(`${R}/pulls/:n/comments`, (req, res) => {
|
|
1011
|
+
const [, pull] = pullOr404(req, res);
|
|
1012
|
+
if (pull)
|
|
1013
|
+
res.json(paged(req, pull.reviewComments.map((c) => ({ ...c, user: userJson(c.author) }))));
|
|
1014
|
+
});
|
|
1015
|
+
router.post(`${R}/pulls/:n/comments`, (req, res) => {
|
|
1016
|
+
const [, pull] = pullOr404(req, res);
|
|
1017
|
+
if (!pull)
|
|
1018
|
+
return;
|
|
1019
|
+
const { body, path, line, side, in_reply_to, commit_id } = req.body ?? {};
|
|
1020
|
+
if (!body)
|
|
1021
|
+
return res.status(422).json({ message: 'Validation Failed', errors: [{ message: 'body is missing' }] });
|
|
1022
|
+
const root = in_reply_to ? pull.reviewComments.find((c) => c.id === in_reply_to) : null;
|
|
1023
|
+
if (in_reply_to && !root)
|
|
1024
|
+
return notFound(res);
|
|
1025
|
+
const now = new Date().toISOString();
|
|
1026
|
+
const comment = {
|
|
1027
|
+
id: Math.floor(Math.random() * 1e9), body, author: 'dev', created_at: now, updated_at: now,
|
|
1028
|
+
path: root?.path ?? path, line: root?.line ?? line, side: root?.side ?? side ?? 'RIGHT', commit_id: commit_id ?? repoSha(pull),
|
|
1029
|
+
...(in_reply_to ? { in_reply_to_id: in_reply_to } : {}),
|
|
1030
|
+
};
|
|
1031
|
+
pull.reviewComments.push(comment);
|
|
1032
|
+
pull.updated_at = now;
|
|
1033
|
+
res.status(201).json({ ...comment, user: userJson('dev') });
|
|
1034
|
+
});
|
|
1035
|
+
const repoSha = (pull) => pull.head.ref;
|
|
1036
|
+
router.get(`${R}/pulls/:n/reviews`, (req, res) => {
|
|
1037
|
+
const [, pull] = pullOr404(req, res);
|
|
1038
|
+
if (pull)
|
|
1039
|
+
res.json(paged(req, pull.reviews.map((r) => ({ ...r, user: userJson(r.author) }))));
|
|
1040
|
+
});
|
|
1041
|
+
router.post(`${R}/pulls/:n/reviews`, (req, res) => {
|
|
1042
|
+
const [, pull] = pullOr404(req, res);
|
|
1043
|
+
if (!pull)
|
|
1044
|
+
return;
|
|
1045
|
+
const review = { id: pull.reviews.length + 1, author: 'dev', state: req.body?.event === 'APPROVE' ? 'APPROVED' : 'COMMENTED', body: req.body?.body ?? '' };
|
|
1046
|
+
pull.reviews.push(review);
|
|
1047
|
+
res.status(200).json({ ...review, user: userJson('dev') });
|
|
1048
|
+
});
|
|
1049
|
+
router.post(`${R}/pulls/:n/requested_reviewers`, (req, res) => {
|
|
1050
|
+
const [repo, pull] = pullOr404(req, res);
|
|
1051
|
+
if (!pull)
|
|
1052
|
+
return;
|
|
1053
|
+
pull.reviewers = [...new Set([...(pull.reviewers ?? []), ...(req.body?.reviewers ?? [])])];
|
|
1054
|
+
res.status(201).json(pullJson(repo, pull));
|
|
1055
|
+
});
|
|
1056
|
+
router.delete(`${R}/pulls/:n/requested_reviewers`, (req, res) => {
|
|
1057
|
+
const [repo, pull] = pullOr404(req, res);
|
|
1058
|
+
if (!pull)
|
|
1059
|
+
return;
|
|
1060
|
+
pull.reviewers = (pull.reviewers ?? []).filter((login) => !(req.body?.reviewers ?? []).includes(login));
|
|
1061
|
+
res.json(pullJson(repo, pull));
|
|
1062
|
+
});
|
|
1063
|
+
// The search of issues: the qualifiers LINEA sends (`repo:`, `state:`, `author:`, `assignee:`, `label:`, `milestone:`) and the free text.
|
|
1064
|
+
router.get('/search/issues', (req, res) => {
|
|
1065
|
+
const q = String(req.query.q ?? '');
|
|
1066
|
+
const qualifiers = {};
|
|
1067
|
+
const flags = [];
|
|
1068
|
+
const text = q.replace(/(\w+):("[^"]*"|\S+)/g, (_all, key, value) => { const clean = value.replace(/^"|"$/g, ''); if (key === 'is')
|
|
1069
|
+
flags.push(clean);
|
|
1070
|
+
else
|
|
1071
|
+
qualifiers[key] = clean; return ''; }).trim();
|
|
1072
|
+
const repo = [...state.repos.values()].find((r) => r.fullName === qualifiers.repo);
|
|
1073
|
+
if (!repo)
|
|
1074
|
+
return res.json({ total_count: 0, items: [] });
|
|
1075
|
+
if (flags.includes('pr')) {
|
|
1076
|
+
const wantOpen = flags.includes('open');
|
|
1077
|
+
const found = repo.pulls
|
|
1078
|
+
.filter((p) => (flags.includes('merged') ? Boolean(p.merged_at) : flags.includes('unmerged') ? !p.merged_at : true)
|
|
1079
|
+
&& (wantOpen ? p.state === 'open' : flags.includes('closed') || flags.includes('merged') ? p.state === 'closed' : true)
|
|
1080
|
+
&& (!qualifiers.author || (p.author ?? 'dev') === qualifiers.author)
|
|
1081
|
+
&& (!qualifiers['review-requested'] || (p.reviewers ?? []).includes(qualifiers['review-requested']))
|
|
1082
|
+
&& (!qualifiers.label || (p.labels ?? []).includes(qualifiers.label))
|
|
1083
|
+
&& (!qualifiers.base || p.base.ref === qualifiers.base)
|
|
1084
|
+
&& (!text || `${p.title} ${p.body ?? ''}`.toLowerCase().includes(text.toLowerCase())))
|
|
1085
|
+
.sort((a, b) => b.number - a.number);
|
|
1086
|
+
return res.json({ total_count: found.length, items: paged(req, found).map((p) => ({ number: p.number, title: p.title, pull_request: {} })) });
|
|
1087
|
+
}
|
|
1088
|
+
const milestone = qualifiers.milestone ? repo.milestones.find((m) => m.title === qualifiers.milestone)?.number ?? -1 : undefined;
|
|
1089
|
+
const found = filterIssues(repo, { state: qualifiers.state ?? 'all', creator: qualifiers.author, assignee: qualifiers.assignee, labels: qualifiers.label, milestone, text });
|
|
1090
|
+
res.json({ total_count: found.length, items: paged(req, found).map((i) => issueJson(repo, i)) });
|
|
1091
|
+
});
|
|
1092
|
+
app.use(router);
|
|
1093
|
+
app.use((req, res) => res.status(501).json({ message: `githubDouble: no handler for ${req.method} ${req.originalUrl}` }));
|
|
1094
|
+
return { app, state, gitHttp };
|
|
1095
|
+
}
|
|
1096
|
+
export async function startGithubDouble() {
|
|
1097
|
+
const holder = {};
|
|
1098
|
+
const server = await new Promise((resolve, reject) => {
|
|
1099
|
+
// the app has to know its own address (links, redirects): it is only known once the server listens
|
|
1100
|
+
const placeholder = express();
|
|
1101
|
+
placeholder.use((req, res, next) => holder.app(req, res, next));
|
|
1102
|
+
const s = placeholder.listen(0, '127.0.0.1', () => resolve(s));
|
|
1103
|
+
s.on('error', reject);
|
|
1104
|
+
});
|
|
1105
|
+
const url = `http://127.0.0.1:${server.address().port}`;
|
|
1106
|
+
const { app, state, gitHttp } = createGithubDoubleApp(url);
|
|
1107
|
+
holder.app = app;
|
|
1108
|
+
return {
|
|
1109
|
+
url,
|
|
1110
|
+
state,
|
|
1111
|
+
roles: ROLES,
|
|
1112
|
+
addOrg: (login, role) => state.addOrg(login, role),
|
|
1113
|
+
setCollaborator: (repoId, login, role) => {
|
|
1114
|
+
const repo = state.repos.get(repoId);
|
|
1115
|
+
const found = repo.collaborators.find((c) => c.login === login);
|
|
1116
|
+
if (found)
|
|
1117
|
+
found.role = role;
|
|
1118
|
+
else
|
|
1119
|
+
repo.collaborators.push({ login, id: 99, role });
|
|
1120
|
+
},
|
|
1121
|
+
addRun: (repoId, run) => state.repos.get(repoId).runs.unshift(run),
|
|
1122
|
+
/** The next request (the next one whose path matches `fault.match`, when given) gets this answer instead of being handled: a rate limit, an outage. */
|
|
1123
|
+
inject: (fault) => state.faults.push(fault),
|
|
1124
|
+
getLog: () => [...state.log],
|
|
1125
|
+
reset: () => { state.reset(); gitHttp.reset(); },
|
|
1126
|
+
close: () => new Promise((resolve) => server.close(() => { gitHttp.dispose(); resolve(); })),
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
//# sourceMappingURL=github-double.js.map
|