From 7ed4293231c8e7c1aef1e7bc6afd7624cdb34744 Mon Sep 17 00:00:00 2001 From: Francois Picalausa Date: Sat, 2 Nov 2024 09:30:47 +0900 Subject: [PATCH] feat: add a summary after running --- dist/index.js | 42 ++++++++++++++++++------- package.json | 2 +- src/removeStaleBranches.ts | 63 +++++++++++++++++++++++++++----------- 3 files changed, 77 insertions(+), 30 deletions(-) diff --git a/dist/index.js b/dist/index.js index e4734cb..05dc01d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30392,6 +30392,18 @@ function planBranchAction(now, branch, filters, commitComments, params) { }; }); } +function logActionRunConfiguration(params, staleCutoff, removeCutoff) { + if (params.isDryRun) { + console.log("Running in dry-run mode. No branch will be removed."); + } + console.log(`Branches updated before ${(0, formatISO_1.formatISO)(staleCutoff)} will be marked as stale`); + if (params.daysBeforeBranchDelete == 0) { + console.log("Branches will be instantly removed due to days-before-branch-delete being set to 0."); + } + else { + console.log(`Branches marked stale before ${(0, formatISO_1.formatISO)(removeCutoff)} will be removed`); + } +} function removeStaleBranches(octokit, params) { return __awaiter(this, void 0, void 0, function* () { var _a, e_1, _b, _c; @@ -30421,20 +30433,18 @@ function removeStaleBranches(octokit, params) { }; const commitComments = new commitComments_1.TaggedCommitComments(repo, octokit, headers); let operations = 0; + let summary = { + remove: 0, + "mark stale": 0, + "keep stale": 0, + skip: 0, + scanned: 0, + }; if (params.ignoreUnknownAuthors && !params.defaultRecipient) { console.error("When ignoring unknown authors, you must specify a default recipient"); return; } - if (params.isDryRun) { - console.log("Running in dry-run mode. No branch will be removed."); - } - console.log(`Branches updated before ${(0, formatISO_1.formatISO)(staleCutoff)} will be marked as stale`); - if (params.daysBeforeBranchDelete == 0) { - console.log("Branches will be instantly removed due to days-before-branch-delete being set to 0."); - } - else { - console.log(`Branches marked stale before ${(0, formatISO_1.formatISO)(removeCutoff)} will be removed`); - } + logActionRunConfiguration(params, staleCutoff, removeCutoff); const icons = { remove: "❌", "mark stale": "✏", @@ -30446,6 +30456,7 @@ function removeStaleBranches(octokit, params) { _c = _g.value; _e = false; const branch = _c; + summary.scanned++; if (!((_d = branch.author) === null || _d === void 0 ? void 0 : _d.username) && !params.ignoreUnknownAuthors) { console.error("🛑 Failed to find author associated with branch " + branch.branchName + @@ -30453,6 +30464,7 @@ function removeStaleBranches(octokit, params) { throw new Error("Failed to find author for branch " + branch.branchName); } const plan = yield planBranchAction(now.getTime(), branch, filters, commitComments, params); + summary[plan.action]++; core.startGroup(`${icons[plan.action]} branch ${branch.branchName}`); try { yield processBranch(plan, branch, commitComments, params); @@ -30464,7 +30476,7 @@ function removeStaleBranches(octokit, params) { core.endGroup(); } if (operations >= params.operationsPerRun) { - console.log("Exiting after " + operations + " operations"); + console.log("Stopping after " + operations + " operations"); return; } } @@ -30476,6 +30488,14 @@ function removeStaleBranches(octokit, params) { } finally { if (e_1) throw e_1.error; } } + const actionSummary = [ + `${summary.scanned} scanned`, + `${icons.skip} ${summary.skip} skipped`, + `${icons["mark stale"]} ${summary["mark stale"]} marked stale`, + `${icons["keep stale"]} ${summary["keep stale"]} kept stale`, + `${icons.remove} ${summary.remove} removed`, + ].join(", "); + console.log(`Summary: ${actionSummary}`); }); } diff --git a/package.json b/package.json index 310afc9..49c5cf3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "remove-stale-branches", "private": true, - "version": "2.1.1", + "version": "2.2.1", "description": "Cleanup stale branches from a repository", "main": "src/index.ts", "author": "Francois Picalausa ", diff --git a/src/removeStaleBranches.ts b/src/removeStaleBranches.ts index f29fe84..6b51a29 100644 --- a/src/removeStaleBranches.ts +++ b/src/removeStaleBranches.ts @@ -195,6 +195,30 @@ async function planBranchAction( }; } +function logActionRunConfiguration( + params: Params, + staleCutoff: number, + removeCutoff: number +) { + if (params.isDryRun) { + console.log("Running in dry-run mode. No branch will be removed."); + } + + console.log( + `Branches updated before ${formatISO(staleCutoff)} will be marked as stale` + ); + + if (params.daysBeforeBranchDelete == 0) { + console.log( + "Branches will be instantly removed due to days-before-branch-delete being set to 0." + ); + } else { + console.log( + `Branches marked stale before ${formatISO(removeCutoff)} will be removed` + ); + } +} + export async function removeStaleBranches( octokit: Octokit, params: Params @@ -226,6 +250,13 @@ export async function removeStaleBranches( }; const commitComments = new TaggedCommitComments(repo, octokit, headers); let operations = 0; + let summary: Record & { scanned: number } = { + remove: 0, + "mark stale": 0, + "keep stale": 0, + skip: 0, + scanned: 0, + }; if (params.ignoreUnknownAuthors && !params.defaultRecipient) { console.error( @@ -234,29 +265,14 @@ export async function removeStaleBranches( return; } - if (params.isDryRun) { - console.log("Running in dry-run mode. No branch will be removed."); - } - - console.log( - `Branches updated before ${formatISO(staleCutoff)} will be marked as stale` - ); - - if (params.daysBeforeBranchDelete == 0) { - console.log("Branches will be instantly removed due to days-before-branch-delete being set to 0.") - } else { - console.log( - `Branches marked stale before ${formatISO(removeCutoff)} will be removed` - ); - } - + logActionRunConfiguration(params, staleCutoff, removeCutoff); const icons: Record = { remove: "❌", "mark stale": "✏", "keep stale": "😐", skip: "✅", - }; + } as const; for await (const branch of readBranches( octokit, @@ -264,6 +280,7 @@ export async function removeStaleBranches( repo, params.protectedOrganizationName )) { + summary.scanned++; if (!branch.author?.username && !params.ignoreUnknownAuthors) { console.error( "🛑 Failed to find author associated with branch " + @@ -280,6 +297,7 @@ export async function removeStaleBranches( commitComments, params ); + summary[plan.action]++; core.startGroup(`${icons[plan.action]} branch ${branch.branchName}`); try { await processBranch(plan, branch, commitComments, params); @@ -292,8 +310,17 @@ export async function removeStaleBranches( } if (operations >= params.operationsPerRun) { - console.log("Exiting after " + operations + " operations"); + console.log("Stopping after " + operations + " operations"); return; } } + + const actionSummary = [ + `${summary.scanned} scanned`, + `${icons.skip} ${summary.skip} skipped`, + `${icons["mark stale"]} ${summary["mark stale"]} marked stale`, + `${icons["keep stale"]} ${summary["keep stale"]} kept stale`, + `${icons.remove} ${summary.remove} removed`, + ].join(", "); + console.log(`Summary: ${actionSummary}`); }