Skip to content

Commit

Permalink
Merge pull request #29 from fpicalausa/feat/summary
Browse files Browse the repository at this point in the history
feat: add a summary after running
  • Loading branch information
fpicalausa authored Nov 2, 2024
2 parents a05e8aa + 7ed4293 commit 4686b59
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 30 deletions.
42 changes: 31 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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": "✏",
Expand All @@ -30446,13 +30456,15 @@ 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 +
". Use ignore-unknown-authors if this is expected.");
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);
Expand All @@ -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;
}
}
Expand All @@ -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}`);
});
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
Expand Down
63 changes: 45 additions & 18 deletions src/removeStaleBranches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -226,6 +250,13 @@ export async function removeStaleBranches(
};
const commitComments = new TaggedCommitComments(repo, octokit, headers);
let operations = 0;
let summary: Record<Plan["action"], number> & { scanned: number } = {
remove: 0,
"mark stale": 0,
"keep stale": 0,
skip: 0,
scanned: 0,
};

if (params.ignoreUnknownAuthors && !params.defaultRecipient) {
console.error(
Expand All @@ -234,36 +265,22 @@ 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<Plan["action"], string> = {
remove: "❌",
"mark stale": "✏",
"keep stale": "😐",
skip: "✅",
};
} as const;

for await (const branch of readBranches(
octokit,
headers,
repo,
params.protectedOrganizationName
)) {
summary.scanned++;
if (!branch.author?.username && !params.ignoreUnknownAuthors) {
console.error(
"🛑 Failed to find author associated with branch " +
Expand All @@ -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);
Expand All @@ -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}`);
}

0 comments on commit 4686b59

Please sign in to comment.