diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-19 15:09:03 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-19 15:09:03 +0300 |
commit | bf420c684d41038b22c72bbdea8a5d42aec70f58 (patch) | |
tree | 9c0988727903f77d957dfbfc354b5191be739852 /app/models | |
parent | 472a7da0e565894f7128becb5b1cf67572a13491 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ci/test_case.rb | 35 | ||||
-rw-r--r-- | app/models/ci/test_case_failure.rb | 29 | ||||
-rw-r--r-- | app/models/ci/unit_test.rb | 46 | ||||
-rw-r--r-- | app/models/ci/unit_test_failure.rb | 29 |
4 files changed, 75 insertions, 64 deletions
diff --git a/app/models/ci/test_case.rb b/app/models/ci/test_case.rb deleted file mode 100644 index 19ecc177436..00000000000 --- a/app/models/ci/test_case.rb +++ /dev/null @@ -1,35 +0,0 @@ -# frozen_string_literal: true - -module Ci - class TestCase < ApplicationRecord - extend Gitlab::Ci::Model - - validates :project, :key_hash, presence: true - - has_many :test_case_failures, class_name: 'Ci::TestCaseFailure' - - belongs_to :project - - scope :by_project_and_keys, -> (project, keys) { where(project_id: project.id, key_hash: keys) } - - class << self - def find_or_create_by_batch(project, test_case_keys) - # Insert records first. Existing ones will be skipped. - insert_all(test_case_attrs(project, test_case_keys)) - - # Find all matching records now that we are sure they all are persisted. - by_project_and_keys(project, test_case_keys) - end - - private - - def test_case_attrs(project, test_case_keys) - # NOTE: Rails 6.1 will add support for insert_all on relation so that - # we will be able to do project.test_cases.insert_all. - test_case_keys.map do |hashed_key| - { project_id: project.id, key_hash: hashed_key } - end - end - end - end -end diff --git a/app/models/ci/test_case_failure.rb b/app/models/ci/test_case_failure.rb deleted file mode 100644 index 8867b954240..00000000000 --- a/app/models/ci/test_case_failure.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -module Ci - class TestCaseFailure < ApplicationRecord - extend Gitlab::Ci::Model - - REPORT_WINDOW = 14.days - - validates :test_case, :build, :failed_at, presence: true - - belongs_to :test_case, class_name: "Ci::TestCase", foreign_key: :test_case_id - belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id - - def self.recent_failures_count(project:, test_case_keys:, date_range: REPORT_WINDOW.ago..Time.current) - joins(:test_case) - .where( - ci_test_cases: { - project_id: project.id, - key_hash: test_case_keys - }, - ci_test_case_failures: { - failed_at: date_range - } - ) - .group(:key_hash) - .count('ci_test_case_failures.id') - end - end -end diff --git a/app/models/ci/unit_test.rb b/app/models/ci/unit_test.rb new file mode 100644 index 00000000000..81623b4f6ad --- /dev/null +++ b/app/models/ci/unit_test.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module Ci + class UnitTest < ApplicationRecord + extend Gitlab::Ci::Model + + MAX_NAME_SIZE = 255 + MAX_SUITE_NAME_SIZE = 255 + + validates :project, :key_hash, :name, :suite_name, presence: true + + has_many :unit_test_failures, class_name: 'Ci::UnitTestFailure' + + belongs_to :project + + scope :by_project_and_keys, -> (project, keys) { where(project_id: project.id, key_hash: keys) } + + class << self + def find_or_create_by_batch(project, unit_test_attrs) + # Insert records first. Existing ones will be skipped. + insert_all(build_insert_attrs(project, unit_test_attrs)) + + # Find all matching records now that we are sure they all are persisted. + by_project_and_keys(project, gather_keys(unit_test_attrs)) + end + + private + + def build_insert_attrs(project, unit_test_attrs) + # NOTE: Rails 6.1 will add support for insert_all on relation so that + # we will be able to do project.test_cases.insert_all. + unit_test_attrs.map do |attrs| + attrs.merge( + project_id: project.id, + name: attrs[:name].truncate(MAX_NAME_SIZE), + suite_name: attrs[:suite_name].truncate(MAX_SUITE_NAME_SIZE) + ) + end + end + + def gather_keys(unit_test_attrs) + unit_test_attrs.map { |attrs| attrs[:key_hash] } + end + end + end +end diff --git a/app/models/ci/unit_test_failure.rb b/app/models/ci/unit_test_failure.rb new file mode 100644 index 00000000000..653a56bd2b3 --- /dev/null +++ b/app/models/ci/unit_test_failure.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Ci + class UnitTestFailure < ApplicationRecord + extend Gitlab::Ci::Model + + REPORT_WINDOW = 14.days + + validates :unit_test, :build, :failed_at, presence: true + + belongs_to :unit_test, class_name: "Ci::UnitTest", foreign_key: :unit_test_id + belongs_to :build, class_name: "Ci::Build", foreign_key: :build_id + + def self.recent_failures_count(project:, unit_test_keys:, date_range: REPORT_WINDOW.ago..Time.current) + joins(:unit_test) + .where( + ci_unit_tests: { + project_id: project.id, + key_hash: unit_test_keys + }, + ci_unit_test_failures: { + failed_at: date_range + } + ) + .group(:key_hash) + .count('ci_unit_test_failures.id') + end + end +end |