| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 4 |
# Copyright (c) 2018, Marcus Watkins <[email protected]> |
| 5 |
# Based on code: |
| 6 |
# Copyright (c) 2013, Phillip Gentry <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: gitlab_hook |
| 14 |
short_description: Manages GitLab project hooks |
| 15 |
description: |
| 16 |
- Adds, updates and removes project hook. |
| 17 |
author: |
| 18 |
- Marcus Watkins (@marwatk) |
| 19 |
- Guillaume Martinez (@Lunik) |
| 20 |
requirements: |
| 21 |
- python-gitlab python module |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._auth_basic |
| 24 |
- community.general._gitlab |
| 25 |
- community.general._attributes |
| 26 |
|
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
|
| 33 |
options: |
| 34 |
project: |
| 35 |
description: |
| 36 |
- ID or Full path of the project in the form of group/name. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
hook_url: |
| 40 |
description: |
| 41 |
- The URL that you want GitLab to post to, this is used as the primary key for updates and deletion. |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
state: |
| 45 |
description: |
| 46 |
- When V(present) the hook is updated to match the input or created if it does not exist. |
| 47 |
- When V(absent) hook is deleted if it exists. |
| 48 |
default: present |
| 49 |
type: str |
| 50 |
choices: ["present", "absent"] |
| 51 |
push_events: |
| 52 |
description: |
| 53 |
- Trigger hook on push events. |
| 54 |
type: bool |
| 55 |
default: true |
| 56 |
push_events_branch_filter: |
| 57 |
description: |
| 58 |
- Branch name of wildcard to trigger hook on push events. |
| 59 |
type: str |
| 60 |
version_added: '0.2.0' |
| 61 |
default: '' |
| 62 |
issues_events: |
| 63 |
description: |
| 64 |
- Trigger hook on issues events. |
| 65 |
type: bool |
| 66 |
default: false |
| 67 |
merge_requests_events: |
| 68 |
description: |
| 69 |
- Trigger hook on merge requests events. |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
tag_push_events: |
| 73 |
description: |
| 74 |
- Trigger hook on tag push events. |
| 75 |
type: bool |
| 76 |
default: false |
| 77 |
note_events: |
| 78 |
description: |
| 79 |
- Trigger hook on note events or when someone adds a comment. |
| 80 |
type: bool |
| 81 |
default: false |
| 82 |
job_events: |
| 83 |
description: |
| 84 |
- Trigger hook on job events. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
pipeline_events: |
| 88 |
description: |
| 89 |
- Trigger hook on pipeline events. |
| 90 |
type: bool |
| 91 |
default: false |
| 92 |
wiki_page_events: |
| 93 |
description: |
| 94 |
- Trigger hook on wiki events. |
| 95 |
type: bool |
| 96 |
default: false |
| 97 |
releases_events: |
| 98 |
description: |
| 99 |
- Trigger hook on release events. |
| 100 |
type: bool |
| 101 |
version_added: '8.4.0' |
| 102 |
hook_validate_certs: |
| 103 |
description: |
| 104 |
- Whether GitLab performs SSL verification when triggering the hook. |
| 105 |
type: bool |
| 106 |
default: false |
| 107 |
aliases: [enable_ssl_verification] |
| 108 |
token: |
| 109 |
description: |
| 110 |
- Secret token to validate hook messages at the receiver. |
| 111 |
- If this is present it always results in a change as it cannot be retrieved from GitLab. |
| 112 |
- It shows up in the C(X-GitLab-Token) HTTP request header. |
| 113 |
type: str |
| 114 |
""" |
| 115 |
|
| 116 |
EXAMPLES = r""" |
| 117 |
- name: "Adding a project hook" |
| 118 |
community.general.gitlab_hook: |
| 119 |
api_url: https://gitlab.example.com/ |
| 120 |
api_token: "{{ access_token }}" |
| 121 |
project: "my_group/my_project" |
| 122 |
hook_url: "https://my-ci-server.example.com/gitlab-hook" |
| 123 |
state: present |
| 124 |
push_events: true |
| 125 |
tag_push_events: true |
| 126 |
token: "my-super-secret-token-that-my-ci-server-will-check" |
| 127 |
|
| 128 |
- name: "Delete the previous hook" |
| 129 |
community.general.gitlab_hook: |
| 130 |
api_url: https://gitlab.example.com/ |
| 131 |
api_token: "{{ access_token }}" |
| 132 |
project: "my_group/my_project" |
| 133 |
hook_url: "https://my-ci-server.example.com/gitlab-hook" |
| 134 |
state: absent |
| 135 |
|
| 136 |
- name: "Delete a hook by numeric project id" |
| 137 |
community.general.gitlab_hook: |
| 138 |
api_url: https://gitlab.example.com/ |
| 139 |
api_token: "{{ access_token }}" |
| 140 |
project: 10 |
| 141 |
hook_url: "https://my-ci-server.example.com/gitlab-hook" |
| 142 |
state: absent |
| 143 |
""" |
| 144 |
|
| 145 |
RETURN = r""" |
| 146 |
msg: |
| 147 |
description: Success or failure message. |
| 148 |
returned: always |
| 149 |
type: str |
| 150 |
sample: "Success" |
| 151 |
|
| 152 |
result: |
| 153 |
description: JSON parsed response from the server. |
| 154 |
returned: always |
| 155 |
type: dict |
| 156 |
|
| 157 |
error: |
| 158 |
description: The error message returned by the GitLab API. |
| 159 |
returned: failed |
| 160 |
type: str |
| 161 |
sample: "400: path is already in use" |
| 162 |
|
| 163 |
hook: |
| 164 |
description: API object. |
| 165 |
returned: always |
| 166 |
type: dict |
| 167 |
""" |
| 168 |
|
| 169 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 170 |
from ansible.module_utils.basic import AnsibleModule |
| 171 |
|
| 172 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 173 |
auth_argument_spec, |
| 174 |
find_project, |
| 175 |
gitlab_authentication, |
| 176 |
list_all_kwargs, |
| 177 |
) |
| 178 |
|
| 179 |
|
| 180 |
class GitLabHook: |
| 181 |
def __init__(self, module, gitlab_instance): |
| 182 |
self._module = module |
| 183 |
self._gitlab = gitlab_instance |
| 184 |
self.hook_object = None |
| 185 |
|
| 186 |
""" |
| 187 |
@param project Project Object |
| 188 |
@param hook_url Url to call on event |
| 189 |
@param description Description of the group |
| 190 |
@param parent Parent group full path |
| 191 |
""" |
| 192 |
|
| 193 |
def create_or_update_hook(self, project, hook_url, options): |
| 194 |
changed = False |
| 195 |
|
| 196 |
hook_arguments = { |
| 197 |
"url": hook_url, |
| 198 |
"push_events": options["push_events"], |
| 199 |
"push_events_branch_filter": options["push_events_branch_filter"], |
| 200 |
"issues_events": options["issues_events"], |
| 201 |
"merge_requests_events": options["merge_requests_events"], |
| 202 |
"tag_push_events": options["tag_push_events"], |
| 203 |
"note_events": options["note_events"], |
| 204 |
"job_events": options["job_events"], |
| 205 |
"pipeline_events": options["pipeline_events"], |
| 206 |
"wiki_page_events": options["wiki_page_events"], |
| 207 |
"enable_ssl_verification": options["enable_ssl_verification"], |
| 208 |
"token": options["token"], |
| 209 |
} |
| 210 |
|
| 211 |
# Because we have already call userExists in main() |
| 212 |
if self.hook_object is None: |
| 213 |
if options["releases_events"] is not None: |
| 214 |
hook_arguments["releases_events"] = options["releases_events"] |
| 215 |
hook = self.create_hook(project, hook_arguments) |
| 216 |
changed = True |
| 217 |
else: |
| 218 |
update_arguments = hook_arguments.copy() |
| 219 |
update_arguments["releases_events"] = options["releases_events"] |
| 220 |
changed, hook = self.update_hook(self.hook_object, update_arguments) |
| 221 |
|
| 222 |
self.hook_object = hook |
| 223 |
if changed: |
| 224 |
if self._module.check_mode: |
| 225 |
self._module.exit_json(changed=True, msg=f"Successfully created or updated the hook {hook_url}") |
| 226 |
|
| 227 |
try: |
| 228 |
hook.save() |
| 229 |
except Exception as e: |
| 230 |
self._module.fail_json(msg=f"Failed to update hook: {e} ") |
| 231 |
|
| 232 |
return changed |
| 233 |
|
| 234 |
""" |
| 235 |
@param project Project Object |
| 236 |
@param arguments Attributes of the hook |
| 237 |
""" |
| 238 |
|
| 239 |
def create_hook(self, project, arguments): |
| 240 |
if self._module.check_mode: |
| 241 |
return True |
| 242 |
|
| 243 |
hook = project.hooks.create(arguments) |
| 244 |
|
| 245 |
return hook |
| 246 |
|
| 247 |
""" |
| 248 |
@param hook Hook Object |
| 249 |
@param arguments Attributes of the hook |
| 250 |
""" |
| 251 |
|
| 252 |
def update_hook(self, hook, arguments): |
| 253 |
changed = False |
| 254 |
|
| 255 |
for arg_key, arg_value in arguments.items(): |
| 256 |
if arg_value is not None: |
| 257 |
if getattr(hook, arg_key, None) != arg_value: |
| 258 |
setattr(hook, arg_key, arg_value) |
| 259 |
changed = True |
| 260 |
|
| 261 |
return (changed, hook) |
| 262 |
|
| 263 |
""" |
| 264 |
@param project Project object |
| 265 |
@param hook_url Url to call on event |
| 266 |
""" |
| 267 |
|
| 268 |
def find_hook(self, project, hook_url): |
| 269 |
for hook in project.hooks.list(**list_all_kwargs): |
| 270 |
if hook.url == hook_url: |
| 271 |
return hook |
| 272 |
|
| 273 |
""" |
| 274 |
@param project Project object |
| 275 |
@param hook_url Url to call on event |
| 276 |
""" |
| 277 |
|
| 278 |
def exists_hook(self, project, hook_url): |
| 279 |
# When project exists, object will be stored in self.project_object. |
| 280 |
hook = self.find_hook(project, hook_url) |
| 281 |
if hook: |
| 282 |
self.hook_object = hook |
| 283 |
return True |
| 284 |
return False |
| 285 |
|
| 286 |
def delete_hook(self): |
| 287 |
if not self._module.check_mode: |
| 288 |
self.hook_object.delete() |
| 289 |
|
| 290 |
|
| 291 |
def main(): |
| 292 |
argument_spec = basic_auth_argument_spec() |
| 293 |
argument_spec.update(auth_argument_spec()) |
| 294 |
argument_spec.update( |
| 295 |
dict( |
| 296 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 297 |
project=dict(type="str", required=True), |
| 298 |
hook_url=dict(type="str", required=True), |
| 299 |
push_events=dict(type="bool", default=True), |
| 300 |
push_events_branch_filter=dict(type="str", default=""), |
| 301 |
issues_events=dict(type="bool", default=False), |
| 302 |
merge_requests_events=dict(type="bool", default=False), |
| 303 |
tag_push_events=dict(type="bool", default=False), |
| 304 |
note_events=dict(type="bool", default=False), |
| 305 |
job_events=dict(type="bool", default=False), |
| 306 |
pipeline_events=dict(type="bool", default=False), |
| 307 |
wiki_page_events=dict(type="bool", default=False), |
| 308 |
releases_events=dict(type="bool"), |
| 309 |
hook_validate_certs=dict(type="bool", default=False, aliases=["enable_ssl_verification"]), |
| 310 |
token=dict(type="str", no_log=True), |
| 311 |
) |
| 312 |
) |
| 313 |
|
| 314 |
module = AnsibleModule( |
| 315 |
argument_spec=argument_spec, |
| 316 |
mutually_exclusive=[ |
| 317 |
["api_username", "api_token"], |
| 318 |
["api_username", "api_oauth_token"], |
| 319 |
["api_username", "api_job_token"], |
| 320 |
["api_token", "api_oauth_token"], |
| 321 |
["api_token", "api_job_token"], |
| 322 |
], |
| 323 |
required_together=[["api_username", "api_password"]], |
| 324 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 325 |
supports_check_mode=True, |
| 326 |
) |
| 327 |
|
| 328 |
# check prerequisites and connect to gitlab server |
| 329 |
gitlab_instance = gitlab_authentication(module) |
| 330 |
|
| 331 |
state = module.params["state"] |
| 332 |
project_identifier = module.params["project"] |
| 333 |
hook_url = module.params["hook_url"] |
| 334 |
push_events = module.params["push_events"] |
| 335 |
push_events_branch_filter = module.params["push_events_branch_filter"] |
| 336 |
issues_events = module.params["issues_events"] |
| 337 |
merge_requests_events = module.params["merge_requests_events"] |
| 338 |
tag_push_events = module.params["tag_push_events"] |
| 339 |
note_events = module.params["note_events"] |
| 340 |
job_events = module.params["job_events"] |
| 341 |
pipeline_events = module.params["pipeline_events"] |
| 342 |
wiki_page_events = module.params["wiki_page_events"] |
| 343 |
releases_events = module.params["releases_events"] |
| 344 |
enable_ssl_verification = module.params["hook_validate_certs"] |
| 345 |
hook_token = module.params["token"] |
| 346 |
|
| 347 |
gitlab_hook = GitLabHook(module, gitlab_instance) |
| 348 |
|
| 349 |
project = find_project(gitlab_instance, project_identifier) |
| 350 |
|
| 351 |
if project is None: |
| 352 |
module.fail_json(msg=f"Failed to create hook: project {project_identifier} doesn't exists") |
| 353 |
|
| 354 |
hook_exists = gitlab_hook.exists_hook(project, hook_url) |
| 355 |
|
| 356 |
if state == "absent": |
| 357 |
if hook_exists: |
| 358 |
gitlab_hook.delete_hook() |
| 359 |
module.exit_json(changed=True, msg=f"Successfully deleted hook {hook_url}") |
| 360 |
else: |
| 361 |
module.exit_json(changed=False, msg="Hook deleted or does not exists") |
| 362 |
|
| 363 |
if state == "present": |
| 364 |
if gitlab_hook.create_or_update_hook( |
| 365 |
project, |
| 366 |
hook_url, |
| 367 |
{ |
| 368 |
"push_events": push_events, |
| 369 |
"push_events_branch_filter": push_events_branch_filter, |
| 370 |
"issues_events": issues_events, |
| 371 |
"merge_requests_events": merge_requests_events, |
| 372 |
"tag_push_events": tag_push_events, |
| 373 |
"note_events": note_events, |
| 374 |
"job_events": job_events, |
| 375 |
"pipeline_events": pipeline_events, |
| 376 |
"wiki_page_events": wiki_page_events, |
| 377 |
"releases_events": releases_events, |
| 378 |
"enable_ssl_verification": enable_ssl_verification, |
| 379 |
"token": hook_token, |
| 380 |
}, |
| 381 |
): |
| 382 |
module.exit_json( |
| 383 |
changed=True, |
| 384 |
msg=f"Successfully created or updated the hook {hook_url}", |
| 385 |
hook=gitlab_hook.hook_object._attrs, |
| 386 |
) |
| 387 |
else: |
| 388 |
module.exit_json( |
| 389 |
changed=False, msg=f"No need to update the hook {hook_url}", hook=gitlab_hook.hook_object._attrs |
| 390 |
) |
| 391 |
|
| 392 |
|
| 393 |
if __name__ == "__main__": |
| 394 |
main() |