summaryrefslogtreecommitdiff
path: root/zuul/driver/gitlab
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2022-02-18 01:38:08 +0000
committerGerrit Code Review <review@openstack.org>2022-02-18 01:38:08 +0000
commit596dc1e275591d9c203a753e5b258dd47af70434 (patch)
tree44b539f7728c7471a19bc74bea6f3b057f9981a9 /zuul/driver/gitlab
parent7212df93c5d9ebc199b8a9a07e78090a912ba761 (diff)
parent56865e57d2bb56c49be58cfded6f01c60565f2ef (diff)
downloadzuul-596dc1e275591d9c203a753e5b258dd47af70434.tar.gz
Merge "Add support for adding and removing labels in gitlab"
Diffstat (limited to 'zuul/driver/gitlab')
-rw-r--r--zuul/driver/gitlab/gitlabconnection.py23
-rw-r--r--zuul/driver/gitlab/gitlabmodel.py15
-rw-r--r--zuul/driver/gitlab/gitlabreporter.py21
-rw-r--r--zuul/driver/gitlab/gitlabtrigger.py4
4 files changed, 61 insertions, 2 deletions
diff --git a/zuul/driver/gitlab/gitlabconnection.py b/zuul/driver/gitlab/gitlabconnection.py
index 88a35d65a..54dff1fc0 100644
--- a/zuul/driver/gitlab/gitlabconnection.py
+++ b/zuul/driver/gitlab/gitlabconnection.py
@@ -156,6 +156,8 @@ class GitlabEventConnector(threading.Thread):
label in body["changes"]["labels"]["current"]]
new_labels = set(current_labels) - set(previous_labels)
event.labels = list(new_labels)
+ removed_labels = set(previous_labels) - set(current_labels)
+ event.unlabels = list(removed_labels)
elif attrs['action'] in ('approved', 'unapproved'):
event.action = attrs['action']
else:
@@ -443,6 +445,17 @@ class GitlabAPIClient():
raise MergeFailure('Merge request merge failed: %s' % e)
return resp[0]
+ # https://docs.gitlab.com/ee/api/merge_requests.html#update-mr
+ def update_mr(self, project_name, number,
+ zuul_event_id=None,
+ **params):
+ path = "/projects/%s/merge_requests/%s" % (
+ quote_plus(project_name), number)
+ resp = self.put(self.baseurl + path, params=params,
+ zuul_event_id=zuul_event_id)
+ self._manage_error(*resp, zuul_event_id=zuul_event_id)
+ return resp[0]
+
class GitlabConnection(ZKChangeCacheMixin, ZKBranchCacheMixin, BaseConnection):
driver_name = 'gitlab'
@@ -713,6 +726,16 @@ class GitlabConnection(ZKChangeCacheMixin, ZKBranchCacheMixin, BaseConnection):
project_name, number, method, zuul_event_id=event)
log.info("Merged MR %s#%s", project_name, number)
+ def updateMRLabels(self, project_name, mr_number, labels, unlabels,
+ zuul_event_id=None):
+ log = get_annotated_logger(self.log, zuul_event_id)
+ self.gl_client.update_mr(
+ project_name, mr_number, zuul_event_id=zuul_event_id,
+ add_labels=','.join(labels),
+ remove_labels=','.join(unlabels))
+ log.debug("Added labels %s to, and removed labels %s from %s#%s",
+ labels, unlabels, project_name, mr_number)
+
class GitlabWebController(BaseWebController):
diff --git a/zuul/driver/gitlab/gitlabmodel.py b/zuul/driver/gitlab/gitlabmodel.py
index 0d000ebc0..33b13054b 100644
--- a/zuul/driver/gitlab/gitlabmodel.py
+++ b/zuul/driver/gitlab/gitlabmodel.py
@@ -86,6 +86,7 @@ class GitlabTriggerEvent(TriggerEvent):
self.title = None
self.action = None
self.labels = []
+ self.unlabels = []
self.change_number = None
self.merge_request_description_changed = None
self.tag = None
@@ -96,6 +97,7 @@ class GitlabTriggerEvent(TriggerEvent):
d["title"] = self.title
d["action"] = self.action
d["labels"] = self.labels
+ d["unlabels"] = self.unlabels
d["change_number"] = self.change_number
d["merge_request_description_changed"] = \
self.merge_request_description_changed
@@ -108,6 +110,7 @@ class GitlabTriggerEvent(TriggerEvent):
self.title = d["title"]
self.action = d["action"]
self.labels = d["labels"]
+ self.unlabels = d.get("unlabels", [])
self.change_number = d["change_number"]
self.merge_request_description_changed = \
d["merge_request_description_changed"]
@@ -122,6 +125,8 @@ class GitlabTriggerEvent(TriggerEvent):
r.append("mr:%s" % self.change_number)
if self.labels:
r.append("labels:%s" % ', '.join(self.labels))
+ if self.unlabels:
+ r.append("unlabels:%s" % ', '.join(self.unlabels))
return ' '.join(r)
def isPatchsetCreated(self):
@@ -136,7 +141,8 @@ class GitlabTriggerEvent(TriggerEvent):
class GitlabEventFilter(EventFilter):
def __init__(
self, connection_name, trigger, types=None, actions=None,
- comments=None, refs=None, labels=None, ignore_deletes=True):
+ comments=None, refs=None, labels=None, unlabels=None,
+ ignore_deletes=True):
super().__init__(connection_name, trigger)
self._types = types or []
self.types = [re.compile(x) for x in self._types]
@@ -146,6 +152,7 @@ class GitlabEventFilter(EventFilter):
self._refs = refs or []
self.refs = [re.compile(x) for x in self._refs]
self.labels = labels or []
+ self.unlabels = unlabels or []
self.ignore_deletes = ignore_deletes
def __repr__(self):
@@ -164,6 +171,8 @@ class GitlabEventFilter(EventFilter):
ret += ' ignore_deletes: %s' % self.ignore_deletes
if self.labels:
ret += ' labels: %s' % ', '.join(self.labels)
+ if self.unlabels:
+ ret += ' unlabels: %s' % ', '.join(self.unlabels)
ret += '>'
return ret
@@ -210,6 +219,10 @@ class GitlabEventFilter(EventFilter):
if not set(event.labels).intersection(set(self.labels)):
return False
+ if self.unlabels:
+ if not set(event.unlabels).intersection(set(self.unlabels)):
+ return False
+
return True
diff --git a/zuul/driver/gitlab/gitlabreporter.py b/zuul/driver/gitlab/gitlabreporter.py
index a38c8ac54..c3abf3dd8 100644
--- a/zuul/driver/gitlab/gitlabreporter.py
+++ b/zuul/driver/gitlab/gitlabreporter.py
@@ -21,6 +21,7 @@ from zuul.model import MERGER_MERGE_RESOLVE, MERGER_MERGE, MERGER_MAP, \
MERGER_SQUASH_MERGE
from zuul.lib.logutil import get_annotated_logger
from zuul.driver.gitlab.gitlabsource import GitlabSource
+from zuul.driver.util import scalar_or_list
from zuul.exceptions import MergeFailure
@@ -42,6 +43,12 @@ class GitlabReporter(BaseReporter):
self._create_comment = self.config.get('comment', True)
self._approval = self.config.get('approval', None)
self._merge = self.config.get('merge', False)
+ self._labels = self.config.get('label', [])
+ if not isinstance(self._labels, list):
+ self._labels = [self._labels]
+ self._unlabels = self.config.get('unlabel', [])
+ if not isinstance(self._unlabels, list):
+ self._unlabels = [self._unlabels]
def report(self, item):
"""Report on an event."""
@@ -57,6 +64,8 @@ class GitlabReporter(BaseReporter):
self.addMRComment(item)
if self._approval is not None:
self.setApproval(item)
+ if self._labels or self._unlabels:
+ self.setLabels(item)
if self._merge:
self.mergeMR(item)
if not item.change.is_merged:
@@ -83,6 +92,16 @@ class GitlabReporter(BaseReporter):
self.connection.approveMR(project, mr_number, patchset,
self._approval, event=item.event)
+ def setLabels(self, item):
+ log = get_annotated_logger(self.log, item.event)
+ project = item.change.project.name
+ mr_number = item.change.number
+ log.debug('Reporting change %s, params %s, labels: %s, unlabels: %s',
+ item.change, self.config, self._labels, self._unlabels)
+ self.connection.updateMRLabels(project, mr_number,
+ self._labels, self._unlabels,
+ zuul_event_id=item.event)
+
def mergeMR(self, item):
project = item.change.project.name
mr_number = item.change.number
@@ -120,5 +139,7 @@ def getSchema():
'comment': bool,
'approval': bool,
'merge': bool,
+ 'label': scalar_or_list(str),
+ 'unlabel': scalar_or_list(str),
})
return gitlab_reporter
diff --git a/zuul/driver/gitlab/gitlabtrigger.py b/zuul/driver/gitlab/gitlabtrigger.py
index eba8787a1..0fdc2ff2f 100644
--- a/zuul/driver/gitlab/gitlabtrigger.py
+++ b/zuul/driver/gitlab/gitlabtrigger.py
@@ -34,6 +34,7 @@ class GitlabTrigger(BaseTrigger):
comments=to_list(trigger.get('comment')),
refs=to_list(trigger.get('ref')),
labels=to_list(trigger.get('labels')),
+ unlabels=to_list(trigger.get('unlabels')),
)
efilters.append(f)
return efilters
@@ -53,6 +54,7 @@ def getSchema():
'action': scalar_or_list(str),
'comment': scalar_or_list(str),
'ref': scalar_or_list(str),
- 'labels': scalar_or_list(str)
+ 'labels': scalar_or_list(str),
+ 'unlabels': scalar_or_list(str),
}
return gitlab_trigger