-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_receive_hook.py
84 lines (74 loc) · 3.89 KB
/
post_receive_hook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Utilities for managing a repo's post-receive hook.
"""
import requests
from utils import log
class PostReceiveHook:
"""Utilities for managing a repo's post-receive hook."""
__post_receive_hook_data__ = {'version': '3', 'locationCount': '1', 'httpMethod': 'POST',
'url': '', 'useAuth': 'true', 'user': 'SVC_PV_jenkins_usr_T',
'pass': '@ZV28$cnEg$fi6e',
'postContentType': 'application/x-www-form-urlencoded',
'postData': '', 'branchFilter': '', 'tagFilter': '',
'userFilter': '([BbTt][0-9]{1,7})'}
@log
def enable_for_project(self, **kwargs):
"""Enables the post receive hook for a project."""
return self._handle_enable_disable_post_receive_hook_request_for_project(
True, **kwargs)
@log
def disable_for_project(self, **kwargs):
"""Disables the post receive hook for a project."""
return self._handle_enable_disable_post_receive_hook_request_for_project(
False, **kwargs)
@log
def enable(self, **kwargs):
"""Enables the post receive hook for a repo."""
return self._handle_enable_disable_post_receive_hook_request(
True, **kwargs)
@log
def disable(self, **kwargs):
"""Disables the post receive hook for a repo."""
return self._handle_enable_disable_post_receive_hook_request(
False, **kwargs)
@log
def configure_for_project(self, **kwargs):
"""Configures the post-receive hook for all repos in this project."""
bitbucket = kwargs.get('bitbucket')
data = self.__post_receive_hook_data__
data['url'] = 'http://' + kwargs.get('jenkins').get(
'host') + '/job/${repository.slug}-build/build'
url = f"http://{bitbucket.get('host')}/rest/api/1.0"
url += f"/projects/{bitbucket.get('project')}/settings/hooks/"
url += 'de.aeffle.stash.plugin.stash-http-get-post-receive-hook:http-get-post-receive-hook/settings'
return requests.request(
'PUT', url, headers=kwargs.get('default_headers'), json=data)
@log
def configure(self, **kwargs):
"""Configures the post-receive hook for this repo."""
bitbucket = kwargs.get('bitbucket')
data = self.__post_receive_hook_data__
data['url'] = 'http://' + kwargs.get('jenkins').get(
'host') + '/job/${repository.slug}-build/build'
url = f"http://{bitbucket.get('host')}/rest/api/1.0"
url += f"/projects/{bitbucket.get('project')}/repos/{bitbucket.get('repo')}"
url += '/settings/hooks/de.aeffle.stash.plugin.stash-http-get-post-receive-hook:'
url += 'http-get-post-receive-hook/settings'
return requests.request(
'PUT', url, headers=kwargs.get('default_headers'), json=data)
def _handle_enable_disable_post_receive_hook_request_for_project(self, enable, **kwargs):
bitbucket = kwargs.get('bitbucket')
url = f"http://{bitbucket.get('host')}/rest/api/1.0/"
url += f"projects/{bitbucket.get('project')}"
url += '/settings/hooks/de.aeffle.stash.plugin.stash-http-get-post-receive-hook:'
url += 'http-get-post-receive-hook/enabled'
return requests.request(
'PUT' if enable else 'DELETE', url, headers=kwargs.get('default_headers'))
def _handle_enable_disable_post_receive_hook_request(self, enable, **kwargs):
bitbucket = kwargs.get('bitbucket')
url = f"http://{bitbucket.get('host')}/rest/api/1.0/"
url += f"projects/{bitbucket.get('project')}/repos/{bitbucket.get('repo')}"
url += '/settings/hooks/de.aeffle.stash.plugin.stash-http-get-post-receive-hook:'
url += 'http-get-post-receive-hook/enabled'
return requests.request(
'PUT' if enable else 'DELETE', url, headers=kwargs.get('default_headers'))