debian-mirror-gitlab/app/services/issues/related_branches_service.rb

50 lines
1.6 KiB
Ruby
Raw Normal View History

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)
2022-08-13 15:12:31 +05:30
branch_names_with_mrs = branches_with_merge_request_for(issue)
branches = branches_with_iid_of(issue).reject { |b| branch_names_with_mrs.include?(b[:name]) }
branches.map { |branch| branch_data(branch) }
2018-12-05 23:21:45 +05:30
end
private
2022-08-13 15:12:31 +05:30
def branch_data(branch)
2020-05-24 23:13:21 +05:30
{
2022-08-13 15:12:31 +05:30
name: branch[:name],
pipeline_status: pipeline_status(branch)
2020-05-24 23:13:21 +05:30
}
end
2022-08-13 15:12:31 +05:30
def pipeline_status(branch)
pipeline = project.latest_pipeline(branch[:name], branch[:target])
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
2023-04-23 21:23:45 +05:30
.new(container: 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)
2022-08-13 15:12:31 +05:30
branch_ref_regex = /\A#{Gitlab::Git::BRANCH_REF_PREFIX}#{issue.iid}-(?!\d+-stable)/i
return [] unless project.repository.exists?
project.repository.list_refs(
[Gitlab::Git::BRANCH_REF_PREFIX + "#{issue.iid}-*"]
).each_with_object([]) do |ref, results|
if ref.name.match?(branch_ref_regex)
results << { name: ref.name.delete_prefix(Gitlab::Git::BRANCH_REF_PREFIX), target: ref.target }
end
2018-12-05 23:21:45 +05:30
end
end
end
end