forked from neuhalje/hack-copy-track-issues-to-gitlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnection.py
106 lines (85 loc) · 3.56 KB
/
Connection.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import requests
__author__ = 'jens'
class Connection(object):
"""
Connection to the gitlab API
"""
def __init__(self, url, access_token):
"""
:param url: "https://www.neuhalfen.name/gitlab/api/v3"
:param access_token: "secretsecretsecret"
"""
self.url = url
self.access_token = access_token
def milestone_by_name(self, project_id, milestone_name):
milestones = self.get("/projects/:project_id/milestones",project_id=project_id)
for milestone in milestones:
if milestone['title'] == milestone_name:
return milestone
def project_by_name(self, project_name):
projects = self.get("/projects")
for project in projects:
if project['path_with_namespace'] == project_name:
return project
def get(self, url_postfix, **keywords):
return self._get(url_postfix, keywords)
def _get(self, url_postfix, keywords):
"""
:param url_postfix: e.g. "/projects/:id/issues"
:param keywords: map, e.g. { "id" : 5 }
:return: json of GET
"""
completed_url = self._complete_url(url_postfix, keywords)
r = requests.get(completed_url)
json = r.json()
return json
def put(self, url_postfix, data, **keywords):
completed_url = self._complete_url(url_postfix, keywords)
r = requests.put(completed_url,data= data)
j = r.json()
return j
def put_json(self, url_postfix, data, **keywords):
completed_url = self._complete_url(url_postfix, keywords)
payload = json.dumps(data)
r = requests.put(completed_url, payload)
j = r.json()
return j
def post_json(self, url_postfix, data, **keywords):
completed_url = self._complete_url(url_postfix, keywords)
payload = json.dumps(data)
r = requests.post(completed_url, payload)
j = r.json()
return j
def create_issue(self, dest_project_id, new_ticket):
return self.post_json("/projects/:id/issues", new_ticket, id=dest_project_id)
def comment_issue(self,project_id,ticket_id, body):
new_note_data = {
"id" : project_id,
"issue_id" :ticket_id,
"body" : body
}
self.post_json( "/projects/:project_id/issues/:issue_id/notes", new_note_data, project_id=project_id, issue_id=ticket_id)
def set_issue_milestone(self,project_id,ticket_id,milestone_id):
new_note_data = {"milestone" : milestone_id}
self.put("/projects/:project_id/issues/:issue_id", new_note_data, project_id=project_id, issue_id=ticket_id)
def close_issue(self,project_id,ticket_id):
#new_note_data = "closed=1"
new_note_data = {"closed": "1"}
self.put("/projects/:project_id/issues/:issue_id", new_note_data, project_id=project_id, issue_id=ticket_id)
def _complete_url(self, url_postfix, keywords):
url_postfix_with_params = self._url_postfix_with_params(url_postfix, keywords)
complete_url = "%s%s?private_token=%s" % (self.url, url_postfix_with_params, self.access_token)
return complete_url
def _url_postfix_with_params(self, url_postfix, keywords):
"""
:param url_postfix: "/projects/:id/issues"
:param keywords: map, e.g. { "id" : 5 }
:return: "/projects/5/issues"
"""
result = url_postfix
for key, value in keywords.iteritems():
k = ":" + str(key)
v = str(value)
result = result.replace(k, v)
return result