From d21098064a2482c3f26387df2d2ec40897584b4b Mon Sep 17 00:00:00 2001 From: Tsvetomir Bonev Date: Fri, 3 Sep 2021 22:32:29 +0300 Subject: [PATCH] Consider branch deletion protection rule (#162) --- dist/index.js | 34 +++++++++++++++++++++++----------- lib/merge.js | 34 +++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/dist/index.js b/dist/index.js index 220544a9..38669013 100755 --- a/dist/index.js +++ b/dist/index.js @@ -1051,26 +1051,38 @@ async function deleteBranch(octokit, pullRequest) { return; } - const { data: branch } = await octokit.repos.getBranch({ + const branchQuery = { owner: pullRequest.head.repo.owner.login, repo: pullRequest.head.repo.name, branch: pullRequest.head.ref - }); + }; + + const { data: branch } = await octokit.repos.getBranch(branchQuery); logger.trace("Branch:", branch); if (branch.protected) { - logger.info("Branch is protected and cannot be deleted:", branch.name); - } else { - logger.debug("Deleting branch", branch.name, "..."); - await octokit.git.deleteRef({ - owner: pullRequest.head.repo.owner.login, - repo: pullRequest.head.repo.name, - ref: `heads/${branch.name}` - }); + const { data: protectionRules } = await octokit.repos.getBranchProtection( + branchQuery + ); - logger.info("Merged branch has been deleted:", branch.name); + if ( + protectionRules.allow_deletions && + !protectionRules.allow_deletions.enabled + ) { + logger.info("Branch is protected and cannot be deleted:", branch.name); + return; + } } + + logger.debug("Deleting branch", branch.name, "..."); + await octokit.git.deleteRef({ + owner: pullRequest.head.repo.owner.login, + repo: pullRequest.head.repo.name, + ref: `heads/${branch.name}` + }); + + logger.info("Merged branch has been deleted:", branch.name); } function skipPullRequest(context, pullRequest, approvalCount) { diff --git a/lib/merge.js b/lib/merge.js index 8f810725..e386736e 100644 --- a/lib/merge.js +++ b/lib/merge.js @@ -133,26 +133,38 @@ async function deleteBranch(octokit, pullRequest) { return; } - const { data: branch } = await octokit.repos.getBranch({ + const branchQuery = { owner: pullRequest.head.repo.owner.login, repo: pullRequest.head.repo.name, branch: pullRequest.head.ref - }); + }; + + const { data: branch } = await octokit.repos.getBranch(branchQuery); logger.trace("Branch:", branch); if (branch.protected) { - logger.info("Branch is protected and cannot be deleted:", branch.name); - } else { - logger.debug("Deleting branch", branch.name, "..."); - await octokit.git.deleteRef({ - owner: pullRequest.head.repo.owner.login, - repo: pullRequest.head.repo.name, - ref: `heads/${branch.name}` - }); + const { data: protectionRules } = await octokit.repos.getBranchProtection( + branchQuery + ); - logger.info("Merged branch has been deleted:", branch.name); + if ( + protectionRules.allow_deletions && + !protectionRules.allow_deletions.enabled + ) { + logger.info("Branch is protected and cannot be deleted:", branch.name); + return; + } } + + logger.debug("Deleting branch", branch.name, "..."); + await octokit.git.deleteRef({ + owner: pullRequest.head.repo.owner.login, + repo: pullRequest.head.repo.name, + ref: `heads/${branch.name}` + }); + + logger.info("Merged branch has been deleted:", branch.name); } function skipPullRequest(context, pullRequest, approvalCount) {