Skip to content

Commit

Permalink
Allow unstable branches to be merged (#25)
Browse files Browse the repository at this point in the history
Added the ability to merge [unstable
branches](https://docs.github.com/en/graphql/reference/enums#mergestatestatus).

This is enabled by default, as GitHub's Auto Merge feature always merges
unstable branches.

Fixes #24
  • Loading branch information
Bullrich authored Mar 6, 2024
1 parent 603f47c commit dc2eac9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
with:
GITHUB_TOKEN: '${{ github.token }}'
MERGE_METHOD: "SQUASH"
ALLOW_UNSTABLE: true
```
#### Inputs
Expand All @@ -48,6 +49,10 @@ You can find all the inputs in [the action file](./action.yml), but let's walk t
- `ALLOWLIST`: List of user accounts which are allowed to use the bot aside from the author and org members.
- **Optional**
- Must be a comma separated value: `user-1,user-2,user-3`.
- `ALLOW_UNSTABLE`: If unstable, ready to merge, PRs can be merged
- An [unstable PR](https://docs.github.com/en/graphql/reference/enums#mergestatestatus) is a PR that can be merged, but a *non required status check* is failing.
- This is only relevant once the PR can be merged. GitHub's auto-merge always merges unstable PRs
- **Optional**: Defaults to `true`

## Usage

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ inputs:
ALLOWLIST:
required: false
description: List of users which are allowed to use the bot. Separated by commas (abc,def,ghi)
ALLOW_UNSTABLE:
required: false
description: If unstable, ready to merge, PRs can be merged. Defaults to true
outputs:
repo:
description: 'The name of the repo in owner/repo pattern'

runs:
using: 'docker'
image: 'docker://ghcr.io/paritytech/auto-merge-bot/action:1.0.0'
image: 'docker://ghcr.io/paritytech/auto-merge-bot/action:1.0.1'
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auto-merge-bot",
"version": "1.0.0",
"version": "1.0.1",
"description": "Bot which enables or disable auto-merge",
"main": "src/index.ts",
"scripts": {
Expand Down
23 changes: 19 additions & 4 deletions src/github/merger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,26 @@ export class Merger {
private readonly gql: typeof graphql,
private readonly logger: ActionLogger,
private readonly mergeMethod: PullRequestMergeMethod,
private readonly allowUnstable: boolean = false,
) {}

errorPermitsToMerge(error: Error): boolean {
// If it's clean it can be merged
if (error.message.includes("Pull request is in clean status")) {
return true;
}

// If it is unstable and allowed, it can also be merged
if (error.message.includes("Pull request is in unstable status")) {
this.logger.warn(
"PR is unstable! Some non required status checks are failing.",
);
return this.allowUnstable;
}

return false;
}

async enableAutoMerge(): Promise<void> {
try {
await this.gql<{
Expand All @@ -48,10 +66,7 @@ export class Merger {
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")
) {
if (error instanceof Error && this.errorPermitsToMerge(error)) {
this.logger.warn(
"Pull Request is ready to merge. Running merge command instead",
);
Expand Down
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ const getMergeMethod = (): PullRequestMergeMethod => {
return method;
};

/** If it should allow PRs that have failed NON REQUIRED status check to be merged */
const getAllowUnstable = (): boolean => {
return getInput("ALLOW_UNSTABLE", { required: false }) !== "false";
};

const silentMode = getInput("SILENT", { required: false }) === "true";

logger.info(
Expand Down Expand Up @@ -80,7 +85,15 @@ if (context.payload.comment) {
const gql = getOctokit(token).graphql.defaults({
headers: { authorization: `token ${token}` },
}) as graphql;
const merger = new Merger(issue.node_id, gql, logger, getMergeMethod());
const mergeMethod = getMergeMethod();
const unstableAllowed = getAllowUnstable();
const merger = new Merger(
issue.node_id,
gql,
logger,
mergeMethod,
unstableAllowed,
);
const bot = new Bot(
comment,
issue,
Expand Down

0 comments on commit dc2eac9

Please sign in to comment.