From 8ab3e335273ae2aabda3e563ed6a7e77b89b9c75 Mon Sep 17 00:00:00 2001 From: Masafumi Koba <473530+ybiquitous@users.noreply.github.com> Date: Wed, 5 Aug 2020 10:04:05 +0900 Subject: [PATCH] feat: set default branch automatically (#192) --- README.md | 2 +- action.yml | 1 - dist/index.js | 60 +++++++++++++++++++++++--------- lib/createOrUpdatePullRequest.js | 24 ++++++------- lib/getDefaultBranch.js | 12 +++++++ lib/index.js | 17 ++++++--- 6 files changed, 82 insertions(+), 34 deletions(-) create mode 100644 lib/getDefaultBranch.js diff --git a/README.md b/README.md index 7400937c..e0c82318 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ jobs: # with: # github_token: ${{ github.token }} # branch: "npm-audit-fix-action/fix" - # default_branch: "master" + # default_branch: # commit_title: "build(deps): npm audit fix" # labels: "dependencies, security" ``` diff --git a/action.yml b/action.yml index 59521c13..a6b8d245 100644 --- a/action.yml +++ b/action.yml @@ -13,7 +13,6 @@ inputs: default_branch: description: "Default branch" required: false - default: "master" commit_title: description: "Commit title" required: false diff --git a/dist/index.js b/dist/index.js index 8cc1d1a2..338ae49d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -7126,6 +7126,7 @@ const updateNpm = __webpack_require__(193); const aggregateReport = __webpack_require__(599); const buildPullRequestBody = __webpack_require__(603); const createOrUpdatePullRequest = __webpack_require__(583); +const getDefaultBranch = __webpack_require__(686); const commaSeparatedList = __webpack_require__(604); /** @@ -7191,14 +7192,22 @@ async function run() { return; } - await core.group("Create or update a pull request", () => { + await core.group("Create or update a pull request", async () => { + const token = core.getInput("github_token"); + const repository = getFromEnv("GITHUB_REPOSITORY"); + + let baseBranch = core.getInput("default_branch"); + if (!baseBranch) { + baseBranch = await getDefaultBranch({ token, repository }); + } + return createOrUpdatePullRequest({ branch: core.getInput("branch"), - token: core.getInput("github_token"), - defaultBranch: core.getInput("default_branch"), + token, + baseBranch, title: core.getInput("commit_title"), body: buildPullRequestBody(report), - repository: getFromEnv("GITHUB_REPOSITORY"), + repository, actor: getFromEnv("GITHUB_ACTOR"), email: "actions@github.com", labels: commaSeparatedList(core.getInput("labels")), @@ -8044,21 +8053,21 @@ const github = __webpack_require__(469); /** * @param {{ - * token: string, - * branch: string, - * defaultBranch: string, - * title: string, - * body: string, - * repository: string, - * actor: string, - * email: string, - * labels: string[], + * token: string, + * branch: string, + * baseBranch: string, + * title: string, + * body: string, + * repository: string, + * actor: string, + * email: string, + * labels: string[], * }} params */ module.exports = async function createOrUpdatePullRequest({ token, branch, - defaultBranch, + baseBranch, title, body, repository, @@ -8075,7 +8084,7 @@ module.exports = async function createOrUpdatePullRequest({ owner, repo, state: "open", - base: defaultBranch, + base: baseBranch, sort: "updated", direction: "desc", per_page: 100, @@ -8105,7 +8114,7 @@ module.exports = async function createOrUpdatePullRequest({ title, body, head: branch, - base: defaultBranch, + base: baseBranch, }); console.log(`The pull request was created successfully: ${newPull.data.html_url}`); @@ -9162,6 +9171,25 @@ function isUnixExecutable(stats) { } //# sourceMappingURL=io-util.js.map +/***/ }), + +/***/ 686: +/***/ (function(module, __unusedexports, __webpack_require__) { + +const github = __webpack_require__(469); + +/** + * @param {{token: string, repository: string}} params + * @returns {Promise} + */ +module.exports = async function getDefaultBranch({ token, repository }) { + const octokit = github.getOctokit(token); + const [owner, repo] = repository.split("/"); + const res = await octokit.repos.get({ owner, repo }); + return res.data.default_branch; +}; + + /***/ }), /***/ 687: diff --git a/lib/createOrUpdatePullRequest.js b/lib/createOrUpdatePullRequest.js index 47ae5790..293e98a4 100644 --- a/lib/createOrUpdatePullRequest.js +++ b/lib/createOrUpdatePullRequest.js @@ -3,21 +3,21 @@ const github = require("@actions/github"); /** * @param {{ - * token: string, - * branch: string, - * defaultBranch: string, - * title: string, - * body: string, - * repository: string, - * actor: string, - * email: string, - * labels: string[], + * token: string, + * branch: string, + * baseBranch: string, + * title: string, + * body: string, + * repository: string, + * actor: string, + * email: string, + * labels: string[], * }} params */ module.exports = async function createOrUpdatePullRequest({ token, branch, - defaultBranch, + baseBranch, title, body, repository, @@ -34,7 +34,7 @@ module.exports = async function createOrUpdatePullRequest({ owner, repo, state: "open", - base: defaultBranch, + base: baseBranch, sort: "updated", direction: "desc", per_page: 100, @@ -64,7 +64,7 @@ module.exports = async function createOrUpdatePullRequest({ title, body, head: branch, - base: defaultBranch, + base: baseBranch, }); console.log(`The pull request was created successfully: ${newPull.data.html_url}`); diff --git a/lib/getDefaultBranch.js b/lib/getDefaultBranch.js new file mode 100644 index 00000000..2c39e438 --- /dev/null +++ b/lib/getDefaultBranch.js @@ -0,0 +1,12 @@ +const github = require("@actions/github"); + +/** + * @param {{token: string, repository: string}} params + * @returns {Promise} + */ +module.exports = async function getDefaultBranch({ token, repository }) { + const octokit = github.getOctokit(token); + const [owner, repo] = repository.split("/"); + const res = await octokit.repos.get({ owner, repo }); + return res.data.default_branch; +}; diff --git a/lib/index.js b/lib/index.js index 4f2f622d..0fb91110 100644 --- a/lib/index.js +++ b/lib/index.js @@ -7,6 +7,7 @@ const updateNpm = require("./updateNpm"); const aggregateReport = require("./aggregateReport"); const buildPullRequestBody = require("./buildPullRequestBody"); const createOrUpdatePullRequest = require("./createOrUpdatePullRequest"); +const getDefaultBranch = require("./getDefaultBranch"); const commaSeparatedList = require("./utils/commaSeparatedList"); /** @@ -72,14 +73,22 @@ async function run() { return; } - await core.group("Create or update a pull request", () => { + await core.group("Create or update a pull request", async () => { + const token = core.getInput("github_token"); + const repository = getFromEnv("GITHUB_REPOSITORY"); + + let baseBranch = core.getInput("default_branch"); + if (!baseBranch) { + baseBranch = await getDefaultBranch({ token, repository }); + } + return createOrUpdatePullRequest({ branch: core.getInput("branch"), - token: core.getInput("github_token"), - defaultBranch: core.getInput("default_branch"), + token, + baseBranch, title: core.getInput("commit_title"), body: buildPullRequestBody(report), - repository: getFromEnv("GITHUB_REPOSITORY"), + repository, actor: getFromEnv("GITHUB_ACTOR"), email: "actions@github.com", labels: commaSeparatedList(core.getInput("labels")),