@bevel-software/platform-core-backend 0.21.0 → 0.22.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/dist/core/create-core-server.d.ts.map +1 -1
- package/dist/core/create-core-server.js +32 -2
- package/dist/core/create-core-server.js.map +1 -1
- package/dist/core/create-core-services.d.ts +13 -1
- package/dist/core/create-core-services.d.ts.map +1 -1
- package/dist/core/create-core-services.js +51 -5
- package/dist/core/create-core-services.js.map +1 -1
- package/dist/modules/access/access-requests.contract.d.ts +75 -0
- package/dist/modules/access/access-requests.contract.d.ts.map +1 -0
- package/dist/modules/access/access-requests.contract.js +20 -0
- package/dist/modules/access/access-requests.contract.js.map +1 -0
- package/dist/modules/access/access-requests.routes.d.ts +35 -0
- package/dist/modules/access/access-requests.routes.d.ts.map +1 -0
- package/dist/modules/access/access-requests.routes.js +237 -0
- package/dist/modules/access/access-requests.routes.js.map +1 -0
- package/dist/modules/access/access-requests.service.d.ts +123 -0
- package/dist/modules/access/access-requests.service.d.ts.map +1 -0
- package/dist/modules/access/access-requests.service.js +337 -0
- package/dist/modules/access/access-requests.service.js.map +1 -0
- package/dist/modules/access-model/access-grammar.d.ts +12 -0
- package/dist/modules/access-model/access-grammar.d.ts.map +1 -1
- package/dist/modules/access-model/access-grammar.js +24 -0
- package/dist/modules/access-model/access-grammar.js.map +1 -1
- package/dist/modules/auth/auth.service.d.ts +26 -1
- package/dist/modules/auth/auth.service.d.ts.map +1 -1
- package/dist/modules/auth/auth.service.js +11 -2
- package/dist/modules/auth/auth.service.js.map +1 -1
- package/dist/modules/github-app/github-app.client.d.ts +101 -0
- package/dist/modules/github-app/github-app.client.d.ts.map +1 -0
- package/dist/modules/github-app/github-app.client.js +222 -0
- package/dist/modules/github-app/github-app.client.js.map +1 -0
- package/dist/modules/github-app/github-app.connection.d.ts +102 -0
- package/dist/modules/github-app/github-app.connection.d.ts.map +1 -0
- package/dist/modules/github-app/github-app.connection.js +185 -0
- package/dist/modules/github-app/github-app.connection.js.map +1 -0
- package/dist/modules/github-app/github-app.routes.d.ts +59 -0
- package/dist/modules/github-app/github-app.routes.d.ts.map +1 -0
- package/dist/modules/github-app/github-app.routes.js +323 -0
- package/dist/modules/github-app/github-app.routes.js.map +1 -0
- package/dist/modules/github-app/index.d.ts +4 -0
- package/dist/modules/github-app/index.d.ts.map +1 -0
- package/dist/modules/github-app/index.js +4 -0
- package/dist/modules/github-app/index.js.map +1 -0
- package/dist/modules/kb-fs/remote-url.d.ts +22 -0
- package/dist/modules/kb-fs/remote-url.d.ts.map +1 -0
- package/dist/modules/kb-fs/remote-url.js +35 -0
- package/dist/modules/kb-fs/remote-url.js.map +1 -0
- package/dist/modules/plugins/join-proposals.d.ts +17 -5
- package/dist/modules/plugins/join-proposals.d.ts.map +1 -1
- package/dist/modules/plugins/join-proposals.js +76 -22
- package/dist/modules/plugins/join-proposals.js.map +1 -1
- package/dist/modules/plugins/join-requests.service.d.ts +111 -18
- package/dist/modules/plugins/join-requests.service.d.ts.map +1 -1
- package/dist/modules/plugins/join-requests.service.js +173 -29
- package/dist/modules/plugins/join-requests.service.js.map +1 -1
- package/dist/modules/plugins/plugins.routes.d.ts.map +1 -1
- package/dist/modules/plugins/plugins.routes.js +3 -2
- package/dist/modules/plugins/plugins.routes.js.map +1 -1
- package/dist/modules/settings/deployment-settings.service.d.ts +22 -1
- package/dist/modules/settings/deployment-settings.service.d.ts.map +1 -1
- package/dist/modules/settings/deployment-settings.service.js +93 -4
- package/dist/modules/settings/deployment-settings.service.js.map +1 -1
- package/dist/modules/settings/managed-repository.d.ts +43 -0
- package/dist/modules/settings/managed-repository.d.ts.map +1 -0
- package/dist/modules/settings/managed-repository.js +60 -0
- package/dist/modules/settings/managed-repository.js.map +1 -0
- package/dist/modules/settings/repository-source.d.ts +128 -0
- package/dist/modules/settings/repository-source.d.ts.map +1 -0
- package/dist/modules/settings/repository-source.js +150 -0
- package/dist/modules/settings/repository-source.js.map +1 -0
- package/dist/modules/settings/setup.routes.d.ts +27 -4
- package/dist/modules/settings/setup.routes.d.ts.map +1 -1
- package/dist/modules/settings/setup.routes.js +187 -13
- package/dist/modules/settings/setup.routes.js.map +1 -1
- package/dist/modules/skills/skill-access-requests.routes.d.ts +6 -8
- package/dist/modules/skills/skill-access-requests.routes.d.ts.map +1 -1
- package/dist/modules/skills/skill-access-requests.routes.js +24 -63
- package/dist/modules/skills/skill-access-requests.routes.js.map +1 -1
- package/dist/modules/tool-helpers/tool-context.d.ts.map +1 -1
- package/dist/modules/tool-helpers/tool-context.js +15 -0
- package/dist/modules/tool-helpers/tool-context.js.map +1 -1
- package/dist/modules/workflow/agent-tools/workflow.tools.d.ts.map +1 -1
- package/dist/modules/workflow/agent-tools/workflow.tools.js +38 -2
- package/dist/modules/workflow/agent-tools/workflow.tools.js.map +1 -1
- package/dist/modules/workflow/git/git.service.d.ts +3 -1
- package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
- package/dist/modules/workflow/git/git.service.js +17 -4
- package/dist/modules/workflow/git/git.service.js.map +1 -1
- package/dist/modules/workflow/git/node-git-runner.d.ts.map +1 -1
- package/dist/modules/workflow/git/node-git-runner.js +5 -0
- package/dist/modules/workflow/git/node-git-runner.js.map +1 -1
- package/dist/modules/workflow/git/pull-request.service.d.ts +45 -0
- package/dist/modules/workflow/git/pull-request.service.d.ts.map +1 -1
- package/dist/modules/workflow/git/pull-request.service.js +99 -0
- package/dist/modules/workflow/git/pull-request.service.js.map +1 -1
- package/dist/modules/workflow/workflow.routes.d.ts.map +1 -1
- package/dist/modules/workflow/workflow.routes.js +3 -1
- package/dist/modules/workflow/workflow.routes.js.map +1 -1
- package/dist/modules/workflow/workflow.service.d.ts +34 -7
- package/dist/modules/workflow/workflow.service.d.ts.map +1 -1
- package/dist/modules/workflow/workflow.service.js +148 -48
- package/dist/modules/workflow/workflow.service.js.map +1 -1
- package/dist/modules/workspace/startup/kb-startup-runner.d.ts +66 -0
- package/dist/modules/workspace/startup/kb-startup-runner.d.ts.map +1 -1
- package/dist/modules/workspace/startup/kb-startup-runner.js +148 -0
- package/dist/modules/workspace/startup/kb-startup-runner.js.map +1 -1
- package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
- package/dist/modules/workspace/workspace.service.js +17 -1
- package/dist/modules/workspace/workspace.service.js.map +1 -1
- package/dist/modules/workspace/workspace.tools.d.ts.map +1 -1
- package/dist/modules/workspace/workspace.tools.js +28 -2
- package/dist/modules/workspace/workspace.tools.js.map +1 -1
- package/dist/shared/domain-errors.d.ts +39 -0
- package/dist/shared/domain-errors.d.ts.map +1 -1
- package/dist/shared/domain-errors.js +55 -0
- package/dist/shared/domain-errors.js.map +1 -1
- package/dist/shared/git.contract.d.ts +10 -0
- package/dist/shared/git.contract.d.ts.map +1 -1
- package/dist/shared/git.contract.js.map +1 -1
- package/package.json +3 -3
- package/src/core/create-core-server.ts +37 -1
- package/src/core/create-core-services.ts +65 -8
- package/src/modules/access/__tests__/access-requests.recut.test.ts +134 -0
- package/src/modules/access/__tests__/access-requests.routes.test.ts +610 -0
- package/src/modules/access/access-requests.contract.ts +93 -0
- package/src/modules/access/access-requests.routes.ts +286 -0
- package/src/modules/access/access-requests.service.ts +420 -0
- package/src/modules/access-model/access-grammar.ts +22 -0
- package/src/modules/auth/__tests__/auth.service.test.ts +38 -0
- package/src/modules/auth/auth.service.ts +28 -1
- package/src/modules/github-app/__tests__/github-app.test.ts +848 -0
- package/src/modules/github-app/github-app.client.ts +268 -0
- package/src/modules/github-app/github-app.connection.ts +204 -0
- package/src/modules/github-app/github-app.routes.ts +359 -0
- package/src/modules/github-app/index.ts +19 -0
- package/src/modules/kb-fs/__tests__/remote-url.test.ts +38 -0
- package/src/modules/kb-fs/remote-url.ts +35 -0
- package/src/modules/plugins/__tests__/join-proposals.test.ts +100 -19
- package/src/modules/plugins/__tests__/join-requests.service.test.ts +27 -11
- package/src/modules/plugins/__tests__/join-requests.settlement.test.ts +371 -0
- package/src/modules/plugins/__tests__/plugins.routes.test.ts +1 -1
- package/src/modules/plugins/join-proposals.ts +87 -19
- package/src/modules/plugins/join-requests.service.ts +199 -39
- package/src/modules/plugins/plugins.routes.ts +3 -2
- package/src/modules/settings/__tests__/repository-source.test.ts +191 -0
- package/src/modules/settings/__tests__/setup.routes.git-mode.test.ts +353 -0
- package/src/modules/settings/__tests__/setup.routes.github-app.test.ts +282 -0
- package/src/modules/settings/__tests__/setup.routes.managed-phase.test.ts +233 -0
- package/src/modules/settings/deployment-settings.service.ts +109 -3
- package/src/modules/settings/managed-repository.ts +68 -0
- package/src/modules/settings/repository-source.ts +197 -0
- package/src/modules/settings/setup.routes.ts +214 -10
- package/src/modules/skills/__tests__/skill-access-requests.routes.test.ts +5 -1
- package/src/modules/skills/skill-access-requests.routes.ts +25 -75
- package/src/modules/tool-helpers/tool-context.ts +15 -0
- package/src/modules/workflow/__tests__/apply-failure.test.ts +9 -1
- package/src/modules/workflow/__tests__/workflow.service.facade.test.ts +159 -8
- package/src/modules/workflow/__tests__/workflow.service.update-from-target.test.ts +146 -40
- package/src/modules/workflow/agent-tools/__tests__/workflow.tools.test.ts +141 -1
- package/src/modules/workflow/agent-tools/workflow.tools.ts +49 -3
- package/src/modules/workflow/git/__tests__/git.service.pull.test.ts +121 -0
- package/src/modules/workflow/git/__tests__/pull-request.service.getPrDetail.test.ts +145 -2
- package/src/modules/workflow/git/__tests__/pull-request.service.viewer-can-delete.test.ts +166 -0
- package/src/modules/workflow/git/git.service.ts +25 -4
- package/src/modules/workflow/git/node-git-runner.ts +6 -0
- package/src/modules/workflow/git/pull-request.service.ts +119 -0
- package/src/modules/workflow/workflow.routes.ts +3 -1
- package/src/modules/workflow/workflow.service.ts +170 -52
- package/src/modules/workspace/__tests__/workspace.service.unknown-branch.test.ts +43 -0
- package/src/modules/workspace/__tests__/workspace.tools.branch-errors.test.ts +233 -7
- package/src/modules/workspace/__tests__/workspace.tools.test.ts +35 -3
- package/src/modules/workspace/startup/__tests__/kb-startup-runner.test.ts +211 -0
- package/src/modules/workspace/startup/kb-startup-runner.ts +159 -0
- package/src/modules/workspace/workspace.service.ts +17 -1
- package/src/modules/workspace/workspace.tools.ts +35 -0
- package/src/shared/domain-errors.ts +58 -0
- package/src/shared/git.contract.ts +10 -0
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import type { IAdminAccessService } from '../admin/admin.interface.js';
|
|
4
|
+
import { logger } from '../../shared/logging.js';
|
|
5
|
+
import type { DeploymentSettingsService } from '../settings/deployment-settings.service.js';
|
|
6
|
+
import { GitHubAppClient, GitHubAppError } from './github-app.client.js';
|
|
7
|
+
import { repositoriesAsSetting, type GitHubAppConnection } from './github-app.connection.js';
|
|
8
|
+
import '../auth/auth.middleware.js'; // Express Request augmentation
|
|
9
|
+
|
|
10
|
+
const log = logger('github-app');
|
|
11
|
+
|
|
12
|
+
const STATE_COOKIE = 'hexis_github_state';
|
|
13
|
+
const STATE_MAX_AGE_S = 15 * 60;
|
|
14
|
+
/** A GitHub organisation's name, as it appears in an address. */
|
|
15
|
+
const ORGANIZATION_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/;
|
|
16
|
+
/** GitHub refuses an app name longer than this. */
|
|
17
|
+
const MAX_APP_NAME = 34;
|
|
18
|
+
|
|
19
|
+
export interface GitHubAppRoutesDeps {
|
|
20
|
+
settings: Pick<DeploymentSettingsService, 'resolve' | 'record'>;
|
|
21
|
+
connection: GitHubAppConnection;
|
|
22
|
+
adminAccess: IAdminAccessService;
|
|
23
|
+
/** Where GitHub sends the browser back to: this deployment's own address. */
|
|
24
|
+
publicBackendUrl: string;
|
|
25
|
+
publicFrontendUrl: string;
|
|
26
|
+
/** Whether the deployment is set up: decides which page a round trip ends on. */
|
|
27
|
+
isComplete(): boolean;
|
|
28
|
+
client?: GitHubAppClient;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The address GitHub is told to send the browser back to once the app is installed. */
|
|
32
|
+
export function githubAppCallbackUrl(publicBackendUrl: string): string {
|
|
33
|
+
return `${publicBackendUrl}/api/setup/github-app/callback`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The manifest a deployment's own GitHub App is created from. It asks for
|
|
38
|
+
* what the deployment does and nothing else: read and write the contents of
|
|
39
|
+
* the repositories it is installed on. No webhook, no events, not public:
|
|
40
|
+
* the app is this deployment's, installed by its owner on the one
|
|
41
|
+
* repository the knowledge base lives in.
|
|
42
|
+
*/
|
|
43
|
+
export function githubAppManifest(publicBackendUrl: string, publicFrontendUrl: string): Record<string, unknown> {
|
|
44
|
+
const host = new URL(publicFrontendUrl).host;
|
|
45
|
+
return {
|
|
46
|
+
name: `Hexis ${host}`.slice(0, MAX_APP_NAME),
|
|
47
|
+
url: publicFrontendUrl,
|
|
48
|
+
description: `Lets the Hexis deployment at ${host} read and write the repository its knowledge base lives in.`,
|
|
49
|
+
redirect_url: `${publicBackendUrl}/api/setup/github-app/registered`,
|
|
50
|
+
callback_urls: [githubAppCallbackUrl(publicBackendUrl)],
|
|
51
|
+
// GitHub then sends the person back signed in, which is what lets the
|
|
52
|
+
// deployment check that the installation they name is one they hold.
|
|
53
|
+
request_oauth_on_install: true,
|
|
54
|
+
public: false,
|
|
55
|
+
default_permissions: { contents: 'write', metadata: 'read' },
|
|
56
|
+
default_events: [],
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function readCookie(req: express.Request, name: string): string | null {
|
|
61
|
+
for (const segment of (req.headers.cookie ?? '').split(';')) {
|
|
62
|
+
const trimmed = segment.trim();
|
|
63
|
+
if (!trimmed.startsWith(`${name}=`)) continue;
|
|
64
|
+
try {
|
|
65
|
+
return decodeURIComponent(trimmed.slice(name.length + 1)) || null;
|
|
66
|
+
} catch {
|
|
67
|
+
return null;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function same(a: string, b: string): boolean {
|
|
74
|
+
const left = Buffer.from(a, 'utf8');
|
|
75
|
+
const right = Buffer.from(b, 'utf8');
|
|
76
|
+
return left.length === right.length && timingSafeEqual(left, right);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Connecting a repository on GitHub through a GitHub App, from the setup
|
|
81
|
+
* screen. Mounted behind the deployment's sign-in; every route is an
|
|
82
|
+
* admin's.
|
|
83
|
+
*
|
|
84
|
+
* GET /setup/github-app where the connection stands
|
|
85
|
+
* POST /setup/github-app/manifest start registering this deployment's own app
|
|
86
|
+
* GET /setup/github-app/registered GitHub sends the browser back: the app exists
|
|
87
|
+
* POST /setup/github-app/install where on GitHub the app is installed
|
|
88
|
+
* POST /setup/github-app/refresh where to sign in, to read again what may be connected
|
|
89
|
+
* GET /setup/github-app/callback GitHub sends the browser back: the app is installed
|
|
90
|
+
* GET /setup/github-app/repositories what the deployment may be pointed at
|
|
91
|
+
*
|
|
92
|
+
* A ROUND TRIP IS STARTED BY A POST AND ENDED BY A GET. The two routes
|
|
93
|
+
* GitHub sends the browser to are ordinary navigations, authenticated by
|
|
94
|
+
* the session cookie; each checks a state this browser was given when the
|
|
95
|
+
* round trip started, and spends it. Nothing that starts one is a link, so
|
|
96
|
+
* a link somebody was sent starts nothing.
|
|
97
|
+
*
|
|
98
|
+
* WHAT THE DEPLOYMENT MAY REACH is never taken from the address the browser
|
|
99
|
+
* came back on. GitHub puts an installation's number there and anyone can
|
|
100
|
+
* write another: with one app serving many deployments, that number is the
|
|
101
|
+
* only thing between a deployment and another organisation's repositories.
|
|
102
|
+
* GitHub sends the person back signed in, and the deployment asks GitHub
|
|
103
|
+
* two things about THAT PERSON: which installations they reach, and which
|
|
104
|
+
* repositories of the one they named they can push to with their own
|
|
105
|
+
* account. The first alone is not enough, since read access to one
|
|
106
|
+
* repository puts an installation on that list. Only the second is what
|
|
107
|
+
* the deployment may be pointed at.
|
|
108
|
+
*/
|
|
109
|
+
export function createGitHubAppRoutes(deps: GitHubAppRoutesDeps): express.Router {
|
|
110
|
+
const router = express.Router();
|
|
111
|
+
const client = deps.client ?? new GitHubAppClient();
|
|
112
|
+
const { settings, connection } = deps;
|
|
113
|
+
const secure = deps.publicBackendUrl.startsWith('https');
|
|
114
|
+
|
|
115
|
+
const requireAdmin: express.RequestHandler = async (req, res, next) => {
|
|
116
|
+
if (!(await deps.adminAccess.isAdmin(req.userEmail))) {
|
|
117
|
+
res.status(403).json({ error: 'Admins only' });
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
next();
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/** A state for one round trip, kept in this browser until it comes back. */
|
|
124
|
+
const beginRoundTrip = (res: express.Response): string => {
|
|
125
|
+
const tag = settings.resolve('githubAppStateTag');
|
|
126
|
+
const state = `${tag ? `${tag}.` : ''}${randomBytes(24).toString('base64url')}`;
|
|
127
|
+
res.cookie(STATE_COOKIE, state, { httpOnly: true, sameSite: 'lax', secure, maxAge: STATE_MAX_AGE_S * 1000, path: '/' });
|
|
128
|
+
return state;
|
|
129
|
+
};
|
|
130
|
+
/** Whether the browser that came back is the one that was sent. */
|
|
131
|
+
const cameBack = (req: express.Request): boolean => {
|
|
132
|
+
const kept = readCookie(req, STATE_COOKIE);
|
|
133
|
+
const given = typeof req.query.state === 'string' ? req.query.state : '';
|
|
134
|
+
return Boolean(kept && given && same(kept, given));
|
|
135
|
+
};
|
|
136
|
+
/** Back to the setup screen, saying how the round trip ended. */
|
|
137
|
+
const finish = (res: express.Response, outcome: string): void => {
|
|
138
|
+
const page = deps.isComplete() ? '/deployment' : '/';
|
|
139
|
+
res.redirect(`${deps.publicFrontendUrl}${page}?github=${encodeURIComponent(outcome)}`);
|
|
140
|
+
};
|
|
141
|
+
const installUrl = (slug: string, state: string): string =>
|
|
142
|
+
`https://github.com/apps/${encodeURIComponent(slug)}/installations/new?state=${encodeURIComponent(state)}`;
|
|
143
|
+
const outcomeOf = (err: unknown): string =>
|
|
144
|
+
err instanceof GitHubAppError && err.kind === 'unreachable' ? 'unreachable' : 'refused';
|
|
145
|
+
|
|
146
|
+
router.get('/setup/github-app', requireAdmin, (_req, res) => {
|
|
147
|
+
const credentials = connection.credentials();
|
|
148
|
+
res.json({
|
|
149
|
+
// Who supplied the app. Null: this deployment has none yet, and can register its own.
|
|
150
|
+
registeredBy: connection.registeredBy(),
|
|
151
|
+
app: credentials ? { slug: credentials.slug, url: `https://github.com/apps/${credentials.slug}` } : null,
|
|
152
|
+
installation: connection.installationId()
|
|
153
|
+
? { id: connection.installationId(), account: settings.resolve('githubInstallationAccount') }
|
|
154
|
+
: null,
|
|
155
|
+
repository: settings.resolve('githubRepository') || null,
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
router.post('/setup/github-app/manifest', requireAdmin, (req, res) => {
|
|
160
|
+
if (connection.credentials()) {
|
|
161
|
+
res.status(409).json({ error: 'This deployment already has a GitHub App.' });
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const organization = (req.body as { organization?: unknown } | undefined)?.organization;
|
|
165
|
+
const org = typeof organization === 'string' ? organization.trim() : '';
|
|
166
|
+
if (org && !ORGANIZATION_PATTERN.test(org)) {
|
|
167
|
+
res.status(400).json({ error: 'Enter the organisation’s name as it appears in its GitHub address.' });
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const state = beginRoundTrip(res);
|
|
171
|
+
const base = org ? `https://github.com/organizations/${org}/settings/apps/new` : 'https://github.com/settings/apps/new';
|
|
172
|
+
// The browser posts the manifest to GitHub itself: a form, not a request of ours.
|
|
173
|
+
res.json({
|
|
174
|
+
action: `${base}?state=${encodeURIComponent(state)}`,
|
|
175
|
+
manifest: githubAppManifest(deps.publicBackendUrl, deps.publicFrontendUrl),
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
router.get('/setup/github-app/registered', requireAdmin, async (req, res) => {
|
|
180
|
+
// Spent on return, however the return ends.
|
|
181
|
+
const returned = cameBack(req);
|
|
182
|
+
res.clearCookie(STATE_COOKIE, { path: '/' });
|
|
183
|
+
if (!returned) {
|
|
184
|
+
finish(res, 'state');
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const code = typeof req.query.code === 'string' ? req.query.code : '';
|
|
188
|
+
if (!code || connection.credentials()) {
|
|
189
|
+
finish(res, code ? 'already-registered' : 'refused');
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
try {
|
|
193
|
+
const app = await client.convertManifest(code);
|
|
194
|
+
await settings.record(
|
|
195
|
+
{
|
|
196
|
+
githubAppId: app.appId,
|
|
197
|
+
githubAppSlug: app.slug,
|
|
198
|
+
githubAppPrivateKey: app.privateKey,
|
|
199
|
+
githubAppClientId: app.clientId,
|
|
200
|
+
githubAppClientSecret: app.clientSecret,
|
|
201
|
+
},
|
|
202
|
+
req.userId ?? null,
|
|
203
|
+
);
|
|
204
|
+
log.info(`GitHub App "${app.slug}" registered for this deployment`, { slug: app.slug, owner: app.owner });
|
|
205
|
+
// Straight on to installing it: the same round trip, the same state.
|
|
206
|
+
res.redirect(installUrl(app.slug, beginRoundTrip(res)));
|
|
207
|
+
} catch (err) {
|
|
208
|
+
log.error('the GitHub App could not be registered:', { detail: err instanceof Error ? err.message : String(err) });
|
|
209
|
+
finish(res, outcomeOf(err));
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* A POST that answers with the address, and the page sends the browser
|
|
215
|
+
* there. As a link it was a round trip anyone could start in an admin's
|
|
216
|
+
* browser by getting them to follow it: the state would be set, and
|
|
217
|
+
* whatever they then installed would come back with a state that matched.
|
|
218
|
+
*/
|
|
219
|
+
router.post('/setup/github-app/install', requireAdmin, (_req, res) => {
|
|
220
|
+
const credentials = connection.credentials();
|
|
221
|
+
if (!credentials) {
|
|
222
|
+
res.status(409).json({ error: 'This deployment has no GitHub App yet. Create it first.' });
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
res.json({ url: installUrl(credentials.slug, beginRoundTrip(res)) });
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Ask GitHub again what the person may connect, WITHOUT installing
|
|
230
|
+
* anything: a sign-in with the app and nothing more, which comes back on
|
|
231
|
+
* the same callback with a code and no installation named.
|
|
232
|
+
*
|
|
233
|
+
* It exists because installing comes back only the first time. Once the
|
|
234
|
+
* app is installed, the address that installs it opens the installation's
|
|
235
|
+
* settings on GitHub, and GitHub sends nobody back from there: a
|
|
236
|
+
* repository added on that page was reached by the app and never offered
|
|
237
|
+
* here, since what is offered is what was recorded when the person
|
|
238
|
+
* connected. The same goes for an installation an organisation's owner
|
|
239
|
+
* approved later, in a browser of their own.
|
|
240
|
+
*/
|
|
241
|
+
router.post('/setup/github-app/refresh', requireAdmin, (_req, res) => {
|
|
242
|
+
const credentials = connection.credentials();
|
|
243
|
+
if (!credentials) {
|
|
244
|
+
res.status(409).json({ error: 'This deployment has no GitHub App yet. Create it first.' });
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const state = beginRoundTrip(res);
|
|
248
|
+
res.json({
|
|
249
|
+
url: `https://github.com/login/oauth/authorize?client_id=${encodeURIComponent(credentials.clientId)}&state=${encodeURIComponent(state)}`,
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
router.get('/setup/github-app/callback', requireAdmin, async (req, res) => {
|
|
254
|
+
const returned = cameBack(req);
|
|
255
|
+
res.clearCookie(STATE_COOKIE, { path: '/' });
|
|
256
|
+
if (!returned) {
|
|
257
|
+
finish(res, 'state');
|
|
258
|
+
return;
|
|
259
|
+
}
|
|
260
|
+
const credentials = connection.credentials();
|
|
261
|
+
if (!credentials) {
|
|
262
|
+
finish(res, 'not-registered');
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
// Someone without the right to install asked their organisation's owner
|
|
266
|
+
// to: nothing is installed yet, and there is nothing to record.
|
|
267
|
+
if (req.query.setup_action === 'request') {
|
|
268
|
+
finish(res, 'requested');
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const code = typeof req.query.code === 'string' ? req.query.code : '';
|
|
272
|
+
const given = typeof req.query.installation_id === 'string' ? req.query.installation_id : '';
|
|
273
|
+
if (!code || (given !== '' && !/^\d+$/.test(given))) {
|
|
274
|
+
finish(res, 'refused');
|
|
275
|
+
return;
|
|
276
|
+
}
|
|
277
|
+
// Back from installing, GitHub names the installation. Back from a
|
|
278
|
+
// sign-in alone (see `refresh`) it names none, and the one in question
|
|
279
|
+
// is the one this deployment has. Either way it is only a name: what
|
|
280
|
+
// decides is what GitHub says the person reaches, below.
|
|
281
|
+
const refreshing = given === '';
|
|
282
|
+
const named = given || connection.installationId();
|
|
283
|
+
try {
|
|
284
|
+
const userToken = await client.exchangeUserCode(credentials, code);
|
|
285
|
+
const reached = await client.installationsOf(userToken);
|
|
286
|
+
let held = named ? reached.find((installation) => installation.id === named) : undefined;
|
|
287
|
+
if (!named) {
|
|
288
|
+
// No installation on record and none named: the app was installed
|
|
289
|
+
// where this browser never came back from (an owner approved it
|
|
290
|
+
// later). One the person reaches is the one; several are a choice
|
|
291
|
+
// only installing from here can make.
|
|
292
|
+
if (reached.length === 0) {
|
|
293
|
+
finish(res, 'not-installed');
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
if (reached.length > 1) {
|
|
297
|
+
finish(res, 'several');
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
held = reached[0];
|
|
301
|
+
}
|
|
302
|
+
if (!held) {
|
|
303
|
+
log.warn('an installation was named that the person who came back cannot reach', { installation: named });
|
|
304
|
+
finish(res, 'not-yours');
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
// Reaching the installation is not holding it: read access to one
|
|
308
|
+
// repository it covers is enough for GitHub to list it. What the
|
|
309
|
+
// deployment may be pointed at is what THIS PERSON can push to.
|
|
310
|
+
const theirs = await client.repositoriesOf(userToken, held.id);
|
|
311
|
+
const writable = theirs.repositories.filter((repository) => repository.writable).map((r) => r.fullName);
|
|
312
|
+
if (writable.length === 0) {
|
|
313
|
+
log.warn('the person who came back can push to nothing the installation covers', { installation: held.id });
|
|
314
|
+
finish(res, 'nothing-to-write');
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
await settings.record(
|
|
318
|
+
{
|
|
319
|
+
githubInstallationId: held.id,
|
|
320
|
+
githubRepositoriesPermitted: repositoriesAsSetting(writable),
|
|
321
|
+
...(held.account ? { githubInstallationAccount: held.account } : {}),
|
|
322
|
+
},
|
|
323
|
+
req.userId ?? null,
|
|
324
|
+
);
|
|
325
|
+
// A token in hand was another installation's.
|
|
326
|
+
connection.forget();
|
|
327
|
+
log.info(
|
|
328
|
+
refreshing
|
|
329
|
+
? `what may be connected through the GitHub App on "${held.account}" was read again`
|
|
330
|
+
: `GitHub App installed on "${held.account}"`,
|
|
331
|
+
{ installation: held.id, account: held.account, repositories: writable.length },
|
|
332
|
+
);
|
|
333
|
+
finish(res, refreshing ? 'refreshed' : 'connected');
|
|
334
|
+
} catch (err) {
|
|
335
|
+
log.error('the installation could not be confirmed:', { detail: err instanceof Error ? err.message : String(err) });
|
|
336
|
+
finish(res, outcomeOf(err));
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
router.get('/setup/github-app/repositories', requireAdmin, async (_req, res) => {
|
|
341
|
+
if (!connection.credentials() || !connection.installationId()) {
|
|
342
|
+
res.status(409).json({ error: 'Connect GitHub first.' });
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
try {
|
|
346
|
+
res.json(await connection.repositories());
|
|
347
|
+
} catch (err) {
|
|
348
|
+
log.error('the repositories could not be listed:', { detail: err instanceof Error ? err.message : String(err) });
|
|
349
|
+
const unreachable = err instanceof GitHubAppError && err.kind === 'unreachable';
|
|
350
|
+
res.status(502).json({
|
|
351
|
+
error: unreachable
|
|
352
|
+
? 'GitHub could not be reached. Try again shortly.'
|
|
353
|
+
: 'GitHub would not list the repositories. The app may have been uninstalled: connect it again.',
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
return router;
|
|
359
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export {
|
|
2
|
+
GitHubAppClient,
|
|
3
|
+
GitHubAppError,
|
|
4
|
+
appJwt,
|
|
5
|
+
normalizePrivateKey,
|
|
6
|
+
type GitHubAppCredentials,
|
|
7
|
+
type GitHubFailureKind,
|
|
8
|
+
type Installation,
|
|
9
|
+
type InstallationRepository,
|
|
10
|
+
type InstallationToken,
|
|
11
|
+
type RegisteredApp,
|
|
12
|
+
} from './github-app.client.js';
|
|
13
|
+
export { GitHubAppConnection, repositoriesAsSetting, type GitHubAppConnectionOptions } from './github-app.connection.js';
|
|
14
|
+
export {
|
|
15
|
+
createGitHubAppRoutes,
|
|
16
|
+
githubAppCallbackUrl,
|
|
17
|
+
githubAppManifest,
|
|
18
|
+
type GitHubAppRoutesDeps,
|
|
19
|
+
} from './github-app.routes.js';
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { normalizeRepositoryAddress, sameRepository } from '../remote-url.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The comparison decides whether a working copy is kept or deleted, so what
|
|
6
|
+
* it must never do is call the SAME repository a different one.
|
|
7
|
+
*/
|
|
8
|
+
describe('sameRepository', () => {
|
|
9
|
+
it.each([
|
|
10
|
+
['https://github.com/org/repo', 'https://github.com/org/repo.git'],
|
|
11
|
+
['https://github.com/org/repo', 'https://github.com/org/repo/'],
|
|
12
|
+
['https://github.com/org/repo.git/', 'https://github.com/org/repo'],
|
|
13
|
+
['https://GitHub.com/org/repo', 'https://github.com/org/repo'],
|
|
14
|
+
['HTTPS://github.com/org/repo', 'https://github.com/org/repo'],
|
|
15
|
+
['https://x-access-token:secret@github.com/org/repo', 'https://github.com/org/repo'],
|
|
16
|
+
[' https://github.com/org/repo ', 'https://github.com/org/repo'],
|
|
17
|
+
['/srv/git/kb.git', '/srv/git/kb'],
|
|
18
|
+
['C:\\git\\kb.git\\', 'C:\\git\\kb'],
|
|
19
|
+
])('%s is %s', (a, b) => {
|
|
20
|
+
expect(sameRepository(a, b)).toBe(true);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it.each([
|
|
24
|
+
['https://github.com/org/repo', 'https://github.com/org/other'],
|
|
25
|
+
['https://github.com/org/repo', 'https://github.com/other/repo'],
|
|
26
|
+
['https://github.com/org/repo', 'https://gitlab.com/org/repo'],
|
|
27
|
+
['https://github.com/org/repo', 'https://github.com/org/repo-kb'],
|
|
28
|
+
['https://github.com/org/Repo', 'https://github.com/org/repo'],
|
|
29
|
+
])('%s is not %s', (a, b) => {
|
|
30
|
+
expect(sameRepository(a, b)).toBe(false);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('never carries a credential into the normalized form', () => {
|
|
34
|
+
expect(normalizeRepositoryAddress('https://user:token@github.com/org/repo.git')).toBe(
|
|
35
|
+
'https://github.com/org/repo',
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether two repository addresses name the same repository.
|
|
3
|
+
*
|
|
4
|
+
* The answer decides whether a working copy on disk is KEPT or DELETED, so it
|
|
5
|
+
* errs towards "same": only differences that cannot change which repository
|
|
6
|
+
* is meant are ignored, and they are ignored on both sides.
|
|
7
|
+
*
|
|
8
|
+
* - a trailing slash, and a `.git` suffix — hosts serve both spellings;
|
|
9
|
+
* - the letter case of the scheme and the host — DNS does not distinguish;
|
|
10
|
+
* - userinfo (`user:token@`) — who asks does not change what is asked for,
|
|
11
|
+
* and a credential must never decide, or appear in, a comparison.
|
|
12
|
+
*
|
|
13
|
+
* The PATH keeps its case: hosts that fold it and hosts that do not both
|
|
14
|
+
* exist, and treating `Org/Repo` and `org/repo` as one would keep a clone of
|
|
15
|
+
* the wrong repository on a host that tells them apart — but deleting a clone
|
|
16
|
+
* of the right one is the worse error, and an operator who only changed the
|
|
17
|
+
* case gets a re-clone, not a loss: the work on that clone was pushed to the
|
|
18
|
+
* same place.
|
|
19
|
+
*/
|
|
20
|
+
export function normalizeRepositoryAddress(address: string): string {
|
|
21
|
+
let a = address.trim();
|
|
22
|
+
const url = /^([a-z][a-z0-9+.-]*):\/\/([^/]*)(\/.*)?$/i.exec(a);
|
|
23
|
+
if (url) {
|
|
24
|
+
const host = url[2]!.replace(/^[^@]*@/, '').toLowerCase();
|
|
25
|
+
a = `${url[1]!.toLowerCase()}://${host}${url[3] ?? ''}`;
|
|
26
|
+
}
|
|
27
|
+
return a
|
|
28
|
+
.replace(/[\\/]+$/, '')
|
|
29
|
+
.replace(/\.git$/i, '')
|
|
30
|
+
.replace(/[\\/]+$/, '');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function sameRepository(a: string, b: string): boolean {
|
|
34
|
+
return normalizeRepositoryAddress(a) === normalizeRepositoryAddress(b);
|
|
35
|
+
}
|
|
@@ -2,8 +2,13 @@ import { describe, it, expect } from 'vitest';
|
|
|
2
2
|
import { pendingProposals } from '../join-proposals.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* What a
|
|
6
|
-
* the request's lifecycle: non-empty ⇒ open, empty ⇒ settled and closed
|
|
5
|
+
* What a request's branch proposes, relative to the default branch. This diff
|
|
6
|
+
* IS the request's lifecycle: non-empty ⇒ open, empty ⇒ settled and closed,
|
|
7
|
+
* null ⇒ the branch could not be read and nothing may be concluded.
|
|
8
|
+
*
|
|
9
|
+
* Which grammar reads it follows the ITEM, and the second describe below is
|
|
10
|
+
* why: a folder's rules are block lists in an access.md body, a file's are its
|
|
11
|
+
* own frontmatter, where a single grant is a scalar.
|
|
7
12
|
*/
|
|
8
13
|
const PATH = 'Plugins/GTM/access.md';
|
|
9
14
|
|
|
@@ -17,7 +22,7 @@ describe('pendingProposals', () => {
|
|
|
17
22
|
const branch = base(
|
|
18
23
|
'read:\n - GTM Team\n - Olga Ivanova <olga@bevel.software>\n - Ali Baba <ali@bevel.software>\n',
|
|
19
24
|
);
|
|
20
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([
|
|
25
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([
|
|
21
26
|
{
|
|
22
27
|
verb: 'read',
|
|
23
28
|
id: 'user:ali@bevel.software',
|
|
@@ -31,7 +36,7 @@ describe('pendingProposals', () => {
|
|
|
31
36
|
// A branch asking for `write` must be visible AS a write request rather
|
|
32
37
|
// than hiding behind "asked to join".
|
|
33
38
|
const branch = base('read:\n - GTM Team\n - Olga Ivanova <olga@bevel.software>\nwrite:\n - Finance Team\n');
|
|
34
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([
|
|
39
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([
|
|
35
40
|
{
|
|
36
41
|
verb: 'write',
|
|
37
42
|
id: 'role:finance team',
|
|
@@ -42,12 +47,12 @@ describe('pendingProposals', () => {
|
|
|
42
47
|
});
|
|
43
48
|
|
|
44
49
|
it('is EMPTY when the branch is identical — the settled state', () => {
|
|
45
|
-
expect(pendingProposals(DEFAULT_MD, DEFAULT_MD, PATH)).toEqual([]);
|
|
50
|
+
expect(pendingProposals(DEFAULT_MD, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
46
51
|
});
|
|
47
52
|
|
|
48
53
|
it('is EMPTY when the branch grants a SUBSET (its proposal already landed)', () => {
|
|
49
54
|
const branch = base('read:\n - Olga Ivanova <olga@bevel.software>\n');
|
|
50
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([]);
|
|
55
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
51
56
|
});
|
|
52
57
|
|
|
53
58
|
it('ignores a REMOVAL — a branch that drops one grant but keeps the rest proposes nothing', () => {
|
|
@@ -55,36 +60,38 @@ describe('pendingProposals', () => {
|
|
|
55
60
|
// ordinary change request in the review UI. (Distinct from the subset
|
|
56
61
|
// case above: here GTM Team survives and only Olga is dropped.)
|
|
57
62
|
const branch = base('read:\n - GTM Team\n');
|
|
58
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([]);
|
|
63
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
59
64
|
});
|
|
60
65
|
|
|
61
66
|
it('ignores a `deny` entry — a denial is not something to accept', () => {
|
|
62
67
|
const branch = base(
|
|
63
68
|
'read:\n - GTM Team\n - Olga Ivanova <olga@bevel.software>\n - deny Ali Baba <ali@bevel.software>\n',
|
|
64
69
|
);
|
|
65
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([]);
|
|
70
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
66
71
|
});
|
|
67
72
|
|
|
68
73
|
it('matches principals canonically (case-insensitive email, canonical role)', () => {
|
|
69
74
|
const branch = base('read:\n - gtm team\n - Olga I <OLGA@Bevel.Software>\n');
|
|
70
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([]);
|
|
75
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
71
76
|
});
|
|
72
77
|
|
|
73
|
-
it('
|
|
74
|
-
// A body naming a verb but shaped wrong is a hard parse error
|
|
75
|
-
//
|
|
76
|
-
|
|
78
|
+
it('says NOTHING about a malformed branch file — not "nothing to propose"', () => {
|
|
79
|
+
// A body naming a verb but shaped wrong is a hard parse error. The
|
|
80
|
+
// difference matters because an empty answer CLOSES the request: it has
|
|
81
|
+
// to mean the branch asks for nothing more, never that it would not say.
|
|
82
|
+
expect(pendingProposals(base('read: GTM Team\n'), DEFAULT_MD, PATH, 'folder')).toBeNull();
|
|
77
83
|
});
|
|
78
84
|
|
|
79
|
-
it('
|
|
80
|
-
expect(pendingProposals(null, DEFAULT_MD, PATH)).
|
|
85
|
+
it('says NOTHING about a missing branch file either', () => {
|
|
86
|
+
expect(pendingProposals(null, DEFAULT_MD, PATH, 'folder')).toBeNull();
|
|
81
87
|
});
|
|
82
88
|
|
|
83
89
|
it('surfaces every branch grant when the DEFAULT file is missing or unreadable', () => {
|
|
84
|
-
// The safe direction: show the
|
|
85
|
-
//
|
|
90
|
+
// The safe direction on THAT side: show the editor proposals to consider
|
|
91
|
+
// rather than swallowing them against a baseline nobody could read.
|
|
86
92
|
const branch = base('read:\n - Ali Baba <ali@bevel.software>\n');
|
|
87
|
-
expect(pendingProposals(branch, null, PATH)).toHaveLength(1);
|
|
93
|
+
expect(pendingProposals(branch, null, PATH, 'folder')).toHaveLength(1);
|
|
94
|
+
expect(pendingProposals(branch, base('read: broken\n'), PATH, 'folder')).toHaveLength(1);
|
|
88
95
|
});
|
|
89
96
|
|
|
90
97
|
it('does not confuse the SELF-frontmatter with folder rules', () => {
|
|
@@ -96,6 +103,80 @@ describe('pendingProposals', () => {
|
|
|
96
103
|
const branch =
|
|
97
104
|
'---\nread:\n - everyone\n - Ali Baba <ali@bevel.software>\n---\n' +
|
|
98
105
|
'read:\n - GTM Team\n - Olga Ivanova <olga@bevel.software>\n';
|
|
99
|
-
expect(pendingProposals(branch, DEFAULT_MD, PATH)).toEqual([]);
|
|
106
|
+
expect(pendingProposals(branch, DEFAULT_MD, PATH, 'folder')).toEqual([]);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A FILE's rules are not an access.md. They live in the node's own
|
|
112
|
+
* frontmatter, and `spliceGrant` writes one grant there as the scalar form
|
|
113
|
+
* the resolver accepts — `write: Rita <rita@x.io>`, not a list.
|
|
114
|
+
*
|
|
115
|
+
* Read with the FOLDER grammar that is a hard parse error ("'write:' must be
|
|
116
|
+
* a list"), the whole file yields no grants, and a branch with a perfectly
|
|
117
|
+
* good proposal on it looks like a branch with nothing left to propose. Every
|
|
118
|
+
* request whose target was a file was closed on the editors' first listing
|
|
119
|
+
* because of it — before anyone saw the line, let alone pressed Accept.
|
|
120
|
+
*/
|
|
121
|
+
describe('pendingProposals on a FILE, whose rules are its own frontmatter', () => {
|
|
122
|
+
const FILE = 'Research/Notes.md';
|
|
123
|
+
const LIVE = '---\nnodeType: "[Note](../NodeTypes/Note.md)"\n---\n# Notes\n\nbody\n';
|
|
124
|
+
/** Exactly what `spliceGrant(..., { allowScalar: true, target: 'node' })` writes. */
|
|
125
|
+
const ASKS_WRITE =
|
|
126
|
+
'---\nnodeType: "[Note](../NodeTypes/Note.md)"\nwrite: Rita Reader <rita@x.io>\n---\n# Notes\n\nbody\n';
|
|
127
|
+
|
|
128
|
+
it('reads the scalar grant the splice actually writes', () => {
|
|
129
|
+
expect(pendingProposals(ASKS_WRITE, LIVE, FILE, 'file')).toEqual([
|
|
130
|
+
{
|
|
131
|
+
verb: 'write',
|
|
132
|
+
id: 'user:rita@x.io',
|
|
133
|
+
principal: { kind: 'user', email: 'rita@x.io', displayName: 'Rita Reader' },
|
|
134
|
+
label: 'Rita Reader',
|
|
135
|
+
},
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('is not fooled by the FOLDER grammar, which cannot read it at all', () => {
|
|
140
|
+
// The regression guard. Before, this answered `[]` — indistinguishable
|
|
141
|
+
// from a finished request, which is what closed them.
|
|
142
|
+
expect(pendingProposals(ASKS_WRITE, LIVE, FILE, 'folder')).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('ignores the non-access keys a node legitimately carries', () => {
|
|
146
|
+
expect(pendingProposals(LIVE, LIVE, FILE, 'file')).toEqual([]);
|
|
147
|
+
// `nodeType` is not a grant, and its presence is not a parse failure.
|
|
148
|
+
expect(pendingProposals(LIVE, null, FILE, 'file')).toEqual([]);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
it('reads a file with no frontmatter as granting nothing, not as unreadable', () => {
|
|
152
|
+
// Nothing to say is a real answer here; a node need not declare rules.
|
|
153
|
+
expect(pendingProposals('# Just a note\n', LIVE, FILE, 'file')).toEqual([]);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('says NOTHING about a file whose frontmatter is broken — not "nothing to propose"', () => {
|
|
157
|
+
// The same rule the folder side keeps: an empty answer closes somebody's
|
|
158
|
+
// request, so a block that could not be read must not produce one.
|
|
159
|
+
const neverClosed = '---\nnodeType: "[Note](x)"\nwrite: Rita Reader <rita@x.io>\n# Notes\n\nbody\n';
|
|
160
|
+
expect(pendingProposals(neverClosed, LIVE, FILE, 'file')).toBeNull();
|
|
161
|
+
const notAMapping = '---\n- just\n- a list\n---\n# Notes\n';
|
|
162
|
+
expect(pendingProposals(notAMapping, LIVE, FILE, 'file')).toBeNull();
|
|
163
|
+
// An empty block is readable, and says nothing.
|
|
164
|
+
expect(pendingProposals('---\n---\n# Notes\n', LIVE, FILE, 'file')).toEqual([]);
|
|
165
|
+
// A broken copy on LIVE stays the safe direction: every grant looks incoming.
|
|
166
|
+
expect(pendingProposals(ASKS_WRITE, neverClosed, FILE, 'file')).toEqual([
|
|
167
|
+
expect.objectContaining({ verb: 'write', id: 'user:rita@x.io' }),
|
|
168
|
+
]);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('reads the list form too, and an Owner request', () => {
|
|
172
|
+
const asksOwner =
|
|
173
|
+
'---\nnodeType: "[Note](x)"\nowner:\n - Rita Reader <rita@x.io>\n---\n# Notes\n';
|
|
174
|
+
expect(pendingProposals(asksOwner, LIVE, FILE, 'file')).toEqual([
|
|
175
|
+
expect.objectContaining({ verb: 'owner', id: 'user:rita@x.io' }),
|
|
176
|
+
]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('still settles a file request once the grant is on live', () => {
|
|
180
|
+
expect(pendingProposals(ASKS_WRITE, ASKS_WRITE, FILE, 'file')).toEqual([]);
|
|
100
181
|
});
|
|
101
182
|
});
|