Skip to content
Open
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
11 changes: 11 additions & 0 deletions packages/source-control-github/schema/github.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions packages/source-control-github/src/server/GitHubPullRequestApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,108 @@ layer("GitHubPullRequestApi.layer", (it) => {
}),
);

const pendingReviewAnswer = (
review: { readonly id: string; readonly commit: string } | null,
) => ({
data: {
repository: {
pullRequest: {
reviews: {
nodes: review === null ? [] : [{ id: review.id, commit: { oid: review.commit } }],
},
},
},
},
});
const pendingAtHead = [
"query ViewerPendingReview",
pendingReviewAnswer({ id: "PRR_1", commit: "abc123" }),
] as const;
const pendingSubmitted = [
"mutation SubmitPendingReview",
{ data: { submitPullRequestReview: { pullRequestReview: { id: "PRR_1" } } } },
] as const;
/** GitHub's answer to creating a review while the viewer already has a pending one. */
const pendingReviewConflict = (
...routes: ReadonlyArray<readonly [needle: string, body: unknown]>
) => {
route(...routes);
const routed = mockedExecute.getMockImplementation();
mockedExecute.mockImplementation((call) =>
call.kind === "rest" && call.path.endsWith("/reviews")
? Effect.fail(
new GitHubRequestPolicy.GitHubApiResponseError({
host: call.host,
operation: call.operation,
status: 422,
githubErrors: ["User can only have one pending review per pull request"],
}),
)
: routed!(call),
);
};
const reviewInput = {
...pullRequestInput,
verdict: "approve" as const,
body: "Looks good",
comments: [],
expectedHeadSha: "abc123",
};

it.effect("submits the viewer's pending review when GitHub refuses a second one", () =>
Effect.gen(function* () {
pendingReviewConflict(pendingAtHead, pendingSubmitted);
const api = yield* GitHubPullRequestApi.GitHubPullRequestApi;
yield* api.submitReview(reviewInput);
// A blank summary leaves the one already written on GitHub in place.
yield* api.submitReview({ ...reviewInput, body: "" });
expect(variablesOf("mutation SubmitPendingReview")).toEqual([
{ reviewId: "PRR_1", event: "APPROVE", body: "Looks good" },
{ reviewId: "PRR_1", event: "APPROVE" },
]);
}),
);

it.effect("keeps GitHub's refusal when the viewer has no pending review", () =>
Effect.gen(function* () {
pendingReviewConflict(["query ViewerPendingReview", pendingReviewAnswer(null)]);
const api = yield* GitHubPullRequestApi.GitHubPullRequestApi;
const error = yield* Effect.flip(api.submitReview(reviewInput));
expect(error.message).toContain("User can only have one pending review per pull request");
expect(variablesOf("mutation SubmitPendingReview")).toEqual([]);
}),
);

it.effect("refuses a pending review it cannot submit as reviewed, and writes nothing", () =>
Effect.gen(function* () {
const api = yield* GitHubPullRequestApi.GitHubPullRequestApi;
const lineComment = {
path: "src/a.ts",
position: { kind: "added", newLine: 9 },
body: "Line",
} as const;
pendingReviewConflict(pendingAtHead);
const refused = [
yield* Effect.flip(api.submitReview({ ...reviewInput, comments: [lineComment] })),
yield* Effect.flip(
api.submitReview({ ...pullRequestInput, verdict: "comment", body: "", comments: [] }),
),
];
pendingReviewConflict([
"query ViewerPendingReview",
pendingReviewAnswer({ id: "PRR_1", commit: "old111" }),
]);
refused.push(
yield* Effect.flip(api.submitReview(reviewInput)),
yield* Effect.flip(api.submitReview({ ...reviewInput, verdict: "comment" })),
);
expect(refused.map((error) => error._tag)).toEqual(
Array(4).fill("GitHubPendingReviewConflictError"),
);
expect(variablesOf("mutation SubmitPendingReview")).toEqual([]);
}),
);

it.effect("tails Actions job logs and re-runs a run's failed jobs together", () =>
Effect.gen(function* () {
const runOf: Record<string, number> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import {
decodePullRequestStacksJson,
buildLabelRequest,
PULL_REQUEST_SEARCH_MAX_ROWS,
REVIEW_EVENTS,
gitHubReactionContent,
reviewThreadConversation,
type GitHubPullRequestActivity,
Expand Down Expand Up @@ -240,6 +241,15 @@ export class GitHubSubjectScopeError extends Schema.TaggedError<GitHubSubjectSco
}
}

export class GitHubPendingReviewConflictError extends Schema.TaggedError<GitHubPendingReviewConflictError>()(
"GitHubPendingReviewConflictError",
{ number: Schema.Int },
) {
override get message(): string {
return `You have a pending review on pull request #${this.number} on GitHub. T3 Code can submit it only when it is on the commit you reviewed and you have no line comments in T3 Code. Submit or discard it on GitHub.`;
}
}

export class GitHubWorkflowApprovalRefusedError extends Schema.TaggedError<GitHubWorkflowApprovalRefusedError>()(
"GitHubWorkflowApprovalRefusedError",
{
Expand Down Expand Up @@ -325,6 +335,7 @@ export type GitHubPullRequestApiError =
| GitHubDiffFileContentsUnavailableError
| GitHubRepositorySelectorError
| GitHubSubjectScopeError
| GitHubPendingReviewConflictError
| GitHubWorkflowApprovalRefusedError
| GitHubWorkflowApprovalHeadUnavailableError
| GitHubWorkflowApprovalHeadChangedError
Expand Down Expand Up @@ -3191,6 +3202,7 @@ export const make = Effect.gen(function* () {

submitReview: Effect.fn("GitHubPullRequestApi.submitReview")(function (input) {
const { owner, name } = parseRepositorySelector(input.repository);
const target = { ...input, operation: "submitReview" };
return api
.rest({
host: input.host,
Expand All @@ -3204,7 +3216,43 @@ export const make = Effect.gen(function* () {
expectedHeadSha: input.expectedHeadSha,
}),
})
.pipe(Effect.asVoid);
.pipe(
Effect.asVoid,
// GitHub refuses a second pending review; one started elsewhere is the one to submit.
Effect.catchTags({
// The enclosing operation owns the span for this inline handler.
GitHubApiResponseError: Effect.fnUntraced(function* (error) {
if (error.status !== 422) return yield* error;
// A failed lookup counts as no pending review, so GitHub's own 422 (which can
// also be a validation error) reaches the caller.
const pending = yield* client
.ViewerPendingReview({ owner, name, number: input.number })
.pipe(
graphql(target),
Effect.map(
(result) => result.repository?.pullRequest?.reviews?.nodes?.[0] ?? null,
),
Effect.orElseSucceed(() => null),
);
if (pending === null) return yield* error;
// GitHub records the verdict on the commit the pending review was started on.
if (
input.comments.length > 0 ||
input.expectedHeadSha === undefined ||
pending.commit?.oid !== input.expectedHeadSha
) {
return yield* new GitHubPendingReviewConflictError({ number: input.number });
}
yield* client
.SubmitPendingReview({
reviewId: pending.id,
event: REVIEW_EVENTS[input.verdict],
...(input.body.trim().length === 0 ? {} : { body: input.body }),
})
.pipe(graphql(target));
}),
}),
);
}),

replyToReviewThread: Effect.fn("GitHubPullRequestApi.replyToReviewThread")(function (input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@ const ReviewSubmissionSchema = Schema.Struct({
),
});

const REVIEW_EVENTS: Record<PullRequestReviewVerdict, "COMMENT" | "APPROVE" | "REQUEST_CHANGES"> = {
export const REVIEW_EVENTS: Record<
PullRequestReviewVerdict,
"COMMENT" | "APPROVE" | "REQUEST_CHANGES"
> = {
comment: "COMMENT",
approve: "APPROVE",
"request-changes": "REQUEST_CHANGES",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ mutation AddReviewThreadReply($threadId: ID!, $body: String!) {
}
}

# Without `$body` GitHub keeps the summary the pending review already has; an empty string clears it.
mutation SubmitPendingReview($reviewId: ID!, $event: PullRequestReviewEvent!, $body: String) {
submitPullRequestReview(input: { pullRequestReviewId: $reviewId, event: $event, body: $body }) {
pullRequestReview {
id
}
}
}

mutation AddReaction($subjectId: ID!, $content: ReactionContent!) {
addReaction(input: { subjectId: $subjectId, content: $content }) {
reaction {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,20 @@ query ReviewDismissals($owner: String!, $name: String!, $number: Int!, $after: S
}
}
}

# GitHub keeps at most one pending review per person on a pull request and shows it only to them,
# so this finds the viewer's own unsubmitted review, if any.
query ViewerPendingReview($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
pullRequest(number: $number) {
reviews(states: [PENDING], first: 1) {
nodes {
id
commit {
oid
}
}
}
}
}
}
Loading
Loading