Skip to content

Commit

Permalink
Add branch limit
Browse files Browse the repository at this point in the history
  • Loading branch information
yqiu24 committed Nov 14, 2023
1 parent 9a3204c commit 8015385
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/cleanup.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ jobs:
with:
github_token: ${{ github.token }}
last_commit_age_days: 100
dry_run: no
dry_run: yes
ignore_branches: test_prefix/one,test_prefix/two,test_prefix_ignored/one
branch_limit: 1
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ inputs:
description: "The API base url to be used in requests to GitHub Enterprise"
required: false
default: "https://api.github.com"
branch_limit:
description: "The max number of branches that can be deleted"
required: false
default: "100"

outputs:
deleted_branches: # id of output
Expand Down
1 change: 1 addition & 0 deletions src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def run_action(options: Options) -> list:
last_commit_age_days=options.last_commit_age_days,
ignore_branches=options.ignore_branches,
allowed_prefixes=options.allowed_prefixes
branch_limit=options.branch_limit
)

print(f"Branches queued for deletion: {branches}")
Expand Down
10 changes: 9 additions & 1 deletion src/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ def get_deletable_branches(
self,
last_commit_age_days: int,
ignore_branches: list[str],
allowed_prefixes: list[str]
allowed_prefixes: list[str],
branch_limit: int
) -> list[str]:
if branch_limit < 1:
return []

# Default branch might not be protected
default_branch = self.get_default_branch()

Expand Down Expand Up @@ -91,6 +95,10 @@ def get_deletable_branches(
print(f'Branch `{branch_name}` meets the criteria for deletion')
deletable_branches.append(branch_name)

# Exit early if we have reached our branch limit
if len(deletable_branches) == branch_limit:
return deletable_branches

# Re-request next page
current_page += 1

Expand Down

0 comments on commit 8015385

Please sign in to comment.