Skip to content

Commit

Permalink
feat: improve commit body (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
ybiquitous committed Aug 12, 2020
1 parent 464e360 commit 223f2bb
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 18 deletions.
59 changes: 48 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,7 +1395,7 @@ module.exports = require("child_process");

var net = __webpack_require__(631);
var tls = __webpack_require__(16);
var http = __webpack_require__(605);
var http = __webpack_require__(876);
var https = __webpack_require__(211);
var events = __webpack_require__(614);
var assert = __webpack_require__(357);
Expand Down Expand Up @@ -4894,7 +4894,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var Stream = _interopDefault(__webpack_require__(794));
var http = _interopDefault(__webpack_require__(605));
var http = _interopDefault(__webpack_require__(876));
var Url = _interopDefault(__webpack_require__(835));
var https = _interopDefault(__webpack_require__(211));
var zlib = _interopDefault(__webpack_require__(761));
Expand Down Expand Up @@ -7125,6 +7125,7 @@ const npmArgs = __webpack_require__(510);
const updateNpm = __webpack_require__(193);
const aggregateReport = __webpack_require__(599);
const buildPullRequestBody = __webpack_require__(603);
const buildCommitBody = __webpack_require__(605);
const createOrUpdatePullRequest = __webpack_require__(583);
const getDefaultBranch = __webpack_require__(686);
const commaSeparatedList = __webpack_require__(604);
Expand Down Expand Up @@ -7206,7 +7207,8 @@ async function run() {
token,
baseBranch,
title: core.getInput("commit_title"),
body: buildPullRequestBody(report),
pullBody: buildPullRequestBody(report),
commitBody: buildCommitBody(report),
repository,
actor: getFromEnv("GITHUB_ACTOR"),
email: "actions@github.com",
Expand All @@ -7230,7 +7232,7 @@ run();

Object.defineProperty(exports, "__esModule", { value: true });
const url = __webpack_require__(835);
const http = __webpack_require__(605);
const http = __webpack_require__(876);
const https = __webpack_require__(211);
const pm = __webpack_require__(950);
let tunnel;
Expand Down Expand Up @@ -8057,7 +8059,8 @@ const github = __webpack_require__(469);
* branch: string,
* baseBranch: string,
* title: string,
* body: string,
* pullBody: string,
* commitBody: string,
* repository: string,
* actor: string,
* email: string,
Expand All @@ -8069,7 +8072,8 @@ module.exports = async function createOrUpdatePullRequest({
branch,
baseBranch,
title,
body,
pullBody,
commitBody,
repository,
actor,
email,
Expand All @@ -8094,7 +8098,7 @@ module.exports = async function createOrUpdatePullRequest({
await exec("git", ["config", "user.name", actor]);
await exec("git", ["config", "user.email", email]);
await exec("git", ["add", "package-lock.json"]);
await exec("git", ["commit", "--message", `${title}\n\n${body}`]);
await exec("git", ["commit", "--message", `${title}\n\n${commitBody}`]);
await exec("git", ["checkout", "-B", branch]);
await exec("git", ["push", remote, `HEAD:${branch}`, ...(pull ? ["--force"] : [])]);

Expand All @@ -8104,15 +8108,15 @@ module.exports = async function createOrUpdatePullRequest({
repo,
pull_number: pull.number,
title,
body,
body: pullBody,
});
console.log(`The pull request was updated successfully: ${pull.html_url}`);
} else {
const newPull = await octokit.pulls.create({
owner,
repo,
title,
body,
body: pullBody,
head: branch,
base: baseBranch,
});
Expand Down Expand Up @@ -8238,7 +8242,7 @@ const { PACKAGE_NAME, PACKAGE_URL, NPM_VERSION } = __webpack_require__(32);

/**
* @param {Report} report
* @returns {String}
* @returns {string}
*/
module.exports = function buildPullRequestBody(report) {
/**
Expand Down Expand Up @@ -8342,7 +8346,33 @@ module.exports = function commaSeparatedList(str) {
/***/ 605:
/***/ (function(module) {

module.exports = require("http");
/**
* @param {Report} report
* @returns {string}
*/
module.exports = function buildCommitBody(report) {
const lines = [];

lines.push("Summary:");
lines.push(`- Updated packages: ${report.updated.length}`);
lines.push(`- Added packages: ${report.added.length}`);
lines.push(`- Removed packages: ${report.removed.length}`);

lines.push("");
if (report.updated.length > 0) {
lines.push("Fixed vulnerabilities:");
report.updated.forEach((e) => {
if ("severity" in e) {
lines.push(`- ${e.name}: "${e.title}" (${e.url})`);
}
});
} else {
lines.push("No fixed vulnerabilities.");
}

return lines.map((line) => `${line}\n`).join("");
};


/***/ }),

Expand Down Expand Up @@ -11488,6 +11518,13 @@ module.exports = function (str) {
};


/***/ }),

/***/ 876:
/***/ (function(module) {

module.exports = require("http");

/***/ }),

/***/ 881:
Expand Down
22 changes: 22 additions & 0 deletions lib/__tests__/buildCommitBody.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const buildCommitBody = require("../buildCommitBody");
const report = require("./report.json");

test("buildCommitBody()", () => {
expect(buildCommitBody(report)).toEqual(`Summary:
- Updated packages: 2
- Added packages: 1
- Removed packages: 1
Fixed vulnerabilities:
- mocha: "Prototype Pollution" (https://npmjs.com/advisories/1179)
`);

expect(buildCommitBody({ added: [], removed: [], updated: [], packageCount: 0, packageUrls: {} }))
.toEqual(`Summary:
- Updated packages: 0
- Added packages: 0
- Removed packages: 0
No fixed vulnerabilities.
`);
});
26 changes: 26 additions & 0 deletions lib/buildCommitBody.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {Report} report
* @returns {string}
*/
module.exports = function buildCommitBody(report) {
const lines = [];

lines.push("Summary:");
lines.push(`- Updated packages: ${report.updated.length}`);
lines.push(`- Added packages: ${report.added.length}`);
lines.push(`- Removed packages: ${report.removed.length}`);

lines.push("");
if (report.updated.length > 0) {
lines.push("Fixed vulnerabilities:");
report.updated.forEach((e) => {
if ("severity" in e) {
lines.push(`- ${e.name}: "${e.title}" (${e.url})`);
}
});
} else {
lines.push("No fixed vulnerabilities.");
}

return lines.map((line) => `${line}\n`).join("");
};
2 changes: 1 addition & 1 deletion lib/buildPullRequestBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { PACKAGE_NAME, PACKAGE_URL, NPM_VERSION } = require("./constants");

/**
* @param {Report} report
* @returns {String}
* @returns {string}
*/
module.exports = function buildPullRequestBody(report) {
/**
Expand Down
12 changes: 7 additions & 5 deletions lib/createOrUpdatePullRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const github = require("@actions/github");
* branch: string,
* baseBranch: string,
* title: string,
* body: string,
* pullBody: string,
* commitBody: string,
* repository: string,
* actor: string,
* email: string,
Expand All @@ -19,7 +20,8 @@ module.exports = async function createOrUpdatePullRequest({
branch,
baseBranch,
title,
body,
pullBody,
commitBody,
repository,
actor,
email,
Expand All @@ -44,7 +46,7 @@ module.exports = async function createOrUpdatePullRequest({
await exec("git", ["config", "user.name", actor]);
await exec("git", ["config", "user.email", email]);
await exec("git", ["add", "package-lock.json"]);
await exec("git", ["commit", "--message", `${title}\n\n${body}`]);
await exec("git", ["commit", "--message", `${title}\n\n${commitBody}`]);
await exec("git", ["checkout", "-B", branch]);
await exec("git", ["push", remote, `HEAD:${branch}`, ...(pull ? ["--force"] : [])]);

Expand All @@ -54,15 +56,15 @@ module.exports = async function createOrUpdatePullRequest({
repo,
pull_number: pull.number,
title,
body,
body: pullBody,
});
console.log(`The pull request was updated successfully: ${pull.html_url}`);
} else {
const newPull = await octokit.pulls.create({
owner,
repo,
title,
body,
body: pullBody,
head: branch,
base: baseBranch,
});
Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const npmArgs = require("./npmArgs");
const updateNpm = require("./updateNpm");
const aggregateReport = require("./aggregateReport");
const buildPullRequestBody = require("./buildPullRequestBody");
const buildCommitBody = require("./buildCommitBody");
const createOrUpdatePullRequest = require("./createOrUpdatePullRequest");
const getDefaultBranch = require("./getDefaultBranch");
const commaSeparatedList = require("./utils/commaSeparatedList");
Expand Down Expand Up @@ -87,7 +88,8 @@ async function run() {
token,
baseBranch,
title: core.getInput("commit_title"),
body: buildPullRequestBody(report),
pullBody: buildPullRequestBody(report),
commitBody: buildCommitBody(report),
repository,
actor: getFromEnv("GITHUB_ACTOR"),
email: "actions@github.com",
Expand Down

0 comments on commit 223f2bb

Please sign in to comment.