Skip to content

Commit d28b19b

Browse files
authored
Update branch option before auto-merge (#30)
- Added option to update a branch before enabling `auto-merge`. - Resolves #23 - Updated graphql hardcoded strings to work as `.graphql` files with auto-type generation. - Updated version to `1.1.0`
1 parent 4e6f275 commit d28b19b

18 files changed

+3013
-104
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
dist
33
.git
4+
src/github/graphql/*

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
!.vscode/extensions.json
1010
!.vscode/settings.json
1111
.idea
12+
13+
src/github/graphql/*.ts

Dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ WORKDIR /action
44

55
COPY package.json yarn.lock ./
66

7-
RUN yarn install --frozen-lockfile
7+
RUN yarn install --frozen-lockfile --ignore-scripts
88

99
COPY . .
1010

11+
RUN yarn run postinstall
12+
1113
RUN yarn run build
1214

1315
FROM node:20-slim

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ You can find all the inputs in [the action file](./action.yml), but let's walk t
5353
- 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.
5454
- This is only relevant once the PR can be merged. GitHub's auto-merge always merges unstable PRs
5555
- **Optional**: Defaults to `true`
56+
- `UPDATE_BEFORE_MERGE`: If the bot should try to update the PR to be up to date before enabling auto-merge
57+
- **Optional**: Defaults to false
5658

5759
## Usage
5860

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ inputs:
2121
ALLOW_UNSTABLE:
2222
required: false
2323
description: If unstable, ready to merge, PRs can be merged. Defaults to true
24+
UPDATE_BEFORE_MERGE:
25+
required: false
26+
description: If the bot should update the Pull Request Branch before enabling auto-merge
2427
outputs:
2528
repo:
2629
description: 'The name of the repo in owner/repo pattern'
2730

2831
runs:
2932
using: 'docker'
30-
image: 'docker://ghcr.io/paritytech/auto-merge-bot/action:1.0.1'
33+
image: 'docker://ghcr.io/paritytech/auto-merge-bot/action:1.1.0'

codegen.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
import type { CodegenConfig } from '@graphql-codegen/cli';
3+
4+
const config: CodegenConfig = {
5+
overwrite: true,
6+
schema: "./node_modules/@octokit/graphql-schema/schema.graphql",
7+
documents: "src/**/*.gql",
8+
generates: {
9+
"src/github/graphql/index.ts": {
10+
plugins: ["typescript", "typescript-operations"],
11+
}
12+
}
13+
};
14+
15+
export default config;

package.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
{
22
"name": "auto-merge-bot",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Bot which enables or disable auto-merge",
55
"main": "src/index.ts",
66
"scripts": {
77
"start": "node dist",
88
"build": "ncc build --license LICENSE",
99
"test": "jest",
1010
"fix": "eslint --fix 'src/**/*'",
11-
"lint": "eslint 'src/**/*'"
11+
"lint": "eslint 'src/**/*'",
12+
"codegen": "graphql-codegen --config codegen.ts",
13+
"copy": "node scripts/copy-files.js",
14+
"postinstall": "npm-run-all codegen copy"
1215
},
1316
"repository": {
1417
"type": "git",
@@ -25,15 +28,20 @@
2528
"@actions/github": "^6.0.0",
2629
"@octokit/graphql": "^8.0.1",
2730
"@octokit/graphql-schema": "^14.58.0",
28-
"@octokit/webhooks-types": "^7.3.2"
31+
"@octokit/webhooks-types": "^7.4.0",
32+
"graphql": "^16.8.1"
2933
},
3034
"devDependencies": {
3135
"@eng-automation/js-style": "^2.3.0",
36+
"@graphql-codegen/cli": "5.0.2",
37+
"@graphql-codegen/typescript": "4.0.6",
38+
"@graphql-codegen/typescript-resolvers": "^4.0.6",
3239
"@types/jest": "^29.5.12",
3340
"@vercel/ncc": "^0.38.1",
3441
"jest": "^29.7.0",
3542
"jest-mock-extended": "^3.0.5",
43+
"npm-run-all": "^4.1.5",
3644
"ts-jest": "^29.1.2",
37-
"typescript": "^5.3.3"
45+
"typescript": "^5.4.3"
3846
}
3947
}

scripts/copy-files.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { readdirSync, writeFileSync, readFileSync } = require("fs");
2+
3+
const files = readdirSync("src/github/graphql/");
4+
5+
/**
6+
* Copy a file and replace it's extension
7+
* @param {string} fileName Name of the file to copy
8+
* @param {string} extension New extension to put
9+
*/
10+
const copyFile = (fileName, extension) => {
11+
console.log("Copying content of %s into a .ts file", fileName);
12+
const content = readFileSync(fileName);
13+
const oldExtension = fileName.split(".").pop();
14+
writeFileSync(
15+
fileName.replace(oldExtension, extension),
16+
`// Generated from ${fileName}\nexport default \`${content}\`;`,
17+
);
18+
};
19+
20+
for (const file of files) {
21+
if (file.endsWith(".gql")) {
22+
copyFile(`src/github/graphql/${file}`, "ts");
23+
}
24+
}

src/bot.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class Bot {
3030
private readonly logger: ActionLogger,
3131
private readonly commentsApi: CommentsApi,
3232
private readonly allowlistedUsers: string[],
33+
private readonly updateBeforeMerge: boolean,
3334
private readonly actionUrl: string,
3435
) {}
3536

@@ -94,6 +95,9 @@ export class Bot {
9495
// Simply `/merge`
9596
case undefined:
9697
await this.commentsApi.reactToComment(this.comment.id, "+1");
98+
if (this.updateBeforeMerge) {
99+
await merger.updatePR();
100+
}
97101
await merger.enableAutoMerge();
98102
await this.commentsApi.comment(
99103
"Enabled `auto-merge` in Pull Request\n\n" + detailCommands,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
mutation DisableAutoMerge($prId: ID!) {
2+
disablePullRequestAutoMerge(input: {pullRequestId: $prId}) {
3+
clientMutationId
4+
}
5+
}

0 commit comments

Comments
 (0)