diff options
Diffstat (limited to 'zuul/driver')
-rw-r--r-- | zuul/driver/gitlab/gitlabconnection.py | 28 | ||||
-rw-r--r-- | zuul/driver/gitlab/gitlabreporter.py | 29 |
2 files changed, 54 insertions, 3 deletions
diff --git a/zuul/driver/gitlab/gitlabconnection.py b/zuul/driver/gitlab/gitlabconnection.py index 363825d64..ef2e216ba 100644 --- a/zuul/driver/gitlab/gitlabconnection.py +++ b/zuul/driver/gitlab/gitlabconnection.py @@ -184,7 +184,7 @@ class GitlabAPIClient(): self.session = requests.Session() self.baseurl = '%s/api/v4/' % baseurl self.api_token = api_token - self.headers = {'Authorization': 'Authorization: Bearer %s' % ( + self.headers = {'Authorization': 'Bearer %s' % ( self.api_token)} def _manage_error(self, data, code, url, verb, zuul_event_id=None): @@ -204,6 +204,15 @@ class GitlabAPIClient(): ret.status_code, ret.text)) return ret.json(), ret.status_code, ret.url, 'GET' + def post(self, url, params=None, zuul_event_id=None): + log = get_annotated_logger(self.log, zuul_event_id) + log.info( + "Posting on resource %s, params (%s) ..." % (url, params)) + ret = self.session.post(url, data=params, headers=self.headers) + log.debug("POST returned (code: %s): %s" % ( + ret.status_code, ret.text)) + return ret.json(), ret.status_code, ret.url, 'POST' + # https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr def get_mr(self, project_name, number, zuul_event_id=None): path = "/projects/%s/merge_requests/%s" % ( @@ -220,6 +229,17 @@ class GitlabAPIClient(): self._manage_error(*resp, zuul_event_id=zuul_event_id) return [branch['name'] for branch in resp[0]] + # https://docs.gitlab.com/ee/api/notes.html#create-new-merge-request-note + def comment_mr(self, project_name, number, msg, zuul_event_id=None): + path = "/projects/%s/merge_requests/%s/notes" % ( + quote_plus(project_name), number) + params = {'body': msg} + resp = self.post( + 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(BaseConnection): driver_name = 'gitlab' @@ -393,6 +413,12 @@ class GitlabConnection(BaseConnection): log.info('Got MR %s#%s', project_name, number) return mr + def commentMR(self, project_name, number, message, event=None): + log = get_annotated_logger(self.log, event) + self.gl_client.comment_mr( + project_name, number, message, zuul_event_id=event) + log.info("Commented on MR %s#%s", project_name, number) + class GitlabWebController(BaseWebController): diff --git a/zuul/driver/gitlab/gitlabreporter.py b/zuul/driver/gitlab/gitlabreporter.py index 753239349..ab14887a8 100644 --- a/zuul/driver/gitlab/gitlabreporter.py +++ b/zuul/driver/gitlab/gitlabreporter.py @@ -16,6 +16,8 @@ import logging import voluptuous as v from zuul.reporter import BaseReporter +from zuul.lib.logutil import get_annotated_logger +from zuul.driver.gitlab.gitlabsource import GitlabSource class GitlabReporter(BaseReporter): @@ -26,10 +28,30 @@ class GitlabReporter(BaseReporter): def __init__(self, driver, connection, pipeline, config=None): super(GitlabReporter, self).__init__(driver, connection, config) + self._create_comment = self.config.get('comment', True) def report(self, item): """Report on an event.""" - raise NotImplementedError() + if not isinstance(item.change.project.source, GitlabSource): + return + + if item.change.project.source.connection.canonical_hostname != \ + self.connection.canonical_hostname: + return + + if hasattr(item.change, 'number'): + if self._create_comment: + self.addMRComment(item) + + def addMRComment(self, item, comment=None): + log = get_annotated_logger(self.log, item.event) + message = comment or self._formatItemReport(item) + project = item.change.project.name + pr_number = item.change.number + log.debug('Reporting change %s, params %s, message: %s', + item.change, self.config, message) + self.connection.commentMR(project, pr_number, message, + event=item.event) def mergePull(self, item): raise NotImplementedError() @@ -39,4 +61,7 @@ class GitlabReporter(BaseReporter): def getSchema(): - return v.Schema({}) + gitlab_reporter = v.Schema({ + 'comment': bool, + }) + return gitlab_reporter |