Public: anyone can read this; the crew can change it.

A new contributor hears they were invited, and the exports name contributors #16396

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

talk/invite-notice → main 0 commits

  • Merged. talk/invite-notice was merged into main as ec5af41.

Review

Sign in to review this change.
6 files changed +83 −7
docs/features/contributors.md +2 −1
@@ -6,8 +6,9 @@ row: Invite someone to work on an app: they ask its crew, you land
66
77 The repository's admin invites a person as a **contributor**. A contributor reads the repository, enters its app and previews, and asks its crew for changes from their own Talk. What they ask lands as pulls the owner lands: a contributor merges nothing, changes no setting, sees no variable or webhook, opens no terminal on the app, and invites nobody. Their asks run on the owner's crew and subscription, so the week's cap holds them as it holds the crew's own work: a run passes the cap only for someone who writes the repository, and that holds for an outsider's run too.
88
9 - **Talk and MCP**: `invite { repo, who }`, one confirm, makes a person with an account a contributor at once; their Talk works on the app from then on. `invite { repo, who, remove: true }` takes them off; `invite { repo }` lists who works on it (a read, never held). The repository's admin only: a contributor is told it is the admin's, anyone else gets not found.
9 - **Talk and MCP**: `invite { repo, who }`, one confirm, makes a person with an account a contributor at once; they hear it on their notice path (`crew/notify.ts`), with a link to their Talk on the app. `invite { repo, who, remove: true }` takes them off; `invite { repo }` lists who works on it (a read, never held). The repository's admin only: a contributor is told it is the admin's, anyone else gets not found.
1010 - **The terminal**: on an app's deploys screen, `W` lists who works on it; `a` invites a person by username and `d` takes the one under the cursor off, each after a confirm.
11 - **Exports**: the owner's account export names who contributes to each repository, by username only; a contributor's names the repositories they contribute to.
1112 - **Taking someone off** is at once: every read, app entry and crew direction asks the row on its request.
1213
1314 Someone without an account cannot be invited yet: a one-use link for them is the next step.
src/actions/invites.ts +18 −1
@@ -6,6 +6,7 @@
66 import type { App } from "../app.ts";
77 import {
88 addContributor,
9 contributedSince,
910 listContributors,
1011 listInvites,
1112 mintInvite,
@@ -14,7 +15,7 @@ import {
1415 revokeInvite,
1516 type Invite,
1617 } from "../core/invites.ts";
17 import { canRead, canWrite, type Repo } from "../core/repos.ts";
18 import { canRead, canWrite, getRepoById, type Repo } from "../core/repos.ts";
1819 import { getUserByName } from "../core/users.ts";
1920 import { requireRepoAdmin, type AccessError } from "./access.ts";
2021 import { err, ok, type Result } from "../kernel/result.ts";
@@ -133,3 +134,19 @@ export function removeContributorFrom(app: App, actor: User | undefined, repo: R
133134 app.bus.append({ kind: "repo.contributor_removed", repoId: repo.id, actorId: admin.value.id, payload: { username: person.username } });
134135 return ok(undefined);
135136 }
137
138 /**
139 * The notice a new contributor hears (`crew/notify.ts`, the forge's one notice path): who let them
140 * in where, and their Talk on it. Each is said once, for a week after.
141 */
142 export function contributorLines(app: App, user: User, now: number = Date.now()): { id: string; line: string; items: readonly string[]; link: string }[] {
143 if (user.kind !== "user") return [];
144 const since = new Date(now - 7 * 86_400_000).toISOString();
145 return contributedSince(app.db, user.id, since).flatMap((c) => {
146 const repo = getRepoById(app.db, c.repoId);
147 if (repo === undefined) return [];
148 const name = `${repo.ownerName}/${repo.name}`;
149 const id = `contributor:${repo.id}@${c.at}`;
150 return [{ id, line: `${c.by ?? "Its owner"} invited you to work on ${name}: ask Talk for a change.`, items: [id], link: `/talk?about=${encodeURIComponent(name)}` }];
151 });
152 }
src/core/account_export.ts +25 −4
@@ -96,9 +96,19 @@ export function accountExport(db: Database, userId: string, extras: ExportExtras
9696 const rows = (table: string) =>
9797 db.query<Record<string, unknown>, [string]>(`SELECT ${cols(table)} FROM ${table} WHERE user_id = ?1 ORDER BY created_at`).all(userId);
9898 const origin = extras.origin.replace(/\/+$/, "");
99 const repositories = db
100 .query<{ owner: string; name: string; visibility: string; owns: number }, [string]>(
101 `SELECT owner.username AS owner, repo.name, repo.visibility, repo.owner_id = ?1 AS owns
99 // Who works on each repository they write, by username only: nothing else of a contributor's.
100 const contributors = new Map<string, string[]>();
101 for (const c of db
102 .query<{ repo: string; username: string }, [string]>(
103 `SELECT rc.repo_id AS repo, person.username AS username
104 FROM repo_contributor rc JOIN repo ON repo.id = rc.repo_id JOIN user AS person ON person.id = rc.user_id
105 WHERE ${WRITES} ORDER BY person.username`,
106 )
107 .all(userId))
108 contributors.set(c.repo, [...(contributors.get(c.repo) ?? []), c.username]);
109 const writes = db
110 .query<{ id: string; owner: string; name: string; visibility: string; owns: number }, [string]>(
111 `SELECT repo.id, owner.username AS owner, repo.name, repo.visibility, repo.owner_id = ?1 AS owns
102112 FROM repo JOIN user AS owner ON owner.id = repo.owner_id
103113 WHERE ${WRITES} ORDER BY owner.username, repo.name`,
104114 )
@@ -108,11 +118,22 @@ export function accountExport(db: Database, userId: string, extras: ExportExtras
108118 visibility: r.visibility,
109119 role: r.owns ? "owner" : "writer",
110120 cloneUrl: `${origin}/${r.owner}/${r.name}.git`,
121 ...(contributors.has(r.id) ? { contributors: contributors.get(r.id)! } : {}),
111122 }));
123 // The repositories they were invited to work on: theirs to know, as a name and a role.
124 const contributes = db
125 .query<{ owner: string; name: string; visibility: string }, [string]>(
126 `SELECT owner.username AS owner, repo.name, repo.visibility
127 FROM repo_contributor rc JOIN repo ON repo.id = rc.repo_id JOIN user AS owner ON owner.id = repo.owner_id
128 WHERE rc.user_id = ?1 ORDER BY owner.username, repo.name`,
129 )
130 .all(userId)
131 .map((r) => ({ name: `${r.owner}/${r.name}`, visibility: r.visibility, role: "contributor", cloneUrl: `${origin}/${r.owner}/${r.name}.git` }));
132 const repositories = [...writes, ...contributes];
112133 return {
113134 about:
114135 "Everything this forge holds about you: profile (with your email and which address your commits use), SSH key names and fingerprints, token names and scopes, " +
115 "signed-in devices, the repositories you own or write, your forges and payments. No password, token, session id or secret is in it.",
136 "signed-in devices, the repositories you own or write (with who contributes to each, by name) or contribute to, your forges and payments. No password, token, session id or secret is in it.",
116137 exportedAt: new Date().toISOString(),
117138 profile,
118139 sshKeys: rows("user_key"),
src/core/invites.ts +15
@@ -186,3 +186,18 @@ export function listContributors(
186186 )
187187 .all(repoId);
188188 }
189
190 /** The repositories `userId` was made a contributor to since `since`, newest first. */
191 export function contributedSince(
192 db: Database,
193 userId: string,
194 since: string,
195 ): { repoId: string; at: string; by: string | null }[] {
196 return db
197 .query<{ repoId: string; at: string; by: string | null }, [string, string]>(
198 `SELECT c.repo_id AS repoId, c.at AS at, b.username AS by
199 FROM repo_contributor c INDEXED BY repo_contributor_user LEFT JOIN user b ON b.id = c.by_id
200 WHERE c.user_id = ?1 AND c.at >= ?2 ORDER BY c.at DESC LIMIT 20`,
201 )
202 .all(userId, since);
203 }
src/crew/notify.ts +2
@@ -23,6 +23,7 @@
2323 import { blockedLines } from "../actions/egress_refused.ts";
2424 import { scheduleLines } from "../actions/schedules.ts";
2525 import { newAccountLines } from "../actions/accounts.ts";
26 import { contributorLines } from "../actions/invites.ts";
2627 import { requestLines } from "../actions/requests.ts";
2728 import { mailCeilingLines } from "../actions/mail.ts";
2829 import { forgetOnboarding, onboardedLines } from "../apps/onboarding.ts";
@@ -532,6 +533,7 @@ export function startNotifier(app: App, options: NotifyOptions): Notifier {
532533 ...onboardedLines(app.db, user, now()),
533534 ...blockedLines(app, user, now()),
534535 ...scheduleLines(app, user),
536 ...contributorLines(app, user, now()),
535537 ];
536538 const before = seen.get(userId);
537539 // What is gone may ask again later, and then it is new again. Seen by
test/invite-contributors.test.ts +21 −1
@@ -39,6 +39,8 @@ import { createToken } from "../src/core/tokens.ts";
3939 import { createUser, type User } from "../src/core/users.ts";
4040 import { createWebhook } from "../src/core/webhooks.ts";
4141 import { mayDirectCrew } from "../src/crew/direct.ts";
42 import { accountExport } from "../src/core/account_export.ts";
43 import { contributorLines } from "../src/actions/invites.ts";
4244 import { createForgeApp } from "../src/tui/forge.ts";
4345 import { contributorsScreen } from "../src/tui/screens/deploys.ts";
4446 import { headlessTerminal } from "../src/tui/term/terminal.ts";
@@ -196,7 +198,7 @@ beforeAll(async () => {
196198 afterAll(() => stopStandInCrew(standIn), 30_000);
197199
198200 describe("a contributor: reads, enters, directs; nothing outside the repository", () => {
199 test("a contributor reads the repository, enters its app but never its terminal, directs its crew", async () => {
201 test("a contributor reads the repository, enters its app but never its terminal, directs its crew, and hears it", async () => {
200202 const { app } = standIn;
201203 expect([
202204 canRead(app.db, fresh(), ben),
@@ -219,6 +221,24 @@ describe("a contributor: reads, enters, directs; nothing outside the repository"
219221 );
220222 expect(readableRepoIds(app.db, ben, [repo.id]).has(repo.id)).toBe(true);
221223 expect((await page(ben, `/${name()}`)).status).toBe(200);
224 // The notice path says it, with his Talk on the app.
225 expect(contributorLines(app, ben)).toEqual([
226 expect.objectContaining({
227 line: "alice invited you to work on alice/garden: ask Talk for a change.",
228 link: "/talk?about=alice%2Fgarden",
229 }),
230 ]);
231 });
232
233 test("each one's export says it: the owner's names who contributes and nothing else of theirs; ben's names the repository", () => {
234 const { app } = standIn;
235 const of = (who: User) => accountExport(app.db, who.id, { origin: url, forges: [], payments: [] })!;
236 const owners = of(alice).repositories.find((r) => r.name === name())!;
237 expect(owners).toMatchObject({ role: "owner", contributors: ["ben"] });
238 expect(JSON.stringify(of(alice))).not.toContain("[email protected]");
239 expect(of(ben).repositories).toContainEqual(expect.objectContaining({ name: name(), role: "contributor" }));
240 // Nobody contributes to the other repository: it names nobody.
241 expect(of(alice).repositories.find((r) => r.name === "alice/vault")).not.toHaveProperty("contributors");
222242 });
223243
224244 test("a contributor walks every Talk tool and nothing the owner alone is told answers: no other repository, no subscription", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs