From 38e70ae0e4c6086c5470583ee4fc5c7e7684284d Mon Sep 17 00:00:00 2001 From: Max Bidlingmaier Date: Thu, 22 Jul 2021 16:55:09 +0200 Subject: [PATCH] github_repo: support GitHub on premise installations (#3039) * added possibility to manage on prem github * added changelog * fixed module tests * Update changelogs/fragments/3038-enhance_github_repo_api_url.yml Co-authored-by: Felix Fontein * Update plugins/modules/source_control/github/github_repo.py Co-authored-by: Felix Fontein Co-authored-by: Max Bidlingmaier Co-authored-by: Felix Fontein --- .../3038-enhance_github_repo_api_url.yml | 2 ++ .../source_control/github/github_repo.py | 19 +++++++++++++++---- .../source_control/github/test_github_repo.py | 18 ++++++++++++------ 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/3038-enhance_github_repo_api_url.yml diff --git a/changelogs/fragments/3038-enhance_github_repo_api_url.yml b/changelogs/fragments/3038-enhance_github_repo_api_url.yml new file mode 100644 index 0000000000..19eda0f66d --- /dev/null +++ b/changelogs/fragments/3038-enhance_github_repo_api_url.yml @@ -0,0 +1,2 @@ +minor_changes: + - github_repo - add new option ``api_url`` to allow working with on premises installations (https://github.com/ansible-collections/community.general/pull/3038). diff --git a/plugins/modules/source_control/github/github_repo.py b/plugins/modules/source_control/github/github_repo.py index 587111fe5a..b5403c6a8d 100644 --- a/plugins/modules/source_control/github/github_repo.py +++ b/plugins/modules/source_control/github/github_repo.py @@ -66,6 +66,12 @@ options: - When I(state) is C(present), the repository will be created in the current user profile. type: str required: false + api_url: + description: + - URL to the GitHub API if not using github.com but you own instance. + type: str + default: 'https://api.github.com' + version_added: "3.5.0" requirements: - PyGithub>=1.54 notes: @@ -119,11 +125,14 @@ except Exception: HAS_GITHUB_PACKAGE = False -def authenticate(username=None, password=None, access_token=None): +def authenticate(username=None, password=None, access_token=None, api_url=None): + if not api_url: + return None + if access_token: - return Github(base_url="https://api.github.com", login_or_token=access_token) + return Github(base_url=api_url, login_or_token=access_token) else: - return Github(base_url="https://api.github.com", login_or_token=username, password=password) + return Github(base_url=api_url, login_or_token=username, password=password) def create_repo(gh, name, organization=None, private=False, description='', check_mode=False): @@ -185,7 +194,8 @@ def delete_repo(gh, name, organization=None, check_mode=False): def run_module(params, check_mode=False): gh = authenticate( - username=params['username'], password=params['password'], access_token=params['access_token']) + username=params['username'], password=params['password'], access_token=params['access_token'], + api_url=params['api_url']) if params['state'] == "absent": return delete_repo( gh=gh, @@ -216,6 +226,7 @@ def main(): organization=dict(type='str', required=False, default=None), private=dict(type='bool', required=False, default=False), description=dict(type='str', required=False, default=''), + api_url=dict(type='str', required=False, default='https://api.github.com'), ) module = AnsibleModule( argument_spec=module_args, diff --git a/tests/unit/plugins/modules/source_control/github/test_github_repo.py b/tests/unit/plugins/modules/source_control/github/test_github_repo.py index 56ec9b7ec7..b3e4f9027f 100644 --- a/tests/unit/plugins/modules/source_control/github/test_github_repo.py +++ b/tests/unit/plugins/modules/source_control/github/test_github_repo.py @@ -159,7 +159,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": False, - "state": "present" + "state": "present", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], True) @@ -177,7 +178,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": True, - "state": "present" + "state": "present", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], True) self.assertEqual(result['repo']['private'], True) @@ -194,7 +196,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": True, - "state": "present" + "state": "present", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], True) self.assertEqual(result['repo']['private'], True) @@ -211,7 +214,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": False, - "state": "absent" + "state": "absent", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], True) @@ -227,7 +231,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": False, - "state": "absent" + "state": "absent", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], True) @@ -243,7 +248,8 @@ class TestGithubRepo(unittest.TestCase): "name": "myrepo", "description": "Just for fun", "private": True, - "state": "absent" + "state": "absent", + "api_url": "https://api.github.com" }) self.assertEqual(result['changed'], False)