Skip to content

Commit

Permalink
Merge pull request #7 from phpdocker-io/retrieve-all-branches
Browse files Browse the repository at this point in the history
Fix retrieval of deletable branches where there are more than the default per_page.
  • Loading branch information
luispabon authored Jun 30, 2021
2 parents c9bc9b1 + a02c341 commit 9dd2b8c
Showing 1 changed file with 48 additions and 30 deletions.
78 changes: 48 additions & 30 deletions src/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ def make_headers(self) -> dict:
'content-type': 'application/vnd.github.v3+json',
}

def get_paginated_branches_url(self, page: int = 0) -> str:
return f'{GH_BASE_URL}/repos/{self.github_repo}/branches?protected=false&per_page=30&page={page}'

def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: list) -> list:
# Default branch might not be protected
default_branch = self.get_default_branch()

url = f'{GH_BASE_URL}/repos/{self.github_repo}/branches'
url = self.get_paginated_branches_url()
headers = self.make_headers()

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

deletable_branches = []
branch: dict
for branch in response.json():
branch_name = branch.get('name')
branches: list = response.json()
current_page = 0

while len(branches) > 0:
for branch in branches:
branch_name = branch.get('name')

commit_hash = branch.get('commit', {}).get('sha')
commit_url = branch.get('commit', {}).get('url')

print(f'Analyzing branch `{branch_name}`...')

# Immediately discard protected branches, default branch and ignored branches
if branch_name == default_branch:
print(f'Ignoring `{branch_name}` because it is the default branch')
continue

commit_hash = branch.get('commit', {}).get('sha')
commit_url = branch.get('commit', {}).get('url')
# We're already retrieving non-protected branches from the API, but it pays being careful when dealing
# with third party apis
if branch.get('protected') is True:
print(f'Ignoring `{branch_name}` because it is protected')
continue

print(f'Analyzing branch `{branch_name}`...')
if branch_name in ignore_branches:
print(f'Ignoring `{branch_name}` because it is on the list of ignored branches')
continue

# Immediately discard protected branches, default branch and ignored branches
if branch_name == default_branch:
print(f'Ignoring branch `{branch_name}` because it is the default branch')
continue
# Move on if commit is in an open pull request
if self.has_open_pulls(commit_hash=commit_hash):
print(f'Ignoring `{branch_name}` because it has open pulls')
continue

if branch.get('protected') is True:
print(f'Ignoring branch `{branch_name}` because it is protected')
continue
# Move on if branch is base for a pull request
if self.is_pull_request_base(branch=branch_name):
print(f'Ignoring `{branch_name}` because it is the base for a pull request of another branch')
continue

if branch_name in ignore_branches:
print(f'Ignoring branch `{branch_name}` because it is on the list of ignored branches')
continue
# Move on if last commit is newer than last_commit_age_days
if self.is_commit_older_than(commit_url=commit_url, older_than_days=last_commit_age_days) is False:
print(f'Ignoring `{branch_name}` because last commit is newer than {last_commit_age_days} days')
continue

# Move on if commit is in an open pull request
if self.has_open_pulls(commit_hash=commit_hash):
print(f'Ignoring branch `{branch_name}` because it has open pulls')
continue
print(f'Branch `{branch_name}` meets the criteria for deletion')
deletable_branches.append(branch_name)

# Move on if branch is base for a pull request
if self.is_pull_request_base(branch=branch_name):
print(f'Ignoring branch `{branch_name}` because it is the base for a pull request of another branch')
continue
# Re-request next page
current_page += 1

# Move on if last commit is newer than last_commit_age_days
if self.is_commit_older_than(commit_url=commit_url, older_than_days=last_commit_age_days) is False:
print(f'Ignoring branch `{branch_name}` because last commit is newer than {last_commit_age_days} days')
continue
response = requests.get(url=self.get_paginated_branches_url(page=current_page), headers=headers)
if response.status_code != 200:
raise RuntimeError(f'Failed to make request to {url}. {response} {response.json()}')

print(f'Branch `{branch_name}` meets the criteria for deletion')
deletable_branches.append(branch_name)
branches: list = response.json()

return deletable_branches

Expand Down

0 comments on commit 9dd2b8c

Please sign in to comment.