Skip to content

Commit 396b716

Browse files
committed
fix(source-control): reselect forgejo credentials before writes
1 parent 6d7c17f commit 396b716

3 files changed

Lines changed: 74 additions & 36 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,8 @@ export const make = Effect.gen(function* () {
657657
});
658658
return { command, login: login.name, repository, baseUrl: login.url.replace(/\/+$/, "") };
659659
});
660-
// Share pending resolution, but reselect the login after it finishes so account and remote
661-
// changes apply to the next request without an extra identity check.
660+
// Share pending read resolution. Writes and permission checks reselect the login so they
661+
// cannot join a target captured before an account change.
662662
const targets = yield* Cache.makeWith(
663663
(key: string) => {
664664
const [cwd = "", reference, repository, host, remoteUrl, baseUrl, requestedHost, scope] = key
@@ -689,7 +689,7 @@ export const make = Effect.gen(function* () {
689689
const api = Effect.fn("ForgejoCli.api")(function* (input: ForgejoApiInput) {
690690
const hostOnly =
691691
input.path.replace(/^\/+/, "") === "user" && (!input.method || input.method === "GET");
692-
const repository = yield* input.freshCredentials
692+
const repository = yield* input.freshCredentials || (input.method && input.method !== "GET")
693693
? resolveTarget(
694694
{
695695
cwd: input.cwd,

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,16 @@ export const make = Effect.gen(function* () {
515515
body: { body: input.body },
516516
}),
517517
submitReview: Effect.fn("ForgejoPullRequestProvider.submitReview")(function* (input) {
518-
const pr = yield* getPull(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+
);
519528
yield* write({
520529
...input,
521530
path: `${pullPath(input)}/reviews`,

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

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ it.effect("refreshes Forgejo viewer permissions immediately after credentials ch
126126
);
127127
});
128128

129-
it.effect("reselects the current tea login between API requests", () => {
129+
it.effect("reselects the current tea login for writes while an older read is pending", () => {
130130
let account = "alice";
131131
const selected: string[] = [];
132132
let holdLogin: Effect.Effect<void> = Effect.void;
@@ -135,7 +135,7 @@ it.effect("reselects the current tea login between API requests", () => {
135135
const input = {
136136
cwd: "/repo\n%work",
137137
repository: "https://forgejo.test/acme/web",
138-
path: "user",
138+
path: "repos/acme/web/pulls/42",
139139
};
140140
assert.strictEqual((yield* cli.api(input)).stdout, '{"login":"alice"}');
141141
account = "bob";
@@ -151,9 +151,14 @@ it.effect("reselects the current tea login between API requests", () => {
151151
(yield* cli.api({ ...input, freshCredentials: true })).stdout,
152152
'{"login":"carol"}',
153153
);
154+
const write = yield* cli
155+
.api({ ...input, method: "PATCH", body: { state: "closed" } })
156+
.pipe(Effect.forkChild);
157+
yield* Effect.yieldNow;
154158
yield* Deferred.succeed(release, undefined);
159+
assert.strictEqual((yield* Fiber.join(write)).stdout, '{"login":"carol"}');
155160
assert.strictEqual((yield* Fiber.join(pending)).stdout, '{"login":"bob"}');
156-
assert.deepStrictEqual(selected, ["alice", "bob", "carol", "bob"]);
161+
assert.deepStrictEqual(selected, ["alice", "bob", "carol", "carol", "bob"]);
157162
}).pipe(
158163
Effect.provideService(
159164
FileSystem.FileSystem,
@@ -288,35 +293,37 @@ it.effect("shares pending Forgejo pull reads and refreshes after writes and fail
288293
}),
289294
);
290295

291-
it.effect("submits a Forgejo review without sending its summary in the preliminary GET", () => {
296+
it.effect("submits a Forgejo review using a fresh head and an empty GET body", () => {
292297
const methods: string[] = [];
298+
let head = "old-head";
299+
let holdPull: Effect.Effect<void> = Effect.void;
293300
const reviewClient = HttpClient.make((request) => {
294301
methods.push(request.method);
295302
if (request.method === "GET") {
296303
assert.strictEqual(request.url, "https://forgejo.test/api/v1/repos/maria/project/pulls/42");
297-
return Effect.succeed(
298-
HttpClientResponse.fromWeb(
299-
request,
300-
new Response(
301-
encodeJson({
302-
number: 42,
303-
title: "Review target",
304-
body: "",
305-
html_url: "https://forgejo.test/maria/project/pulls/42",
306-
user: { login: "maria" },
307-
state: "open",
308-
merged: false,
309-
head: { ref: "feature", sha: "head", repo: null },
310-
base: { ref: "main", sha: "base", repo: null },
311-
created_at: "2026-09-13T00:00:00Z",
312-
updated_at: "2026-09-13T00:00:00Z",
313-
closed_at: null,
314-
merged_at: null,
315-
labels: [],
316-
}),
317-
),
304+
const response = HttpClientResponse.fromWeb(
305+
request,
306+
new Response(
307+
encodeJson({
308+
number: 42,
309+
title: "Review target",
310+
body: "",
311+
html_url: "https://forgejo.test/maria/project/pulls/42",
312+
user: { login: "maria" },
313+
state: "open",
314+
merged: false,
315+
head: { ref: "feature", sha: head, repo: null },
316+
base: { ref: "main", sha: "base", repo: null },
317+
created_at: "2026-09-13T00:00:00Z",
318+
updated_at: "2026-09-13T00:00:00Z",
319+
closed_at: null,
320+
merged_at: null,
321+
labels: [],
322+
}),
318323
),
319324
);
325+
assert.strictEqual(request.body._tag, "Empty");
326+
return holdPull.pipe(Effect.as(response));
320327
}
321328
assert.strictEqual(request.method, "POST");
322329
assert.strictEqual(
@@ -338,16 +345,36 @@ it.effect("submits a Forgejo review without sending its summary in the prelimina
338345
const provider = yield* ForgejoPullRequestProvider.make.pipe(
339346
Effect.provideService(ForgejoCli.ForgejoCli, cli),
340347
);
341-
yield* provider.submitReview({
348+
const started = yield* Deferred.make<void>();
349+
const release = yield* Deferred.make<void>();
350+
holdPull = Deferred.succeed(started, undefined).pipe(Effect.andThen(Deferred.await(release)));
351+
const summary = provider.getChangeRequestSummary;
352+
if (!summary) return yield* Effect.die("narrow reads missing");
353+
const pending = yield* summary({
342354
cwd: "/repo",
343355
repository: "maria/project",
344356
host: "forgejo.test",
345357
number: 42,
346-
verdict: "comment",
347-
body: "Review summary",
348-
comments: [],
349-
});
350-
assert.deepStrictEqual(methods, ["GET", "POST"]);
358+
}).pipe(Effect.forkChild);
359+
yield* Deferred.await(started);
360+
head = "head";
361+
holdPull = Effect.void;
362+
const review = yield* provider
363+
.submitReview({
364+
cwd: "/repo",
365+
repository: "maria/project",
366+
host: "forgejo.test",
367+
number: 42,
368+
verdict: "comment",
369+
body: "Review summary",
370+
comments: [],
371+
})
372+
.pipe(Effect.forkChild);
373+
yield* Effect.yieldNow;
374+
yield* Deferred.succeed(release, undefined);
375+
yield* Fiber.join(review);
376+
assert.deepStrictEqual(methods, ["GET", "GET", "POST"]);
377+
yield* Fiber.join(pending);
351378
}).pipe(
352379
Effect.provideService(HttpClient.HttpClient, reviewClient),
353380
Effect.provideService(
@@ -356,7 +383,9 @@ it.effect("submits a Forgejo review without sending its summary in the prelimina
356383
exists: () => Effect.succeed(true),
357384
readFileString: () =>
358385
Effect.succeed(
359-
encodeJson({ hosts: { "forgejo.test": { type: "Application", token: "test-token" } } }),
386+
encodeJson({
387+
hosts: { "forgejo.test": { type: "Application", token: "test-token" } },
388+
}),
360389
),
361390
}),
362391
),

0 commit comments

Comments
 (0)