Skip to content

Commit

Permalink
Consider branch deletion protection rule (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
invakid404 authored Sep 3, 2021
1 parent e7220c3 commit d210980
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
34 changes: 23 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
34 changes: 23 additions & 11 deletions lib/merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d210980

Please sign in to comment.