import requests


def read_build_cause(job_url, build_id):
    """Read cause why the e2e job has been started."""
    api_query = job_url + "/" + str(build_id) + "/api/json"
    response = requests.get(api_query)
    actions = response.json()["actions"]

    cause = None
    for action in actions:
        if "_class" in action and action["_class"] == "hudson.model.CauseAction":
            cause = action["causes"][0]

    # None or real build cause
    return cause