def index()

in lambdas/scale_out_runner/app.py [0:0]


def index():
    validate_gh_sig(app.current_request)

    if app.current_request.headers.get('X-GitHub-Event', None) != "check_run":
        # Ignore things about installs/permissions etc
        return {'ignored': 'not about check_runs'}

    body = app.current_request.json_body

    repo = body['repository']['full_name']

    sender = body['sender']['login']

    # Other repos configured with this app, but we don't do anything with them
    # yet.
    if repo not in REPO_CONFIGURATION:
        app.log.info("Ignoring event for %r", repo)
        return {'ignored': 'Other repo'}

    interested_branches = REPO_CONFIGURATION[repo]

    branch = body['check_run']['check_suite']['head_branch']

    use_self_hosted = sender in commiters() or branch in interested_branches
    payload = {'sender': sender, 'use_self_hosted': use_self_hosted}

    if body['action'] == 'completed' and body['check_run']['conclusion'] == 'cancelled':
        if use_self_hosted:
            # The only time we get a "cancelled" job is when it wasn't yet running.
            queue_length = increment_dynamodb_counter(-1)
            # Don't scale in the ASG -- let the CloudWatch alarm do that.
            payload['new_queue'] = queue_length
        else:
            payload = {'ignored': 'unknown sender'}

    elif body['action'] != 'created':
        payload = {'ignored': "action is not 'created'"}

    elif body['check_run']['status'] != 'queued':
        # Skipped runs are "created", but are instantly completed. Ignore anything that is not queued
        payload = {'ignored': "check_run.status is not 'queued'"}
    else:
        if use_self_hosted:
            # Increment counter in DynamoDB
            queue_length = increment_dynamodb_counter()
            payload.update(**scale_asg_if_needed(queue_length))
    app.log.info(
        "delivery=%s branch=%s: %r",
        app.current_request.headers.get('X-GitHub-Delivery', None),
        branch,
        payload,
    )
    return payload