Refactor DailyBuildGroupReportResultsFinder

Summary

Following up #270102 (closed) where we fixed a bug for Users who have default branch names that aren't master and were not getting any data in the .csv download for group code coverage.

Problem

We fixed our existing bug by fetching our daily coverage for the default branch.

This is quite slow today according to our SQL query.

Solution

We have an existing index which will improve the performance of this feature that we must use.

I think this is the time to refactor our DailyBuildGroupReportResultsFinder by making it more flexible:

This will help us to get rid of DailyBuildGroupReportResultsByGroupFinder and unify our business logic at a central place by following existing patterns we have today (ProjectsFinder).

Here a proposal for it:

def initialize(params: {}, current_user: nil)
  @params = params
  @current_user = current_user
end

def execute
  return none unless query_allowed?

  collection = Ci::DailyBuildGroupReportResult.recent_results(query_params, limit: params[:limit])
  collection = filter_report_results(collection)
end

private

def filter_report_results(collection)
  collection = by_coverage(collection)
  collection = by_projects_ids(collection)
  collection = by_ref_path(collection)
  collection = by_date(collection)
  collection
end

def by_coverage(items)
  items.with_coverage
end

def by_projects_ids(items)
  params[:project_ids].present? ? items.by_projects(params[:project_ids]) : items
end

def by_ref_path(items)
  params[:ref_path].present? ? items.where(ref_path: params[:ref_path]) : items.with_default_branch
end

def by_date(items)
  params[:start_date].present? ? items.by_date(params[:start_date]) : items
end

def query_allowed?
  can?(current_user, :read_build_report_results, params[:project])
end
Edited by Max Orefice