@@ -20,6 +20,8 @@ class Repository:
20
20
def __init__ (self , user , repository ):
21
21
self .user = user
22
22
self .name = repository
23
+ self .contributors = False
24
+ self .collaborators = {}
23
25
auth = config .getGitToken ()
24
26
self .client = github3 .login (token = auth ['token' ])
25
27
self .client .set_user_agent ('gitconsensus' )
@@ -38,6 +40,17 @@ def getPullRequests(self):
38
40
retpr .append (newpr )
39
41
return retpr
40
42
43
+ def isContributor (self , username ):
44
+ if not self .contributors :
45
+ contributor_list = self .repository .iter_contributors ()
46
+ self .contributors = [contributor ['login' ] for contributor in contributor_list ]
47
+ return username in self .contributors
48
+
49
+ def isCollaborator (username ):
50
+ if username not in self .collaborators :
51
+ self .collaborators [username ] = self .repository .is_collaborator (username )
52
+ return self .repository .is_collaborator (username )
53
+
41
54
42
55
class PullRequest :
43
56
@@ -57,6 +70,21 @@ def __init__(self, repository, number):
57
70
for reaction in reactions :
58
71
content = reaction ['content' ]
59
72
user = reaction ['user' ]
73
+ username = user ['login' ]
74
+
75
+ if 'collaborators_only' in self .repository .rules and self .repository .rules ['collaborators_only' ]:
76
+ if not isCollaborator (username ):
77
+ continue
78
+
79
+ if 'contributors_only' in self .repository .rules and self .repository .rules ['contributors_only' ]:
80
+ if not self .repository .isContributor (username ):
81
+ continue
82
+
83
+ if 'whitelist' in self .repository .rules :
84
+ if username not in self .repository .rules ['whitelist' ]:
85
+ continue
86
+
87
+
60
88
if content == '+1' :
61
89
self .yes .append (user ['login' ])
62
90
elif content == '-1' :
@@ -79,6 +107,9 @@ def daysSinceLastCommit(self):
79
107
delta = commit_date - now
80
108
return delta .days
81
109
110
+ def getIssue (self ):
111
+ return self .repository .repository .issue (self .number )
112
+
82
113
def validate (self ):
83
114
if self .repository .rules == False :
84
115
return False
@@ -87,20 +118,45 @@ def validate(self):
87
118
88
119
def shouldClose (self ):
89
120
if 'timeout' in self .repository .rules :
90
- if self .repository .rules ['timeout' ] < self . daysSinceLastCommit () :
121
+ if self .daysSinceLastCommit () >= self . repository .rules ['timeout' ]:
91
122
return True
92
123
return False
93
124
94
- def merge (self ):
125
+ def vote_merge (self ):
126
+ self .addLabels ([
127
+ 'gc-merged' ,
128
+ 'gc-voters %s' % (len (self .users ),),
129
+ 'gc-yes %s' % (len (self .yes ),),
130
+ 'gc-no %s' % (len (self .no ),),
131
+ 'gc-age %s' % (self .daysSinceLastCommit (),)
132
+ ])
95
133
self .pr .merge ('Consensus Merge' )
96
134
135
+ def addLabels (self , labels ):
136
+ issue = self .getIssue ()
137
+ for label in labels :
138
+ issue .add_labels (label )
139
+
140
+ def getLabelList (self ):
141
+ issue = self .getIssue ()
142
+ return issue .labels
143
+
144
+ def isBlocked (self , pr ):
145
+ labels = [item .lower () for item in self .getLabelList ()]
146
+ if 'wip' in labels :
147
+ return True
148
+ if 'dontmerge' :
149
+ return True
150
+ return False
97
151
98
152
99
153
class Consensus :
100
154
def __init__ (self , rules ):
101
155
self .rules = rules
102
156
103
157
def validate (self , pr ):
158
+ if pr .isBlocked ():
159
+ return False
104
160
if not self .isMergeable (pr ):
105
161
return False
106
162
if not self .hasQuorum (pr ):
@@ -112,7 +168,7 @@ def validate(self, pr):
112
168
return False
113
169
114
170
def isMergeable (self , pr ):
115
- if not pr .mergeable :
171
+ if not pr .pr . mergeable :
116
172
return False
117
173
return True
118
174
0 commit comments