Skip to content

Commit b666957

Browse files
authored
Merge pull request #9 from aschwenn/enterprise-api
allow action to work with github enterprise
2 parents c50755d + 1abb436 commit b666957

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ jobs:
6464
github_token: ${{ github.token }}
6565
last_commit_age_days: 100
6666
ignore_branches: next-version,dont-deleteme
67+
github_base_url: https://github.mycompany.com/api/v3
6768

6869
# Disable dry run and actually get stuff deleted
6970
dry_run: no

action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ inputs:
2222
github_token:
2323
description: "The github token to use on requests to the github api"
2424
required: true
25+
github_base_url:
26+
description: "The API base url to be used in requests to GitHub Enterprise"
27+
required: false
28+
default: "https://api.github.com"
2529

2630
outputs:
2731
deleted_branches: # id of output
@@ -35,3 +39,4 @@ runs:
3539
- ${{ inputs.last_commit_age_days }}
3640
- ${{ inputs.dry_run }}
3741
- ${{ inputs.github_token }}
42+
- ${{ inputs.github_base_url }}

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
from src import actions, io
22

33
if __name__ == '__main__':
4-
ignore_branches, last_commit_age_days, dry_run, github_token, github_repo = io.parse_input()
4+
ignore_branches, last_commit_age_days, dry_run, github_token, github_repo, github_base_url = io.parse_input()
55

66
deleted_branches = actions.run_action(
77
ignore_branches=ignore_branches,
88
last_commit_age_days=last_commit_age_days,
99
dry_run=dry_run,
1010
github_repo=github_repo,
1111
github_token=github_token,
12+
github_base_url=github_base_url
1213
)
1314

1415
io.format_output({'deleted_branches': deleted_branches})

src/actions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ def run_action(
66
ignore_branches: list,
77
last_commit_age_days: int,
88
github_token: str,
9+
github_base_url: str = 'https://api.github.com',
910
dry_run: bool = True
1011
) -> list:
1112
input_data = {
1213
'github_repo': github_repo,
1314
'ignore_branches': ignore_branches,
1415
'last_commit_age_days': last_commit_age_days,
1516
'dry_run': dry_run,
17+
'github_base_url': github_base_url
1618
}
1719

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

20-
github = Github(github_repo=github_repo, github_token=github_token)
22+
github = Github(github_repo=github_repo, github_token=github_token, github_base_url=github_base_url)
2123

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

src/github.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
from src import requests
44

5-
GH_BASE_URL = "https://api.github.com"
6-
7-
85
class Github:
9-
def __init__(self, github_repo: str, github_token: str):
6+
def __init__(self, github_repo: str, github_token: str, github_base_url: str):
107
self.github_token = github_token
118
self.github_repo = github_repo
9+
self.github_base_url = github_base_url
1210

1311
def make_headers(self) -> dict:
1412
return {
@@ -17,7 +15,7 @@ def make_headers(self) -> dict:
1715
}
1816

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

2220
def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: list) -> list:
2321
# Default branch might not be protected
@@ -91,7 +89,7 @@ def get_deletable_branches(self, last_commit_age_days: int, ignore_branches: lis
9189
def delete_branches(self, branches: list) -> None:
9290
for branch in branches:
9391
print(f'Deleting branch `{branch}`...')
94-
url = f'{GH_BASE_URL}/repos/{self.github_repo}/git/refs/heads/{branch}'
92+
url = f'{self.github_base_url}/repos/{self.github_repo}/git/refs/heads/{branch}'
9593

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

103101
def get_default_branch(self) -> str:
104-
url = f'{GH_BASE_URL}/repos/{self.github_repo}'
102+
url = f'{self.github_base_url}/repos/{self.github_repo}'
105103
headers = self.make_headers()
106104

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

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

src/io.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
def parse_input() -> (list, int, bool, str, str):
77
args: List[str] = sys.argv
88

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

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

2626
github_repo = getenv('GITHUB_REPOSITORY')
2727

28-
return ignore_branches, last_commit_age_days, dry_run, github_token, github_repo
28+
github_base_url = args[5]
29+
30+
return ignore_branches, last_commit_age_days, dry_run, github_token, github_repo, github_base_url
2931

3032

3133
def format_output(output_strings: dict) -> None:

0 commit comments

Comments
 (0)