Skip to content

Commit

Permalink
feat: Delete old merged branches
Browse files Browse the repository at this point in the history
  • Loading branch information
fast-facts committed Sep 21, 2022
1 parent 8c6ec2b commit abadc6a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/github.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,37 @@ export class GithubService {
return res.data.filter(pr => pr.head.ref === head)[0]?.number;
}

public async getClosedPRsBranches(base: string, title: string, branchSuffix: string): Promise<string[]> {

public async getClosedPRsBranches(base: string, title: string, branchPrefix: string): Promise<string[]> {
const res = await this.gbClient.rest.pulls.list({
owner: this.owner,
repo: this.repo,
state: 'closed',
base
});

return res.data//
.filter(pr => !pr.locked)//
.filter(pr => !pr.merged_at)//
.filter(pr => pr.head.ref.indexOf(branchSuffix) > 0 || pr.title === title)//
return res.data
.filter(pr => !pr.locked)
.filter(pr => pr.head.ref.indexOf(branchPrefix) >= 0 || pr.title === title)
.map(pr => pr.head.ref);
}

public async deleteClosedPRsBranches(base: string, title: string, branchSuffix: string): Promise<void> {
const branches = await this.getClosedPRsBranches(base, title, branchSuffix);
for (const branch of Object.keys(branches)) {
const res = await this.gbClient.rest.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: branch
});
if (res.status === 204)
core.info(`🤖 >> Branch '${branch}' has been deleted`);
else if (res.status !== 422) // 422 = branch already gone
core.warning(`🤖 >> Branch '${branch}' could not be deleted. Status was: ${res.status}`);
public async deleteClosedPRsBranches(base: string, title: string, branchPrefix: string): Promise<void> {
const branches = await this.getClosedPRsBranches(base, title, branchPrefix);
for (const branch of branches) {
try {
const res = await this.gbClient.rest.git.deleteRef({
owner: this.owner,
repo: this.repo,
ref: `heads/${branch}`
});
if (res.status === 204)
core.info(`🤖 >> Branch '${branch}' has been deleted`);
else if (res.status !== 422) // 422 = branch already gone
core.warning(`🤖 >> Branch '${branch}' could not be deleted. Status was: ${res.status}`);
}
catch (ex: unknown) {
core.warning(`🤖 >> Branch '${branch}' could not be deleted. Error was: ${(ex as Error).message}`);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void (async () => {
const deleteClosedPRBranches = core.getInput('delete-closed-pr-branches') === 'true';
if (deleteClosedPRBranches) {
await core.group('🤖 Deleting branches related to closed PRs created by this action...', async () => {
await gbService.deleteClosedPRsBranches(baseBranch, prBranchPrefix, prTitle);
await gbService.deleteClosedPRsBranches(baseBranch, prTitle, prBranchPrefix);
});
}
} catch (ex: any) {
Expand Down

0 comments on commit abadc6a

Please sign in to comment.