Extract reports out of Ci::Pipeline and Ci::Build

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Problem

Ci::Pipeline and Ci::Build have both a lot of logic related to generating reports. This makes us not having a standardized way to treat reports #362876 (closed). Any time we need to add a new report there is a lot of copy-paste and customized logic.

The name report is sometimes used interchangeably with artifacts. For example: Ci::Build#each_report returns a list of artifacts of a given type.

In addition to that, we are not explicitly describing the fact that a pipeline can have 1 report of a given type. If multiple artifacts of a given type are present in the pipeline, they should all get combined together in order to always return a single report.

flowchart TD

r[Code Quality report] --> Pipeline --> Job1
Pipeline --> Job2
Pipeline --> Job3
Job1 --> cqart1[CodeQuality artifact 1]
Job2 --> cqart2[CodeQuality artifact 2]

Note from Sec: A pipeline can actually have multiple reports of the same type. We support that in Sec since the beginning. For instance, we can have multiple jobs reporting Dependency Scanning analysis results.

One way to extract this could be:

class Gitlab::Ci::Reports::TestReport # singular name
  ARTIFACT_TYPE = Ci::JobArtifact::TEST_REPORT_FILE_TYPES

  def initialize(pipeline:)
    @pipeline = pipeline
  end

  def data
    builds.find_each do |build|
      suite = get_suite(build.group_name)
      each_artifact_blob(build) do |file_type, blob|
        Gitlab::Ci::Parsers.fabricate!(artifact.file_type).parse!(blob, suite, job: build)
      end
    end
  end

  private

  def each_artifact_blob(build)
    build.job_artifacts_for_types(ARTIFACT_TYPES).each do |artifact|
      artifact.each_blob do |blob|
        yield(artifact, blob)
      end
    end
  end

  def builds
    @builds ||= pipeline.builds.latest.with_reports(::Ci::JobArtifact.with_reports)
  end
end
  • Remove *_reports methods from Ci::Pipeline
  • Remove collect_*_reports methods from Ci::Build
  • Remove knowledge of Gitlab::Ci::Parsers from inside Ci::Build
Edited by 🤖 GitLab Bot 🤖