From d57b6c1d4bcfce35cba5af153c4ef5031326abfd Mon Sep 17 00:00:00 2001 From: ABHISHEK Joshi Date: Tue, 3 Aug 2021 23:32:27 +0530 Subject: [PATCH] Add amount keyword (#90) * added amount keyword for changes_requested * run build and format * Update dist/index.js Co-authored-by: Andre_601 <11576465+Andre601@users.noreply.github.com> * Update src/config.js Co-authored-by: Andre_601 <11576465+Andre601@users.noreply.github.com> * run build and format * Update commitFile.js * run build and format * Update amountReplacer.js * run build and format * Revert "Update amountReplacer.js" This reverts commit 28fd35aa832e6a7c47ca22efcd7f2bdc0267e308. * run build and format Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Andre_601 <11576465+Andre601@users.noreply.github.com> --- .eslintrc.json | 3 +- dist/index.js | 52 +++++++++++++++++++++++++--- src/config.js | 2 +- src/events/PullRequestReviewEvent.js | 4 +++ src/functions/amountReplacer.js | 24 +++++++++++++ src/functions/commitFile.js | 1 + src/functions/execute.js | 3 +- src/functions/filterContent.js | 7 +++- src/functions/makeCustomUrl.js | 2 +- src/functions/toUrlFormat.js | 2 +- 10 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 src/functions/amountReplacer.js diff --git a/.eslintrc.json b/.eslintrc.json index f4386165d8..497d1abaeb 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -20,6 +20,7 @@ "no-underscore-dangle": "off", "class-methods-use-this": "off", "prefer-destructuring": ["error", { "object": true, "array": false }], - "no-unused-vars": ["error", { "argsIgnorePattern": "req|res|next|val" }] + "no-unused-vars": ["warn", { "argsIgnorePattern": "req|res|next|val" }], + "no-prototype-builtins": "off" } } diff --git a/dist/index.js b/dist/index.js index 1a01794831..beac4ecec9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -25169,7 +25169,7 @@ const defaultVals = { wiki_create: "📖 Created new wiki page {WIKI} in {REPO}", added_member: "🤝 Became collaborator on {REPO}", changes_approved: "👍 Approved {ID} in {REPO}", - changes_requested: "🔴 Requested changes in {ID} in {REPO}", + changes_requested: "🔴 Requested {AMOUNT} change(s) for {ID} in {REPO}", new_release: "✌️ Released {ID} in {REPO}", new_star: "⭐ Starred {REPO}", commit_name: "readme-bot", @@ -25473,6 +25473,7 @@ module.exports = PullRequestReviewCommentEvent; /***/ ((module, __unused_webpack_exports, __nccwpck_require__) => { const { changes_approved, changes_requested } = __nccwpck_require__(4570); +const { amountUpdate } = __nccwpck_require__(3195); const makeCustomUrl = __nccwpck_require__(434); const toUrlFormat = __nccwpck_require__(5879); @@ -25489,6 +25490,9 @@ const PullRequestReviewEvent = (item) => { item.payload.action === "created" && item.payload.review.state == "changes_requested" ) { + if (!amountUpdate(item.payload.review.pull_request_url)) { + return ""; + } return changes_requested .replace(/{ID}/g, toUrlFormat(item, "pr_review")) .replace(/{REPO}/g, toUrlFormat(item.repo.name, "pr_review")) @@ -25546,6 +25550,37 @@ const WatchEvent = (item) => { module.exports = WatchEvent; +/***/ }), + +/***/ 3195: +/***/ ((module) => { + +const identifier = []; +const amount = []; + +const amountUpdate = (data) => { + let amt = identifier.indexOf(data); + if (amt !== -1) { + amount[amt] += 1; + return false; + } else { + identifier.push(data); + amount.push(1); + return true; + } +}; + +const amountReplace = (data) => { + amount.forEach((amt) => { + data = data.replace("{AMOUNT}", amt); + }); + + return data; +}; + +module.exports = { amountUpdate, amountReplace }; + + /***/ }), /***/ 4922: @@ -25673,6 +25708,7 @@ const commitFile = async () => { await exec("git", ["config", "--global", "user.email", commit_email], false); await exec("git", ["config", "--global", "user.name", commit_name], false); await exec("git", ["add", readme_file], false); + await exec("git", ["pull"], false); await exec("git", ["commit", "-m", commit_msg], false); await exec("git", ["push"], true); }; @@ -25721,7 +25757,8 @@ const exec = (cmd, args = [], callAPI) => }); app.on("close", (code) => { if (code !== 0 && !stdout.includes("nothing to commit")) { - err = new Error(`Invalid status code: ${code}`); + console.log(stdout); + let err = new Error(`Invalid status code: ${code}`); err.code = code; if (callAPI) { apiRequest({ ...reqParams, status: "failure" }, () => reject(err)); @@ -25763,11 +25800,12 @@ module.exports = exec; const serializers = __nccwpck_require__(3169); const { max_lines } = __nccwpck_require__(4570); +const { amountReplace } = __nccwpck_require__(3195); const filterContent = (eventData) => { let temp_content = []; - for (i = 0; i < eventData.length; i++) { + for (let i = 0; i < eventData.length; i++) { let event_string = serializers[eventData[i].type](eventData[i]); if (event_string !== "") { @@ -25781,6 +25819,10 @@ const filterContent = (eventData) => { temp_content = temp_content.flat(); temp_content.length = max_lines; + temp_content = temp_content.join("\n\n"); + + temp_content = amountReplace(temp_content).split("\n\n"); + console.log(temp_content); return temp_content; @@ -25888,7 +25930,7 @@ const makeCustomUrl = (item, type) => { `](${item.payload.release.html_url})`; break; default: - tools.exit.failure("Failed while creating the url string."); + url = ""; break; } return url; @@ -25968,7 +26010,7 @@ const toUrlFormat = (item, type) => { url = `[${item.payload.release.name}](${item.payload.release.html_url})`; break; default: - tools.exit.failure("Failed while creating the url format."); + url = ""; break; } return url; diff --git a/src/config.js b/src/config.js index 47bd55c7cb..c24b6b5d86 100644 --- a/src/config.js +++ b/src/config.js @@ -31,7 +31,7 @@ const defaultVals = { wiki_create: "📖 Created new wiki page {WIKI} in {REPO}", added_member: "🤝 Became collaborator on {REPO}", changes_approved: "👍 Approved {ID} in {REPO}", - changes_requested: "🔴 Requested changes in {ID} in {REPO}", + changes_requested: "🔴 Requested {AMOUNT} change(s) for {ID} in {REPO}", new_release: "✌️ Released {ID} in {REPO}", new_star: "⭐ Starred {REPO}", commit_name: "readme-bot", diff --git a/src/events/PullRequestReviewEvent.js b/src/events/PullRequestReviewEvent.js index b9c56d5017..30b452e834 100644 --- a/src/events/PullRequestReviewEvent.js +++ b/src/events/PullRequestReviewEvent.js @@ -1,4 +1,5 @@ const { changes_approved, changes_requested } = require("../config"); +const { amountUpdate } = require("../functions/amountReplacer"); const makeCustomUrl = require("../functions/makeCustomUrl"); const toUrlFormat = require("../functions/toUrlFormat"); @@ -15,6 +16,9 @@ const PullRequestReviewEvent = (item) => { item.payload.action === "created" && item.payload.review.state == "changes_requested" ) { + if (!amountUpdate(item.payload.review.pull_request_url)) { + return ""; + } return changes_requested .replace(/{ID}/g, toUrlFormat(item, "pr_review")) .replace(/{REPO}/g, toUrlFormat(item.repo.name, "pr_review")) diff --git a/src/functions/amountReplacer.js b/src/functions/amountReplacer.js new file mode 100644 index 0000000000..090e48cac6 --- /dev/null +++ b/src/functions/amountReplacer.js @@ -0,0 +1,24 @@ +const identifier = []; +const amount = []; + +const amountUpdate = (data) => { + let amt = identifier.indexOf(data); + if (amt !== -1) { + amount[amt] += 1; + return false; + } else { + identifier.push(data); + amount.push(1); + return true; + } +}; + +const amountReplace = (data) => { + amount.forEach((amt) => { + data = data.replace("{AMOUNT}", amt); + }); + + return data; +}; + +module.exports = { amountUpdate, amountReplace }; diff --git a/src/functions/commitFile.js b/src/functions/commitFile.js index b69f91bf6d..2ea271ffd6 100644 --- a/src/functions/commitFile.js +++ b/src/functions/commitFile.js @@ -22,6 +22,7 @@ const commitFile = async () => { await exec("git", ["config", "--global", "user.email", commit_email], false); await exec("git", ["config", "--global", "user.name", commit_name], false); await exec("git", ["add", readme_file], false); + await exec("git", ["pull"], false); await exec("git", ["commit", "-m", commit_msg], false); await exec("git", ["push"], true); }; diff --git a/src/functions/execute.js b/src/functions/execute.js index 22ff7a4eac..e17bca6ed0 100644 --- a/src/functions/execute.js +++ b/src/functions/execute.js @@ -34,7 +34,8 @@ const exec = (cmd, args = [], callAPI) => }); app.on("close", (code) => { if (code !== 0 && !stdout.includes("nothing to commit")) { - err = new Error(`Invalid status code: ${code}`); + console.log(stdout); + let err = new Error(`Invalid status code: ${code}`); err.code = code; if (callAPI) { apiRequest({ ...reqParams, status: "failure" }, () => reject(err)); diff --git a/src/functions/filterContent.js b/src/functions/filterContent.js index 9b361ec0c9..af02e421a0 100644 --- a/src/functions/filterContent.js +++ b/src/functions/filterContent.js @@ -5,11 +5,12 @@ const serializers = require("../serializers"); const { max_lines } = require("../config"); +const { amountReplace } = require("./amountReplacer"); const filterContent = (eventData) => { let temp_content = []; - for (i = 0; i < eventData.length; i++) { + for (let i = 0; i < eventData.length; i++) { let event_string = serializers[eventData[i].type](eventData[i]); if (event_string !== "") { @@ -23,6 +24,10 @@ const filterContent = (eventData) => { temp_content = temp_content.flat(); temp_content.length = max_lines; + temp_content = temp_content.join("\n\n"); + + temp_content = amountReplace(temp_content).split("\n\n"); + console.log(temp_content); return temp_content; diff --git a/src/functions/makeCustomUrl.js b/src/functions/makeCustomUrl.js index 3ff224c820..e7e79c95dd 100644 --- a/src/functions/makeCustomUrl.js +++ b/src/functions/makeCustomUrl.js @@ -92,7 +92,7 @@ const makeCustomUrl = (item, type) => { `](${item.payload.release.html_url})`; break; default: - tools.exit.failure("Failed while creating the url string."); + url = ""; break; } return url; diff --git a/src/functions/toUrlFormat.js b/src/functions/toUrlFormat.js index 5e31c45efa..2578085098 100644 --- a/src/functions/toUrlFormat.js +++ b/src/functions/toUrlFormat.js @@ -38,7 +38,7 @@ const toUrlFormat = (item, type) => { url = `[${item.payload.release.name}](${item.payload.release.html_url})`; break; default: - tools.exit.failure("Failed while creating the url format."); + url = ""; break; } return url;