Skip to content

Commit 70b3d84

Browse files
committed
Tweak github.get_deletable_branches to ensure we cycle over ALL possible results, since we're being paginated
1 parent c9bc9b1 commit 70b3d84

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

src/github.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ def make_headers(self) -> dict:
1616
'content-type': 'application/vnd.github.v3+json',
1717
}
1818

19+
def get_paginated_branches_url(self, page: int = 0):
20+
return f'{GH_BASE_URL}/repos/{self.github_repo}/branches?per_page=30&page={page}'
21+
1922
def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: list) -> list:
2023
# Default branch might not be protected
2124
default_branch = self.get_default_branch()
2225

23-
url = f'{GH_BASE_URL}/repos/{self.github_repo}/branches'
26+
url = self.get_paginated_branches_url()
2427
headers = self.make_headers()
2528

2629
response = requests.get(url=url, headers=headers)
@@ -29,44 +32,53 @@ def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: lis
2932

3033
deletable_branches = []
3134
branch: dict
32-
for branch in response.json():
33-
branch_name = branch.get('name')
35+
branches: list = response.json()
36+
current_page = 0
37+
38+
while len(branches) > 0:
39+
for branch in response.json():
40+
branch_name = branch.get('name')
41+
42+
commit_hash = branch.get('commit', {}).get('sha')
43+
commit_url = branch.get('commit', {}).get('url')
3444

35-
commit_hash = branch.get('commit', {}).get('sha')
36-
commit_url = branch.get('commit', {}).get('url')
45+
print(f'Analyzing branch `{branch_name}`...')
3746

38-
print(f'Analyzing branch `{branch_name}`...')
47+
# Immediately discard protected branches, default branch and ignored branches
48+
if branch_name == default_branch:
49+
print(f'Ignoring `{branch_name}` because it is the default branch')
50+
continue
3951

40-
# Immediately discard protected branches, default branch and ignored branches
41-
if branch_name == default_branch:
42-
print(f'Ignoring branch `{branch_name}` because it is the default branch')
43-
continue
52+
if branch.get('protected') is True:
53+
print(f'Ignoring `{branch_name}` because it is protected')
54+
continue
4455

45-
if branch.get('protected') is True:
46-
print(f'Ignoring branch `{branch_name}` because it is protected')
47-
continue
56+
if branch_name in ignore_branches:
57+
print(f'Ignoring `{branch_name}` because it is on the list of ignored branches')
58+
continue
4859

49-
if branch_name in ignore_branches:
50-
print(f'Ignoring branch `{branch_name}` because it is on the list of ignored branches')
51-
continue
60+
# Move on if commit is in an open pull request
61+
if self.has_open_pulls(commit_hash=commit_hash):
62+
print(f'Ignoring `{branch_name}` because it has open pulls')
63+
continue
5264

53-
# Move on if commit is in an open pull request
54-
if self.has_open_pulls(commit_hash=commit_hash):
55-
print(f'Ignoring branch `{branch_name}` because it has open pulls')
56-
continue
65+
# Move on if branch is base for a pull request
66+
if self.is_pull_request_base(branch=branch_name):
67+
print(f'Ignoring `{branch_name}` because it is the base for a pull request of another branch')
68+
continue
5769

58-
# Move on if branch is base for a pull request
59-
if self.is_pull_request_base(branch=branch_name):
60-
print(f'Ignoring branch `{branch_name}` because it is the base for a pull request of another branch')
61-
continue
70+
# Move on if last commit is newer than last_commit_age_days
71+
if self.is_commit_older_than(commit_url=commit_url, older_than_days=last_commit_age_days) is False:
72+
print(f'Ignoring `{branch_name}` because last commit is newer than {last_commit_age_days} days')
73+
continue
6274

63-
# Move on if last commit is newer than last_commit_age_days
64-
if self.is_commit_older_than(commit_url=commit_url, older_than_days=last_commit_age_days) is False:
65-
print(f'Ignoring branch `{branch_name}` because last commit is newer than {last_commit_age_days} days')
66-
continue
75+
print(f'Branch `{branch_name}` meets the criteria for deletion')
76+
deletable_branches.append(branch_name)
6777

68-
print(f'Branch `{branch_name}` meets the criteria for deletion')
69-
deletable_branches.append(branch_name)
78+
# Re-request next page
79+
current_page += 1
80+
response = requests.get(url=self.get_paginated_branches_url(page=current_page), headers=headers)
81+
branches = response.json()
7082

7183
return deletable_branches
7284

0 commit comments

Comments
 (0)