Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge PR when checks are ready #13

Merged
merged 3 commits into from
Sep 28, 2023
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
43 changes: 36 additions & 7 deletions src/github/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ mutation($prId: ID!) {
}
}`;

// https://docs.github.com/en/graphql/reference/mutations#mergepullrequest
export const MERGE_PULL_REQUEST = `
mutation($prId: ID!, $mergeMethod: PullRequestMergeMethod!) {
mergePullRequest(input: {pullRequestId: $prId, mergeMethod: $mergeMethod}) {
clientMutationId
}
}`;

export type MergeMethod = "SQUASH" | "MERGE" | "REBASE";

export class Merger {
Expand All @@ -30,13 +38,34 @@ export class Merger {
) {}

async enableAutoMerge(): Promise<void> {
await this.gql<{
enablePullRequestAutoMerge: { clientMutationId: unknown };
}>(ENABLE_AUTO_MERGE, {
prId: this.nodeId,
mergeMethod: this.mergeMethod,
});
this.logger.info("Succesfully enabled auto-merge");
try {
await this.gql<{
enablePullRequestAutoMerge: { clientMutationId: unknown };
}>(ENABLE_AUTO_MERGE, {
prId: this.nodeId,
mergeMethod: this.mergeMethod,
});
this.logger.info("Succesfully enabled auto-merge");
} catch (error) {
this.logger.warn(error as Error);
if (
error instanceof Error &&
error.message.includes("Pull request is in clean status")
) {
this.logger.warn(
"Pull Request is ready to merge. Running merge command instead",
);
await this.gql<{
mergePullRequest: { clientMutationId: unknown };
}>(MERGE_PULL_REQUEST, {
prId: this.nodeId,
mergeMethod: this.mergeMethod,
});
this.logger.info("Succesfully merged PR");
} else {
throw error;
}
}
}

async disableAutoMerge(): Promise<void> {
Expand Down
10 changes: 9 additions & 1 deletion src/test/queries.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { validate } from "@octokit/graphql-schema";

import { DISABLE_AUTO_MERGE, ENABLE_AUTO_MERGE } from "../github/merger";
import {
DISABLE_AUTO_MERGE,
ENABLE_AUTO_MERGE,
MERGE_PULL_REQUEST,
} from "../github/merger";

describe("Schemas", () => {
test("ENABLE_AUTO_MERGE", () => {
Expand All @@ -10,4 +14,8 @@ describe("Schemas", () => {
test("DISABLE_AUTO_MERGE", () => {
expect(validate(DISABLE_AUTO_MERGE)).toEqual([]);
});

test("MERGE_PULL_REQUEST", () => {
expect(validate(MERGE_PULL_REQUEST)).toEqual([]);
});
});
Loading