From dd2854c66f5d95611297967d8abfde307331ec24 Mon Sep 17 00:00:00 2001
From: Maxime Orefice <morefice@gitlab.com>
Date: Tue, 21 Apr 2020 14:11:15 -0400
Subject: [PATCH] Add accessibility report for a pipeline

---
 app/models/ci/pipeline.rb       |  8 ++++++
 spec/factories/ci/pipelines.rb  |  8 ++++++
 spec/models/ci/pipeline_spec.rb | 46 +++++++++++++++++++++++++++++++++
 3 files changed, 62 insertions(+)

diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index e2ef9e3dd5f..a0ab00f0f07 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -811,6 +811,14 @@ module Ci
       end
     end
 
+    def accessibility_reports
+      Gitlab::Ci::Reports::AccessibilityReports.new.tap do |accessibility_reports|
+        builds.latest.with_reports(Ci::JobArtifact.accessibility_reports).each do |build|
+          build.collect_accessibility_reports!(accessibility_reports)
+        end
+      end
+    end
+
     def coverage_reports
       Gitlab::Ci::Reports::CoverageReports.new.tap do |coverage_reports|
         builds.latest.with_reports(Ci::JobArtifact.coverage_reports).each do |build|
diff --git a/spec/factories/ci/pipelines.rb b/spec/factories/ci/pipelines.rb
index f71225eac22..0b3653a01ed 100644
--- a/spec/factories/ci/pipelines.rb
+++ b/spec/factories/ci/pipelines.rb
@@ -83,6 +83,14 @@ FactoryBot.define do
         end
       end
 
+      trait :with_accessibility_reports do
+        status { :success }
+
+        after(:build) do |pipeline, evaluator|
+          pipeline.builds << build(:ci_build, :accessibility_reports, pipeline: pipeline, project: pipeline.project)
+        end
+      end
+
       trait :with_coverage_reports do
         status { :success }
 
diff --git a/spec/models/ci/pipeline_spec.rb b/spec/models/ci/pipeline_spec.rb
index b8e10f43ef4..a6fc33ec011 100644
--- a/spec/models/ci/pipeline_spec.rb
+++ b/spec/models/ci/pipeline_spec.rb
@@ -364,6 +364,16 @@ describe Ci::Pipeline, :mailer do
       end
     end
 
+    context 'when pipeline has an accessibility report' do
+      subject { described_class.with_reports(Ci::JobArtifact.accessibility_reports) }
+
+      let(:pipeline_with_report) { create(:ci_pipeline, :with_accessibility_reports) }
+
+      it 'selects the pipeline' do
+        is_expected.to eq([pipeline_with_report])
+      end
+    end
+
     context 'when pipeline has a terraform report' do
       it 'selects the pipeline' do
         pipeline_with_report = create(:ci_pipeline, :with_terraform_reports)
@@ -2799,6 +2809,42 @@ describe Ci::Pipeline, :mailer do
     end
   end
 
+  describe '#accessibility_reports' do
+    subject { pipeline.accessibility_reports }
+
+    context 'when pipeline has multiple builds with accessibility reports' do
+      let(:build_rspec) { create(:ci_build, :success, name: 'rspec', pipeline: pipeline, project: project) }
+      let(:build_golang) { create(:ci_build, :success, name: 'golang', pipeline: pipeline, project: project) }
+
+      before do
+        create(:ci_job_artifact, :accessibility, job: build_rspec, project: project)
+        create(:ci_job_artifact, :accessibility_without_errors, job: build_golang, project: project)
+      end
+
+      it 'returns accessibility report with collected data' do
+        expect(subject.urls.keys).to match_array([
+          "https://pa11y.org/",
+          "https://about.gitlab.com/"
+        ])
+      end
+
+      context 'when builds are retried' do
+        let(:build_rspec) { create(:ci_build, :retried, :success, name: 'rspec', pipeline: pipeline, project: project) }
+        let(:build_golang) { create(:ci_build, :retried, :success, name: 'golang', pipeline: pipeline, project: project) }
+
+        it 'returns empty urls for accessibility reports' do
+          expect(subject.urls).to be_empty
+        end
+      end
+    end
+
+    context 'when pipeline does not have any builds with accessibility reports' do
+      it 'returns empty urls for accessibility reports' do
+        expect(subject.urls).to be_empty
+      end
+    end
+  end
+
   describe '#coverage_reports' do
     subject { pipeline.coverage_reports }
 
-- 
2.30.9