def __init__(self, app): self.app = app self.projects = None self.user = self.app.read_config('Database', 'user', 'AUGUR_DB_USER', 'augur') self.password = self.app.read_config('Database', 'password', 'AUGUR_DB_PASS', 'password') self.host = self.app.read_config('Database', 'host', 'AUGUR_DB_HOST', '127.0.0.1') self.port = self.app.read_config('Database', 'port', 'AUGUR_DB_PORT', '5433') self.dbname = self.app.read_config('Database', 'database', 'AUGUR_DB_NAME', 'augur') self.schema = self.app.read_config('Database', 'schema', 'AUGUR_DB_SCHEMA', 'augur_data') self.database_connection_string = 'postgresql://{}:{}@{}:{}/{}'.format( self.user, self.password, self.host, self.port, self.dbname ) self.database = s.create_engine(self.database_connection_string, poolclass=s.pool.NullPool, connect_args={'options': '-csearch_path={}'.format(self.schema)}) spdx_schema = 'spdx' self.spdx_db = s.create_engine(self.database_connection_string, poolclass=s.pool.NullPool, connect_args={'options': '-csearch_path={},{}'.format(spdx_schema, self.schema)}) logger.debug('Augur DB: Connecting to {} schema of {}:{}/{} as {}'.format(self.schema, self.host, self.port, self.dbname, self.user)) # TODO: not hardcode this create_commit_metrics(self) create_contributor_metrics(self) create_experimental_metrics(self) create_insight_metrics(self) create_issue_metrics(self) create_message_metrics(self) create_platform_metrics(self) create_pull_request_metrics(self) create_repo_meta_metrics(self) create_util_metrics(self)
def sbom_download(self, repo_group_id, repo_id=None): """REQUIRES SBOMS TO BE PRESENT IN THE DATABASE :param repo_id: The repository's repo_id, defaults to None :return: dosocs sbom """ dosocs_SQL = s.sql.text(""" select * from augur_data.repo_sbom_scans where repo_id = :repo_id; """) logger.debug(dosocs_SQL) params = {'repo_id': repo_id} return pd.read_sql(dosocs_SQL, self.database, params=params)
def cii_best_practices_badge(self, repo_group_id, repo_id=None): """Returns the CII best practices badge level :param repo_group_id: The repository's repo_group_id :param repo_id: The repository's repo_id, defaults to None :return: CII best parctices badge level """ # Welcome to the Twilight Zone if repo_id: cii_best_practices_badge_SQL = s.sql.text(""" SELECT repo_name, rg_name, repo_badging.badge_level, achieve_passing_status, achieve_silver_status, tiered_percentage, repo_url, id, repo_badging.updated_at as date FROM repo_badging, repo, repo_groups WHERE repo.repo_group_id = repo_groups.repo_group_id AND repo.repo_id = repo_badging.repo_id AND repo_badging.repo_id = :repo_id ORDER BY date DESC LIMIT 1 """) logger.debug(cii_best_practices_badge_SQL) params = {'repo_id': repo_id} else: cii_best_practices_badge_SQL = s.sql.text(""" SELECT repo_name, rg_name, repo_badging.badge_level, achieve_passing_status, achieve_silver_status, tiered_percentage, repo_url, id, repo_badging.updated_at as date FROM repo_badging, repo, repo_groups WHERE repo.repo_group_id = repo_groups.repo_group_id AND repo.repo_id = repo_badging.repo_id AND repo.repo_group_id = :repo_group_id ORDER BY date DESC LIMIT 1 """) logger.debug(cii_best_practices_badge_SQL) params = {'repo_group_id': repo_group_id, 'repo_id': repo_id} return pd.read_sql(cii_best_practices_badge_SQL, self.database, params=params)
def __enter__(self): """ Update context """ self.lock.acquire(timeout=0) logger.info('Git: Updating %s', self.url) if not os.path.exists(self.path): logger.debug('Cloning %s', self.url) git.Git(self.containing_folder).clone(self.url, self.path) else: try: repo = self.git(is_updater=True) logger.debug('Pulling %s', self.url) repo.git.pull() except Exception as e: logger.debug('Re-Cloning %s because %s', self.url, str(e)) shutil.rmtree(self.path) git.Git(self.containing_folder).clone(self.url, self.path) return self
def batch(): """ Execute multiple requests, submitted as a batch. :statuscode 207: Multi status """ """ to have on future batch request for each individual chart: - timeseries/metric - props that are in current card files (title) - do any of these things act like the vuex states? - what would singular card(dashboard) look like now? """ self.show_metadata = False if request.method == 'GET': """this will return sensible defaults in the future""" return app.make_response( '{"status": "501", "response": "Defaults for batch requests not implemented. Please POST a JSON array of requests to this endpoint for now."}' ) try: requests = json.loads(request.data.decode('utf-8')) except ValueError as e: request.abort(400) responses = [] for index, req in enumerate(requests): method = req['method'] path = req['path'] body = req.get('body', None) try: logger.debug('batch-internal-loop: %s %s' % (method, path)) with app.app_context(): with app.test_request_context(path, method=method, data=body): try: # Can modify flask.g here without affecting # flask.g of the root request for the batch # Pre process Request rv = app.preprocess_request() if rv is None: # Main Dispatch rv = app.dispatch_request() except Exception as e: rv = app.handle_user_exception(e) response = app.make_response(rv) # Post process Request response = app.process_response(response) # Response is a Flask response object. # _read_response(response) reads response.response # and returns a string. If your endpoints return JSON object, # this string would be the response as a JSON string. responses.append({ "path": path, "status": response.status_code, "response": str(response.get_data(), 'utf8'), }) except Exception as e: responses.append({ "path": path, "status": 500, "response": str(e) }) return Response(response=json.dumps(responses), status=207, mimetype="application/json")