Commit 41964d9c authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Merge branch 'group-coverage-reporting-csv' into 'master'

Fill out response from CoverageReportsController

See merge request gitlab-org/gitlab!38520
parents ceade7ca 1e96ce27
---
title: Add CoverageReportsController#index CSV response
merge_request: 38520
author:
type: added
......@@ -3,12 +3,42 @@
class Groups::Analytics::CoverageReportsController < Groups::Analytics::ApplicationController
check_feature_flag Gitlab::Analytics::CYCLE_ANALYTICS_FEATURE_FLAG
COVERAGE_PARAM = 'coverage'.freeze
before_action :load_group
before_action -> { check_feature_availability!(:group_coverage_reports) }
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 render_csv(collection)
CsvBuilders::SingleBatch.new(
collection,
{
date: 'date',
group_name: 'group_name',
project_name: -> (record) { record.project.name },
COVERAGE_PARAM => -> (record) { record.data[COVERAGE_PARAM] }
}
).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: Date.parse(params.require(:start_date)),
end_date: Date.parse(params.require(:end_date))
}
end
end
......@@ -5,6 +5,21 @@ 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(:valid_request_params) do
{
group_id: group.name,
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 +28,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do
it 'responds 403' do
get :index, params: { group_id: group.name, format: :csv }
get :index, params: valid_request_params
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -33,7 +48,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 }
get :index, params: valid_request_params
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -48,7 +63,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 }
get :index, params: valid_request_params
expect(response).to have_gitlab_http_status(:forbidden)
end
......@@ -60,11 +75,37 @@ 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
get :index, params: valid_request_params
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
context 'with an invalid format' do
it 'responds 404' do
get :index, params: valid_request_params.merge(format: :json)
expect(response).to have_gitlab_http_status(:not_found)
end
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