import requests


def fetch_last_deploy_timestamp(access_token, environment):
    """
    Return the timestamp of the last deploy in `environment`
    """
    # The /api/1/deploys endpoint returns all deploys in descending order by timestamp
    # We'll iterate through them until we find one that matches `environment`.

    page = 1
    while True:
        result = requests.get('https://api.rollbar.com/api/1/deploys?access_token=%s&page=%s' % (access_token, page))
        deploys = result.json()['result']['deploys']
        if not deploys:
            # we've reached the end of the list
            return None

        for deploy in deploys:
            if deploy['environment'] == environment:
                return deploy['start_time']
        page += 1