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 @@ ...@@ -3,12 +3,42 @@
class Groups::Analytics::CoverageReportsController < Groups::Analytics::ApplicationController class Groups::Analytics::CoverageReportsController < Groups::Analytics::ApplicationController
check_feature_flag Gitlab::Analytics::CYCLE_ANALYTICS_FEATURE_FLAG check_feature_flag Gitlab::Analytics::CYCLE_ANALYTICS_FEATURE_FLAG
COVERAGE_PARAM = 'coverage'.freeze
before_action :load_group before_action :load_group
before_action -> { check_feature_availability!(:group_coverage_reports) } before_action -> { check_feature_availability!(:group_coverage_reports) }
def index def index
respond_to do |format| 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 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
end end
...@@ -5,6 +5,21 @@ require 'spec_helper' ...@@ -5,6 +5,21 @@ require 'spec_helper'
RSpec.describe Groups::Analytics::CoverageReportsController do RSpec.describe Groups::Analytics::CoverageReportsController do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:group) { create(:group) } 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 context 'without permissions' do
before do before do
...@@ -13,7 +28,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -13,7 +28,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do describe 'GET index' do
it 'responds 403' 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) expect(response).to have_gitlab_http_status(:forbidden)
end end
...@@ -33,7 +48,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -33,7 +48,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do describe 'GET index' do
it 'responds 403 because the feature is not licensed' 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) expect(response).to have_gitlab_http_status(:forbidden)
end end
...@@ -48,7 +63,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -48,7 +63,7 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
describe 'GET index' do describe 'GET index' do
it 'responds 403 because the feature is not licensed' 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) expect(response).to have_gitlab_http_status(:forbidden)
end end
...@@ -60,11 +75,37 @@ RSpec.describe Groups::Analytics::CoverageReportsController do ...@@ -60,11 +75,37 @@ RSpec.describe Groups::Analytics::CoverageReportsController do
stub_licensed_features(group_coverage_reports: true) stub_licensed_features(group_coverage_reports: true)
end end
it 'responds 200 OK' do it 'responds 200 with CSV coverage data' do
get :index, params: { group_id: group.name, format: :csv } get :index, params: valid_request_params
expect(response).to have_gitlab_http_status(:ok) 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
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
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