1
- import yaml
2
1
import logging as log
3
- import shlex
4
2
import fnmatch
3
+ import shlex
5
4
6
5
from . import gitlab
7
6
@@ -27,14 +26,24 @@ def refetch_info(self):
27
26
def get_approvers_ce (self ):
28
27
"""get approvers status using thumbs on merge request
29
28
"""
29
+ owner_globs = self .get_codeowners_ce ()
30
+ if len (owner_globs ) == 0 :
31
+ log .info ("No CODEOWNERS file in master, continuing without approvers flow" )
32
+ return dict (self ._info , approvals_left = 0 , approved_by = [], codeowners = [])
33
+
34
+ codeowners = self .determine_responsible_owners (owner_globs , self .get_changes_ce ())
35
+
36
+ if len (codeowners ) == 0 :
37
+ log .info ("No matched code owners, continuing without approvers flow" )
38
+ return dict (self ._info , approvals_left = 0 , approved_by = [], codeowners = [])
30
39
31
- codeowners = self .determine_responsible_owners (self .get_codeowners_ce (), self .get_changes_ce ())
32
40
awards = self .get_awards_ce ()
33
41
34
42
up_votes = [e for e in awards if e ['name' ] == 'thumbsup' and e ['user' ]['username' ] in codeowners ]
35
43
approver_count = len (codeowners )
36
44
approvals_left = max (approver_count - len (up_votes ), 0 )
37
- return dict (self ._info , approvals_left = approvals_left , approved_by = up_votes )
45
+
46
+ return dict (self ._info , approvals_left = approvals_left , approved_by = up_votes , codeowners = codeowners )
38
47
39
48
def determine_responsible_owners (self , owners_glob , changes ):
40
49
owners = set ([])
@@ -44,6 +53,7 @@ def determine_responsible_owners(self, owners_glob, changes):
44
53
owners .update (owners_glob ['*' ])
45
54
46
55
if 'changes' not in changes :
56
+ log .info ("No changes in merge request!?" )
47
57
return owners
48
58
49
59
for change in changes ['changes' ]:
@@ -71,9 +81,9 @@ def get_codeowners_ce(self):
71
81
if config_file is None :
72
82
return owner_globs
73
83
74
- for line in config_file .splitlines ():
84
+ for line in config_file [ 'content' ] .splitlines ():
75
85
if line != "" and not line .startswith (' ' ) and not line .startswith ('#' ):
76
- elements = shlex .split (line ) # line.split()
86
+ elements = shlex .split (line )
77
87
glob = elements .pop (0 )
78
88
owner_globs .setdefault (glob , set ([]))
79
89
@@ -107,18 +117,29 @@ def approver_ids(self):
107
117
"""Return the uids of the approvers."""
108
118
return [who ['user' ]['id' ] for who in self .info ['approved_by' ]]
109
119
120
+ @property
121
+ def codeowners (self ):
122
+ """Only used for gitlab CE"""
123
+ if 'approvers' in self .info :
124
+ return self .info ['codeowners' ]
125
+
126
+ return []
127
+
110
128
def reapprove (self ):
111
129
"""Impersonates the approvers and re-approves the merge_request as them.
112
130
113
131
The idea is that we want to get the approvers, push the rebased branch
114
132
(which may invalidate approvals, depending on GitLab settings) and then
115
133
restore the approval status.
116
134
"""
117
- if self ._api .version ().release >= (9 , 2 , 2 ):
135
+ gitlab_version = self ._api .version ()
136
+
137
+ if gitlab_version .release >= (9 , 2 , 2 ):
118
138
approve_url = '/projects/{0.project_id}/merge_requests/{0.iid}/approve' .format (self )
119
139
else :
120
140
# GitLab botched the v4 api before 9.2.3
121
141
approve_url = '/projects/{0.project_id}/merge_requests/{0.id}/approve' .format (self )
122
142
123
- for uid in self .approver_ids :
124
- self ._api .call (POST (approve_url ), sudo = uid )
143
+ if gitlab_version .is_ee :
144
+ for uid in self .approver_ids :
145
+ self ._api .call (POST (approve_url ), sudo = uid )
0 commit comments