2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# Copyright (c) 2018, Marcus Watkins <marwatk@marcuswatkins.net>
|
2020-03-09 10:11:07 +01:00
|
|
|
# Based on code:
|
2022-08-05 13:17:19 +02:00
|
|
|
# Copyright (c) 2013, Phillip Gentry <phillip@cx.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: gitlab_hook
|
|
|
|
short_description: Manages GitLab project hooks.
|
|
|
|
description:
|
2021-12-20 21:59:12 +01:00
|
|
|
- Adds, updates and removes project hook
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- Marcus Watkins (@marwatk)
|
|
|
|
- Guillaume Martinez (@Lunik)
|
|
|
|
requirements:
|
|
|
|
- python >= 2.7
|
|
|
|
- python-gitlab python module
|
|
|
|
extends_documentation_fragment:
|
2021-12-20 21:59:12 +01:00
|
|
|
- community.general.auth_basic
|
|
|
|
- community.general.gitlab
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
options:
|
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- Id or Full path of the project in the form of group/name.
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
hook_url:
|
|
|
|
description:
|
|
|
|
- The url that you want GitLab to post to, this is used as the primary key for updates and deletion.
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- When C(present) the hook will be updated to match the input or created if it doesn't exist.
|
|
|
|
- When C(absent) hook will be deleted if it exists.
|
|
|
|
default: present
|
|
|
|
type: str
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
push_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on push events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
push_events_branch_filter:
|
|
|
|
description:
|
|
|
|
- Branch name of wildcard to trigger hook on push events
|
|
|
|
type: str
|
2020-06-13 15:01:19 +02:00
|
|
|
version_added: '0.2.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
issues_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on issues events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
merge_requests_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on merge requests events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
tag_push_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on tag push events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
note_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on note events or when someone adds a comment.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
job_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on job events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
pipeline_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on pipeline events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
wiki_page_events:
|
|
|
|
description:
|
|
|
|
- Trigger hook on wiki events.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
hook_validate_certs:
|
|
|
|
description:
|
|
|
|
- Whether GitLab will do SSL verification when triggering the hook.
|
|
|
|
type: bool
|
2022-08-24 20:21:52 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
aliases: [ enable_ssl_verification ]
|
|
|
|
token:
|
|
|
|
description:
|
|
|
|
- Secret token to validate hook messages at the receiver.
|
|
|
|
- If this is present it will always result in a change as it cannot be retrieved from GitLab.
|
|
|
|
- Will show up in the X-GitLab-Token HTTP request header.
|
|
|
|
required: false
|
|
|
|
type: str
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: "Adding a project hook"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_hook:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ access_token }}"
|
|
|
|
project: "my_group/my_project"
|
|
|
|
hook_url: "https://my-ci-server.example.com/gitlab-hook"
|
|
|
|
state: present
|
2022-08-24 20:21:52 +02:00
|
|
|
push_events: true
|
|
|
|
tag_push_events: true
|
|
|
|
hook_validate_certs: false
|
2020-03-09 10:11:07 +01:00
|
|
|
token: "my-super-secret-token-that-my-ci-server-will-check"
|
|
|
|
|
|
|
|
- name: "Delete the previous hook"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_hook:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ access_token }}"
|
|
|
|
project: "my_group/my_project"
|
|
|
|
hook_url: "https://my-ci-server.example.com/gitlab-hook"
|
|
|
|
state: absent
|
|
|
|
|
|
|
|
- name: "Delete a hook by numeric project id"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_hook:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ access_token }}"
|
|
|
|
project: 10
|
|
|
|
hook_url: "https://my-ci-server.example.com/gitlab-hook"
|
|
|
|
state: absent
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
msg:
|
|
|
|
description: Success or failure message
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Success"
|
|
|
|
|
|
|
|
result:
|
|
|
|
description: json parsed response from the server
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
|
|
|
|
error:
|
|
|
|
description: the error message returned by the GitLab API
|
|
|
|
returned: failed
|
|
|
|
type: str
|
|
|
|
sample: "400: path is already in use"
|
|
|
|
|
|
|
|
hook:
|
|
|
|
description: API object
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.api import basic_auth_argument_spec
|
2022-09-12 21:13:59 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-09-12 21:13:59 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.gitlab import (
|
|
|
|
auth_argument_spec, find_project, gitlab_authentication, ensure_gitlab_package
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GitLabHook(object):
|
|
|
|
def __init__(self, module, gitlab_instance):
|
|
|
|
self._module = module
|
|
|
|
self._gitlab = gitlab_instance
|
2021-11-16 13:01:32 +01:00
|
|
|
self.hook_object = None
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project Object
|
|
|
|
@param hook_url Url to call on event
|
|
|
|
@param description Description of the group
|
|
|
|
@param parent Parent group full path
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_or_update_hook(self, project, hook_url, options):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
# Because we have already call userExists in main()
|
2021-11-16 13:01:32 +01:00
|
|
|
if self.hook_object is None:
|
|
|
|
hook = self.create_hook(project, {
|
2020-03-09 10:11:07 +01:00
|
|
|
'url': hook_url,
|
|
|
|
'push_events': options['push_events'],
|
|
|
|
'push_events_branch_filter': options['push_events_branch_filter'],
|
|
|
|
'issues_events': options['issues_events'],
|
|
|
|
'merge_requests_events': options['merge_requests_events'],
|
|
|
|
'tag_push_events': options['tag_push_events'],
|
|
|
|
'note_events': options['note_events'],
|
|
|
|
'job_events': options['job_events'],
|
|
|
|
'pipeline_events': options['pipeline_events'],
|
|
|
|
'wiki_page_events': options['wiki_page_events'],
|
|
|
|
'enable_ssl_verification': options['enable_ssl_verification'],
|
2021-11-16 13:01:32 +01:00
|
|
|
'token': options['token'],
|
|
|
|
})
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
else:
|
2021-11-16 13:01:32 +01:00
|
|
|
changed, hook = self.update_hook(self.hook_object, {
|
2020-03-09 10:11:07 +01:00
|
|
|
'push_events': options['push_events'],
|
|
|
|
'push_events_branch_filter': options['push_events_branch_filter'],
|
|
|
|
'issues_events': options['issues_events'],
|
|
|
|
'merge_requests_events': options['merge_requests_events'],
|
|
|
|
'tag_push_events': options['tag_push_events'],
|
|
|
|
'note_events': options['note_events'],
|
|
|
|
'job_events': options['job_events'],
|
|
|
|
'pipeline_events': options['pipeline_events'],
|
|
|
|
'wiki_page_events': options['wiki_page_events'],
|
|
|
|
'enable_ssl_verification': options['enable_ssl_verification'],
|
2021-11-16 13:01:32 +01:00
|
|
|
'token': options['token'],
|
|
|
|
})
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
self.hook_object = hook
|
2020-03-09 10:11:07 +01:00
|
|
|
if changed:
|
|
|
|
if self._module.check_mode:
|
|
|
|
self._module.exit_json(changed=True, msg="Successfully created or updated the hook %s" % hook_url)
|
|
|
|
|
|
|
|
try:
|
|
|
|
hook.save()
|
|
|
|
except Exception as e:
|
|
|
|
self._module.fail_json(msg="Failed to update hook: %s " % e)
|
2022-09-12 21:14:22 +02:00
|
|
|
|
|
|
|
return changed
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project Object
|
|
|
|
@param arguments Attributes of the hook
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_hook(self, project, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
if self._module.check_mode:
|
|
|
|
return True
|
|
|
|
|
|
|
|
hook = project.hooks.create(arguments)
|
|
|
|
|
|
|
|
return hook
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param hook Hook Object
|
|
|
|
@param arguments Attributes of the hook
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def update_hook(self, hook, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
for arg_key, arg_value in arguments.items():
|
2022-09-12 21:14:22 +02:00
|
|
|
if arg_value is not None:
|
|
|
|
if getattr(hook, arg_key, None) != arg_value:
|
|
|
|
setattr(hook, arg_key, arg_value)
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
|
|
|
|
return (changed, hook)
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project object
|
|
|
|
@param hook_url Url to call on event
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def find_hook(self, project, hook_url):
|
2022-04-13 13:32:25 +02:00
|
|
|
hooks = project.hooks.list(all=True)
|
2020-03-09 10:11:07 +01:00
|
|
|
for hook in hooks:
|
|
|
|
if (hook.url == hook_url):
|
|
|
|
return hook
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project object
|
|
|
|
@param hook_url Url to call on event
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def exists_hook(self, project, hook_url):
|
|
|
|
# When project exists, object will be stored in self.project_object.
|
|
|
|
hook = self.find_hook(project, hook_url)
|
2020-03-09 10:11:07 +01:00
|
|
|
if hook:
|
2021-11-16 13:01:32 +01:00
|
|
|
self.hook_object = hook
|
2020-03-09 10:11:07 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def delete_hook(self):
|
2022-09-12 21:14:22 +02:00
|
|
|
if not self._module.check_mode:
|
|
|
|
self.hook_object.delete()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = basic_auth_argument_spec()
|
2021-12-20 21:59:12 +01:00
|
|
|
argument_spec.update(auth_argument_spec())
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec.update(dict(
|
|
|
|
state=dict(type='str', default="present", choices=["absent", "present"]),
|
|
|
|
project=dict(type='str', required=True),
|
|
|
|
hook_url=dict(type='str', required=True),
|
|
|
|
push_events=dict(type='bool', default=True),
|
|
|
|
push_events_branch_filter=dict(type='str', default=''),
|
|
|
|
issues_events=dict(type='bool', default=False),
|
|
|
|
merge_requests_events=dict(type='bool', default=False),
|
|
|
|
tag_push_events=dict(type='bool', default=False),
|
|
|
|
note_events=dict(type='bool', default=False),
|
|
|
|
job_events=dict(type='bool', default=False),
|
|
|
|
pipeline_events=dict(type='bool', default=False),
|
|
|
|
wiki_page_events=dict(type='bool', default=False),
|
|
|
|
hook_validate_certs=dict(type='bool', default=False, aliases=['enable_ssl_verification']),
|
|
|
|
token=dict(type='str', no_log=True),
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=[
|
|
|
|
['api_username', 'api_token'],
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_oauth_token'],
|
|
|
|
['api_username', 'api_job_token'],
|
|
|
|
['api_token', 'api_oauth_token'],
|
|
|
|
['api_token', 'api_job_token'],
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
required_together=[
|
|
|
|
['api_username', 'api_password']
|
|
|
|
],
|
|
|
|
required_one_of=[
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_token', 'api_oauth_token', 'api_job_token']
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2022-09-12 21:13:59 +02:00
|
|
|
ensure_gitlab_package(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
project_identifier = module.params['project']
|
|
|
|
hook_url = module.params['hook_url']
|
|
|
|
push_events = module.params['push_events']
|
|
|
|
push_events_branch_filter = module.params['push_events_branch_filter']
|
|
|
|
issues_events = module.params['issues_events']
|
|
|
|
merge_requests_events = module.params['merge_requests_events']
|
|
|
|
tag_push_events = module.params['tag_push_events']
|
|
|
|
note_events = module.params['note_events']
|
|
|
|
job_events = module.params['job_events']
|
|
|
|
pipeline_events = module.params['pipeline_events']
|
|
|
|
wiki_page_events = module.params['wiki_page_events']
|
|
|
|
enable_ssl_verification = module.params['hook_validate_certs']
|
|
|
|
hook_token = module.params['token']
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
gitlab_instance = gitlab_authentication(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
gitlab_hook = GitLabHook(module, gitlab_instance)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
project = find_project(gitlab_instance, project_identifier)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if project is None:
|
|
|
|
module.fail_json(msg="Failed to create hook: project %s doesn't exists" % project_identifier)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
hook_exists = gitlab_hook.exists_hook(project, hook_url)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
if hook_exists:
|
2021-11-16 13:01:32 +01:00
|
|
|
gitlab_hook.delete_hook()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True, msg="Successfully deleted hook %s" % hook_url)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, msg="Hook deleted or does not exists")
|
|
|
|
|
|
|
|
if state == 'present':
|
2021-11-16 13:01:32 +01:00
|
|
|
if gitlab_hook.create_or_update_hook(project, hook_url, {
|
|
|
|
"push_events": push_events,
|
|
|
|
"push_events_branch_filter": push_events_branch_filter,
|
|
|
|
"issues_events": issues_events,
|
|
|
|
"merge_requests_events": merge_requests_events,
|
|
|
|
"tag_push_events": tag_push_events,
|
|
|
|
"note_events": note_events,
|
|
|
|
"job_events": job_events,
|
|
|
|
"pipeline_events": pipeline_events,
|
|
|
|
"wiki_page_events": wiki_page_events,
|
|
|
|
"enable_ssl_verification": enable_ssl_verification,
|
|
|
|
"token": hook_token,
|
|
|
|
}):
|
|
|
|
|
|
|
|
module.exit_json(changed=True, msg="Successfully created or updated the hook %s" % hook_url, hook=gitlab_hook.hook_object._attrs)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-11-16 13:01:32 +01:00
|
|
|
module.exit_json(changed=False, msg="No need to update the hook %s" % hook_url, hook=gitlab_hook.hook_object._attrs)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|