Skip to content

Commit ba46f92

Browse files
authored
fix(pull-requests): protect provider API budgets (pingdotgg#6466)
1 parent fee10de commit ba46f92

41 files changed

Lines changed: 1781 additions & 192 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export const RPC_REQUIRED_SCOPES = {
5656
[WS_METHODS.pullRequestsListStats]: AuthOrchestrationReadScope,
5757
[WS_METHODS.pullRequestsDetail]: AuthOrchestrationReadScope,
5858
[WS_METHODS.pullRequestsActivity]: AuthOrchestrationReadScope,
59+
[WS_METHODS.pullRequestsThreadComments]: AuthOrchestrationReadScope,
5960
[WS_METHODS.pullRequestsDiffFileContents]: AuthOrchestrationReadScope,
6061
[WS_METHODS.pullRequestsRunAction]: AuthOrchestrationOperateScope,
6162
[WS_METHODS.pullRequestsUpdate]: AuthOrchestrationOperateScope,

‎apps/server/src/pullRequest/AzureDevOpsPullRequestProvider.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3t
44
import * as AzureDevOpsPullRequestCli from "./AzureDevOpsPullRequestCli.ts";
55
import {
66
PullRequestProviderError,
7+
type PullRequestProviderFailure,
78
type ProviderChangeRequest,
89
type ProviderChangeRequestActivity,
910
type ProviderChangeRequestDetail,
@@ -64,12 +65,13 @@ export const AZURE_DEVOPS_VIEWER_PERMISSIONS: PullRequestViewerPermissions = {
6465
};
6566

6667
/** The CLI tags that mean the tool itself is unusable, rather than one request failing. */
67-
function reasonFor(
68+
export function azureDevOpsProviderFailure(
6869
error: AzureDevOpsPullRequestCli.AzureDevOpsPullRequestCliError,
69-
): PullRequestProviderError["reason"] {
70-
if (error._tag === "AzureDevOpsCliUnavailableError") return "missing-tool";
71-
if (error._tag === "AzureDevOpsCliAuthenticationError") return "unauthenticated";
72-
return "failed";
70+
): PullRequestProviderFailure {
71+
if (error._tag === "AzureDevOpsCliUnavailableError") return { reason: "missing-tool" };
72+
if (error._tag === "AzureDevOpsCliAuthenticationError") return { reason: "unauthenticated" };
73+
if (error._tag === "AzureDevOpsCliRateLimitError") return { reason: "rate-limited" };
74+
return { reason: "failed" };
7375
}
7476

7577
function toChangeRequest(pullRequest: AzureDevOpsPullRequest): ProviderChangeRequest {
@@ -103,7 +105,7 @@ export const make = Effect.gen(function* () {
103105
new PullRequestProviderError({
104106
provider: "azure-devops",
105107
operation,
106-
reason: reasonFor(error),
108+
...azureDevOpsProviderFailure(error),
107109
detail: error.detail,
108110
cause: error,
109111
});

‎apps/server/src/pullRequest/BitbucketPullRequestProvider.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { describe, expect, it } from "vite-plus/test";
22

33
import * as BitbucketApi from "../sourceControl/BitbucketApi.ts";
44
import {
5-
bitbucketErrorReason,
5+
bitbucketProviderFailure,
66
bitbucketViewerPermissions,
77
} from "./BitbucketPullRequestProvider.ts";
88

9-
describe("bitbucketErrorReason", () => {
9+
describe("bitbucketProviderFailure", () => {
1010
it("treats only an HTTP 401 as unusable credentials", () => {
1111
const responseError = (status: number) =>
1212
new BitbucketApi.BitbucketResponseError({
@@ -15,8 +15,8 @@ describe("bitbucketErrorReason", () => {
1515
responseBodyLength: 0,
1616
});
1717

18-
expect(bitbucketErrorReason(responseError(401))).toBe("unauthenticated");
19-
expect(bitbucketErrorReason(responseError(403))).toBe("failed");
18+
expect(bitbucketProviderFailure(responseError(401)).reason).toBe("unauthenticated");
19+
expect(bitbucketProviderFailure(responseError(403)).reason).toBe("failed");
2020
});
2121
});
2222

‎apps/server/src/pullRequest/BitbucketPullRequestProvider.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { PullRequestCapabilities, PullRequestViewerPermissions } from "@t3t
44
import * as BitbucketPullRequestApi from "./BitbucketPullRequestApi.ts";
55
import {
66
PullRequestProviderError,
7+
type PullRequestProviderFailure,
78
type ProviderChangeRequest,
89
type ProviderChangeRequestActivity,
910
type ProviderChangeRequestDetail,
@@ -59,15 +60,21 @@ export function bitbucketViewerPermissions(input: {
5960
}
6061

6162
/** The failures that mean the credentials are the problem, rather than one request. */
62-
export function bitbucketErrorReason(
63+
export function bitbucketProviderFailure(
6364
error: BitbucketPullRequestApi.BitbucketPullRequestApiError,
64-
): PullRequestProviderError["reason"] {
65+
): PullRequestProviderFailure {
6566
// Bitbucket is read over HTTP with credentials from the environment, so there is no tool to be
6667
// missing: unusable always means the credentials are absent or refused.
6768
if (error._tag === "BitbucketResponseError" && error.status === 401) {
68-
return "unauthenticated";
69+
return { reason: "unauthenticated" };
6970
}
70-
return "failed";
71+
if (error._tag === "BitbucketResponseError" && error.status === 429) {
72+
return {
73+
reason: "rate-limited",
74+
...(error.retryAt === undefined ? {} : { retryAt: error.retryAt }),
75+
};
76+
}
77+
return { reason: "failed" };
7178
}
7279

7380
function toChangeRequest(pullRequest: BitbucketPullRequest): ProviderChangeRequest {
@@ -100,7 +107,7 @@ export const make = Effect.gen(function* () {
100107
new PullRequestProviderError({
101108
provider: "bitbucket",
102109
operation,
103-
reason: bitbucketErrorReason(error),
110+
...bitbucketProviderFailure(error),
104111
// Every Bitbucket failure states its own fact; this names the operation around it, so
105112
// the two do not stack into "failed in x: failed in y: ...".
106113
detail: error.detail,

0 commit comments

Comments
 (0)