forked from JamesIves/perforce-commit-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4.py
174 lines (150 loc) · 5.2 KB
/
p4.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from enum import Enum
import subprocess
import os
import re
import requests
from requests.auth import HTTPBasicAuth
class Change:
user = "Perforce User"
changelist = ""
text = ""
header = ""
workspace = "WORKSPACE"
review = ""
class Status(Enum):
INVALID = 0
SUBMITTED = 1
PENDING = 2
SHELVED = 3
class Perforce:
server = None
depot = None
status = Status.SUBMITTED
limit = 1
swarm = ""
class Server:
host = None
user = None
password = None
decode = 'ISO-8859-1'
class Storage:
changes = []
class SwarmUrls:
changelist = ""
review = ""
user = ""
def make_status(input):
if input == "submitted":
return Status.SUBMITTED
elif input == "pending":
return Status.PENDING
elif input == "selved":
return Status.SHELVED
else:
return Status.INVALID
def init(config):
perforce = Perforce()
perforce.server = Server()
perforce.server.host = config['p4']["host"]
perforce.server.user = config['p4']["user"]
perforce.server.password = config['p4']["password"]
perforce.server.decode = config['p4']["decode"]
perforce.limit = config['p4']["limit"]
perforce.status = make_status(config['p4']["status"])
perforce.depot = config['p4']["depot"]
perforce.swarm = config['p4']["swarm"]
return perforce
def check_for_changes(storage,changes):
new_changes = []
for change in changes:
if validate_change(storage.changes,change):
storage.changes.append(change)
new_changes.append(change)
return new_changes
def validate_change(changes,new_change):
for change in changes:
if change.changelist == new_change.changelist:
return False
return True
def make_swarm_urls(change,perforce):
swarm_urls = SwarmUrls()
swarm_urls.user = perforce.swarm + 'users/'+change.user
swarm_urls.changelist = perforce.swarm + "changes/"+change.changelist
swarm_urls.review = perforce.swarm+ "reviews/"+change.review
return swarm_urls
def build_login_command(perforce):
user = ""
if perforce.server.user != None:
user = "-u "+ perforce.server.user
host = ""
if perforce.server.host != None and perforce.server.host != "":
host = "-p "+perforce.server.host
return "p4 "+host+" "+user+" login -pa"
def build_command(perforce):
status = "submitted"
if perforce.status == Status.PENDING:
status = "pending"
elif perforce.status == Status.SHELVED:
status = "shelved"
user = ''
if perforce.server.user != None:
user = "-u "+ perforce.server.user
password = ''
if perforce.server.password != None:
password = "-P "+perforce.server.password
depo = ''
if perforce.depot != None:
depo = perforce.depot
host = ""
if perforce.server.host != None and perforce.server.host != "":
host = "-p "+perforce.server.host
command = "p4 "+host+" "+user+" "+password+" changes -l -m "+str(perforce.limit)+" -s "+status
return command
def request_review(perforce, id):
# sending get request and saving the response as response object
swarm = perforce.swarm + "api/v9/reviews/" + id
regex = r"(Enter|\s+|:+|password|\\n|\\r)"
p4login = subprocess.Popen(build_login_command(perforce), stdout=subprocess.PIPE,stdin=subprocess.PIPE, shell=True)
#p4login.stdin.write('yourPassword\n')
#p4login.stdin.flush()
com = p4login.communicate(input=perforce.server.password)
key = re.sub(regex, "", com[0])
r = requests.get(url=swarm, auth=HTTPBasicAuth(perforce.server.user, key), verify=False) # TODO:make secure
result = {"id": id, "key": key, "data": None, "comments": []}
if r.status_code == 200:
# extracting data in json format
data = r.json()
result["data"] = data["review"]
else:
print 'Error: Login request returned status code ' + str(r.status_code) + '. Should be 200.'
comments = requests.get(url=perforce.swarm + "api/v9/comments/?topic=reviews/" + id, auth=HTTPBasicAuth(perforce.server.user, key), verify=False)
if comments.status_code == 200:
data = comments.json()
result["comments"] = data["comments"]
return result
else:
print 'Error: Comments request returned status code ' + str(comments.status_code) + '. Should be 200.'
return None
def request_changes(perforce):
command = build_command(perforce)
p4_changes = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True).stdout.readlines()
regex = r"(Change\s([0-9]+))"
user_regex = r"\s([\w]+)@([\w-]+)"
changes = []
change = Change()
for line in p4_changes:
content = line.decode(perforce.server.decode)
match = re.search(regex,content)
if match != None:
user_match = re.search(user_regex,content)
changelist = match.group(2)
change = Change()
changes.append(change)
change.changelist = changelist
change.header = content
change.text = ""
change.user = user_match.group(1)
change.workspace = user_match.group(2)
else:
change.text += content
return changes