This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathvalidator.py
108 lines (82 loc) · 3.84 KB
/
validator.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
from util import PullValidator, GitMerger
import pystache, yaml
from github3 import login
from github3.pulls import PullRequest
from collections import deque
class PullBot(PullValidator, GitMerger):
config = yaml.load(open("config.yml"))
def __init__(self):
GitMerger.__init__(self)
PullValidator.__init__(self)
self.gh = login(self.config["user"], token=self.config["auth_token"])
self.repo = self.gh.repository(self.config["owner"], self.config["repo"])
def get_pull(self, pr=None):
if type(pr) == int:
return self.repo.pull_request(pr)
elif type(pr) == PullRequest:
return pr
return None
#validate a pr, pr is an int
def validate(self, pr=None, debug=False):
self.repo.refresh()
pr = self.get_pull(pr)
if not pr: return False
issue = self.repo.issue(pr.number)
last_commit = deque(pr.iter_commits(), maxlen=1).pop()
self.repo.create_status(sha=last_commit.sha, state="pending")
status = PullValidator.validate(self, pr, debug)
if status.warnings or status.ini_issues or status.file_issues or status.version_issues:
#report them to the cops
data = {
"login": pr.user.login,
"has-ini-issues": status.ini_issues != [],
"info-issues": status.ini_issues,
"has-file-issues": status.file_issues != [],
"file-issues": status.file_issues,
"has-warnings": status.warnings != [],
"warnings": status.warnings,
"has-version-issues": status.version_issues != [],
"version-issues": status.version_issues,
"qotd": self.gh.zen()
}
with open("templates/pr-issue.tpl") as f:
issue_template = f.read()
comments_md = pystache.render(issue_template, data)
#failure commit status
files = {"{0}.md".format(pr.number): {"content": comments_md}}
gist = self.gh.create_gist("Validation of PR #{0} for jsdelivr/jsdelivr".format(pr.number), files=files, public=False)
state = "error" if status.error_occured else "failure"
message = "Automatic validation encountered an error" if status.error_occured else "Failed automatic validation"
self.repo.create_status(sha=last_commit.sha, state=state, target_url=gist.html_url, description=message)
if (debug):
print comments_md
#create a comment if nothings happening
if not any(c.user.login == self.config["user"] for c in issue.iter_comments()):
issue.create_comment(comments_md)
else:
if (debug == True):
print 'Validation success'
#success status and attempt to merge it
self.repo.create_status(sha=last_commit.sha, state="success", target_url="http://www.lgtm.in/g", description="\"LGTM\" - bot")
self.merge(pr)
def is_trusted(self, user):
return user.login in self.config["trusted_users"]
def delete_branch(self, pr):
r = self.gh.repository(*pr.head.repo)
try:
r.ref('heads/' + pr.head.ref).delete()
except: pass
# Automatically close branches on bot pull requests when rejected.
def closed_pr(self, pr):
pr = self.get_pull(pr)
if (self.gh.user() == pr.user):
self.delete_branch(pr)
def merge(self, pr):
# only merge trusted users
if self.is_trusted(pr.user):
# Attempt to delete the branch (if the bot has permission for the repo)
try:
pr.merge("http://www.lgtm.in/g")
# Until https://github.com/sigmavirus24/github3.py/pull/351 is sorted out
self.delete_branch(pr)
except: pass