From: Tom Powell Date: Mon, 29 Oct 2018 00:47:03 +0000 (-0700) Subject: Fix webhooks X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=6907b2b7e2eee5343ce1e9ef21d2d0aa3d2d41ae;p=GitLab%2Fstricted-build%2Flineage_builder.git Fix webhooks --- diff --git a/test.py b/test.py index 76898c1..8f9ddab 100644 --- a/test.py +++ b/test.py @@ -74,7 +74,27 @@ class TestWebhooks(UsesModels): assert response.status_code == 200 build = Build.query.filter_by(build_id=1).first() assert build.build_id == 1 - assert build.build_runner_id == "foobar" + assert build.build_runner_id == None + + def test_success(self): + with open("tests/pipeline_success.json") as f: + response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') + assert response.status_code == 200 + runner = Runner.query.filter_by(runner_id="530365").first() + build = Build.query.filter_by(build_id=34618162).first() + assert build.build_runner.runner_name == "phenom-test" + assert build.build_version == "lineage-15.1" + assert build.build_device == "FAKE_TEST" + print(build.build_status) + assert build.build_status == "success" + assert build.build_duration == 81 + + + def test_start(self): + with open("tests/pipeline_start.json") as f: + response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') + assert response.status_code == 200 + class TestWeb(UsesModels): diff --git a/tests/pipeline_start.json b/tests/pipeline_start.json new file mode 100644 index 0000000..f77f9ec --- /dev/null +++ b/tests/pipeline_start.json @@ -0,0 +1,163 @@ +{ + "object_kind": "pipeline", + "object_attributes": { + "id": 34618162, + "ref": "master", + "tag": false, + "sha": "e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "before_sha": "0000000000000000000000000000000000000000", + "status": "running", + "detailed_status": "running", + "stages": [ + "build", + "sign" + ], + "created_at": "2018-10-29 00:01:33 UTC", + "finished_at": "2018-10-29 00:04:42 UTC", + "duration": 81, + "variables": [ + { + "key": "DEVICE", + "value": "FAKE_TEST" + }, + { + "key": "TYPE", + "value": "userdebug" + }, + { + "key": "VERSION", + "value": "lineage-15.1" + } + ] + }, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "project": { + "id": 9081391, + "name": "android", + "description": "", + "web_url": "https://gitlab.com/LineageOS/builder/android", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:LineageOS/builder/android.git", + "git_http_url": "https://gitlab.com/LineageOS/builder/android.git", + "namespace": "LineageOS Builder", + "visibility_level": 20, + "path_with_namespace": "LineageOS/builder/android", + "default_branch": "master", + "ci_config_path": "" + }, + "commit": { + "id": "e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "message": "sleep is for the weak\\n", + "timestamp": "2018-10-27T07:26:39Z", + "url": "https://gitlab.com/LineageOS/builder/android/commit/e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "author": { + "name": "Tom Powell", + "email": "tom@powell.io" + } + }, + "builds": [ + { + "id": 113643062, + "stage": "build", + "name": "build", + "status": "pending", + "created_at": "2018-10-29 00:11:14 UTC", + "started_at": null, + "finished_at": null, + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": null, + "artifacts_file": { + "filename": null, + "size": 0 + } + }, + { + "id": 113642275, + "stage": "build", + "name": "build", + "status": "success", + "created_at": "2018-10-29 00:03:41 UTC", + "started_at": "2018-10-29 00:03:41 UTC", + "finished_at": "2018-10-29 00:04:42 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 530365, + "description": "phenom-test", + "active": true, + "is_shared": false + }, + "artifacts_file": { + "filename": null, + "size": 1075 + } + }, + { + "id": 113642098, + "stage": "build", + "name": "build", + "status": "success", + "created_at": "2018-10-29 00:01:33 UTC", + "started_at": "2018-10-29 00:01:33 UTC", + "finished_at": "2018-10-29 00:02:35 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 530365, + "description": "phenom-test", + "active": true, + "is_shared": false + }, + "artifacts_file": { + "filename": null, + "size": 1075 + } + }, + { + "id": 113642099, + "stage": "sign", + "name": "sign", + "status": "success", + "created_at": "2018-10-29 00:01:33 UTC", + "started_at": "2018-10-29 00:02:35 UTC", + "finished_at": "2018-10-29 00:02:55 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 380987, + "description": "shared-runners-manager-6.gitlab.com", + "active": true, + "is_shared": true + }, + "artifacts_file": { + "filename": null, + "size": 1345 + } + } + ] +} diff --git a/tests/pipeline_success.json b/tests/pipeline_success.json new file mode 100644 index 0000000..d68edba --- /dev/null +++ b/tests/pipeline_success.json @@ -0,0 +1,168 @@ +{ + "object_kind": "pipeline", + "object_attributes": { + "id": 34618162, + "ref": "master", + "tag": false, + "sha": "e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "before_sha": "0000000000000000000000000000000000000000", + "status": "success", + "detailed_status": "passed", + "stages": [ + "build", + "sign" + ], + "created_at": "2018-10-29 00:01:33 UTC", + "finished_at": "2018-10-29 00:12:16 UTC", + "duration": 81, + "variables": [ + { + "key": "DEVICE", + "value": "FAKE_TEST" + }, + { + "key": "TYPE", + "value": "userdebug" + }, + { + "key": "VERSION", + "value": "lineage-15.1" + } + ] + }, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "project": { + "id": 9081391, + "name": "android", + "description": "", + "web_url": "https://gitlab.com/LineageOS/builder/android", + "avatar_url": null, + "git_ssh_url": "git@gitlab.com:LineageOS/builder/android.git", + "git_http_url": "https://gitlab.com/LineageOS/builder/android.git", + "namespace": "LineageOS Builder", + "visibility_level": 20, + "path_with_namespace": "LineageOS/builder/android", + "default_branch": "master", + "ci_config_path": "" + }, + "commit": { + "id": "e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "message": "sleep is for the weak\\n", + "timestamp": "2018-10-27T07:26:39Z", + "url": "https://gitlab.com/LineageOS/builder/android/commit/e36217f9fbf04f7ac7024a7909332c0e0e2c1599", + "author": { + "name": "Tom Powell", + "email": "tom@powell.io" + } + }, + "builds": [ + { + "id": 113643062, + "stage": "build", + "name": "build", + "status": "success", + "created_at": "2018-10-29 00:11:14 UTC", + "started_at": "2018-10-29 00:11:15 UTC", + "finished_at": "2018-10-29 00:12:16 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 530365, + "description": "phenom-test", + "active": true, + "is_shared": false + }, + "artifacts_file": { + "filename": null, + "size": 0 + } + }, + { + "id": 113642275, + "stage": "build", + "name": "build", + "status": "success", + "created_at": "2018-10-29 00:03:41 UTC", + "started_at": "2018-10-29 00:03:41 UTC", + "finished_at": "2018-10-29 00:04:42 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 530365, + "description": "phenom-test", + "active": true, + "is_shared": false + }, + "artifacts_file": { + "filename": null, + "size": 1075 + } + }, + { + "id": 113642098, + "stage": "build", + "name": "build", + "status": "success", + "created_at": "2018-10-29 00:01:33 UTC", + "started_at": "2018-10-29 00:01:33 UTC", + "finished_at": "2018-10-29 00:02:35 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 530365, + "description": "phenom-test", + "active": true, + "is_shared": false + }, + "artifacts_file": { + "filename": null, + "size": 1075 + } + }, + { + "id": 113642099, + "stage": "sign", + "name": "sign", + "status": "success", + "created_at": "2018-10-29 00:01:33 UTC", + "started_at": "2018-10-29 00:02:35 UTC", + "finished_at": "2018-10-29 00:02:55 UTC", + "when": "on_success", + "manual": false, + "user": { + "name": "Tom Powell", + "username": "zifnab06", + "avatar_url": "https://secure.gravatar.com/avatar/40b74a6668897c1bbc7849defa0559ce?s=80&d=identicon" + }, + "runner": { + "id": 380987, + "description": "shared-runners-manager-6.gitlab.com", + "active": true, + "is_shared": true + }, + "artifacts_file": { + "filename": null, + "size": 1345 + } + } + ] +} diff --git a/tests/pipeline_webhook.json b/tests/pipeline_webhook.json index 510cc48..5d8e2e7 100644 --- a/tests/pipeline_webhook.json +++ b/tests/pipeline_webhook.json @@ -37,7 +37,7 @@ "username": "root", "avatar_url": "" }, - "runner": "foobar" + "runner": null }, { "id": 377, @@ -45,7 +45,7 @@ "name": "sign", "status": "success", "created_at": "2016-08-12 15:23:28 UTC", - "runner": "foobar" + "runner": null } ] } diff --git a/ui/gitlab/webhooks.py b/ui/gitlab/webhooks.py index 16612d3..2750d94 100644 --- a/ui/gitlab/webhooks.py +++ b/ui/gitlab/webhooks.py @@ -13,8 +13,8 @@ def process(request): pipeline = data.get('object_attributes') stages = data.get('builds') build = Build.get_or_create_by_id(pipeline.get('id')) - build.status = pipeline.get('status') - build.duration = pipeline.get('duration') + build.build_status = pipeline.get('status') + build.build_duration = pipeline.get('duration') for variable in pipeline.get("variables"): if variable.get('key') == "VERSION": build.build_version = variable.get("value") @@ -26,7 +26,11 @@ def process(request): for stage in stages: if stage.get('name') == 'build': build_stage = stage - if build_stage.get("runner"): - build.build_runner = Runner.get_or_create_by_id(build_stage['runner']) + runner = build_stage.get("runner") + print(type(runner)) + if runner: + build.build_runner = Runner.get_or_create_by_id(runner.get('id')) + if not build.build_runner.runner_name: + build.build_runner.runner_name = runner.get("description") db.session.add(build) db.session.commit() diff --git a/ui/migrations/versions/448614888e24_add_duration.py b/ui/migrations/versions/448614888e24_add_duration.py new file mode 100644 index 0000000..f25ace0 --- /dev/null +++ b/ui/migrations/versions/448614888e24_add_duration.py @@ -0,0 +1,30 @@ +"""add duration + +Revision ID: 448614888e24 +Revises: c3a5cacf3bff +Create Date: 2018-10-28 17:34:59.818607 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '448614888e24' +down_revision = 'c3a5cacf3bff' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('build', sa.Column('build_duration', sa.Integer(), nullable=True)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('build') as batch_op: + batch_op.drop_column('build_duration') +# op.drop_column('build', 'build_duration') + # ### end Alembic commands ### diff --git a/ui/models.py b/ui/models.py index a65fb16..0abbe70 100644 --- a/ui/models.py +++ b/ui/models.py @@ -9,6 +9,7 @@ class Build(db.Model): build_version = db.Column(db.String) build_type = db.Column(db.String) build_date = db.Column(db.Date) + build_duration = db.Column(db.Integer) build_runner_id = db.Column(db.String, db.ForeignKey('runner.runner_id')) build_runner = db.relationship('Runner', backref=db.backref('builds', lazy=True))