1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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 <felix@fontein.de>

* Update plugins/modules/source_control/github/github_repo.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Max Bidlingmaier <Max-Florian.Bidlingmaier@sap.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Max Bidlingmaier 2021-07-22 16:55:09 +02:00 committed by GitHub
parent 11cdb1b661
commit 38e70ae0e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 10 deletions

View file

@ -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).

View file

@ -66,6 +66,12 @@ options:
- When I(state) is C(present), the repository will be created in the current user profile. - When I(state) is C(present), the repository will be created in the current user profile.
type: str type: str
required: false 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: requirements:
- PyGithub>=1.54 - PyGithub>=1.54
notes: notes:
@ -119,11 +125,14 @@ except Exception:
HAS_GITHUB_PACKAGE = False 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: 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: 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): 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): def run_module(params, check_mode=False):
gh = authenticate( 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": if params['state'] == "absent":
return delete_repo( return delete_repo(
gh=gh, gh=gh,
@ -216,6 +226,7 @@ def main():
organization=dict(type='str', required=False, default=None), organization=dict(type='str', required=False, default=None),
private=dict(type='bool', required=False, default=False), private=dict(type='bool', required=False, default=False),
description=dict(type='str', required=False, default=''), description=dict(type='str', required=False, default=''),
api_url=dict(type='str', required=False, default='https://api.github.com'),
) )
module = AnsibleModule( module = AnsibleModule(
argument_spec=module_args, argument_spec=module_args,

View file

@ -159,7 +159,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": False, "private": False,
"state": "present" "state": "present",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], True) self.assertEqual(result['changed'], True)
@ -177,7 +178,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": True, "private": True,
"state": "present" "state": "present",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], True) self.assertEqual(result['changed'], True)
self.assertEqual(result['repo']['private'], True) self.assertEqual(result['repo']['private'], True)
@ -194,7 +196,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": True, "private": True,
"state": "present" "state": "present",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], True) self.assertEqual(result['changed'], True)
self.assertEqual(result['repo']['private'], True) self.assertEqual(result['repo']['private'], True)
@ -211,7 +214,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": False, "private": False,
"state": "absent" "state": "absent",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], True) self.assertEqual(result['changed'], True)
@ -227,7 +231,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": False, "private": False,
"state": "absent" "state": "absent",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], True) self.assertEqual(result['changed'], True)
@ -243,7 +248,8 @@ class TestGithubRepo(unittest.TestCase):
"name": "myrepo", "name": "myrepo",
"description": "Just for fun", "description": "Just for fun",
"private": True, "private": True,
"state": "absent" "state": "absent",
"api_url": "https://api.github.com"
}) })
self.assertEqual(result['changed'], False) self.assertEqual(result['changed'], False)