Commit | Line | Data |
---|---|---|
c95384ec TP |
1 | import datetime |
2 | ||
31329c3f TP |
3 | from flask import abort |
4 | ||
5 | from ui import config | |
6 | from ui.gitlab import api | |
7 | from ui.models import Build, Runner, db | |
089ea75b | 8 | from ui.slack.webhook import post_build |
31329c3f TP |
9 | |
10 | def process(request): | |
11 | if request.headers.get('X-Gitlab-Token', None) != config.GITLAB_WEBHOOK_TOKEN: | |
12 | abort(403) | |
13 | webhook_type = request.headers.get('X-Gitlab-Event') | |
14 | data = request.get_json() | |
15 | if webhook_type == 'Pipeline Hook': | |
16 | pipeline = data.get('object_attributes') | |
17 | stages = data.get('builds') | |
18 | build = Build.get_or_create_by_id(pipeline.get('id')) | |
6907b2b7 | 19 | build.build_status = pipeline.get('status') |
dd11ceff TP |
20 | if build.build_status == "success": |
21 | build.build_duration = pipeline.get('duration') | |
31329c3f TP |
22 | for variable in pipeline.get("variables"): |
23 | if variable.get('key') == "VERSION": | |
24 | build.build_version = variable.get("value") | |
25 | elif variable.get("key") == "DEVICE": | |
26 | build.build_device = variable.get("value") | |
e8bc2586 | 27 | elif variable.get("key") == "RELEASE_TYPE": |
31329c3f TP |
28 | build.build_type = variable.get("value") |
29 | build_stage = {} | |
30 | for stage in stages: | |
31 | if stage.get('name') == 'build': | |
32 | build_stage = stage | |
6907b2b7 | 33 | runner = build_stage.get("runner") |
6907b2b7 | 34 | if runner: |
ee9e64d3 | 35 | build.build_runner = Runner.get_or_create_by_id(str(runner.get('id'))) |
6907b2b7 TP |
36 | if not build.build_runner.runner_name: |
37 | build.build_runner.runner_name = runner.get("description") | |
c95384ec TP |
38 | date = datetime.datetime.strptime(build_stage.get("created_at"), "%Y-%m-%d %H:%M:%S UTC") |
39 | build.build_date = date | |
31329c3f TP |
40 | db.session.add(build) |
41 | db.session.commit() | |
089ea75b TP |
42 | if build.build_status in ['failed', 'canceled']: |
43 | post_build(build.build_status, build.build_device, build.build_version, build.build_type, build.build_id) |