lib/abak-flow/branch.rb in abak-flow-1.0.3 vs lib/abak-flow/branch.rb in abak-flow-1.0.4

- old
+ new

@@ -2,41 +2,46 @@ module Abak::Flow class Branch FOLDER_HOTFIX = "hotfix".freeze FOLDER_FEATURE = "feature".freeze - TASK_FORMAT = /^\w+\-\d{1,}$/.freeze + TASK_FORMAT = '\w+\-\d{1,}'.freeze + MAGICK_WORDS = %w{close closes closed fix fixes fixed + resolve resolves resolved}.freeze DEVELOPMENT = "develop".freeze MASTER = "master".freeze MAPPING = { FOLDER_HOTFIX => MASTER, FOLDER_FEATURE => DEVELOPMENT }.freeze - def initialize(branch, manager) - @manager = manager + attr_reader :folder + attr_reader :task + + def initialize(branch) + @manager = Manager.instance @branch = branch.is_a?(Git::Branch) ? branch - : manager.git.branch(branch) + : @manager.git.branch(branch) + + parse_branch_name end def name @branch.full end - # TODO : Брать коммит мессадж до перевода строки def message - @branch.gcommit.message - end + content = @branch.gcommit.message.split("\n", 2).first + return content if content.length < 72 - def folder - split_prefix_and_task.first + content[0...72] << "..." end - def task - split_prefix_and_task.last + def to_s + @branch.to_s end def compare_link(branch) diff = "#{@manager.repository.upstream.owner}:#{branch}...#{@branch}" @@ -45,10 +50,29 @@ @manager.repository.origin.to_s, "compare", diff ] end + def extract_base_name(options = Hash.new) + mappable? ? MAPPING[folder] + : options.fetch(:if_undef, name) + end + + def extract_title + tracker_task? ? task + : message + end + + # TODO : Сделать настраевыемым трекер и формат задачи + def extract_body + return I18n.t("commands.publish.nothing") if + tasks_from_commit_message.empty? && !tracker_task? + + [tasks_from_commit_message, task].flatten + .map { |x| "http://jira.railsc.ru/browse/#{x}" } * "\n" + end + def update origin = @manager.repository.origin.repo @manager.git.push(origin, @branch) end @@ -59,34 +83,16 @@ def delete_on_local @branch.delete end - def pick_up_base_name(options = Hash.new) - mappable? ? MAPPING[folder] - : options.fetch(:or_use, name) - end - - def pick_up_title - tracker_task? ? task - : message - end - - # TODO : Сделать настраевыемым трекер и формат задачи - # TODO : Смотреть в коммит мессадж и искать там Fixes/Closes/Close/Fix - def pick_up_body - head.mappable? && - head.tracker_task? ? "http://jira.railsc.ru/browse/#{task}" - : I18n.t("commands.publish.nothing") - end - def develop? - @branch.name == DEVELOPMENT + name == DEVELOPMENT end def master? - @branch.name == MASTER + name == MASTER end def hotfix? folder == FOLDER_HOTFIX end @@ -94,11 +100,11 @@ def feature? folder == FOLDER_FEATURE end def tracker_task? - !(task =~ TASK_FORMAT).nil? + !(task =~ /^#{TASK_FORMAT}$/).nil? end def mappable? hotfix? || feature? end @@ -109,20 +115,21 @@ def valid? !@branch.name.empty? end - def to_s - @branch.to_s + private + def tasks_from_commit_message + @parsed_tasks ||= + @branch.gcommit.message + .scan(/(?:#{MAGICK_WORDS * "|"})\s+(#{TASK_FORMAT})/i) + .reject { |x| x == task.to_s } end - private - def split_prefix_and_task - return @folder_and_task if defined? @folder_and_task - + def parse_branch_name matches = name.match(/^(?<prefix>.+)\/(?<task>.+)$/) - @folder_and_task = matches.nil? ? [nil, nil] - : [matches[:prefix], matches[:task]] + @folder, @task = matches.nil? ? [nil, nil] + : [matches[:prefix], matches[:task]] end end end