2750d9406e9f9abf58c44f2f7e139bbd906cedcb
[GitLab/stricted-build/lineage_builder.git] / ui / gitlab / webhooks.py
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
7 def process(request):
8 if request.headers.get('X-Gitlab-Token', None) != config.GITLAB_WEBHOOK_TOKEN:
9 abort(403)
10 webhook_type = request.headers.get('X-Gitlab-Event')
11 data = request.get_json()
12 if webhook_type == 'Pipeline Hook':
13 pipeline = data.get('object_attributes')
14 stages = data.get('builds')
15 build = Build.get_or_create_by_id(pipeline.get('id'))
16 build.build_status = pipeline.get('status')
17 build.build_duration = pipeline.get('duration')
18 for variable in pipeline.get("variables"):
19 if variable.get('key') == "VERSION":
20 build.build_version = variable.get("value")
21 elif variable.get("key") == "DEVICE":
22 build.build_device = variable.get("value")
23 elif variable.get("key") == "TYPE":
24 build.build_type = variable.get("value")
25 build_stage = {}
26 for stage in stages:
27 if stage.get('name') == 'build':
28 build_stage = stage
29 runner = build_stage.get("runner")
30 print(type(runner))
31 if runner:
32 build.build_runner = Runner.get_or_create_by_id(runner.get('id'))
33 if not build.build_runner.runner_name:
34 build.build_runner.runner_name = runner.get("description")
35 db.session.add(build)
36 db.session.commit()