From 764406674e55cee59535c65e8186611bcff40353 Mon Sep 17 00:00:00 2001 From: Salman Date: Wed, 18 Nov 2020 17:39:58 +0530 Subject: [PATCH] feat: make merge_method configurable --- action.yml | 6 +++++- package.json | 2 +- index.js => src/index.js | 11 +++-------- src/log.js | 12 ++++++++++++ src/util.js | 26 ++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 10 deletions(-) rename index.js => src/index.js (76%) create mode 100644 src/log.js create mode 100644 src/util.js diff --git a/action.yml b/action.yml index c8e1d2b4..48caa5ac 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,10 @@ inputs: description: "Packages that you want to manually review before upgrading" required: false default: [] + merge-method: + description: "The merge method you would like to use (squash, merge, rebase)" + required: false + default: "squash" runs: using: "node12" - main: "index.js" + main: "src/index.js" diff --git a/package.json b/package.json index 7bdea462..ede280e4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "github-action-merge-dependabot", "version": "1.0.1", "description": "", - "main": "index.js", + "main": "src/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/index.js b/src/index.js similarity index 76% rename from index.js rename to src/index.js index fd9deb05..ddfe0055 100644 --- a/index.js +++ b/src/index.js @@ -1,14 +1,9 @@ const core = require('@actions/core') const github = require('@actions/github') -const GITHUB_TOKEN = core.getInput('github-token', { required: true }) -const EXCLUDE_PKGS = core.getInput('exclude') || [] +const { getInputs } = require('./util') -const getMergeMethod = (repo) => { - if (repo.allow_merge_commit) return 'merge' - if (repo.allow_squash_merge) return 'squash' - return 'rebase' -} +const { GITHUB_TOKEN, MERGE_METHOD, EXCLUDE_PKGS } = getInputs() async function run () { try { @@ -43,7 +38,7 @@ async function run () { owner, repo, pull_number: prNumber, - merge_method: getMergeMethod(pr.head.repo) + merge_method: MERGE_METHOD }) } catch (error) { core.setFailed(error.message) diff --git a/src/log.js b/src/log.js new file mode 100644 index 00000000..62cb6cc5 --- /dev/null +++ b/src/log.js @@ -0,0 +1,12 @@ +const { debug, error, info, warning } = require('@actions/core') + +const stringify = (msg) => ( + typeof msg === 'string' ? msg : (msg.stack || msg.toString()) +) + +const log = logger => message => logger(stringify(message)) + +exports.logDebug = log(debug) +exports.logError = log(error) +exports.logInfo = log(info) +exports.logWarning = log(warning) diff --git a/src/util.js b/src/util.js new file mode 100644 index 00000000..45e46c5d --- /dev/null +++ b/src/util.js @@ -0,0 +1,26 @@ +const core = require('@actions/core') + +const { logWarning } = require('./log') + +const mergeMethods = { + merge: 'merge', + squash: 'squash', + rebase: 'rebase' +} + +const getMergeMethod = () => { + const input = core.getInput('merge-method') + + if (!input || !mergeMethods[input]) { + logWarning('merge-method input is ignored because it is malformed, defaulting to `squash`.') + return mergeMethods.squash + } + + return mergeMethods[input] +} + +exports.getInputs = () => ({ + GITHUB_TOKEN: core.getInput('github-token', { required: true }), + MERGE_METHOD: getMergeMethod(), + EXCLUDE_PKGS: core.getInput('exclude') || [] +})