diff --git a/README.md b/README.md index fbc94f9..4d6efa2 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/action.yml b/action.yml index c806e48..a7fae31 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -35,3 +39,4 @@ runs: - ${{ inputs.last_commit_age_days }} - ${{ inputs.dry_run }} - ${{ inputs.github_token }} + - ${{ inputs.github_base_url }} diff --git a/main.py b/main.py index 2f31651..e20146b 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ 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, @@ -9,6 +9,7 @@ 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}) diff --git a/src/actions.py b/src/actions.py index 7c2115d..e8de6fb 100644 --- a/src/actions.py +++ b/src/actions.py @@ -6,6 +6,7 @@ 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 = { @@ -13,11 +14,12 @@ def run_action( '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) diff --git a/src/github.py b/src/github.py index d54d014..a7d5dc1 100644 --- a/src/github.py +++ b/src/github.py @@ -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 { @@ -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 @@ -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: @@ -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) @@ -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' @@ -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' diff --git a/src/io.py b/src/io.py index 05327f3..bf52e9a 100644 --- a/src/io.py +++ b/src/io.py @@ -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] @@ -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: