From 95af70299df825610b44d71b19244aa733815f12 Mon Sep 17 00:00:00 2001 From: Francois Picalausa Date: Thu, 2 Jun 2022 13:25:14 +0900 Subject: [PATCH] fix: don't fail if the commit doesn't have an attached user --- dist/index.js | 13 +++++++++---- dist/types.d.ts | 2 +- src/commitComments.ts | 3 ++- src/readBranches.ts | 7 +++---- src/removeStaleBranches.ts | 8 ++++++-- src/types.ts | 2 +- 6 files changed, 22 insertions(+), 13 deletions(-) diff --git a/dist/index.js b/dist/index.js index b57a359..fe9e58b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -31317,12 +31317,13 @@ class TaggedCommitComments { this.headers = headers; } static formatCommentMessage(messageTemplate, branch, config, repo) { + const username = branch.username || "(Unknown user)"; return messageTemplate .replace(/[{]branchName[}]/g, branch.branchName) .replace(/[{]branchUrl[}]/g, `https://github.com/${encodeURIComponent(repo.owner)}/${encodeURIComponent(repo.repo)}/tree/${encodeURIComponent(branch.branchName)}`) .replace(/[{]repoOwner[}]/g, repo.owner) .replace(/[{]repoName[}]/g, repo.repo) - .replace(/[{]author[}]/g, branch.username) + .replace(/[{]author[}]/g, username) .replace(/[{]daysBeforeBranchStale[}]/g, String(config.daysBeforeBranchStale)) .replace(/[{]daysBeforeBranchDelete[}]/g, String(config.daysBeforeBranchDelete)); } @@ -31555,7 +31556,9 @@ function readBranches(octokit, headers, repo, organization) { const { repository: { refs: { edges, pageInfo }, }, } = yield __await(octokit.graphql(organization ? GRAPHQL_QUERY_WITH_ORG : GRAPHQL_QUERY, params)); for (let i = 0; i < edges.length; ++i) { const ref = edges[i]; - const { node: { branchName, prefix, refUpdateRule, target: { oid, author: { date, user: { login, organization }, }, }, }, } = ref; + const { node: { branchName, prefix, refUpdateRule, target: { oid, author: { date, user }, }, }, } = ref; + const login = user ? user.login : null; + const organization = user ? user.organization.id : null; yield yield __await({ date: Date.parse(date), branchName, @@ -31633,7 +31636,7 @@ const date_fns_1 = __nccwpck_require__(3314); function processBranch(plan, branch, commitComments, params) { return __awaiter(this, void 0, void 0, function* () { console.log("-> branch was last updated by " + - branch.username + + (branch.username || "(unknown user)") + " on " + (0, formatISO_1.default)(branch.date)); if (plan.action === "skip") { @@ -31693,7 +31696,9 @@ function planBranchAction(now, branch, filters, commitComments, params) { if (params.protectedOrganizationName && branch.belongsToOrganization) { return skip(`author ${branch.username} belongs to protected organization ${params.protectedOrganizationName}`); } - if (filters.authorsRegex && filters.authorsRegex.test(branch.username)) { + if (filters.authorsRegex && + branch.username && + filters.authorsRegex.test(branch.username)) { return skip(`author ${branch.username} is exempted`); } if (filters.branchRegex && filters.branchRegex.test(branch.branchName)) { diff --git a/dist/types.d.ts b/dist/types.d.ts index 71e5699..cb0a933 100644 --- a/dist/types.d.ts +++ b/dist/types.d.ts @@ -4,7 +4,7 @@ export declare type Branch = { branchName: string; prefix: string; commitId: string; - username: string; + username: string | null; isProtected: boolean; }; export declare type Repo = { diff --git a/src/commitComments.ts b/src/commitComments.ts index a1f9f56..71bf252 100644 --- a/src/commitComments.ts +++ b/src/commitComments.ts @@ -34,6 +34,7 @@ export class TaggedCommitComments { config: Pick, repo: Repo ) { + const username = branch.username || "(Unknown user)"; return messageTemplate .replace(/[{]branchName[}]/g, branch.branchName) .replace( @@ -46,7 +47,7 @@ export class TaggedCommitComments { ) .replace(/[{]repoOwner[}]/g, repo.owner) .replace(/[{]repoName[}]/g, repo.repo) - .replace(/[{]author[}]/g, branch.username) + .replace(/[{]author[}]/g, username) .replace( /[{]daysBeforeBranchStale[}]/g, String(config.daysBeforeBranchStale) diff --git a/src/readBranches.ts b/src/readBranches.ts index 25b123a..3787054 100644 --- a/src/readBranches.ts +++ b/src/readBranches.ts @@ -113,14 +113,13 @@ export async function* readBranches( refUpdateRule, target: { oid, - author: { - date, - user: { login, organization }, - }, + author: { date, user }, }, }, } = ref; + const login = user ? user.login : null; + const organization = user ? user.organization.id : null; yield { date: Date.parse(date), branchName, diff --git a/src/removeStaleBranches.ts b/src/removeStaleBranches.ts index d820d63..2507836 100644 --- a/src/removeStaleBranches.ts +++ b/src/removeStaleBranches.ts @@ -23,7 +23,7 @@ async function processBranch( ) { console.log( "-> branch was last updated by " + - branch.username + + (branch.username || "(unknown user)") + " on " + formatISO(branch.date) ); @@ -126,7 +126,11 @@ async function planBranchAction( ); } - if (filters.authorsRegex && filters.authorsRegex.test(branch.username)) { + if ( + filters.authorsRegex && + branch.username && + filters.authorsRegex.test(branch.username) + ) { return skip(`author ${branch.username} is exempted`); } diff --git a/src/types.ts b/src/types.ts index f521b09..9e49132 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,7 @@ export type Branch = { branchName: string; prefix: string; commitId: string; - username: string; + username: string | null; isProtected: boolean; };