Skip to content

Commit

Permalink
added ability to change merge method
Browse files Browse the repository at this point in the history
  • Loading branch information
Bullrich committed Sep 26, 2023
1 parent 685bf2c commit 7f22891
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 21 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
GITHUB_TOKEN:
required: true
description: The token to access the repo
MERGE_METHOD:
required: false
description: The merge method to use. Must be one of MERGE, SQUASH or REBASE.
default: SQUASH
outputs:
repo:
description: 'The name of the repo in owner/repo pattern'
Expand Down
11 changes: 7 additions & 4 deletions src/github/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { ActionLogger } from "./types";

// https://docs.github.com/en/graphql/reference/mutations#enablepullrequestautomerge
export const ENABLE_AUTO_MERGE = `
mutation($prId: ID!) {
enablePullRequestAutoMerge(input: {pullRequestId: $prId, mergeMethod: SQUASH}) {
mutation($prId: ID!, $method: PullRequestMergeMethod!) {
enablePullRequestAutoMerge(input: {pullRequestId: $prId, mergeMethod: $method}) {
clientMutationId
}
}`;
Expand All @@ -17,15 +17,18 @@ mutation($prId: ID!) {
}
}`;

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

export class Merger {
constructor(private readonly nodeId: string, private readonly gql: typeof graphql, private readonly logger: ActionLogger) {
constructor(private readonly nodeId: string, private readonly gql: typeof graphql, private readonly logger: ActionLogger, private readonly mergeMethod: MergeMethod) {

}

async enableAutoMerge() {
const mergeRequest = await this.gql<{ enablePullRequestAutoMerge: { clientMutationId: unknown } }>(ENABLE_AUTO_MERGE,
{
prId: this.nodeId
prId: this.nodeId,
method: this.mergeMethod
});
this.logger.info("Succesfully enabled auto-merge");
}
Expand Down
51 changes: 34 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { getInput, info, setFailed, setOutput } from "@actions/core";
import { getInput, setFailed, setOutput } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { Context } from "@actions/github/lib/context";
import { graphql } from "@octokit/graphql/dist-types/types";
import { Issue, IssueComment } from "@octokit/webhooks-types";
import { PullRequest } from "@octokit/webhooks-types";

import { Bot } from "./bot";
import { CommentsApi } from "./github/comments";
import { MergeMethod, Merger } from "./github/merger";
import { generateCoreLogger } from "./util";
import { Bot } from "./bot";
import { graphql } from "@octokit/graphql/dist-types/types";
import { Merger } from "./github/merger";

const getRepo = (ctx: Context) => {
let repo = getInput("repo", { required: false });
Expand All @@ -25,30 +24,48 @@ const getRepo = (ctx: Context) => {
};

const repo = getRepo(context);
const logger = generateCoreLogger();

setOutput("repo", `${repo.owner}/${repo.repo}`);

console.log("Event received", context.payload);
console.log("Job", context.job);
console.log("Action", context.action);
console.log("event name", context.eventName);
logger.debug("Event received: " + JSON.stringify(context.payload));
logger.info(`Received event of typer ${context.eventName}`);

if (context.eventName !== "issue_comment") {
throw new Error("Wrong event type");
} else if (!context.payload.issue?.pull_request) {
console.log("Comment happened on an issue, not a PR");
throw new Error("Comment happened on an issue, not a PR");
}

const getMergeMethod = (): MergeMethod => {
const method = (getInput("MERGE_METHOD", { required: false }) ??
"SQUASH") as MergeMethod;
if (method !== "SQUASH" && method !== "MERGE" && method !== "REBASE") {
throw new Error(
"MERGE_METHOD must be either 'SQUASH', 'MERGE' or 'REBASE'",
);
}

return method;
};

if (context.payload.comment) {
const token = getInput("token", { required: true });
const token = getInput("GITHUB_TOKEN", { required: true });
const comment = context.payload.comment as unknown as IssueComment;
const issue = context.payload.issue as unknown as Issue;
const logger = generateCoreLogger();
const commentsApi = new CommentsApi(getOctokit(token), logger, { ...repo, number: issue.number });
const gql = getOctokit(token).graphql.defaults({ headers: { authorization: `token ${token}` } }) as graphql;
const merger = new Merger(issue.node_id, gql, logger);
const commentsApi = new CommentsApi(getOctokit(token), logger, {
...repo,
number: issue.number,
});
const gql = getOctokit(token).graphql.defaults({
headers: { authorization: `token ${token}` },
}) as graphql;
const merger = new Merger(issue.node_id, gql, logger, getMergeMethod());
const bot = new Bot(comment, issue, logger, commentsApi);
bot.run(merger).then(() => logger.info("Finished!")).catch(setFailed);
bot
.run(merger)
.then(() => logger.info("Finished!"))
.catch(setFailed);
} else {
console.error("No 'comment' object in the payload!");
throw new Error("No 'comment' object in the payload!");
}

0 comments on commit 7f22891

Please sign in to comment.