From 66886a223bb5370950fbf91fdcdb2902bb8bf99d Mon Sep 17 00:00:00 2001 From: Frank Niessink Date: Tue, 11 Feb 2025 21:53:12 +0100 Subject: [PATCH] Next to ignoring branches, tags, and jobs, also allow for including branches, tags, and jobs when measuring failed CI-jobs with GitLab as source. Closes #4520. --- .../collector/src/source_collectors/gitlab/base.py | 9 +++++---- .../tests/source_collectors/gitlab/base.py | 12 ++++++++++++ .../src/shared_data_model/sources/gitlab.py | 14 ++++++++++++++ docs/src/changelog.md | 4 ++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/components/collector/src/source_collectors/gitlab/base.py b/components/collector/src/source_collectors/gitlab/base.py index 50b93b0718..1d66ae2177 100644 --- a/components/collector/src/source_collectors/gitlab/base.py +++ b/components/collector/src/source_collectors/gitlab/base.py @@ -118,11 +118,12 @@ def newer(job1: Job, job2: Job) -> Job: def _include_entity(self, entity: Entity) -> bool: """Return whether to count the job.""" + jobs_to_include = self._parameter("jobs_to_include") + refs_to_include = self._parameter("refs_to_include") return ( - not match_string_or_regular_expression( - entity["name"], - self._parameter("jobs_to_ignore"), - ) + (match_string_or_regular_expression(entity["name"], jobs_to_include) if jobs_to_include else True) + and (match_string_or_regular_expression(entity["branch"], refs_to_include) if refs_to_include else True) + and not match_string_or_regular_expression(entity["name"], self._parameter("jobs_to_ignore")) and not match_string_or_regular_expression(entity["branch"], self._parameter("refs_to_ignore")) and entity["build_datetime"] >= self._lookback_datetime() ) diff --git a/components/collector/tests/source_collectors/gitlab/base.py b/components/collector/tests/source_collectors/gitlab/base.py index 8c72f5bc7d..d41e7168fb 100644 --- a/components/collector/tests/source_collectors/gitlab/base.py +++ b/components/collector/tests/source_collectors/gitlab/base.py @@ -80,8 +80,20 @@ async def test_ignore_job_by_name(self): response = await self.collect(get_request_json_return_value=self.gitlab_jobs_json) self.assert_measurement(response, value="1", entities=self.expected_entities[:-1]) + async def test_include_job_by_name(self): + """Test that jobs can be included by name.""" + self.set_source_parameter("jobs_to_include", ["job1"]) + response = await self.collect(get_request_json_return_value=self.gitlab_jobs_json) + self.assert_measurement(response, value="1", entities=self.expected_entities[:-1]) + async def test_ignore_job_by_ref(self): """Test that jobs can be ignored by ref.""" self.set_source_parameter("refs_to_ignore", ["develop"]) response = await self.collect(get_request_json_return_value=self.gitlab_jobs_json) self.assert_measurement(response, value="1", entities=self.expected_entities[:-1]) + + async def test_include_job_by_ref(self): + """Test that jobs can be included by ref.""" + self.set_source_parameter("refs_to_include", ["main"]) + response = await self.collect(get_request_json_return_value=self.gitlab_jobs_json) + self.assert_measurement(response, value="1", entities=self.expected_entities[:-1]) diff --git a/components/shared_code/src/shared_data_model/sources/gitlab.py b/components/shared_code/src/shared_data_model/sources/gitlab.py index 454d8dd341..338d5faad1 100644 --- a/components/shared_code/src/shared_data_model/sources/gitlab.py +++ b/components/shared_code/src/shared_data_model/sources/gitlab.py @@ -141,6 +141,13 @@ help_url=GITLAB_BRANCH_HELP_URL, metrics=["change_failure_rate", "failed_jobs", "job_runs_within_time_period", "unused_jobs"], ), + "refs_to_include": MultipleChoiceWithAdditionParameter( + name="Branches and tags to include (regular expressions, branch names or tag names)", + short_name="branches and tags to include", + help_url=GITLAB_BRANCH_HELP_URL, + placeholder="all branches and tags", + metrics=["change_failure_rate", "failed_jobs", "job_runs_within_time_period", "unused_jobs"], + ), "inactive_days": Days( name="Number of days since last commit after which to consider branches inactive", short_name="number of days since last commit", @@ -161,6 +168,13 @@ help="Jobs to ignore can be specified by job name or by regular expression.", metrics=["change_failure_rate", "failed_jobs", "job_runs_within_time_period", "unused_jobs"], ), + "jobs_to_include": MultipleChoiceWithAdditionParameter( + name="Jobs to include (regular expressions or job names)", + short_name="jobs to include", + help="Jobs to include can be specified by job name or by regular expression.", + placeholder="all jobs", + metrics=["change_failure_rate", "failed_jobs", "job_runs_within_time_period", "unused_jobs"], + ), "lookback_days": Days( name="Number of days to look back for selecting pipeline jobs", short_name="number of days to look back", diff --git a/docs/src/changelog.md b/docs/src/changelog.md index fc3f0c0d50..af34dc7cbd 100644 --- a/docs/src/changelog.md +++ b/docs/src/changelog.md @@ -14,6 +14,10 @@ If your currently installed *Quality-time* version is not the latest version, pl ## [Unreleased] +### Added + +- Next to ignoring branches, tags, and jobs, also allow for including branches, tags, and jobs when measuring 'failed CI-jobs', 'unused CI-jobs', 'change failure rate', and 'jobs runs within time period' with GitLab as source. Closes [#4520](https://github.com/ICTU/quality-time/issues/4520). + ### Fixed - Use ARGON2 hashes to verify user LDAP passwords instead of SSHA1. Fixes [#6233](https://github.com/ICTU/quality-time/issues/6233).