Commit c283e982 authored by drew cimino's avatar drew cimino

Fill out response from CoverageReportsController

A Groups::Analytics controller based on
Ci::DailyBuildGroupReportResultsController that uses the new
DailyBuildGroupReportResultByGroupFinder to return results for projects
across an entire group
parent f6e53638
---
title: Add CoverageReportsController#index CSV response
merge_request: 38520
author:
type: added
......@@ -3,12 +3,70 @@
class Groups::Analytics::CoverageReportsController < Groups::Analytics::ApplicationController
check_feature_flag Gitlab::Analytics::CYCLE_ANALYTICS_FEATURE_FLAG
REPORT_WINDOW = 90.days.freeze
before_action :load_group
before_action -> { check_feature_availability!(:group_coverage_reports) }
before_action :validate_param_type!
def index
respond_to do |format|
format.csv { send_data([].to_csv, type: 'text/csv; charset=utf-8') }
format.csv { send_data(render_csv(report_results), type: 'text/csv; charset=utf-8') }
end
end
private
def validate_param_type!
respond_422 unless allowed_param_types.include?(param_type)
end
def render_csv(collection)
CsvBuilders::SingleBatch.new(
collection,
{
date: 'date',
group_name: 'group_name',
project_name: -> (record) { record.project.name },
param_type => -> (record) { record.data[param_type] }
}
).render
end
def report_results
Ci::DailyBuildGroupReportResultsByGroupFinder.new(finder_params).execute
end
def finder_params
{
current_user: current_user,
group: @group,
ref_path: params.require(:ref_path),
start_date: start_date,
end_date: end_date
}
end
def start_date
strong_memoize(:start_date) do
start_date = Date.parse(params.require(:start_date))
# The start_date cannot be older than `end_date - 90 days`
[start_date, end_date - REPORT_WINDOW].max
end
end
def end_date
strong_memoize(:end_date) do
Date.parse(params.require(:end_date))
end
end
def allowed_param_types
Ci::DailyBuildGroupReportResult::PARAM_TYPES
end
def param_type
params.require(:param_type)
end
end
......@@ -5,6 +5,22 @@ require 'spec_helper'
RSpec.describe Groups::Analytics::CoverageReportsController do
let(:user) { create(:user) }
let(:group) { create(:group) }
let(:project) { create(:project, namespace: group) }
let(:ref_path) { 'refs/heads/master' }
let!(:first_coverage) { create_daily_coverage('rspec', 79.0, '2020-03-09') }
let!(:last_coverage) { create_daily_coverage('karma', 95.0, '2020-03-10') }
let(:index_request) do
get :index, params: {
group_id: group.name,
param_type: 'coverage',
start_date: '2020-03-01',
end_date: '2020-03-31',
ref_path: ref_path,
format: :csv
}
end
context 'without permissions' do
before do
......@@ -13,7 +29,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do
it 'responds 403' do
get :index, params: { group_id: group.name, format: :csv }
index_request
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -33,7 +49,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do
it 'responds 403 because the feature is not licensed' do
get :index, params: { group_id: group.name, format: :csv }
index_request
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -48,7 +64,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do
it 'responds 403 because the feature is not licensed' do
get :index, params: { group_id: group.name, format: :csv }
index_request
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -60,11 +76,29 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
stub_licensed_features(group_coverage_reports: true)
end
it 'responds 200 OK' do
get :index, params: { group_id: group.name, format: :csv }
it 'responds 200 with CSV coverage data' do
index_request
expect(response).to have_gitlab_http_status(:ok)
expect(csv_response).to eq([
%w[date group_name project_name coverage],
[last_coverage.date.to_s, last_coverage.group_name, project.name, last_coverage.data['coverage'].to_s],
[first_coverage.date.to_s, first_coverage.group_name, project.name, first_coverage.data['coverage'].to_s]
])
end
end
end
private
def create_daily_coverage(group_name, coverage, date)
create(
:ci_daily_build_group_report_result,
project: project,
ref_path: ref_path,
group_name: group_name,
data: { 'coverage' => coverage },
date: date
)
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment