Browse Source

Merge "[gitlab] approvals: fix error with community edition"

changes/40/751140/7
Zuul 1 day ago
committed by Gerrit Code Review
parent
commit
b3e9130e55
3 changed files with 44 additions and 4 deletions
  1. +15
    -3
      tests/base.py
  2. +23
    -0
      tests/unit/test_gitlab_driver.py
  3. +6
    -1
      zuul/driver/gitlab/gitlabconnection.py

+ 15
- 3
tests/base.py View File

@@ -1628,6 +1628,12 @@ class FakeGitlabConnection(gitlabconnection.GitlabConnection):
}
return (name, data)

@contextmanager
def enable_community_edition(self):
self.gl_client.community_edition = True
yield
self.gl_client.community_edition = False


class FakeGitlabAPIClient(gitlabconnection.GitlabAPIClient):
log = logging.getLogger("zuul.test.FakeGitlabAPIClient")
@@ -1635,6 +1641,7 @@ class FakeGitlabAPIClient(gitlabconnection.GitlabAPIClient):
def __init__(self, baseurl, api_token, merge_requests_db={}):
super(FakeGitlabAPIClient, self).__init__(baseurl, api_token)
self.merge_requests = merge_requests_db
self.community_edition = False

def gen_error(self, verb):
return {
@@ -1676,9 +1683,14 @@ class FakeGitlabAPIClient(gitlabconnection.GitlabAPIClient):
r'.+/projects/(.+)/merge_requests/(\d+)/approvals$', url)
if match:
mr = self._get_mr(match)
return {
'approvals_left': 0 if mr.approved else 1,
}, 200, "", "GET"
if not self.community_edition:
return {
'approvals_left': 0 if mr.approved else 1,
}, 200, "", "GET"
else:
return {
'approved': mr.approved,
}, 200, "", "GET"

def post(self, url, params=None, zuul_event_id=None):



+ 23
- 0
tests/unit/test_gitlab_driver.py View File

@@ -559,6 +559,29 @@ class TestGitlabDriver(ZuulTestCase):
self.waitUntilSettled()
self.assertEqual(1, len(self.history))

@simple_layout('layouts/requirements-gitlab.yaml', driver='gitlab')
def test_approval_require_community_edition(self):

with self.fake_gitlab.enable_community_edition():
A = self.fake_gitlab.openFakeMergeRequest(
'org/project2', 'master', 'A')

self.fake_gitlab.emitEvent(A.getMergeRequestOpenedEvent())
self.waitUntilSettled()
self.assertEqual(0, len(self.history))

A.approved = True

self.fake_gitlab.emitEvent(A.getMergeRequestUpdatedEvent())
self.waitUntilSettled()
self.assertEqual(1, len(self.history))

A.approved = False

self.fake_gitlab.emitEvent(A.getMergeRequestUpdatedEvent())
self.waitUntilSettled()
self.assertEqual(1, len(self.history))

@simple_layout('layouts/requirements-gitlab.yaml', driver='gitlab')
def test_label_require(self):



+ 6
- 1
zuul/driver/gitlab/gitlabconnection.py View File

@@ -554,7 +554,12 @@ class GitlabConnection(BaseConnection):
mr_approval_status = self.gl_client.get_mr_approvals_status(
project_name, number, zuul_event_id=event)
log.info('Got MR approval status %s#%s', project_name, number)
mr['approved'] = mr_approval_status['approvals_left'] == 0
if 'approvals_left' in mr_approval_status:
# 'approvals_left' is not present when 'Required Merge Request
# Approvals' feature isn't available
mr['approved'] = mr_approval_status['approvals_left'] == 0
else:
mr['approved'] = mr_approval_status['approved']
return mr

def commentMR(self, project_name, number, message, event=None):


Loading…
Cancel
Save