Skip to content

Commit 1d21d9f

Browse files
committed
add labels when merging, add user filters
1 parent c1dd369 commit 1d21d9f

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

gitconsensus/repository.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class Repository:
2020
def __init__(self, user, repository):
2121
self.user = user
2222
self.name = repository
23+
self.contributors = False
24+
self.collaborators = {}
2325
auth = config.getGitToken()
2426
self.client = github3.login(token=auth['token'])
2527
self.client.set_user_agent('gitconsensus')
@@ -38,6 +40,17 @@ def getPullRequests(self):
3840
retpr.append(newpr)
3941
return retpr
4042

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+
4154

4255
class PullRequest:
4356

@@ -57,6 +70,21 @@ def __init__(self, repository, number):
5770
for reaction in reactions:
5871
content = reaction['content']
5972
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+
6088
if content == '+1':
6189
self.yes.append(user['login'])
6290
elif content == '-1':
@@ -79,6 +107,9 @@ def daysSinceLastCommit(self):
79107
delta = commit_date - now
80108
return delta.days
81109

110+
def getIssue(self):
111+
return self.repository.repository.issue(self.number)
112+
82113
def validate(self):
83114
if self.repository.rules == False:
84115
return False
@@ -87,20 +118,45 @@ def validate(self):
87118

88119
def shouldClose(self):
89120
if 'timeout' in self.repository.rules:
90-
if self.repository.rules['timeout'] < self.daysSinceLastCommit():
121+
if self.daysSinceLastCommit() >= self.repository.rules['timeout']:
91122
return True
92123
return False
93124

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+
])
95133
self.pr.merge('Consensus Merge')
96134

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
97151

98152

99153
class Consensus:
100154
def __init__(self, rules):
101155
self.rules = rules
102156

103157
def validate(self, pr):
158+
if pr.isBlocked():
159+
return False
104160
if not self.isMergeable(pr):
105161
return False
106162
if not self.hasQuorum(pr):
@@ -112,7 +168,7 @@ def validate(self, pr):
112168
return False
113169

114170
def isMergeable(self, pr):
115-
if not pr.mergeable:
171+
if not pr.pr.mergeable:
116172
return False
117173
return True
118174

0 commit comments

Comments
 (0)