1- import yaml
21import logging as log
3- import shlex
42import fnmatch
3+ import shlex
54
65from . import gitlab
76
@@ -27,14 +26,24 @@ def refetch_info(self):
2726 def get_approvers_ce (self ):
2827 """get approvers status using thumbs on merge request
2928 """
29+ owner_globs = self .get_codeowners_ce ()
30+ if not owner_globs :
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 not codeowners :
37+ log .info ("No matched code owners, continuing without approvers flow" )
38+ return dict (self ._info , approvals_left = 0 , approved_by = [], codeowners = [])
3039
31- codeowners = self .determine_responsible_owners (self .get_codeowners_ce (), self .get_changes_ce ())
3240 awards = self .get_awards_ce ()
3341
3442 up_votes = [e for e in awards if e ['name' ] == 'thumbsup' and e ['user' ]['username' ] in codeowners ]
3543 approver_count = len (codeowners )
3644 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 )
3847
3948 def determine_responsible_owners (self , owners_glob , changes ):
4049 owners = set ([])
@@ -44,6 +53,7 @@ def determine_responsible_owners(self, owners_glob, changes):
4453 owners .update (owners_glob ['*' ])
4554
4655 if 'changes' not in changes :
56+ log .info ("No changes in merge request!?" )
4757 return owners
4858
4959 for change in changes ['changes' ]:
@@ -71,9 +81,9 @@ def get_codeowners_ce(self):
7181 if config_file is None :
7282 return owner_globs
7383
74- for line in config_file .splitlines ():
84+ for line in config_file [ 'content' ] .splitlines ():
7585 if line != "" and not line .startswith (' ' ) and not line .startswith ('#' ):
76- elements = shlex .split (line ) # line.split()
86+ elements = shlex .split (line )
7787 glob = elements .pop (0 )
7888 owner_globs .setdefault (glob , set ([]))
7989
@@ -107,18 +117,29 @@ def approver_ids(self):
107117 """Return the uids of the approvers."""
108118 return [who ['user' ]['id' ] for who in self .info ['approved_by' ]]
109119
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+
110128 def reapprove (self ):
111129 """Impersonates the approvers and re-approves the merge_request as them.
112130
113131 The idea is that we want to get the approvers, push the rebased branch
114132 (which may invalidate approvals, depending on GitLab settings) and then
115133 restore the approval status.
116134 """
117- if self ._api .version ().release >= (9 , 2 , 2 ):
135+ gitlab_version = self ._api .version ()
136+
137+ if gitlab_version .release >= (9 , 2 , 2 ):
118138 approve_url = '/projects/{0.project_id}/merge_requests/{0.iid}/approve' .format (self )
119139 else :
120140 # GitLab botched the v4 api before 9.2.3
121141 approve_url = '/projects/{0.project_id}/merge_requests/{0.id}/approve' .format (self )
122142
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