feat(server): pull requests talk to GitHub's API instead of the gh CLI (#16320)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
1564aaa5d2
commit
adc3c9327a
15 files changed
+2871
-2597
No files matched your search
@@ -8,7 +8,7 @@ import * as Clock from "effect/Clock";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
|
||||
import * as GitHubApi from "../sourceControl/GitHubApi.ts";
|
||||
import { decodePullRequestStacksJson } from "./gitHubPullRequestJson.ts";
|
||||
|
||||
const stackErrorIdentity = {
|
||||
@@ -21,10 +21,6 @@ export class GitHubStackChangedError extends Schema.TaggedError<GitHubStackChang
|
||||
"GitHubStackChangedError",
|
||||
{ ...stackErrorIdentity, completed: Schema.Int },
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return this.completed > 0
|
||||
? `The stack changed at PR #${this.number} after ${this.completed} layers. Earlier updates remain on GitHub. Refresh it before trying again.`
|
||||
@@ -36,10 +32,6 @@ export class GitHubStackUnsupportedError extends Schema.TaggedError<GitHubStackU
|
||||
"GitHubStackUnsupportedError",
|
||||
stackErrorIdentity,
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return "This operation is not supported for this stack.";
|
||||
}
|
||||
@@ -49,10 +41,6 @@ export class GitHubStackResponseInvalidError extends Schema.TaggedError<GitHubSt
|
||||
"GitHubStackResponseInvalidError",
|
||||
{ ...stackErrorIdentity, cause: Schema.optional(Schema.Defect()) },
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return "GitHub returned an unreadable stack operation response.";
|
||||
}
|
||||
@@ -62,10 +50,6 @@ export class GitHubStackMergeRejectedError extends Schema.TaggedError<GitHubStac
|
||||
"GitHubStackMergeRejectedError",
|
||||
{ ...stackErrorIdentity, cause: Schema.Defect() },
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return "GitHub refused the stack merge. Check the stack's branch rules and merge requirements.";
|
||||
}
|
||||
@@ -75,10 +59,6 @@ export class GitHubStackMergePendingError extends Schema.TaggedError<GitHubStack
|
||||
"GitHubStackMergePendingError",
|
||||
stackErrorIdentity,
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return "The merge is still running on GitHub. Check its status there before submitting another request.";
|
||||
}
|
||||
@@ -88,10 +68,6 @@ export class GitHubStackPermissionError extends Schema.TaggedError<GitHubStackPe
|
||||
"GitHubStackPermissionError",
|
||||
stackErrorIdentity,
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return "You cannot update every branch in this stack. Check write access and fork maintainer permissions before retrying.";
|
||||
}
|
||||
@@ -101,10 +77,6 @@ export class GitHubStackRebaseFailedError extends Schema.TaggedError<GitHubStack
|
||||
"GitHubStackRebaseFailedError",
|
||||
{ ...stackErrorIdentity, completed: Schema.Int, cause: Schema.Defect() },
|
||||
) {
|
||||
get detail(): string {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Stack rebase stopped at PR #${this.number} after ${this.completed} layers. Earlier updates remain on GitHub; resolve the failing layer before retrying.`;
|
||||
}
|
||||
@@ -179,8 +151,6 @@ const decodeRebaseResponse = Schema.decodeEffect(
|
||||
),
|
||||
);
|
||||
|
||||
const encodeNodeIds = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
|
||||
|
||||
const decodeMergeResponse = Schema.decodeEffect(Schema.fromJsonString(MergeResponse));
|
||||
|
||||
/** Remote-only updates: a stack rebase never switches or rewrites the environment's checkout. */
|
||||
@@ -194,7 +164,7 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
action: PullRequestAction;
|
||||
mergeMethod?: PullRequestMergeMethod;
|
||||
}) {
|
||||
const github = yield* GitHubCli.GitHubCli;
|
||||
const api = yield* GitHubApi.GitHubApi;
|
||||
const identity = {
|
||||
repository: input.repository,
|
||||
number: input.number,
|
||||
@@ -203,11 +173,12 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
if (input.action !== "merge" && input.action !== "update-branch")
|
||||
return yield* new GitHubStackUnsupportedError({ ...identity });
|
||||
const endpoint = `repos/${input.repository}`;
|
||||
const read = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: ["api", "--hostname", input.host, `${endpoint}/stacks?pull_request=${input.number}`],
|
||||
const read = yield* api.rest({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
path: `${endpoint}/stacks?pull_request=${input.number}`,
|
||||
});
|
||||
const decoded = decodePullRequestStacksJson(read.stdout);
|
||||
const decoded = decodePullRequestStacksJson(read.body);
|
||||
if (Result.isFailure(decoded))
|
||||
return yield* new GitHubStackResponseInvalidError({ ...identity, cause: decoded.failure });
|
||||
const stack = decoded.success;
|
||||
@@ -243,27 +214,19 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
return yield* new GitHubStackUnsupportedError({ ...identity });
|
||||
if (input.action === "update-branch") {
|
||||
const [owner, name] = input.repository.split("/");
|
||||
const permissions = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--hostname",
|
||||
input.host,
|
||||
"graphql",
|
||||
"-f",
|
||||
`owner=${owner}`,
|
||||
"-f",
|
||||
`name=${name}`,
|
||||
"-f",
|
||||
`query=query($owner:String!,$name:String!){repository(owner:$owner,name:$name){${open
|
||||
.map(
|
||||
(layer) =>
|
||||
`pr${layer.number}:pullRequest(number:${layer.number}){headRepository{viewerPermission} maintainerCanModify}`,
|
||||
)
|
||||
.join(" ")}}}`,
|
||||
],
|
||||
const permissions = yield* api.graphql({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
allowReserve: true,
|
||||
variables: { owner, name },
|
||||
query: `query($owner:String!,$name:String!){repository(owner:$owner,name:$name){${open
|
||||
.map(
|
||||
(layer) =>
|
||||
`pr${layer.number}:pullRequest(number:${layer.number}){headRepository{viewerPermission} maintainerCanModify}`,
|
||||
)
|
||||
.join(" ")}}}`,
|
||||
});
|
||||
const access = yield* decodeBranchAccess(permissions.stdout).pipe(
|
||||
const access = yield* decodeBranchAccess(permissions).pipe(
|
||||
Effect.mapError((cause) => new GitHubStackResponseInvalidError({ ...identity, cause })),
|
||||
);
|
||||
// viewerCanUpdateBranch is false for an already-current layer, even if rebasing its parent
|
||||
@@ -282,35 +245,27 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
const processed: Array<{ id: string; number: number; headSha: string }> = [];
|
||||
for (const [index, layer] of open.entries()) {
|
||||
yield* Effect.gen(function* () {
|
||||
const read = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--hostname",
|
||||
input.host,
|
||||
"graphql",
|
||||
"-f",
|
||||
`owner=${owner}`,
|
||||
"-f",
|
||||
`name=${name}`,
|
||||
"-F",
|
||||
`number=${layer.number}`,
|
||||
"-f",
|
||||
`sha=${layer.headSha}`,
|
||||
"-f",
|
||||
`query=query($owner:String!,$name:String!,$number:Int!,$sha:String!){${
|
||||
processed.length === 0
|
||||
? ""
|
||||
: `processed:nodes(ids:${encodeNodeIds(processed.map((head) => head.id))}){... on PullRequest{headRefOid}}`
|
||||
} repository(owner:$owner,name:$name){pullRequest(number:$number){id headRefOid baseRef{compare(headRef:$sha){behindBy}}}}}`,
|
||||
],
|
||||
const read = yield* api.graphql({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
allowReserve: true,
|
||||
variables: {
|
||||
owner,
|
||||
name,
|
||||
number: layer.number,
|
||||
sha: layer.headSha,
|
||||
ids: processed.map((head) => head.id),
|
||||
},
|
||||
// GitHub rejects a declared variable the document never uses, so `$ids` is always
|
||||
// selected; an empty list asks for nothing.
|
||||
query: `query($owner:String!,$name:String!,$number:Int!,$sha:String!,$ids:[ID!]!){processed:nodes(ids:$ids){... on PullRequest{headRefOid}} repository(owner:$owner,name:$name){pullRequest(number:$number){id headRefOid baseRef{compare(headRef:$sha){behindBy}}}}}`,
|
||||
});
|
||||
const {
|
||||
data: {
|
||||
processed: observed,
|
||||
repository: { pullRequest: pr },
|
||||
},
|
||||
} = yield* decodeRebaseBranch(read.stdout);
|
||||
} = yield* decodeRebaseBranch(read);
|
||||
// A push to an earlier layer must not silently become the next layer's new base.
|
||||
const changed = processed.find(
|
||||
(head, index) => observed?.[index]?.headRefOid !== head.headSha,
|
||||
@@ -332,22 +287,14 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
return;
|
||||
}
|
||||
// Pass the reviewed revision to GitHub, including when a push races this read.
|
||||
const updated = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--hostname",
|
||||
input.host,
|
||||
"graphql",
|
||||
"-f",
|
||||
`id=${pr.id}`,
|
||||
"-f",
|
||||
`sha=${layer.headSha}`,
|
||||
"-f",
|
||||
"query=mutation($id:ID!,$sha:GitObjectID!){updatePullRequestBranch(input:{pullRequestId:$id,expectedHeadOid:$sha,updateMethod:REBASE}){pullRequest{headRefOid}}}",
|
||||
],
|
||||
const updated = yield* api.graphql({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
variables: { id: pr.id, sha: layer.headSha },
|
||||
query:
|
||||
"mutation($id:ID!,$sha:GitObjectID!){updatePullRequestBranch(input:{pullRequestId:$id,expectedHeadOid:$sha,updateMethod:REBASE}){pullRequest{headRefOid}}}",
|
||||
});
|
||||
const response = yield* decodeRebaseResponse(updated.stdout);
|
||||
const response = yield* decodeRebaseResponse(updated);
|
||||
processed.push({
|
||||
id: pr.id,
|
||||
number: layer.number,
|
||||
@@ -374,24 +321,18 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
decodeMergeResponse(raw).pipe(
|
||||
Effect.mapError((cause) => new GitHubStackResponseInvalidError({ ...identity, cause })),
|
||||
);
|
||||
const request = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--hostname",
|
||||
input.host,
|
||||
"--method",
|
||||
"PUT",
|
||||
`${endpoint}/pulls/${input.number}/merge-async`,
|
||||
"-f",
|
||||
`merge_method=${input.mergeMethod ?? "merge"}`,
|
||||
"-f",
|
||||
"merge_action=default",
|
||||
"-f",
|
||||
`sha=${target.headSha}`,
|
||||
],
|
||||
const request = yield* api.rest({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
method: "PUT",
|
||||
path: `${endpoint}/pulls/${input.number}/merge-async`,
|
||||
body: {
|
||||
merge_method: input.mergeMethod ?? "merge",
|
||||
merge_action: "default",
|
||||
sha: target.headSha,
|
||||
},
|
||||
});
|
||||
let result = yield* decode(request.stdout);
|
||||
let result = yield* decode(request.body);
|
||||
const deadline = (yield* Clock.currentTimeMillis) + 5 * 60_000;
|
||||
for (
|
||||
let attempt = 0;
|
||||
@@ -401,16 +342,12 @@ export const runGitHubStackAction = Effect.fn("runGitHubStackAction")(function*
|
||||
const uuid = result.details.uuid;
|
||||
if (!uuid) return yield* new GitHubStackResponseInvalidError({ ...identity });
|
||||
yield* Effect.sleep(Math.min(1_000 * 2 ** attempt, 10_000));
|
||||
const poll = yield* github.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
"--hostname",
|
||||
input.host,
|
||||
`${endpoint}/pulls/${input.number}/merge-async/${encodeURIComponent(uuid)}`,
|
||||
],
|
||||
const poll = yield* api.rest({
|
||||
host: input.host,
|
||||
operation: "runGitHubStackAction",
|
||||
path: `${endpoint}/pulls/${input.number}/merge-async/${encodeURIComponent(uuid)}`,
|
||||
});
|
||||
result = yield* decode(poll.stdout);
|
||||
result = yield* decode(poll.body);
|
||||
}
|
||||
if (result.status === "pending") return yield* new GitHubStackMergePendingError({ ...identity });
|
||||
if (result.status === "failed")
|
||||
|
||||
Reference in new issue
Block a user