| 1 | from flask import abort |
| 2 | |
| 3 | from ui import config |
| 4 | from ui.gitlab import api |
| 5 | from ui.models import Build, Runner, db |
| 6 | from ui.slack.webhook import post_build |
| 7 | |
| 8 | def process(request): |
| 9 | if request.headers.get('X-Gitlab-Token', None) != config.GITLAB_WEBHOOK_TOKEN: |
| 10 | abort(403) |
| 11 | webhook_type = request.headers.get('X-Gitlab-Event') |
| 12 | data = request.get_json() |
| 13 | if webhook_type == 'Pipeline Hook': |
| 14 | pipeline = data.get('object_attributes') |
| 15 | stages = data.get('builds') |
| 16 | build = Build.get_or_create_by_id(pipeline.get('id')) |
| 17 | build.build_status = pipeline.get('status') |
| 18 | build.build_duration = pipeline.get('duration') |
| 19 | for variable in pipeline.get("variables"): |
| 20 | if variable.get('key') == "VERSION": |
| 21 | build.build_version = variable.get("value") |
| 22 | elif variable.get("key") == "DEVICE": |
| 23 | build.build_device = variable.get("value") |
| 24 | elif variable.get("key") == "TYPE": |
| 25 | build.build_type = variable.get("value") |
| 26 | build_stage = {} |
| 27 | for stage in stages: |
| 28 | if stage.get('name') == 'build': |
| 29 | build_stage = stage |
| 30 | runner = build_stage.get("runner") |
| 31 | print(type(runner)) |
| 32 | if runner: |
| 33 | build.build_runner = Runner.get_or_create_by_id(runner.get('id')) |
| 34 | if not build.build_runner.runner_name: |
| 35 | build.build_runner.runner_name = runner.get("description") |
| 36 | db.session.add(build) |
| 37 | db.session.commit() |
| 38 | if build.build_status in ['failed', 'canceled']: |
| 39 | post_build(build.build_status, build.build_device, build.build_version, build.build_type, build.build_id) |