diff --git a/run/centos-ci/cico_gitlab_trigger.py b/run/centos-ci/cico_gitlab_trigger.py index 9e9a7d0ba68ec31a341bab51f2bb3463ab23153e..410c53bec8e5d5b76f50b3d7eeadb52f6ac42198 100644 --- a/run/centos-ci/cico_gitlab_trigger.py +++ b/run/centos-ci/cico_gitlab_trigger.py @@ -161,11 +161,34 @@ class GitlabTrigger(object): for _ in range(3): try: com.comments.create({"note": text}) - return + return True except Exception as e: exc = e time.sleep(1) print(f"Unable to post comment to gitlab:\n{text}\n\nException: {exc}") + return False + + def post_mr_comment(self, text, parent=None, resolved=None): + mr_id = self.merge_request_id + mr = self.gl_project.mergerequests.get(mr_id) + discussions = mr.discussions.list(all=True) + discussion = None + if parent is None: + discussion = mr.discussion.create({"body": text}) + else: + for d in discussions: + notes = d.attributes.get("notes") + for note in notes: + if parent in note["body"]: + discussion = d + break + if discussion is None: + return False + if text: + discussion.notes.create({"body": text}) + if resolved is not None: + discussion.resolved = resolved + return True def play_commit_job(self): pipeline = self.pipeline @@ -191,12 +214,12 @@ class GitlabTrigger(object): @property def merge_request_url(self): - mr_id = None + mr_url = None if self.request_type == "note": - mr_id = self.data["merge_request"]["url"] + mr_url = self.data["merge_request"]["url"] elif self.request_type == "merge_request": - mr_id = self.data["object_attributes"]["url"] - return mr_id + mr_url = self.data["object_attributes"]["url"] + return mr_url @property def repository(self): @@ -294,6 +317,11 @@ class GitlabTrigger(object): exc = e time.sleep(1) print(f"Unable to set commit status in gitlab:\nException: {exc}") + # Post comment directly to MR, if no permissions to set commit status + title = f"Pipeline {pipeline_name}: {os.environ['BUILD_ID']}" + if not self.post_mr_comment("", parent=title): + self.post_mr_comment(title) + self.post_mr_comment(description, parent=title) except Exception as e: print(str(e)) diff --git a/run/centos-ci/node_runner.py b/run/centos-ci/node_runner.py index 7a3ad0ce2957a95c4ac1e5add3d8070b5a069494..c1901a55d0505e94213d2875e984f824985535ef 100644 --- a/run/centos-ci/node_runner.py +++ b/run/centos-ci/node_runner.py @@ -767,7 +767,7 @@ class Runner: self.gitlab.play_commit_job() except Exception: pass - self.gitlab.post_commit_comment(message) + self.gitlab.post_mr_comment(message, resolved=not self.failed_tests) def _generate_junit(self): logging.debug("Generate JUNIT")