From ad7c7eaa8d3f3e913a631174c0aac16bf2d44239 Mon Sep 17 00:00:00 2001 From: Andrew Schwenn Date: Mon, 18 Jul 2022 14:40:28 -0500 Subject: [PATCH 1/3] allow action to work with github enterprise add github_base_url to action.yaml --- README.md | 1 + action.yml | 4 ++++ main.py | 3 ++- src/actions.py | 4 +++- src/github.py | 15 ++++++--------- src/io.py | 4 +++- 6 files changed, 19 insertions(+), 12 deletions(-) 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..bb284d5 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,9 @@ 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 outputs: deleted_branches: # id of output @@ -35,3 +38,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..e1baa7d 100644 --- a/src/github.py +++ b/src/github.py @@ -2,11 +2,8 @@ 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 @@ -17,7 +14,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'{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 +88,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'{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 +98,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'{github_base_url}/repos/{self.github_repo}' headers = self.make_headers() response = requests.get(url=url, headers=headers) @@ -115,7 +112,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'{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 +131,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'{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..22ffd90 100644 --- a/src/io.py +++ b/src/io.py @@ -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: From 3ae2c8160e3bfd7341f7ccd84d740a592b178a3a Mon Sep 17 00:00:00 2001 From: Andrew Schwenn Date: Tue, 19 Jul 2022 15:17:58 -0500 Subject: [PATCH 2/3] modify parse_input() --- action.yml | 1 + src/io.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/action.yml b/action.yml index bb284d5..a7fae31 100644 --- a/action.yml +++ b/action.yml @@ -25,6 +25,7 @@ inputs: 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 diff --git a/src/io.py b/src/io.py index 22ffd90..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] From 1abb436ade0ceb5ec4d7f786f9c375520e0d18c9 Mon Sep 17 00:00:00 2001 From: Andrew Schwenn Date: Tue, 19 Jul 2022 15:20:59 -0500 Subject: [PATCH 3/3] forgot how to write python --- src/github.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/github.py b/src/github.py index e1baa7d..a7d5dc1 100644 --- a/src/github.py +++ b/src/github.py @@ -6,6 +6,7 @@ class Github: 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 { @@ -14,7 +15,7 @@ def make_headers(self) -> dict: } def get_paginated_branches_url(self, page: int = 0) -> str: - return f'{github_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 @@ -88,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'{github_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: @@ -98,7 +99,7 @@ def delete_branches(self, branches: list) -> None: print(f'Branch `{branch}` DELETED!') def get_default_branch(self) -> str: - url = f'{github_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) @@ -112,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'{github_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' @@ -131,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'{github_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'