|
| 1 | +import gitlab |
| 2 | +import html |
| 3 | +import re |
| 4 | +from app.pkgs.devops.devops_interface import DevopsInterface |
| 5 | +from config import GITLAB_TOKEN, GITLAB_URL, GITLAB_CLONE_URL |
| 6 | + |
| 7 | +class DevopsGitlab(DevopsInterface): |
| 8 | + def triggerPipeline(self, branch_name, repopath): |
| 9 | + try: |
| 10 | + gl = gitlab.Gitlab(GITLAB_URL, GITLAB_TOKEN, api_version='4') |
| 11 | + |
| 12 | + project = gl.projects.get(repopath) |
| 13 | + pipeline = project.pipelines.create({'ref': branch_name}) |
| 14 | + pipeline_url = GITLAB_URL + \ |
| 15 | + repopath + '/-/pipelines/' + str(pipeline.id) |
| 16 | + return "Get pipline status...", str(pipeline.get_id()), pipeline_url, True |
| 17 | + except Exception as e: |
| 18 | + return "Failed to trigger pipline:" + str(e), 0, "", False |
| 19 | + |
| 20 | + |
| 21 | + def getPipelineStatus(self, pipline_id, repopath): |
| 22 | + try: |
| 23 | + gl = gitlab.Gitlab(GITLAB_URL, GITLAB_TOKEN, api_version='4') |
| 24 | + |
| 25 | + project = gl.projects.get(repopath) |
| 26 | + |
| 27 | + pipeline = project.pipelines.get(pipline_id) |
| 28 | + |
| 29 | + print("pipeline:", pipeline.status) |
| 30 | + |
| 31 | + jobs = pipeline.jobs.list() |
| 32 | + |
| 33 | + job_info = [] |
| 34 | + for job in jobs: |
| 35 | + print("job:", job) |
| 36 | + job_info.append({ |
| 37 | + 'job_id': job.id, |
| 38 | + 'job_name': job.name, |
| 39 | + 'status': job.status, |
| 40 | + 'duration': job.duration, |
| 41 | + 'log': get_pipeline_job_logs(repopath, pipline_id, job.id) |
| 42 | + }) |
| 43 | + |
| 44 | + return list(reversed(job_info)) |
| 45 | + except Exception as e: |
| 46 | + return "Failed to get pipline status:" + str(e) |
| 47 | + |
| 48 | + def getPipelineJobLogs(self, repopath, pipeline_id, job_id): |
| 49 | + try: |
| 50 | + gl = gitlab.Gitlab(GITLAB_URL, GITLAB_TOKEN, api_version='4') |
| 51 | + |
| 52 | + project = gl.projects.get(repopath) |
| 53 | + job = project.jobs.get(job_id) |
| 54 | + logs = job.trace() |
| 55 | + |
| 56 | + return self.remove_color_codes(logs) |
| 57 | + except Exception as e: |
| 58 | + return "Failed to get log: " + str(e) |
| 59 | + |
| 60 | + |
| 61 | + def remove_color_codes(log_string): |
| 62 | + unicode_string = log_string.decode('unicode_escape') |
| 63 | + color_regex = re.compile(r'\x1b\[[0-9;]*m') |
| 64 | + cleaned_string = re.sub(color_regex, '', unicode_string) |
| 65 | + cleaned_string = re.sub(r'\n', '<br>', unicode_string) |
| 66 | + cleaned_string = re.sub(r'\r', '<br>', cleaned_string) |
| 67 | + cleaned_string = re.sub('"', ' ', cleaned_string) |
| 68 | + cleaned_string = re.sub("'", ' ', cleaned_string) |
| 69 | + cleaned_string = re.sub( |
| 70 | + r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]', ' ', cleaned_string) |
| 71 | + cleaned_string = html.escape(cleaned_string) |
| 72 | + return cleaned_string |
| 73 | + |
| 74 | + # def getFileContent(self, file_path, branch_name, repopath): |
| 75 | + # try: |
| 76 | + # print("get_file_content-repopath:" + repopath) |
| 77 | + # print("get_file_content-branch_name:" + branch_name) |
| 78 | + # print("get_file_content-file_path:" + file_path) |
| 79 | + # gl = gitlab.Gitlab(GITLAB_URL, private_token=GITLAB_TOKEN, api_version='4') |
| 80 | + |
| 81 | + # project = gl.projects.get(repopath) |
| 82 | + |
| 83 | + # file = project.files.get(file_path, ref=branch_name) |
| 84 | + |
| 85 | + # content = codecs.decode(file.decode(), 'utf-8') |
| 86 | + |
| 87 | + # print(content) |
| 88 | + |
| 89 | + # return True, content |
| 90 | + # except Exception as e: |
| 91 | + # return False, str(e) |
0 commit comments