Exemplo n.º 1
0
def set_build_status(commit_hash, build_status, key, name, url, description,
                     debug, driver):

    post_url = driver.get_post_url(commit_hash)

    data = {
        "state": build_status,
        "key": key,
        "name": name,
        "url": url,
        "description": description
    }

    response = requests.post(post_url,
                             json=data,
                             **driver.get_request_options())

    if debug:
        print_error("Request result: " + str(response))

    # Check status code. Bitbucket brakes rest a bit  by returning 200 or 201
    # depending on it's the first time the status is posted.
    if response.status_code not in driver.get_valid_response_status():
        try:
            message = ERROR_MAP[response.status_code]
        except KeyError:
            message = json_pp(response.json())

        raise BitbucketException(message)
    def get_post_url(self, commit_hash):
        url = '{endpoint}/rest/build-status/1.0/commits/{commit}'.format(
            endpoint=self.endpoint.rstrip('/'), commit=commit_hash)

        if self.verify_ssl is False:
            disable_ssl_warnings()
            if self.debug:
                print_error("SSL warnings disabled\n")

        return url
def request_access_token(client_id, secret, debug):
    response = requests.post('https://bitbucket.org/site/oauth2/access_token',
                             auth=HTTPBasicAuth(client_id, secret),
                             data={'grant_type': 'client_credentials'})

    if debug:
        print_error("Access token result: " + str(response) +
                    str(response.content))

    if response.status_code != 200:
        try:
            message = ERROR_MAP[response.status_code]
        except KeyError:
            message = json_pp(response.json())

        raise BitbucketException(message)

    return response.json()['access_token']
Exemplo n.º 4
0
def post_result(url, user, password, verify, data, debug):
    r = requests.post(url,
                      auth=HTTPBasicAuth(user, password),
                      verify=verify,
                      json=data)

    if debug:
        print_error("Request result: " + str(r))

    if r.status_code == 403:
        print_error(
            "HTTP 403 Forbidden - Does your bitbucket user have rights to the repo?"
        )
    elif r.status_code == 401:
        print_error(
            "HTTP 401 Unauthorized - Are your bitbucket credentials correct?")

    # All other errors, just dump the JSON
    if r.status_code != 204:  # 204 is a success per Bitbucket docs
        print_error(json_pp(r.json()))

    return r