Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions apps/server/src/pullRequest/PullRequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,71 @@ it.effect("stops new reads after a rate limit while leaving manual actions avail
}),
);

it.effect(
"opening a pull request during a rate-limit pause still reads its detail and activity",
() =>
Effect.gen(function* () {
let detailReads = 0;
let activityReads = 0;
const service = yield* makeService({
projects: [
project({ id: "p1", title: "cloud", workspaceRoot: "/cloud", repository: "acme/web" }),
],
providers: [
fakePullRequestProvider(SourceControlProviderKind.make("github"), {
getChangeRequest: ({ number }) =>
Effect.suspend(() => {
detailReads += 1;
return number === 1
? Effect.fail(
new PullRequestProviderError({
provider: SourceControlProviderKind.make("github"),
operation: "getChangeRequest",
reason: "rate-limited",
detail: "GitHub API rate limit exceeded.",
}),
)
: Effect.succeed({ ...hostedChangeRequest("Body"), number });
}),
getChangeRequestActivity: () =>
Effect.sync(() => {
activityReads += 1;
return {
comments: [],
commentCount: 0,
commentsTruncated: false,
reviewThreads: [],
commits: [],
};
}),
}),
],
});
const reference = { projectId: "p1" as ProjectId, repository: "acme/web", number: 2 };

yield* Effect.result(service.detail({ ...reference, number: 1 }));
const background = yield* Effect.result(service.detail(reference));
const opened = yield* Effect.result(
service
.detail(reference)
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)),
);
const timeline = yield* Effect.result(
service
.activity(reference)
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true)),
);

expect(background).toMatchObject({ _tag: "Failure" });
expect({ opened, timeline }).toMatchObject({
opened: { _tag: "Success", success: { number: 2 } },
timeline: { _tag: "Success", success: { commentCount: 0 } },
});
assert.strictEqual(detailReads, 2);
assert.strictEqual(activityReads, 1);
}),
);

it.effect("refuses a repository that does not belong to the requested project", () =>
Effect.gen(function* () {
const service = yield* makeService({
Expand Down
59 changes: 32 additions & 27 deletions apps/server/src/pullRequest/PullRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,32 +468,37 @@ function withRateLimitBackoff(
const protect = <A>(
operation: string,
effect: Effect.Effect<A, PullRequestProviderError>,
allowPaused: boolean,
alwaysInteractive: boolean,
) =>
limits.check(key, allowPaused ? { allowPaused: true } : undefined).pipe(
Effect.mapError(
(error) =>
new PullRequestProviderError({
provider: api.kind,
operation,
reason: "rate-limited",
detail: error.detail,
retryAt: error.retryAt,
cause: error,
}),
),
Effect.flatMap((lease) =>
effect.pipe(
Effect.provideService(SourceControlRateLimit.Interactive, allowPaused),
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
Effect.tapError((error) =>
error.reason === "rate-limited"
? limits.recordRateLimit({
...key,
lease,
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
})
: Effect.void,
SourceControlRateLimit.Interactive.pipe(
Effect.map((callerInteractive) => alwaysInteractive || callerInteractive),
Effect.flatMap((allowPaused) =>
limits.check(key, allowPaused ? { allowPaused: true } : undefined).pipe(
Effect.mapError(
(error) =>
new PullRequestProviderError({
provider: api.kind,
operation,
reason: "rate-limited",
detail: error.detail,
retryAt: error.retryAt,
cause: error,
}),
),
Effect.flatMap((lease) =>
effect.pipe(
Effect.provideService(SourceControlRateLimit.Interactive, allowPaused),
Effect.tap(() => limits.recordSuccess({ ...key, lease })),
Effect.tapError((error) =>
error.reason === "rate-limited"
? limits.recordRateLimit({
...key,
lease,
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
})
: Effect.void,
),
),
),
),
),
Expand All @@ -502,10 +507,10 @@ function withRateLimitBackoff(
<Args extends ReadonlyArray<unknown>, A>(
operation: string,
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
allowPaused = false,
alwaysInteractive = false,
) =>
(...args: Args) =>
protect(operation, call(...args), allowPaused);
protect(operation, call(...args), alwaysInteractive);
const interactive = <Args extends ReadonlyArray<unknown>, A>(
operation: string,
call: (...args: Args) => Effect.Effect<A, PullRequestProviderError>,
Expand Down
7 changes: 5 additions & 2 deletions apps/server/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts";
import * as PairingGrantStore from "./auth/PairingGrantStore.ts";
import * as SessionStore from "./auth/SessionStore.ts";
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
import { isNetworkPath } from "@t3tools/shared/path";
import * as RelayClient from "@t3tools/shared/relayClient";
import {
Expand Down Expand Up @@ -1544,6 +1545,8 @@ export const layerWsRpc = (
yield* SourceControlRepositoryService.SourceControlRepositoryService;
const sourceControlSignIn = yield* SourceControlSignIn.SourceControlSignIn;
const withPullRequestViewer = pullRequests.withRoutingCredential;
// The panel's reads for a pull request the user opened go out during a rate-limit pause.
const openedPullRequestRead = Effect.provideService(SourceControlRateLimit.Interactive, true);
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
const sessions = yield* SessionStore.SessionStore;
const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;
Expand Down Expand Up @@ -2804,13 +2807,13 @@ export const layerWsRpc = (
),
),
[WS_METHODS.pullRequestsDetail]: (input) =>
withPullRequestViewer(input, pullRequests.detail(input)),
withPullRequestViewer(input, openedPullRequestRead(pullRequests.detail(input))),
[WS_METHODS.pullRequestsPreview]: (input) =>
withPullRequestViewer(input, pullRequests.preview(input)),
[WS_METHODS.pullRequestsChecks]: (input) =>
withPullRequestViewer(input, pullRequests.checks(input)),
[WS_METHODS.pullRequestsActivity]: (input) =>
withPullRequestViewer(input, pullRequests.activity(input)),
withPullRequestViewer(input, openedPullRequestRead(pullRequests.activity(input))),
[WS_METHODS.pullRequestsReviewGuide]: (input) =>
withPullRequestViewer(input, reviewGuides.get(input)),
[WS_METHODS.pullRequestsGenerateReviewGuide]: (input) =>
Expand Down
Loading