summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorJacopo <beschi.jacopo@gmail.com>2018-04-19 08:59:37 +0200
committerJacopo <beschi.jacopo@gmail.com>2018-04-19 13:56:37 +0200
commit6ae3098eb8f01406190942e8952866dd9af81dde (patch)
tree478fd89327d9248b1daadea5f3c80e29fb6180e3 /app/models
parent4f2e494772eb5f31929ecfdb439dfa4baa56521c (diff)
downloadgitlab-ce-6ae3098eb8f01406190942e8952866dd9af81dde.tar.gz
Uses Uniquify to calculate Issue#suggested_branch_name
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/uniquify.rb7
-rw-r--r--app/models/issue.rb8
2 files changed, 9 insertions, 6 deletions
diff --git a/app/models/concerns/uniquify.rb b/app/models/concerns/uniquify.rb
index a7fe5951b6e..db51ed2dbeb 100644
--- a/app/models/concerns/uniquify.rb
+++ b/app/models/concerns/uniquify.rb
@@ -3,11 +3,14 @@ class Uniquify
# by appending a counter to it. Uniqueness is determined by
# repeated calls to the passed block.
#
+ # You can pass an initial value for the counter, if not given
+ # counting starts from 1.
+ #
# If `base` is a function/proc, we expect that calling it with a
# candidate counter returns a string to test/return.
- def string(base)
+ def string(base, counter = nil)
@base = base
- @counter = nil
+ @counter = counter
increment_counter! while yield(base_string)
base_string
diff --git a/app/models/issue.rb b/app/models/issue.rb
index c34c35bcd34..51028a404c2 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -197,10 +197,10 @@ class Issue < ActiveRecord::Base
def suggested_branch_name
return to_branch_name unless project.repository.branch_exists?(to_branch_name)
- index = 2
- index += 1 while project.repository.branch_exists?("#{to_branch_name}-#{index}")
-
- "#{to_branch_name}-#{index}"
+ start_counting_from = 2
+ Uniquify.new.string(-> (counter) { "#{to_branch_name}-#{counter}" }, start_counting_from) do |suggested_branch_name|
+ project.repository.branch_exists?(suggested_branch_name)
+ end
end
# Returns boolean if a related branch exists for the current issue