refactor(source-control): PullRequestService reads GitHub resolvers, not its kind (#17619)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
49f849ecce
commit
2ce3e4ae74
11 files changed
+49
-31
No files matched your search
@@ -275,7 +275,7 @@ describe("GitHubApi", () => {
|
||||
retryAt: reset * 1000,
|
||||
});
|
||||
// A user's own request may spend the reserve, and GraphQL has a quota of its own.
|
||||
yield* api.rest(read).pipe(Effect.provideService(GitHubApi.AllowGitHubReserve, true));
|
||||
yield* api.rest(read).pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
|
||||
yield* api.graphql({ host: "github.com", operation: "x", query: "query { viewer { id } }" });
|
||||
expect(requests).toHaveLength(3);
|
||||
// The reset gives the background its quota back.
|
||||
@@ -344,7 +344,7 @@ describe("GitHubApi", () => {
|
||||
method: "PUT",
|
||||
path: "repos/acme/web/pulls/7/merge",
|
||||
})
|
||||
.pipe(Effect.provideService(GitHubApi.AllowGitHubReserve, true));
|
||||
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
|
||||
expect(merged.status).toBe(200);
|
||||
expect(requests).toHaveLength(2);
|
||||
}).pipe(Effect.provide(layer));
|
||||
|
||||
@@ -34,16 +34,6 @@ export const PinnedGitHubCredential = Context.Reference<{
|
||||
defaultValue: () => null,
|
||||
});
|
||||
|
||||
/**
|
||||
* Set by interactive callers (a user's read or write, not a background sweep). Requests made
|
||||
* under it may spend the GraphQL reserve and go through a rate-limit pause: a user acting on a
|
||||
* pull request should not be refused because a background read exhausted the quota.
|
||||
*/
|
||||
export const AllowGitHubReserve = Context.Reference<boolean>(
|
||||
"@t3tools/source-control-github/server/GitHubApi/AllowGitHubReserve",
|
||||
{ defaultValue: () => false },
|
||||
);
|
||||
|
||||
export class GitHubApiRequestError extends Schema.TaggedError<GitHubApiRequestError>()(
|
||||
"GitHubApiRequestError",
|
||||
{ host: Schema.String, operation: Schema.String, cause: Schema.Defect() },
|
||||
@@ -137,7 +127,7 @@ export interface GitHubRestInput {
|
||||
readonly maxResponseBytes?: number;
|
||||
/** Defaults to 30 seconds; a whole pull request's patch may need longer. */
|
||||
readonly timeout?: Duration.Input;
|
||||
/** Overrides `AllowGitHubReserve` for this one request. */
|
||||
/** Overrides `SourceControlRateLimit.Interactive` for this one request. */
|
||||
readonly allowReserve?: boolean;
|
||||
}
|
||||
|
||||
@@ -525,7 +515,7 @@ export const make = Effect.gen(function* () {
|
||||
input.body === undefined
|
||||
? withEtag
|
||||
: withEtag.pipe(HttpClientRequest.bodyJsonUnsafe(input.body));
|
||||
return AllowGitHubReserve.pipe(
|
||||
return SourceControlRateLimit.Interactive.pipe(
|
||||
Effect.flatMap((interactive) =>
|
||||
send({
|
||||
host: input.host,
|
||||
@@ -545,7 +535,7 @@ export const make = Effect.gen(function* () {
|
||||
const host = normalizeHost(input.host);
|
||||
const { fingerprint } = yield* credential(host);
|
||||
const scope = (yield* SourceControlRateLimit.CredentialScope) || fingerprint;
|
||||
const allowReserve = input.allowReserve ?? (yield* AllowGitHubReserve);
|
||||
const allowReserve = input.allowReserve ?? (yield* SourceControlRateLimit.Interactive);
|
||||
// The document, never its variables: user text (bodies, search terms) travels as variables.
|
||||
yield* Effect.annotateCurrentSpan({
|
||||
"github.operation": input.operation,
|
||||
|
||||
@@ -12,7 +12,6 @@ import * as Redacted from "effect/Redacted";
|
||||
|
||||
import { HttpClient, HttpClientResponse } from "effect/http";
|
||||
|
||||
import { AllowGitHubReserve } from "./GitHubApi.ts";
|
||||
import * as GitHubApi from "./GitHubApi.ts";
|
||||
import * as GitHubCredentials from "./GitHubCredentials.ts";
|
||||
import * as GitHubQuota from "./GitHubQuota.ts";
|
||||
@@ -78,7 +77,7 @@ const mockApi = Layer.effect(
|
||||
const quota = yield* GitHubQuota.GitHubQuota;
|
||||
return GitHubApi.GitHubApi.of({
|
||||
graphql: (input) =>
|
||||
GitHubApi.AllowGitHubReserve.pipe(
|
||||
SourceControlRateLimit.Interactive.pipe(
|
||||
Effect.flatMap((interactive) =>
|
||||
quota.admit(input.host, "graphql", {
|
||||
allowReserve: input.allowReserve ?? interactive,
|
||||
@@ -3864,7 +3863,9 @@ layer("GitHubPullRequestApi.layer", (it) => {
|
||||
const error = yield* Effect.flip(cli.getPullRequestDetail(input));
|
||||
expect(error._tag).toBe("SourceControlRateLimitPausedError");
|
||||
expect(mockedExecute).toHaveBeenCalledOnce();
|
||||
yield* cli.getPullRequestDetail(input).pipe(Effect.provideService(AllowGitHubReserve, true));
|
||||
yield* cli
|
||||
.getPullRequestDetail(input)
|
||||
.pipe(Effect.provideService(SourceControlRateLimit.Interactive, true));
|
||||
expect(mockedExecute).toHaveBeenCalledTimes(2);
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -1526,7 +1526,7 @@ export const make = Effect.gen(function* () {
|
||||
|
||||
const getPullRequestDetail: GitHubPullRequestApi["Service"]["getPullRequestDetail"] = (input) => {
|
||||
const { owner, name } = parseRepositorySelector(input.repository);
|
||||
return GitHubApi.AllowGitHubReserve.pipe(
|
||||
return SourceControlRateLimit.Interactive.pipe(
|
||||
Effect.flatMap((allowReserve) =>
|
||||
graphqlRead({
|
||||
allowReserve,
|
||||
|
||||
@@ -7,6 +7,7 @@ import type { PullRequestReaction } from "@t3tools/contracts";
|
||||
|
||||
import { decodePullRequestDetailJson } from "./gitHubPullRequestJson.ts";
|
||||
import * as GitHubApi from "./GitHubApi.ts";
|
||||
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
|
||||
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
|
||||
import type { GitHubPullRequestCore } from "./gitHubPullRequestJson.ts";
|
||||
import { gitHubViewerPermissions, loginAvatarUrl, make } from "./GitHubPullRequestProvider.ts";
|
||||
@@ -725,7 +726,7 @@ describe("getViewerPermissions", () => {
|
||||
Layer.mock(GitHubPullRequestApi.GitHubPullRequestApi)({
|
||||
revalidateChecks: (_input, read) => read,
|
||||
getPullRequestDetail: () =>
|
||||
GitHubApi.AllowGitHubReserve.pipe(
|
||||
SourceControlRateLimit.Interactive.pipe(
|
||||
Effect.tap((allowReserve) => Effect.sync(() => onDetail(allowReserve))),
|
||||
Effect.flatMap(() => detail),
|
||||
),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { removeAgentCredits } from "@t3tools/source-control-core/server/mergeMessage";
|
||||
import * as Effect from "effect/Effect";
|
||||
import type {
|
||||
PullRequestActor,
|
||||
@@ -7,7 +8,7 @@ import type {
|
||||
PullRequestViewerPermissions,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import * as GitHubApi from "./GitHubApi.ts";
|
||||
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
|
||||
import * as GitHubPullRequestApi from "./GitHubPullRequestApi.ts";
|
||||
import {
|
||||
PullRequestProviderError,
|
||||
@@ -262,6 +263,7 @@ export const make = Effect.gen(function* () {
|
||||
const provider: PullRequestProviderApi = {
|
||||
kind: "github",
|
||||
capabilities: CAPABILITIES,
|
||||
mergeMessageRewrite: removeAgentCredits,
|
||||
getRoutingIdentity: (input) =>
|
||||
cli.getRoutingIdentity(input).pipe(Effect.mapError(fail("routeIdentity"))),
|
||||
withVerifiedCredential: (input, use) =>
|
||||
@@ -526,7 +528,7 @@ export const make = Effect.gen(function* () {
|
||||
// comparison, so one read usually answers what used to take three. When that heavier read
|
||||
// fails, the light access read still answers, withholding only update-branch.
|
||||
return cli.getPullRequestDetail(input).pipe(
|
||||
Effect.provideService(GitHubApi.AllowGitHubReserve, true),
|
||||
Effect.provideService(SourceControlRateLimit.Interactive, true),
|
||||
Effect.map((pullRequest) =>
|
||||
gitHubViewerPermissions({
|
||||
...pullRequest.viewerAccess,
|
||||
|
||||
@@ -963,7 +963,7 @@ export const make = Effect.gen(function* () {
|
||||
},
|
||||
listChangeRequests: (input) =>
|
||||
// An open lookup is a user waiting on a status; the rest may be a background sweep.
|
||||
(input.state === "open" ? Effect.succeed(true) : GitHubApi.AllowGitHubReserve).pipe(
|
||||
(input.state === "open" ? Effect.succeed(true) : SourceControlRateLimit.Interactive).pipe(
|
||||
Effect.flatMap((allowReserve) =>
|
||||
listByHead({
|
||||
cwd: input.cwd,
|
||||
|
||||
Reference in new issue
Block a user