Skip to content

Commit 9634a52

Browse files
committed
feat: Specify merge-method
1 parent da533c2 commit 9634a52

File tree

9 files changed

+813
-51
lines changed

9 files changed

+813
-51
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@
66

77
A Github action for enabling [Github Auto-Merge](https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/automatically-merging-a-pull-request) in a workflow for a pull-request.
88
You might want to use this for automatically merging Dependabot pull-requests.
9+
10+
## Running Locally
11+
Github Action developer-experience isn't fantastic, so for now we mimic the Github Action environment in `./src/local.ts`.
12+
13+
This file sets environment variables locally to enable action inputs, and points to a sample pull-request webhook event in `./stub/example-pull-request.json`.
14+
15+
1. Make sure you're running a recent version of Node (the correct version will always be in `.nvmrc` and `action.yml`)
16+
2. Set `GITHUB_TOKEN` locally. (You can do this via `$ export GITHUB_TOKEN=blah`)
17+
3. Optionally(!) set `MERGE_METHOD` locally. (You can do this via `$ export MERGE_METHOD=MERGE`)
18+
4. Run with `npm run local`.

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ inputs:
77
github-token:
88
description: 'The GITHUB_TOKEN secret'
99
required: true
10+
merge-method:
11+
description: 'Preferred merge method for automatic merges.'
12+
required: false
1013
runs:
1114
using: 'node12'
1215
main: 'dist/index.js'

dist/index.js

Lines changed: 120 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8822,6 +8822,117 @@ function wrappy (fn, cb) {
88228822
}
88238823

88248824

8825+
/***/ }),
8826+
8827+
/***/ 4020:
8828+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
8829+
8830+
"use strict";
8831+
8832+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8833+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8834+
return new (P || (P = Promise))(function (resolve, reject) {
8835+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8836+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8837+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8838+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8839+
});
8840+
};
8841+
Object.defineProperty(exports, "__esModule", ({ value: true }));
8842+
const core_1 = __nccwpck_require__(2186);
8843+
class EnableGithubAutomergeAction {
8844+
constructor(client, context) {
8845+
this.client = client;
8846+
this.context = context;
8847+
}
8848+
run() {
8849+
return __awaiter(this, void 0, void 0, function* () {
8850+
// Find out where we are!
8851+
const { repo } = this.context;
8852+
if (!repo) {
8853+
throw new Error("Could not find repository!");
8854+
}
8855+
// Make sure this is actually a pull-request!
8856+
// We need this to retrieve the pull-request node ID.
8857+
const { pull_request: pullRequest } = this.context.payload;
8858+
if (!pullRequest) {
8859+
throw new Error("Event payload missing `pull_request`, is this a pull-request?");
8860+
}
8861+
const pullRequestId = pullRequest.node_id;
8862+
// Step 1. Retrieve the merge method!
8863+
core_1.debug(`Retrieving merge-method...`);
8864+
const mergeMethod = yield this.getMergeMethod(repo);
8865+
core_1.debug(`Successfully retrieved merge-method: ${mergeMethod}`);
8866+
// Step 2. Enable auto-merge.
8867+
core_1.debug(`Enabling auto-merge for pull-request #${pullRequest.number}`);
8868+
yield this.enableAutoMerge(pullRequestId, mergeMethod);
8869+
core_1.debug(`Successfully enabled auto-merge for pull-request #${pullRequest.number}`);
8870+
});
8871+
}
8872+
getMergeMethod(repo) {
8873+
var _a;
8874+
return __awaiter(this, void 0, void 0, function* () {
8875+
const preferredMergeMethod = core_1.getInput("merge-method", { required: false });
8876+
// Allow users to specify a merge method.
8877+
if (preferredMergeMethod) {
8878+
return preferredMergeMethod;
8879+
}
8880+
//
8881+
// Otherwise try and discover one.
8882+
//
8883+
// Merge is the default behaviour.
8884+
let mergeMethod = `MERGE`;
8885+
// Try to discover the repository's default merge method.
8886+
try {
8887+
const repositorySettings = yield this.client.graphql(`
8888+
query($repository: String!, $owner: String!) {
8889+
repository(name:$repository, owner:$owner) {
8890+
viewerDefaultMergeMethod
8891+
}
8892+
}
8893+
`, {
8894+
repository: repo.repo,
8895+
owner: repo.owner
8896+
});
8897+
const viewerDefaultMergeMethod = ((_a = repositorySettings === null || repositorySettings === void 0 ? void 0 : repositorySettings.repository) === null || _a === void 0 ? void 0 : _a.viewerDefaultMergeMethod) || undefined;
8898+
if (viewerDefaultMergeMethod) {
8899+
mergeMethod = viewerDefaultMergeMethod;
8900+
}
8901+
}
8902+
catch (err) {
8903+
core_1.error(`Failed to read default merge method: ${err.message}`);
8904+
}
8905+
return mergeMethod;
8906+
});
8907+
}
8908+
enableAutoMerge(pullRequestId, mergeMethod) {
8909+
return __awaiter(this, void 0, void 0, function* () {
8910+
return yield this.client.graphql(`
8911+
mutation(
8912+
$pullRequestId: ID!,
8913+
$mergeMethod: PullRequestMergeMethod!
8914+
) {
8915+
enablePullRequestAutoMerge(input: {
8916+
pullRequestId: $pullRequestId,
8917+
mergeMethod: $mergeMethod
8918+
}) {
8919+
clientMutationId
8920+
pullRequest {
8921+
id
8922+
state
8923+
}
8924+
}
8925+
}
8926+
`, {
8927+
pullRequestId,
8928+
mergeMethod
8929+
});
8930+
});
8931+
}
8932+
}
8933+
exports.default = EnableGithubAutomergeAction;
8934+
8935+
88258936
/***/ }),
88268937

88278938
/***/ 399:
@@ -8857,42 +8968,29 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
88578968
step((generator = generator.apply(thisArg, _arguments || [])).next());
88588969
});
88598970
};
8971+
var __importDefault = (this && this.__importDefault) || function (mod) {
8972+
return (mod && mod.__esModule) ? mod : { "default": mod };
8973+
};
88608974
Object.defineProperty(exports, "__esModule", ({ value: true }));
8975+
exports.run = void 0;
88618976
const core = __importStar(__nccwpck_require__(2186));
88628977
const github = __importStar(__nccwpck_require__(5438));
8978+
const enable_github_automerge_action_1 = __importDefault(__nccwpck_require__(4020));
88638979
function run() {
88648980
return __awaiter(this, void 0, void 0, function* () {
88658981
try {
8982+
const { context } = github;
88668983
const token = core.getInput("github-token", { required: true });
88678984
const client = github.getOctokit(token);
8868-
const { pull_request: pr } = github.context.payload;
8869-
if (!pr) {
8870-
throw new Error("Event payload missing `pull_request`");
8871-
}
8872-
core.debug(`Enabling auto-merge for pull-request #${pr.number}`);
8873-
client.graphql(`
8874-
mutation {
8875-
enablePullRequestAutoMerge(input:{
8876-
pullRequestId: "${pr.node_id}"
8877-
}) {
8878-
actor {
8879-
login
8880-
}
8881-
clientMutationId
8882-
pullRequest {
8883-
id
8884-
state
8885-
}
8886-
}
8887-
}
8888-
`);
8889-
core.debug(`Successfully enabled auto-merge for pull-request #${pr.number}`);
8985+
const automergeAction = new enable_github_automerge_action_1.default(client, context);
8986+
yield automergeAction.run();
88908987
}
88918988
catch (error) {
88928989
core.setFailed(error.message);
88938990
}
88948991
});
88958992
}
8993+
exports.run = run;
88968994
run();
88978995

88988996

package-lock.json

Lines changed: 67 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
{
22
"name": "enable-github-automerge-action",
3-
"version": "0.0.0-alpha.1",
3+
"version": "0.0.0-alpha.2",
44
"description": "",
55
"main": "dist/main.ts",
66
"scripts": {
77
"build": "ncc build src/main.ts",
88
"format": "prettier --write **/*.ts",
9-
"format-check": "prettier --check **/*.ts"
9+
"format-check": "prettier --check **/*.ts",
10+
"local": "ts-node src/local.ts"
1011
},
1112
"author": "Alex Wilson <alex@alexwilson.tech>",
1213
"license": "ISC",
1314
"devDependencies": {
1415
"@types/node": "^14.14.25",
1516
"@vercel/ncc": "^0.27.0",
1617
"prettier": "^2.2.1",
18+
"ts-node": "^9.1.1",
1719
"typescript": "^4.1.3"
1820
},
1921
"dependencies": {

0 commit comments

Comments
 (0)