Skip to content

Commit

Permalink
Added support for GitHub Enterprise to github_deploy_key module
Browse files Browse the repository at this point in the history
  • Loading branch information
bincyber committed Dec 5, 2019
1 parent c266fc3 commit 99f34c9
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/ansible/modules/source_control/github/github_deploy_key.py
Expand Up @@ -22,6 +22,13 @@
username and password and 2-factor authentication code (OTP), OAuth2 token, or personal access token. Admin
rights on the repository are required."
options:
github_url:
description:
- The base URL of the GitHub API
required: false
type: str
version_added: "2.9"
default: https://api.github.com
owner:
description:
- The name of the individual account or organization that owns the GitHub repository.
Expand Down Expand Up @@ -122,6 +129,17 @@
username: "johndoe"
password: "supersecretpassword"
otp: 123456
# add a read-only deploy key to a repository hosted on GitHub Enterprise
- github_deploy_key:
github_url: "https://api.example.com"
owner: "janedoe"
repo: "example"
name: "new-deploy-key"
key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAwXxn7kIMNWzcDfou..."
read_only: yes
username: "janedoe"
password: "supersecretpassword"
'''

RETURN = '''
Expand Down Expand Up @@ -159,6 +177,7 @@ class GithubDeployKey(object):
def __init__(self, module):
self.module = module

self.github_url = self.module.params['github_url']
self.name = module.params['name']
self.key = module.params['key']
self.state = module.params['state']
Expand All @@ -173,7 +192,7 @@ def __init__(self, module):
def url(self):
owner = self.module.params['owner']
repo = self.module.params['repo']
return "https://api.github.com/repos/{0}/{1}/keys".format(owner, repo)
return "{0}/repos/{1}/{2}/keys".format(self.github_url, owner, repo)

@property
def headers(self):
Expand Down Expand Up @@ -249,7 +268,7 @@ def handle_error(self, method, info, key_id=None):
err = self.module.from_json(body)['message']

if status_code == 401:
self.module.fail_json(msg="Failed to connect to github.com due to invalid credentials", http_status_code=status_code, error=err)
self.module.fail_json(msg="Failed to connect to {0} due to invalid credentials".format(self.github_url), http_status_code=status_code, error=err)
elif status_code == 404:
self.module.fail_json(msg="GitHub repository does not exist", http_status_code=status_code, error=err)
else:
Expand All @@ -264,6 +283,7 @@ def handle_error(self, method, info, key_id=None):
def main():
module = AnsibleModule(
argument_spec=dict(
github_url=dict(required=False, type='str', default="https://api.github.com"),
owner=dict(required=True, type='str', aliases=['account', 'organization']),
repo=dict(required=True, type='str', aliases=['repository']),
name=dict(required=True, type='str', aliases=['title', 'label']),
Expand Down

0 comments on commit 99f34c9

Please sign in to comment.