def _include_job(self, job: Job) -> bool: """Return whether the job should be counted.""" jobs_to_include = self._parameter("jobs_to_include") if len(jobs_to_include) > 0 and not match_string_or_regular_expression( job["name"], jobs_to_include): return False return not match_string_or_regular_expression( job["name"], self._parameter("jobs_to_ignore"))
def include(transaction) -> bool: """Return whether the transaction should be included.""" name = self._name(transaction) if transactions_to_include and not match_string_or_regular_expression( name, transactions_to_include): return False return not match_string_or_regular_expression( name, transactions_to_ignore)
def _include_job(self, job: Job) -> bool: """Return whether this job should be included.""" if not job.get("latestCompletedBuild", {}).get("result"): return False # The job has no completed builds jobs_to_include = self._parameter("jobs_to_include") if len(jobs_to_include) > 0 and not match_string_or_regular_expression(job["name"], jobs_to_include): return False return not match_string_or_regular_expression(self.__job_name(job), self._parameter("jobs_to_ignore"))
async def _parse_source_responses(self, responses: SourceResponses) -> SourceMeasurement: test_results = cast(List[str], self._parameter("test_result")) test_run_names_to_include = cast(List[str], self._parameter("test_run_names_to_include")) or ["all"] test_run_states_to_include = [ value.lower() for value in self._parameter("test_run_states_to_include")] or ["all"] runs = (await responses[0].json()).get("value", []) highest_build: Dict[str, TestRun] = defaultdict(TestRun) for run in runs: name = run.get("name", "Unknown test run name") if test_run_names_to_include != ["all"] and \ not match_string_or_regular_expression(name, test_run_names_to_include): continue state = run.get("state", "Unknown test run state") if test_run_states_to_include != ["all"] and state.lower() not in test_run_states_to_include: continue build_nr = int(run.get("build", {}).get("id", -1)) if build_nr < highest_build[name].build_nr: continue if build_nr > highest_build[name].build_nr: highest_build[name] = TestRun(build_nr) counted_tests = sum(run.get(test_result, 0) for test_result in test_results) highest_build[name].test_count += counted_tests highest_build[name].total_test_count += run.get("totalTests", 0) highest_build[name].entities.append( Entity( key=run["id"], name=name, state=state, build_id=str(build_nr), url=run.get("webAccessUrl", ""), started_date=run.get("startedDate", ""), completed_date=run.get("completedDate", ""), counted_tests=str(counted_tests), incomplete_tests=str(run.get("incompleteTests", 0)), not_applicable_tests=str(run.get("notApplicableTests", 0)), passed_tests=str(run.get("passedTests", 0)), unanalyzed_tests=str(run.get("unanalyzedTests", 0)), total_tests=str(run.get("totalTests", 0)))) test_count = sum(build.test_count for build in highest_build.values()) total_test_count = sum(build.total_test_count for build in highest_build.values()) test_runs = list(itertools.chain.from_iterable([build.entities for build in highest_build.values()])) return SourceMeasurement(value=str(test_count), total=str(total_test_count), entities=test_runs)
async def __parse_response( cls, response: Response, transactions_to_include: list[str], transactions_to_ignore: list[str] ) -> dict[str, int]: """Parse the transactions from the response.""" soup = await cls._soup(response) count = dict(failed=0, success=0) for transaction in soup.find(id="responsetimestable_begin").select("tr.transaction"): name = cls._name(transaction) if transactions_to_include and not match_string_or_regular_expression(name, transactions_to_include): continue if match_string_or_regular_expression(name, transactions_to_ignore): continue columns = transaction.find_all("td") for status, column_index in cls.COLUMN_INDICES.items(): nr_tests = int(columns[column_index].string or 0) count[status] += nr_tests return count
def __language_ncloc(self, metrics: Dict[str, str]) -> List[List[str]]: """Return the languages and non-commented lines of code per language, ignoring languages if so specified.""" languages_to_ignore = self._parameter("languages_to_ignore") return [ language_count.split("=") for language_count in metrics["ncloc_language_distribution"].split(";") if not match_string_or_regular_expression( language_count.split("=")[0], languages_to_ignore) ]
def __include_violation(self, impact: str, tags: Collection[str]) -> bool: """Return whether to include the violation.""" if impact not in self._parameter("impact"): return False if tags_to_include := self._parameter("tags_to_include"): for tag in tags: if match_string_or_regular_expression(tag, tags_to_include): break else: return False
async def _unmerged_branches( self, responses: SourceResponses) -> List[Dict[str, Any]]: branches = await responses[0].json() return [ branch for branch in branches if not branch["default"] and not branch["merged"] and days_ago(self._commit_datetime(branch)) > int( cast(str, self._parameter("inactive_days"))) and not match_string_or_regular_expression( branch["name"], self._parameter("branches_to_ignore")) ]
async def _unmerged_branches(self, responses: SourceResponses) -> list[dict[str, Any]]: """Override to return a list of unmerged and inactive branches.""" branches = [] for response in responses: branches.extend(await response.json()) return [ branch for branch in branches if not branch["default"] and not branch["merged"] and days_ago(self._commit_datetime(branch)) > int(cast(str, self._parameter("inactive_days"))) and not match_string_or_regular_expression(branch["name"], self._parameter("branches_to_ignore")) ]
async def _unmerged_branches( self, responses: SourceResponses) -> list[dict[str, Any]]: """Override to get the unmerged branches response. Branches are considered unmerged if they have a base branch, have commits that are not on the base branch, have not been committed to for a minimum number of days, and are not to be ignored. """ return [ branch for branch in (await responses[0].json())["value"] if not branch["isBaseVersion"] and int(branch["aheadCount"]) > 0 and days_ago(self._commit_datetime(branch)) > int( cast(str, self._parameter("inactive_days"))) and not match_string_or_regular_expression( branch["name"], self._parameter("branches_to_ignore")) ]
def _include_merge_request(self, merge_request) -> bool: """Return whether the merge request should be counted.""" request_matches_state = merge_request["state"] in self._parameter( "merge_request_state") branches = self._parameter("target_branches_to_include") target_branch = merge_request["target_branch"] request_matches_branches = match_string_or_regular_expression( target_branch, branches) if branches else True # If the required number of upvotes is zero, merge requests are included regardless of how many upvotes they # actually have. If the required number of upvotes is more than zero then only merge requests that have fewer # than the minimum number of upvotes are included in the count: required_upvotes = int(cast(str, self._parameter("upvotes"))) request_has_fewer_than_min_upvotes = required_upvotes == 0 or int( merge_request["upvotes"]) < required_upvotes return request_matches_state and request_matches_branches and request_has_fewer_than_min_upvotes
def __language_ncloc(self, metrics: dict[str, str]) -> list[list[str]]: """Return the languages and non-commented lines of code per language, ignoring languages if so specified.""" languages_to_ignore = [ language.lower() for language in self._parameter("languages_to_ignore") ] keys_to_ignore = [ key for key, language in self.LANGUAGES.items() if language.lower() in languages_to_ignore ] return [ language_count.split("=") for language_count in metrics["ncloc_language_distribution"].split(";") if not match_string_or_regular_expression( language_count.split("=")[0], keys_to_ignore) ]
class AxeSeleniumPythonAccessibility(JSONFileSourceCollector): """Collector class to get accessibility violations.""" async def _parse_entities(self, responses: SourceResponses) -> Entities: """Override to parse the violations.""" entity_attributes = [] for response in responses: json = await response.json(content_type=None) url = json["url"] for violation in json.get("violations", []): for node in violation.get("nodes", []): tags = violation.get("tags", []) impact = node.get("impact") if self.__include_violation(impact, tags): entity_attributes.append( dict( description=violation.get("description"), element=node.get("html"), help=violation.get("helpUrl"), impact=impact, page=url, url=url, tags=", ".join(sorted(tags)), violation_type=violation.get("id"), )) return [ Entity(key=self.__create_key(attributes), **attributes) for attributes in entity_attributes ] def __include_violation(self, impact: str, tags: Collection[str]) -> bool: """Return whether to include the violation.""" if impact not in self._parameter("impact"): return False if tags_to_include := self._parameter("tags_to_include"): for tag in tags: if match_string_or_regular_expression(tag, tags_to_include): break else: return False if tags_to_ignore := self._parameter("tags_to_ignore"): for tag in tags: if match_string_or_regular_expression(tag, tags_to_ignore): return False
async def _parse_source_responses( self, responses: SourceResponses) -> SourceMeasurement: loc = 0 entities = [] languages_to_ignore = self._parameter("languages_to_ignore") for response in responses: for key, value in (await response.json(content_type=None)).items(): if key not in ("header", "SUM" ) and not match_string_or_regular_expression( key, languages_to_ignore): loc += value["code"] entities.append( Entity(key=key, language=key, blank=str(value["blank"]), comment=str(value["comment"]), code=str(value["code"]), nr_files=str(value["nFiles"]))) return SourceMeasurement(value=str(loc), entities=entities)
async def _parse_source_responses( self, responses: SourceResponses) -> SourceMeasurement: transactions_to_ignore = self._parameter("transactions_to_ignore") count = dict(failed=0, success=0) column_indices = dict(failed=7, success=1) total = 0 for response in responses: soup = await self._soup(response) for transaction in soup.find( id="responsetimestable_begin").select("tr.transaction"): if match_string_or_regular_expression(self._name(transaction), transactions_to_ignore): continue columns = transaction.find_all("td") for status, column_index in column_indices.items(): nr_tests = int(columns[column_index].string or 0) count[status] += nr_tests total += nr_tests value = sum(count[status] for status in self._parameter("test_result")) return SourceMeasurement(value=str(value), total=str(total))
def _count_job(self, job: Job) -> bool: """Return whether the job should be counted.""" return not match_string_or_regular_expression(job["name"], self._parameter("jobs_to_ignore"))
def _ignore_job(self, job: Job) -> bool: """Return whether this job should be ignored""" if not job.get("latestCompletedBuild", {}).get("result"): return True # The job has no completed builds return match_string_or_regular_expression(self.__job_name(job), self._parameter("jobs_to_ignore"))
def include(transaction) -> bool: """Return whether the transaction should be included.""" return not match_string_or_regular_expression( self.__name(transaction), transactions_to_ignore)
async def _unmerged_branches(self, responses: SourceResponses) -> List[Dict[str, Any]]: return [branch for branch in (await responses[0].json())["value"] if not branch["isBaseVersion"] and int(branch["aheadCount"]) > 0 and days_ago(self._commit_datetime(branch)) > int(cast(str, self._parameter("inactive_days"))) and not match_string_or_regular_expression(branch["name"], self._parameter("branches_to_ignore"))]
def _count_job(self, job: Job) -> bool: """Return whether to count the job.""" return not match_string_or_regular_expression(job["name"], self._parameter("jobs_to_ignore")) and \ not match_string_or_regular_expression(job["ref"], self._parameter("refs_to_ignore"))