Skip to content

Commit ea1b4fd

Browse files
committed
fix(source-control): keep credential reads and incomplete diffs fresh
1 parent 396b716 commit ea1b4fd

7 files changed

Lines changed: 187 additions & 130 deletions

File tree

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestCli.test.ts‎

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
11531153
}),
11541154
);
11551155

1156-
it.effect("stops following pages when one of them does not move the cursor on", () =>
1156+
it.effect("retries truncated changes without a push and retains complete results", () =>
11571157
Effect.gen(function* () {
11581158
mockedExecute
11591159
.mockReturnValueOnce(
@@ -1206,17 +1206,35 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
12061206
}),
12071207
),
12081208
),
1209-
);
1209+
)
1210+
.mockReturnValueOnce(Effect.succeed(output(json(oneIteration))))
1211+
.mockReturnValueOnce(
1212+
Effect.succeed(
1213+
output(
1214+
json({
1215+
changeEntries: [
1216+
{ changeType: "edit", item: { path: "/a.ts", objectId: "8f80" } },
1217+
{ changeType: "edit", item: { path: "/b.ts", objectId: "0ca4" } },
1218+
{ changeType: "add", item: { path: "/unlisted.ts", objectId: "cafe" } },
1219+
],
1220+
}),
1221+
),
1222+
),
1223+
)
1224+
.mockReturnValueOnce(Effect.succeed(output(json(oneIteration))));
12101225
const provider = yield* AzureDevOpsPullRequestProvider.make;
1211-
assert.isDefined(provider.getFileRevisions);
1226+
const read = provider.getFileRevisions;
1227+
assert.isDefined(read);
12121228

1213-
const answer = yield* provider.getFileRevisions({
1214-
cwd: "/w",
1215-
repository: "web",
1216-
host: "dev.azure.com",
1217-
number: 42,
1218-
paths: ["a.ts", "b.ts", "unlisted.ts"],
1219-
});
1229+
const ask = () =>
1230+
read({
1231+
cwd: "/w",
1232+
repository: "web",
1233+
host: "dev.azure.com",
1234+
number: 42,
1235+
paths: ["a.ts", "b.ts", "unlisted.ts"],
1236+
});
1237+
const answer = yield* ask();
12201238

12211239
// Four reads and no more: a page pointing at where it already is would be read forever.
12221240
assert.strictEqual(mockedExecute.mock.calls.length, 4);
@@ -1226,6 +1244,22 @@ layer("AzureDevOpsPullRequestCli.layer", (it) => {
12261244
["a.ts", "8f80"],
12271245
["b.ts", "0ca4"],
12281246
]);
1247+
1248+
const recovered = yield* ask();
1249+
assert.strictEqual(mockedExecute.mock.calls.length, 6);
1250+
expect(argsOfCall(4)).toContain("pullRequestIterations");
1251+
expect(argsOfCall(5)).toContain("pullRequestIterationChanges");
1252+
expect(argsOfCall(5)).toContain("iterationId=1");
1253+
expect([...recovered.revisions]).toEqual([
1254+
["a.ts", "8f80"],
1255+
["b.ts", "0ca4"],
1256+
["unlisted.ts", "cafe"],
1257+
]);
1258+
1259+
const cached = yield* ask();
1260+
assert.strictEqual(mockedExecute.mock.calls.length, 7);
1261+
expect(argsOfCall(6)).toContain("pullRequestIterations");
1262+
expect([...cached.revisions]).toEqual([...recovered.revisions]);
12291263
}),
12301264
);
12311265

‎packages/source-control-azure-devops/src/server/AzureDevOpsPullRequestProvider.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,13 +349,15 @@ export const make = Effect.gen(function* () {
349349

350350
/**
351351
* What one push changed, against the merge base. Never changes once the push lands, so the
352-
* detail's file count, every slice of the diff and the viewed marks share one read of it.
352+
* detail's file count, every slice of the diff and the viewed marks share one complete read.
353+
* Incomplete reads are retried rather than keeping missing files for the life of the push.
353354
*/
354355
const iterationChanges = yield* Cache.makeWith(
355356
(key: string) => cli.listIterationChanges(fromIterationKey(key)),
356357
{
357358
capacity: ITERATION_CHANGES_CAPACITY,
358-
timeToLive: (exit) => (Exit.isSuccess(exit) ? Duration.infinity : Duration.zero),
359+
timeToLive: (exit) =>
360+
Exit.isSuccess(exit) && !exit.value.truncated ? Duration.infinity : Duration.zero,
359361
},
360362
);
361363

‎packages/source-control-forgejo/src/server/ForgejoCli.ts‎

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as Cache from "effect/Cache";
21
import * as Context from "effect/Context";
32
import * as Effect from "effect/Effect";
43
import * as Layer from "effect/Layer";
@@ -110,7 +109,7 @@ export interface ForgejoRepositoryInput {
110109
readonly host?: string;
111110
}
112111

113-
/** What resolving a target reads from a `ForgejoRepositoryInput`, flat so it can key a cache. */
112+
/** What resolving a target reads from a `ForgejoRepositoryInput`. */
114113
interface ForgejoTargetInput {
115114
readonly cwd: string;
116115
readonly reference?: string | undefined;
@@ -132,7 +131,6 @@ export interface ForgejoApiInput extends ForgejoRepositoryInput {
132131
readonly path: string;
133132
readonly method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
134133
readonly body?: unknown;
135-
readonly freshCredentials?: boolean;
136134
}
137135

138136
export class ForgejoCli extends Context.Service<
@@ -657,52 +655,26 @@ export const make = Effect.gen(function* () {
657655
});
658656
return { command, login: login.name, repository, baseUrl: login.url.replace(/\/+$/, "") };
659657
});
660-
// Share pending read resolution. Writes and permission checks reselect the login so they
661-
// cannot join a target captured before an account change.
662-
const targets = yield* Cache.makeWith(
663-
(key: string) => {
664-
const [cwd = "", reference, repository, host, remoteUrl, baseUrl, requestedHost, scope] = key
665-
.split("\n")
666-
.map(decodeURIComponent)
667-
.map((value) => value || undefined);
668-
return resolveTarget(
669-
{ cwd, reference, repository, host, remoteUrl, providerBaseUrl: baseUrl, requestedHost },
670-
scope === "host",
671-
);
672-
},
673-
{ capacity: 64, timeToLive: () => 0 },
674-
);
675-
const targetKey = (input: ForgejoRepositoryInput, hostOnly = false) =>
676-
[
677-
input.cwd,
678-
input.reference,
679-
input.repository,
680-
input.host,
681-
input.context?.remoteUrl,
682-
input.context?.provider.baseUrl,
683-
input.context?.requestedHost,
684-
hostOnly ? "host" : "",
685-
]
686-
.map((part) => encodeURIComponent(part ?? ""))
687-
.join("\n");
688-
const resolveRepository = (input: ForgejoRepositoryInput) => Cache.get(targets, targetKey(input));
658+
// Select credentials for every request; a pending selection can belong to another account.
659+
const resolveRepository = (input: ForgejoRepositoryInput) =>
660+
resolveTarget({
661+
...input,
662+
remoteUrl: input.context?.remoteUrl,
663+
providerBaseUrl: input.context?.provider.baseUrl,
664+
requestedHost: input.context?.requestedHost,
665+
});
689666
const api = Effect.fn("ForgejoCli.api")(function* (input: ForgejoApiInput) {
690667
const hostOnly =
691668
input.path.replace(/^\/+/, "") === "user" && (!input.method || input.method === "GET");
692-
const repository = yield* input.freshCredentials || (input.method && input.method !== "GET")
693-
? resolveTarget(
694-
{
695-
cwd: input.cwd,
696-
reference: input.reference,
697-
repository: input.repository,
698-
host: input.host,
699-
remoteUrl: input.context?.remoteUrl,
700-
providerBaseUrl: input.context?.provider.baseUrl,
701-
requestedHost: input.context?.requestedHost,
702-
},
703-
hostOnly,
704-
)
705-
: Cache.get(targets, targetKey(input, hostOnly));
669+
const repository = yield* resolveTarget(
670+
{
671+
...input,
672+
remoteUrl: input.context?.remoteUrl,
673+
providerBaseUrl: input.context?.provider.baseUrl,
674+
requestedHost: input.context?.requestedHost,
675+
},
676+
hostOnly,
677+
);
706678
const stdin =
707679
input.body === undefined
708680
? undefined

‎packages/source-control-forgejo/src/server/ForgejoPullRequestProvider.ts‎

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { SourceControlProviderKind } from "@t3tools/contracts";
2-
import * as Cache from "effect/Cache";
32
import * as Effect from "effect/Effect";
43
import * as Schema from "effect/Schema";
54
import * as Result from "effect/Result";
@@ -149,51 +148,20 @@ export const make = Effect.gen(function* () {
149148
}
150149
return { items, truncated: true };
151150
});
152-
// Share pending reads only: credentials and repository permissions can change between requests.
153-
const cacheKey = (...parts: ReadonlyArray<string | number | undefined>) =>
154-
parts.map((part) => encodeURIComponent(String(part ?? ""))).join("\n");
155-
const pulls = yield* Cache.makeWith(
156-
(key: string) => {
157-
const [cwd = "", repository = "", host = "", number = ""] = key
158-
.split("\n")
159-
.map(decodeURIComponent);
160-
const ref = { cwd, repository, host, number: Number(number) };
161-
return read({ ...ref, path: pullPath(ref) }, ForgejoPullRequest);
162-
},
163-
{ capacity: 64, timeToLive: () => 0 },
164-
);
151+
// These reads select the current account independently of older pending requests.
165152
const getPull = (input: ProviderRepositoryRef & { readonly number: number }) =>
166-
Cache.get(pulls, cacheKey(input.cwd, input.repository, input.host, input.number));
167-
const write = (input: ForgejoCli.ForgejoApiInput) =>
168-
request(input).pipe(
169-
Effect.onExit(() => Cache.invalidateAll(pulls)),
170-
Effect.asVoid,
153+
read(
154+
{ cwd: input.cwd, repository: input.repository, host: input.host, path: pullPath(input) },
155+
ForgejoPullRequest,
171156
);
172-
const repos = yield* Cache.makeWith(
173-
(key: string) => {
174-
const [cwd = "", repository = "", host = ""] = key.split("\n").map(decodeURIComponent);
175-
const ref = { cwd, repository, host };
176-
return read({ ...ref, path: repoPath(ref) }, ForgejoRepository);
177-
},
178-
{ capacity: 32, timeToLive: () => 0 },
179-
);
180-
const viewers = yield* Cache.makeWith(
181-
(key: string) => {
182-
const [cwd = "", repository, host] = key.split("\n").map(decodeURIComponent);
183-
return read(
184-
{ cwd, ...(repository ? { repository } : {}), ...(host ? { host } : {}), path: "user" },
185-
ForgejoUser,
186-
).pipe(Effect.map((user) => user.login));
187-
},
188-
{ capacity: 32, timeToLive: () => 0 },
189-
);
157+
const write = (input: ForgejoCli.ForgejoApiInput) => request(input).pipe(Effect.asVoid);
190158
const getRepo = (input: ProviderRepositoryRef) =>
191-
Cache.get(repos, cacheKey(input.cwd, input.repository, input.host));
159+
read({ ...input, path: repoPath(input) }, ForgejoRepository);
192160
const getViewer = (input: {
193161
readonly cwd: string;
194162
readonly repository?: string;
195163
readonly host?: string;
196-
}) => Cache.get(viewers, cacheKey(input.cwd, input.repository, input.host));
164+
}) => read({ ...input, path: "user" }, ForgejoUser).pipe(Effect.map((user) => user.login));
197165
const permissions = (
198166
repo: typeof ForgejoRepository.Type,
199167
pr: typeof ForgejoPullRequest.Type,
@@ -224,14 +192,8 @@ export const make = Effect.gen(function* () {
224192
const getPermissions = Effect.fn("ForgejoPullRequestProvider.getPermissions")(function* (
225193
input: ProviderRepositoryRef & { readonly number: number },
226194
) {
227-
// A write must not join a detail read started before the account or access changed.
228-
const fresh = { ...input, freshCredentials: true };
229195
const [repo, pr, viewer] = yield* Effect.all(
230-
[
231-
read({ ...fresh, path: repoPath(input) }, ForgejoRepository),
232-
read({ ...fresh, path: pullPath(input) }, ForgejoPullRequest),
233-
read({ ...fresh, path: "user" }, ForgejoUser).pipe(Effect.map((user) => user.login)),
234-
],
196+
[getRepo(input), getPull(input), getViewer(input)],
235197
{ concurrency: 3 },
236198
);
237199
return permissions(repo, pr, viewer);
@@ -515,16 +477,7 @@ export const make = Effect.gen(function* () {
515477
body: { body: input.body },
516478
}),
517479
submitReview: Effect.fn("ForgejoPullRequestProvider.submitReview")(function* (input) {
518-
const pr = yield* read(
519-
{
520-
cwd: input.cwd,
521-
repository: input.repository,
522-
host: input.host,
523-
path: pullPath(input),
524-
freshCredentials: true,
525-
},
526-
ForgejoPullRequest,
527-
);
480+
const pr = yield* getPull(input);
528481
yield* write({
529482
...input,
530483
path: `${pullPath(input)}/reviews`,

‎packages/source-control-forgejo/src/server/ForgejoSourceControlProvider.test.ts‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
6262
assert.strictEqual(bob.requestReviewers, false);
6363
assert.strictEqual(requests.length, 9);
6464
assert.deepStrictEqual(renewals, ["alice", "bob"]);
65+
const freshDetail = yield* provider.getChangeRequest(input).pipe(Effect.forkChild);
66+
yield* Effect.yieldNow;
6567
yield* Deferred.succeed(release, undefined);
68+
assert.deepStrictEqual((yield* Fiber.join(freshDetail)).viewerPermissions?.verdicts, [
69+
"comment",
70+
"approve",
71+
"request-changes",
72+
]);
6673
assert.deepStrictEqual((yield* Fiber.join(detail)).viewerPermissions?.verdicts, ["comment"]);
74+
assert.strictEqual(requests.length, 14);
6775
}).pipe(
6876
Effect.provideService(
6977
FileSystem.FileSystem,
@@ -147,10 +155,7 @@ it.effect("reselects the current tea login for writes while an older read is pen
147155
yield* Deferred.await(started);
148156
account = "carol";
149157
holdLogin = Effect.void;
150-
assert.strictEqual(
151-
(yield* cli.api({ ...input, freshCredentials: true })).stdout,
152-
'{"login":"carol"}',
153-
);
158+
assert.strictEqual((yield* cli.api(input)).stdout, '{"login":"carol"}');
154159
const write = yield* cli
155160
.api({ ...input, method: "PATCH", body: { state: "closed" } })
156161
.pipe(Effect.forkChild);
@@ -203,7 +208,7 @@ it.effect("reselects the current tea login for writes while an older read is pen
203208
);
204209
});
205210

206-
it.effect("shares pending Forgejo pull reads and refreshes after writes and failures", () =>
211+
it.effect("keeps Forgejo pull reads fresh during pending reads, writes and failures", () =>
207212
Effect.gen(function* () {
208213
const started = yield* Deferred.make<void>();
209214
const release = yield* Deferred.make<void>();
@@ -274,22 +279,22 @@ it.effect("shares pending Forgejo pull reads and refreshes after writes and fail
274279
).pipe(Effect.forkChild);
275280
yield* Deferred.await(started);
276281
yield* Effect.yieldNow;
277-
assert.strictEqual(pullReads, 1);
282+
assert.strictEqual(pullReads, 3);
278283
yield* provider.runAction({ ...input, action: "close" });
279284
const updated = yield* checks(input);
280285
assert.strictEqual(updated.state, "closed");
281-
assert.strictEqual(pullReads, 2);
286+
assert.strictEqual(pullReads, 4);
282287
assert.include(paths, "repos/acme/web/statuses/after?sort=recentupdate&limit=50&page=1");
283288
yield* Deferred.succeed(release, undefined);
284289
const original = yield* Fiber.join(reads);
285290
assert.strictEqual(original[0].state, "open");
286291
reject = true;
287292
const failed = yield* summary(input).pipe(Effect.result);
288293
assert.strictEqual(failed._tag, "Failure");
289-
assert.strictEqual(pullReads, 3);
294+
assert.strictEqual(pullReads, 5);
290295
reject = false;
291296
assert.strictEqual((yield* summary(input)).state, "closed");
292-
assert.strictEqual(pullReads, 4);
297+
assert.strictEqual(pullReads, 6);
293298
}),
294299
);
295300

0 commit comments

Comments
 (0)