| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright (c) 2018, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: github_webhook |
| 11 |
short_description: Manage GitHub webhooks |
| 12 |
description: |
| 13 |
- Create and delete GitHub webhooks. |
| 14 |
requirements: |
| 15 |
- "PyGithub >= 1.3.5" |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
attributes: |
| 19 |
check_mode: |
| 20 |
support: none |
| 21 |
diff_mode: |
| 22 |
support: none |
| 23 |
options: |
| 24 |
repository: |
| 25 |
description: |
| 26 |
- Full name of the repository to configure a hook for. |
| 27 |
type: str |
| 28 |
required: true |
| 29 |
aliases: |
| 30 |
- repo |
| 31 |
url: |
| 32 |
description: |
| 33 |
- URL to which payloads are delivered. |
| 34 |
type: str |
| 35 |
required: true |
| 36 |
content_type: |
| 37 |
description: |
| 38 |
- The media type used to serialize the payloads. |
| 39 |
type: str |
| 40 |
choices: [form, json] |
| 41 |
default: form |
| 42 |
secret: |
| 43 |
description: |
| 44 |
- The shared secret between GitHub and the payload URL. |
| 45 |
type: str |
| 46 |
insecure_ssl: |
| 47 |
description: |
| 48 |
- Flag to indicate that GitHub should skip SSL verification when calling the hook. |
| 49 |
type: bool |
| 50 |
default: false |
| 51 |
events: |
| 52 |
description: |
| 53 |
- A list of GitHub events the hook is triggered for. Events are listed at U(https://developer.github.com/v3/activity/events/types/). |
| 54 |
Required unless O(state=absent). |
| 55 |
type: list |
| 56 |
elements: str |
| 57 |
active: |
| 58 |
description: |
| 59 |
- Whether or not the hook is active. |
| 60 |
type: bool |
| 61 |
default: true |
| 62 |
state: |
| 63 |
description: |
| 64 |
- Whether the hook should be present or absent. |
| 65 |
type: str |
| 66 |
choices: [absent, present] |
| 67 |
default: present |
| 68 |
user: |
| 69 |
description: |
| 70 |
- User to authenticate to GitHub as. |
| 71 |
type: str |
| 72 |
required: true |
| 73 |
password: |
| 74 |
description: |
| 75 |
- Password to authenticate to GitHub with. |
| 76 |
type: str |
| 77 |
token: |
| 78 |
description: |
| 79 |
- Token to authenticate to GitHub with. |
| 80 |
type: str |
| 81 |
github_url: |
| 82 |
description: |
| 83 |
- Base URL of the GitHub API. |
| 84 |
type: str |
| 85 |
default: https://api.github.com |
| 86 |
|
| 87 |
author: |
| 88 |
- "Chris St. Pierre (@stpierre)" |
| 89 |
""" |
| 90 |
|
| 91 |
EXAMPLES = r""" |
| 92 |
- name: Create a new webhook that triggers on push (password auth) |
| 93 |
community.general.github_webhook: |
| 94 |
repository: ansible/ansible |
| 95 |
url: https://www.example.com/hooks/ |
| 96 |
events: |
| 97 |
- push |
| 98 |
user: "{{ github_user }}" |
| 99 |
password: "{{ github_password }}" |
| 100 |
|
| 101 |
- name: Create a new webhook in a github enterprise installation with multiple event triggers (token auth) |
| 102 |
community.general.github_webhook: |
| 103 |
repository: myorg/myrepo |
| 104 |
url: https://jenkins.example.com/ghprbhook/ |
| 105 |
content_type: json |
| 106 |
secret: "{{ github_shared_secret }}" |
| 107 |
insecure_ssl: true |
| 108 |
events: |
| 109 |
- issue_comment |
| 110 |
- pull_request |
| 111 |
user: "{{ github_user }}" |
| 112 |
token: "{{ github_user_api_token }}" |
| 113 |
github_url: https://github.example.com |
| 114 |
|
| 115 |
- name: Delete a webhook (password auth) |
| 116 |
community.general.github_webhook: |
| 117 |
repository: ansible/ansible |
| 118 |
url: https://www.example.com/hooks/ |
| 119 |
state: absent |
| 120 |
user: "{{ github_user }}" |
| 121 |
password: "{{ github_password }}" |
| 122 |
""" |
| 123 |
|
| 124 |
RETURN = r""" |
| 125 |
hook_id: |
| 126 |
description: The GitHub ID of the hook created/updated. |
| 127 |
returned: when state is 'present' |
| 128 |
type: int |
| 129 |
sample: 6206 |
| 130 |
""" |
| 131 |
|
| 132 |
import traceback |
| 133 |
|
| 134 |
GITHUB_IMP_ERR = None |
| 135 |
try: |
| 136 |
import github |
| 137 |
|
| 138 |
HAS_GITHUB = True |
| 139 |
except ImportError: |
| 140 |
GITHUB_IMP_ERR = traceback.format_exc() |
| 141 |
HAS_GITHUB = False |
| 142 |
|
| 143 |
from ansible.module_utils.basic import AnsibleModule, missing_required_lib |
| 144 |
|
| 145 |
|
| 146 |
def _create_hook_config(module): |
| 147 |
hook_config = { |
| 148 |
"url": module.params["url"], |
| 149 |
"content_type": module.params["content_type"], |
| 150 |
"insecure_ssl": "1" if module.params["insecure_ssl"] else "0", |
| 151 |
} |
| 152 |
|
| 153 |
secret = module.params.get("secret") |
| 154 |
if secret: |
| 155 |
hook_config["secret"] = secret |
| 156 |
|
| 157 |
return hook_config |
| 158 |
|
| 159 |
|
| 160 |
def create_hook(repo, module): |
| 161 |
config = _create_hook_config(module) |
| 162 |
try: |
| 163 |
hook = repo.create_hook( |
| 164 |
name="web", config=config, events=module.params["events"], active=module.params["active"] |
| 165 |
) |
| 166 |
except github.GithubException as err: |
| 167 |
module.fail_json(msg=f"Unable to create hook for repository {repo.full_name}: {err}") |
| 168 |
|
| 169 |
data = {"hook_id": hook.id} |
| 170 |
return True, data |
| 171 |
|
| 172 |
|
| 173 |
def update_hook(repo, hook, module): |
| 174 |
config = _create_hook_config(module) |
| 175 |
try: |
| 176 |
hook.update() |
| 177 |
hook.edit(name="web", config=config, events=module.params["events"], active=module.params["active"]) |
| 178 |
|
| 179 |
changed = hook.update() |
| 180 |
except github.GithubException as err: |
| 181 |
module.fail_json(msg=f"Unable to modify hook for repository {repo.full_name}: {err}") |
| 182 |
|
| 183 |
data = {"hook_id": hook.id} |
| 184 |
return changed, data |
| 185 |
|
| 186 |
|
| 187 |
def main(): |
| 188 |
module = AnsibleModule( |
| 189 |
argument_spec=dict( |
| 190 |
repository=dict(type="str", required=True, aliases=["repo"]), |
| 191 |
url=dict(type="str", required=True), |
| 192 |
content_type=dict(type="str", choices=("json", "form"), default="form"), |
| 193 |
secret=dict(type="str", no_log=True), |
| 194 |
insecure_ssl=dict(type="bool", default=False), |
| 195 |
events=dict( |
| 196 |
type="list", |
| 197 |
elements="str", |
| 198 |
), |
| 199 |
active=dict(type="bool", default=True), |
| 200 |
state=dict(type="str", choices=("absent", "present"), default="present"), |
| 201 |
user=dict(type="str", required=True), |
| 202 |
password=dict(type="str", no_log=True), |
| 203 |
token=dict(type="str", no_log=True), |
| 204 |
github_url=dict(type="str", default="https://api.github.com"), |
| 205 |
), |
| 206 |
mutually_exclusive=(("password", "token"),), |
| 207 |
required_one_of=(("password", "token"),), |
| 208 |
required_if=(("state", "present", ("events",)),), |
| 209 |
) |
| 210 |
|
| 211 |
if not HAS_GITHUB: |
| 212 |
module.fail_json(msg=missing_required_lib("PyGithub"), exception=GITHUB_IMP_ERR) |
| 213 |
|
| 214 |
try: |
| 215 |
github_conn = github.Github( |
| 216 |
module.params["user"], |
| 217 |
module.params.get("password") or module.params.get("token"), |
| 218 |
base_url=module.params["github_url"], |
| 219 |
) |
| 220 |
except github.GithubException as err: |
| 221 |
module.fail_json(msg=f"Could not connect to GitHub at {module.params['github_url']}: {err}") |
| 222 |
|
| 223 |
try: |
| 224 |
repo = github_conn.get_repo(module.params["repository"]) |
| 225 |
except github.BadCredentialsException as err: |
| 226 |
module.fail_json(msg=f"Could not authenticate to GitHub at {module.params['github_url']}: {err}") |
| 227 |
except github.UnknownObjectException as err: |
| 228 |
module.fail_json( |
| 229 |
msg=f"Could not find repository {module.params['repository']} in GitHub at {module.params['github_url']}: {err}" |
| 230 |
) |
| 231 |
except Exception as err: |
| 232 |
module.fail_json( |
| 233 |
msg=f"Could not fetch repository {module.params['repository']} from GitHub at {module.params['github_url']}: {err}", |
| 234 |
exception=traceback.format_exc(), |
| 235 |
) |
| 236 |
|
| 237 |
hook = None |
| 238 |
try: |
| 239 |
for hook in repo.get_hooks(): |
| 240 |
if hook.config.get("url") == module.params["url"]: |
| 241 |
break |
| 242 |
else: |
| 243 |
hook = None |
| 244 |
except github.GithubException as err: |
| 245 |
module.fail_json(msg=f"Unable to get hooks from repository {module.params['repository']}: {err}") |
| 246 |
|
| 247 |
changed = False |
| 248 |
data = {} |
| 249 |
if hook is None and module.params["state"] == "present": |
| 250 |
changed, data = create_hook(repo, module) |
| 251 |
elif hook is not None and module.params["state"] == "absent": |
| 252 |
try: |
| 253 |
hook.delete() |
| 254 |
except github.GithubException as err: |
| 255 |
module.fail_json(msg=f"Unable to delete hook from repository {repo.full_name}: {err}") |
| 256 |
else: |
| 257 |
changed = True |
| 258 |
elif hook is not None and module.params["state"] == "present": |
| 259 |
changed, data = update_hook(repo, hook, module) |
| 260 |
# else, there is no hook and we want there to be no hook |
| 261 |
|
| 262 |
module.exit_json(changed=changed, **data) |
| 263 |
|
| 264 |
|
| 265 |
if __name__ == "__main__": |
| 266 |
main() |