Exemplo n.º 1
0
 def post(self, hook, build_xid):
     return upsert_build(
         repository=hook.repository,
         provider=hook.provider,
         external_id=build_xid,
         data=request.get_json() or {},
     )
Exemplo n.º 2
0
    def post(self, hook):
        current_app.logger.info("travis.received-webhook")

        payload = request.form.get("payload")
        if not payload:
            current_app.logger.error("travis.webhook-missing-payload")
            return Response(status=400)

        signature = request.headers.get("Signature")
        if not signature:
            current_app.logger.error("travis.webhook-missing-signature")
            return Response(status=400)

        try:
            signature = b64decode(signature)
        except ValueError:
            current_app.logger.error("travis.webhook-invalid-signature", exc_info=True)
            return Response(status=400)

        api_domain = (hook.config or {}).get("domain", "api.travis-ci.org")
        public_key = get_travis_public_key(api_domain)
        try:
            verify_signature(public_key, signature, payload.encode("utf-8"))
        except InvalidSignature:
            current_app.logger.error("travis.webhook-invalid-signature", exc_info=True)
            return Response(status=400)

        try:
            payload = json.loads(payload)
        except (TypeError, ValueError):
            current_app.logger.error("travis.webhook-invalid-payload", exc_info=True)
            return Response(status=400)

        data = {"ref": payload["commit"], "url": payload["build_url"]}

        domain = urlparse(data["url"]).netloc

        try:
            if payload["pull_request"]:
                data["label"] = "PR #{} - {}".format(
                    payload["pull_request_number"], payload["pull_request_title"]
                )

                upsert_change_request(
                    repository=hook.repository,
                    provider="github",
                    external_id=str(payload["pull_request_number"]),
                    data={
                        "parent_revision_sha": payload["base_commit"],
                        "head_revision_sha": payload["head_commit"],
                        "message": payload["pull_request_title"],
                    },
                )

            response = upsert_build(
                hook=hook, external_id=str(payload["id"]), data=data
            )
            build = Build.query.get(response.json()["id"])
            for job_payload in payload["matrix"]:
                upsert_job(
                    build=build,
                    hook=hook,
                    external_id=str(job_payload["id"]),
                    data={
                        "status": (
                            "finished"
                            if job_payload["status"] is not None
                            else "in_progress"
                        ),
                        "result": get_result(job_payload["state"]),
                        "allow_failure": bool(job_payload["allow_failure"]),
                        "label": get_job_label(job_payload),
                        "url": "https://{domain}/{owner}/{name}/jobs/{job_id}".format(
                            domain=domain,
                            owner=payload["repository"]["owner_name"],
                            name=payload["repository"]["name"],
                            job_id=job_payload["id"],
                        ),
                        "started_at": (
                            dateutil.parser.parse(job_payload["started_at"])
                            if job_payload["started_at"]
                            else None
                        ),
                        "finished_at": (
                            dateutil.parser.parse(job_payload["finished_at"])
                            if job_payload["finished_at"]
                            else None
                        ),
                    },
                )
        except ApiError:
            current_app.logger.error("travis.webhook-unexpected-error", exc_info=True)
            raise

        return Response(status=200)
Exemplo n.º 3
0
def process_travis_webhook(hook_id: str, payload: dict, timestamp_ms: int):
    # TODO(dcramer): we want to utilize timestamp_ms to act as a version and
    # ensure we dont process older updates after newer updates are already present
    with nplusone.ignore("eager_load"):
        hook = (Hook.query.unrestricted_unsafe().options(
            joinedload("repository")).get(hook_id))

    auth.set_current_tenant(
        auth.Tenant(access={hook.repository_id: Permission.admin}))

    data = {"ref": payload["commit"], "url": payload["build_url"]}

    domain = urlparse(data["url"]).netloc

    try:
        if payload["pull_request"]:
            data["label"] = "PR #{} - {}".format(
                payload["pull_request_number"], payload["pull_request_title"])

            upsert_change_request(
                repository=hook.repository,
                provider="github",
                external_id=str(payload["pull_request_number"]),
                data={
                    "parent_revision_sha": payload["base_commit"],
                    "head_revision_sha": payload["head_commit"],
                    "message": payload["pull_request_title"],
                },
            )

        response = upsert_build(hook=hook,
                                external_id=str(payload["id"]),
                                data=data)
        build = Build.query.get(response.json()["id"])
        for job_payload in payload["matrix"]:
            upsert_job(
                build=build,
                hook=hook,
                external_id=str(job_payload["id"]),
                data={
                    "status": ("finished" if job_payload["status"] is not None
                               else "in_progress"),
                    "result":
                    get_result(job_payload["state"]),
                    "allow_failure":
                    bool(job_payload["allow_failure"]),
                    "label":
                    get_job_label(job_payload),
                    "url":
                    "https://{domain}/{owner}/{name}/jobs/{job_id}".format(
                        domain=domain,
                        owner=payload["repository"]["owner_name"],
                        name=payload["repository"]["name"],
                        job_id=job_payload["id"],
                    ),
                    "started_at":
                    (dateutil.parser.parse(job_payload["started_at"])
                     if job_payload["started_at"] else None),
                    "finished_at":
                    (dateutil.parser.parse(job_payload["finished_at"])
                     if job_payload["finished_at"] else None),
                },
            )
    except ApiError:
        current_app.logger.error("travis.webhook-unexpected-error",
                                 exc_info=True)
        raise
Exemplo n.º 4
0
 def post(self, hook, build_xid):
     return upsert_build(hook=hook,
                         external_id=build_xid,
                         data=request.get_json() or {})
Exemplo n.º 5
0
    def post(self, hook):
        current_app.logger.info('travis.received-webhook')

        payload = request.form.get('payload')
        if not payload:
            current_app.logger.error('travis.webhook-missing-payload')
            return Response(status=400)

        signature = request.headers.get('Signature')
        if not signature:
            current_app.logger.error('travis.webhook-missing-signature')
            return Response(status=400)

        try:
            signature = b64decode(signature)
        except ValueError:
            current_app.logger.error('travis.webhook-invalid-signature',
                                     exc_info=True)
            return Response(status=400)

        domain = (hook.config or {}).get('domain', 'api.travis-ci.org')
        public_key = get_travis_public_key(domain)
        try:
            verify_signature(public_key, signature, payload.encode('utf-8'))
        except InvalidSignature:
            current_app.logger.error('travis.webhook-invalid-signature',
                                     exc_info=True)
            return Response(status=400)

        try:
            payload = json.loads(payload)
        except (TypeError, ValueError):
            current_app.logger.error('travis.webhook-invalid-payload',
                                     exc_info=True)
            return Response(status=400)

        data = {
            'ref': payload['commit'],
            'url': payload['build_url'],
        }

        domain = urlparse(data['url']).netloc

        if payload['pull_request']:
            data['label'] = 'PR #{} - {}'.format(
                payload['pull_request_number'], payload['pull_request_title'])

        response = upsert_build(
            repository=hook.repository,
            provider=hook.provider,
            external_id=str(payload['id']),
            data=data,
        )
        build = Build.query.get(response.json()['id'])
        for job_payload in payload['matrix']:
            upsert_job(
                build=build,
                provider=hook.provider,
                external_id=str(job_payload['id']),
                data={
                    'status':
                    'finished'
                    if job_payload['status'] is not None else 'in_progress',
                    'result':
                    get_result(job_payload['state']),
                    'allow_failure':
                    bool(job_payload['allow_failure']),
                    'label':
                    get_job_label(job_payload),
                    'url':
                    'https://{domain}/{owner}/{name}/jobs/{job_id}'.format(
                        domain=domain,
                        owner=payload['repository']['owner_name'],
                        name=payload['repository']['name'],
                        job_id=job_payload['id'],
                    ),
                    'started_at':
                    dateutil.parser.parse(job_payload['started_at'])
                    if job_payload['started_at'] else None,
                    'finished_at':
                    dateutil.parser.parse(job_payload['finished_at'])
                    if job_payload['finished_at'] else None,
                })

        return Response(status=200)