Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ jobs:
github_token: ${{ github.token }}
last_commit_age_days: 100
ignore_branches: next-version,dont-deleteme
github_base_url: https://github.mycompany.com/api/v3

# Disable dry run and actually get stuff deleted
dry_run: no
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ inputs:
github_token:
description: "The github token to use on requests to the github api"
required: true
github_base_url:
description: "The API base url to be used in requests to GitHub Enterprise"
required: false
default: "https://api.github.com"

outputs:
deleted_branches: # id of output
Expand All @@ -35,3 +39,4 @@ runs:
- ${{ inputs.last_commit_age_days }}
- ${{ inputs.dry_run }}
- ${{ inputs.github_token }}
- ${{ inputs.github_base_url }}
3 changes: 2 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from src import actions, io

if __name__ == '__main__':
ignore_branches, last_commit_age_days, dry_run, github_token, github_repo = io.parse_input()
ignore_branches, last_commit_age_days, dry_run, github_token, github_repo, github_base_url = io.parse_input()

deleted_branches = actions.run_action(
ignore_branches=ignore_branches,
last_commit_age_days=last_commit_age_days,
dry_run=dry_run,
github_repo=github_repo,
github_token=github_token,
github_base_url=github_base_url
)

io.format_output({'deleted_branches': deleted_branches})
4 changes: 3 additions & 1 deletion src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ def run_action(
ignore_branches: list,
last_commit_age_days: int,
github_token: str,
github_base_url: str = 'https://api.github.com',
dry_run: bool = True
) -> list:
input_data = {
'github_repo': github_repo,
'ignore_branches': ignore_branches,
'last_commit_age_days': last_commit_age_days,
'dry_run': dry_run,
'github_base_url': github_base_url
}

print(f"Starting github action to cleanup old branches. Input: {input_data}")

github = Github(github_repo=github_repo, github_token=github_token)
github = Github(github_repo=github_repo, github_token=github_token, github_base_url=github_base_url)

branches = github.get_deletable_branches(last_commit_age_days=last_commit_age_days, ignore_branches=ignore_branches)

Expand Down
16 changes: 7 additions & 9 deletions src/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

from src import requests

GH_BASE_URL = "https://api.github.com"


class Github:
def __init__(self, github_repo: str, github_token: str):
def __init__(self, github_repo: str, github_token: str, github_base_url: str):
self.github_token = github_token
self.github_repo = github_repo
self.github_base_url = github_base_url

def make_headers(self) -> dict:
return {
Expand All @@ -17,7 +15,7 @@ def make_headers(self) -> dict:
}

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}'
return f'{self.github_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
Expand Down Expand Up @@ -91,7 +89,7 @@ def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: lis
def delete_branches(self, branches: list) -> None:
for branch in branches:
print(f'Deleting branch `{branch}`...')
url = f'{GH_BASE_URL}/repos/{self.github_repo}/git/refs/heads/{branch}'
url = f'{self.github_base_url}/repos/{self.github_repo}/git/refs/heads/{branch}'

response = requests.request(method='DELETE', url=url, headers=self.make_headers())
if response.status_code != 204:
Expand All @@ -101,7 +99,7 @@ def delete_branches(self, branches: list) -> None:
print(f'Branch `{branch}` DELETED!')

def get_default_branch(self) -> str:
url = f'{GH_BASE_URL}/repos/{self.github_repo}'
url = f'{self.github_base_url}/repos/{self.github_repo}'
headers = self.make_headers()

response = requests.get(url=url, headers=headers)
Expand All @@ -115,7 +113,7 @@ def has_open_pulls(self, commit_hash: str) -> bool:
"""
Returns true if commit is part of an open pull request or the branch is the base for a pull request
"""
url = f'{GH_BASE_URL}/repos/{self.github_repo}/commits/{commit_hash}/pulls'
url = f'{self.github_base_url}/repos/{self.github_repo}/commits/{commit_hash}/pulls'
headers = self.make_headers()
headers['accept'] = 'application/vnd.github.groot-preview+json'

Expand All @@ -134,7 +132,7 @@ def is_pull_request_base(self, branch: str) -> bool:
"""
Returns true if the given branch is base for another pull request.
"""
url = f'{GH_BASE_URL}/repos/{self.github_repo}/pulls?base={branch}'
url = f'{self.github_base_url}/repos/{self.github_repo}/pulls?base={branch}'
headers = self.make_headers()
headers['accept'] = 'application/vnd.github.groot-preview+json'

Expand Down
8 changes: 5 additions & 3 deletions src/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
def parse_input() -> (list, int, bool, str, str):
args: List[str] = sys.argv

if len(args) != 5:
if len(args) != 6:
input_string = ' '.join(args)
expected_string = f'{args[0]} ignore_branches last_commit_age_days dry_run_yes_no'
expected_string = f'{args[0]} ignore_branches last_commit_age_days dry_run_yes_no github_token github_repo github_base_url'
raise RuntimeError(f'Incorrect input: {input_string}. Expected: {expected_string}')

branches_raw: str = args[1]
Expand All @@ -25,7 +25,9 @@ def parse_input() -> (list, int, bool, str, str):

github_repo = getenv('GITHUB_REPOSITORY')

return ignore_branches, last_commit_age_days, dry_run, github_token, github_repo
github_base_url = args[5]

return ignore_branches, last_commit_age_days, dry_run, github_token, github_repo, github_base_url


def format_output(output_strings: dict) -> None:
Expand Down