Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for GitHub Enterprise to github_deploy_key module #59564

Merged
merged 1 commit into from Feb 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.10"
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