Skip to content

Commit

Permalink
feat: set default branch automatically (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous authored Aug 5, 2020
1 parent 135de4c commit 8ab3e33
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 34 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
# with:
# github_token: ${{ github.token }}
# branch: "npm-audit-fix-action/fix"
# default_branch: "master"
# default_branch: <automatically set>
# commit_title: "build(deps): npm audit fix"
# labels: "dependencies, security"
```
Expand Down
1 change: 0 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ inputs:
default_branch:
description: "Default branch"
required: false
default: "master"
commit_title:
description: "Commit title"
required: false
Expand Down
60 changes: 44 additions & 16 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

/**
Expand Down Expand Up @@ -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")),
Expand Down Expand Up @@ -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,
Expand All @@ -8075,7 +8084,7 @@ module.exports = async function createOrUpdatePullRequest({
owner,
repo,
state: "open",
base: defaultBranch,
base: baseBranch,
sort: "updated",
direction: "desc",
per_page: 100,
Expand Down Expand Up @@ -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}`);

Expand Down Expand Up @@ -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<string>}
*/
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:
Expand Down
24 changes: 12 additions & 12 deletions lib/createOrUpdatePullRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,7 +34,7 @@ module.exports = async function createOrUpdatePullRequest({
owner,
repo,
state: "open",
base: defaultBranch,
base: baseBranch,
sort: "updated",
direction: "desc",
per_page: 100,
Expand Down Expand Up @@ -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}`);

Expand Down
12 changes: 12 additions & 0 deletions lib/getDefaultBranch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const github = require("@actions/github");

/**
* @param {{token: string, repository: string}} params
* @returns {Promise<string>}
*/
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;
};
17 changes: 13 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

/**
Expand Down Expand Up @@ -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")),
Expand Down

0 comments on commit 8ab3e33

Please sign in to comment.