From 35d8bc4485f3151dcf9ea5964b9cc18bdfd7d3d4 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Tue, 19 Apr 2016 12:07:13 +0200 Subject: [PATCH] Refactor banzai code that finds cross-project labels --- lib/banzai/filter/label_reference_filter.rb | 42 ++++++++++++--------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/lib/banzai/filter/label_reference_filter.rb b/lib/banzai/filter/label_reference_filter.rb index db4d60c019..8488a493b5 100644 --- a/lib/banzai/filter/label_reference_filter.rb +++ b/lib/banzai/filter/label_reference_filter.rb @@ -18,9 +18,7 @@ module Banzai def references_in(text, pattern = Label.reference_pattern) text.gsub(pattern) do |match| - project = project_from_ref($~[:project]) - params = label_params($~[:label_id].to_i, $~[:label_name]) - label = project.labels.find_by(params) if project + label = find_label($~[:project], $~[:label_id], $~[:label_name]) if label yield match, label.id, $~[:project], $~ @@ -30,6 +28,29 @@ module Banzai end end + def find_label(project_ref, label_id, label_name) + project = project_from_ref(project_ref) + return unless project + + label_params = label_params(label_id, label_name) + project.labels.find_by(label_params) + end + + # Parameters to pass to `Label.find_by` based on the given arguments + # + # id - Integer ID to pass. If present, returns {id: id} + # name - String name to pass. If `id` is absent, finds by name without + # surrounding quotes. + # + # Returns a Hash. + def label_params(id, name) + if name + { name: name.tr('"', '') } + else + { id: id.to_i } + end + end + def url_for_object(label, project) h = Gitlab::Routing.url_helpers h.namespace_project_issues_url(project.namespace, project, label_name: label.name, @@ -43,21 +64,6 @@ module Banzai LabelsHelper.render_colored_cross_project_label(object) end end - - # Parameters to pass to `Label.find_by` based on the given arguments - # - # id - Integer ID to pass. If present, returns {id: id} - # name - String name to pass. If `id` is absent, finds by name without - # surrounding quotes. - # - # Returns a Hash. - def label_params(id, name) - if name - { name: name.tr('"', '') } - else - { id: id } - end - end end end end