2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This service fetches all branches containing the current issue's ID, except for
|
|
|
|
# those with a merge request open referencing the current issue.
|
|
|
|
module Issues
|
|
|
|
class RelatedBranchesService < Issues::BaseService
|
|
|
|
def execute(issue)
|
2020-05-24 23:13:21 +05:30
|
|
|
branch_names = branches_with_iid_of(issue) - branches_with_merge_request_for(issue)
|
|
|
|
branch_names.map { |branch_name| branch_data(branch_name) }
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def branch_data(branch_name)
|
|
|
|
{
|
|
|
|
name: branch_name,
|
|
|
|
pipeline_status: pipeline_status(branch_name)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def pipeline_status(branch_name)
|
|
|
|
branch = project.repository.find_branch(branch_name)
|
|
|
|
target = branch&.dereferenced_target
|
|
|
|
|
|
|
|
return unless target
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
pipeline = project.latest_pipeline(branch_name, target.sha)
|
2020-05-24 23:13:21 +05:30
|
|
|
pipeline.detailed_status(current_user) if can?(current_user, :read_pipeline, pipeline)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def branches_with_merge_request_for(issue)
|
|
|
|
Issues::ReferencedMergeRequestsService
|
2021-06-08 01:23:25 +05:30
|
|
|
.new(project: project, current_user: current_user)
|
2018-12-05 23:21:45 +05:30
|
|
|
.referenced_merge_requests(issue)
|
|
|
|
.map(&:source_branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
def branches_with_iid_of(issue)
|
|
|
|
project.repository.branch_names.select do |branch|
|
|
|
|
branch =~ /\A#{issue.iid}-(?!\d+-stable)/i
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|