def valid_credentials(): """Verify that the user's credentials are valid.""" LOG.debug("Determine if the user's credentials are valid.") req = requests.get(HOST_ROOT, auth=get_credentials()) if req.status_code == 401: remove_credentials() raise AuthenticationError("Your credentials were invalid. Please try again.")
def trigger_arbitrary_job(repo_name, builder, revision, files=[], dry_run=False, extra_properties=None): """ Request buildapi to trigger a job for us. We return the request or None if dry_run is True. """ url = _builders_api_url(repo_name, builder, revision) payload = _payload(repo_name, revision, files, extra_properties) if dry_run: LOG.info("Dry-run: We were going to request a job for '%s'" % builder) LOG.info(" with this payload: %s" % str(payload)) return None # NOTE: A good response returns json with request_id as one of the keys req = requests.post( url, headers={'Accept': 'application/json'}, data=payload, auth=get_credentials() ) if req.status_code == 401: remove_credentials() raise AuthenticationError("Your credentials were invalid. Please try again.") content = req.json() LOG.debug("Status of the request: %s" % _jobs_api_url(content["request_id"])) return req
def valid_credentials(): """ Verify that the user's credentials are valid. Raises an AuthenticationError if the credentials are invalid. """ LOG.debug("Determine if the user's credentials are valid.") req = requests.get(HOST_ROOT, auth=get_credentials()) if req.status_code == 401: remove_credentials() raise AuthenticationError( "Your credentials were invalid. Please try again.")
def query_repositories(clobber=False): """ Return dictionary with information about the various repositories. The data about a repository looks like this: .. code-block:: python "ash": { "repo": "https://hg.mozilla.org/projects/ash", "graph_branches": ["Ash"], "repo_type": "hg" } Raises an AuthenticationError if the user credentials are invalid. """ global REPOSITORIES if clobber: REPOSITORIES = {} if os.path.exists(REPOSITORIES_FILE): os.remove(REPOSITORIES_FILE) if REPOSITORIES: return REPOSITORIES if os.path.exists(REPOSITORIES_FILE): LOG.debug("Loading %s" % REPOSITORIES_FILE) fd = open(REPOSITORIES_FILE) REPOSITORIES = json.load(fd) else: url = "%s/branches?format=json" % HOST_ROOT LOG.debug("About to fetch %s" % url) req = requests.get(url, auth=get_credentials()) if req.status_code == 401: remove_credentials() raise AuthenticationError( "Your credentials were invalid. Please try again.") REPOSITORIES = req.json() with open(REPOSITORIES_FILE, "wb") as fd: json.dump(REPOSITORIES, fd) return REPOSITORIES
def query_repositories(clobber=False): """ Return dictionary with information about the various repositories. The data about a repository looks like this: .. code-block:: python "ash": { "repo": "https://hg.mozilla.org/projects/ash", "graph_branches": ["Ash"], "repo_type": "hg" } Raises an AuthenticationError if the user credentials are invalid. """ global REPOSITORIES if clobber: REPOSITORIES = {} if os.path.exists(REPOSITORIES_FILE): os.remove(REPOSITORIES_FILE) if REPOSITORIES: return REPOSITORIES if os.path.exists(REPOSITORIES_FILE): LOG.debug("Loading %s" % REPOSITORIES_FILE) fd = open(REPOSITORIES_FILE) REPOSITORIES = json.load(fd) else: try: REPOSITORIES = make_query_repositories_request( auth=get_credentials(), dry_run=False) except BuildapiAuthError: remove_credentials() raise AuthenticationError( "Your credentials were invalid. Please try again.") with open(REPOSITORIES_FILE, "wb") as fd: json.dump(REPOSITORIES, fd) return REPOSITORIES
def query_repositories(clobber=False): """ Return dictionary with information about the various repositories. The data about a repository looks like this: .. code-block:: python "ash": { "repo": "https://hg.mozilla.org/projects/ash", "graph_branches": ["Ash"], "repo_type": "hg" } Raises an AuthenticationError if the user credentials are invalid. """ global REPOSITORIES if clobber: REPOSITORIES = {} if os.path.exists(REPOSITORIES_FILE): os.remove(REPOSITORIES_FILE) if REPOSITORIES: return REPOSITORIES if os.path.exists(REPOSITORIES_FILE): LOG.debug("Loading %s" % REPOSITORIES_FILE) fd = open(REPOSITORIES_FILE) REPOSITORIES = json.load(fd) else: url = "%s/branches?format=json" % HOST_ROOT LOG.debug("About to fetch %s" % url) req = requests.get(url, auth=get_credentials()) if req.status_code == 401: remove_credentials() raise AuthenticationError("Your credentials were invalid. Please try again.") REPOSITORIES = req.json() with open(REPOSITORIES_FILE, "wb") as fd: json.dump(REPOSITORIES, fd) return REPOSITORIES
def trigger_arbitrary_job(repo_name, builder, revision, files=[], dry_run=False, extra_properties=None): """ Request buildapi to trigger a job for us. We return the request or None if dry_run is True. Raises AuthenticationError if credentials are invalid. """ url = _builders_api_url(repo_name, builder, revision) payload = _payload(repo_name, revision, files, extra_properties) if dry_run: LOG.info("Dry-run: We were going to request a job for '%s'" % builder) LOG.info(" with this payload: %s" % str(payload)) return None # NOTE: A good response returns json with request_id as one of the keys req = requests.post(url, headers={'Accept': 'application/json'}, data=payload, auth=get_credentials()) if req.status_code == 401: remove_credentials() raise AuthenticationError( "Your credentials were invalid. Please try again.") try: content = req.json() LOG.debug("Status of the request: %s" % _jobs_api_url(content["request_id"])) return req except ValueError: LOG.warning("We did not get info from %s (status code: %s)" % (url, req.status_code)) return None
def query_repositories(clobber=False): """ Return dictionary with information about the various repositories. The data about a repository looks like this: .. code-block:: python "ash": { "repo": "https://hg.mozilla.org/projects/ash", "graph_branches": ["Ash"], "repo_type": "hg" } Raises an AuthenticationError if the user credentials are invalid. """ global REPOSITORIES if clobber: REPOSITORIES = {} if os.path.exists(REPOSITORIES_FILE): os.remove(REPOSITORIES_FILE) if REPOSITORIES: return REPOSITORIES if os.path.exists(REPOSITORIES_FILE): LOG.debug("Loading %s" % REPOSITORIES_FILE) fd = open(REPOSITORIES_FILE) REPOSITORIES = json.load(fd) else: try: REPOSITORIES = make_query_repositories_request(auth=get_credentials(), dry_run=False) except BuildapiAuthError: remove_credentials() raise AuthenticationError("Your credentials were invalid. Please try again.") with open(REPOSITORIES_FILE, "wb") as fd: json.dump(REPOSITORIES, fd) return REPOSITORIES