-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGitLabPy.py
More file actions
121 lines (109 loc) · 4.36 KB
/
Copy pathGitLabPy.py
File metadata and controls
121 lines (109 loc) · 4.36 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import json
class GitLab:
"""
Instantiate with 1 argument, parsed json string <string>
"""
def __init__(self, arg1):
self.json_data = arg1
# Types of build and issue JSON from GitLab Webhook
self.issue_types = ["open", "update", "closed"]
# Below is for common JSON objects
self.object_kind = arg1.get("object_kind", "")
self.project_id = arg1.get("project_id", "")
self.ref = arg1.get("ref", "")
self.project_name = arg1.get("project_name", "")
self.user = arg1.get("user", "")
self.commit = arg1.get("commit", "")
self.object_attributes = arg1.get("object_attributes", "")
self.build_status = arg1.get("build_status", "")
repo_obj = arg1.get("repository", "")
if repo_obj != "":
self.repository = repo_obj
self.repo_name = repo_obj.get("name", "")
self.repo_homepage = repo_obj.get("homepage", "")
def check_repository_atttr(self):
if self.repository == "":
return False
else:
return True
def get_url(self, url=None):
"""
Arguments:
- url : type of url to return <string>
* input types:
- "http" : retrieve git http url
- "ssh" : retrieve git ssh url
- "homepage" : retrieve homepage url
- (no input) : retrieve default git url, which is: "repository": {"url": <url>}
"""
if check_repository_atttr():
return False
if url.lower() == "ssh":
return self.repository.get("git_ssh_url")
elif url.lower() == "http":
return self.repository.get("git_http_url")
elif url.lower() == "homepage":
return self.repository.get("homepage")
else:
return self.repository.get("url")
def get_repo_description(self):
"""
Arguments: None
Checks to see if the key 'description' is in the webhook
"""
if check_repository_atttr():
return self.repository.get("description")
else:
return False
def build(self, *args):
"""
Checks to see if JSON data from GitLab is build data and also checks to see that you want build data. If so, return True
Arguments: *args <bool><string><list>
- can be a list of certain build data you want
- build types are 'success', 'failed', 'runnning'
"""
if self.object_kind == "build" and self.build_status in args:
return True
else:
return False
def note(self, note_types=False):
"""
Checks to see if JSON data is a note (comment) and you want note JSON data. If so, return True
Arguments: note_types <bool>
- Returns True if you want note type JSON data to return True and JSON data is a note
- Default - False
"""
if note_types and self.object_kind == "note":
return True
else:
return False
def merge_request(self, merge_request_types=False):
"""
Checks to see if JSON data is a merge request and if you want merge_request data. If so, return True
Arguments: merge_request_types <bool>
- True if you want merge request data from GitLab to be returned if JSON data is merge request.
- Default - False
"""
if merge_request_types and self.object_kind == "merge_request":
return True
else:
return False
def issue(self, *args):
"""
Checks to see if JSON data is an issue from GitLabJSON
Arguments: *args <bool><string><list>
- can be list of issue types or boolean
- issue types are 'open', 'update', 'closed'
- True if you want all issues pass this conditional function
"""
if self.object_kind != "issue":
return False
if "open" in args or "closed" in args:
if self.object_attributes.get("state") == "closed" and "closed" in args:
return True
elif self.object_attributes.get("action") == "open" and "open" in args:
return True
elif "update" in args and self.object_attributes.get("action") == "update":
return True
else:
return False