Module: Gitcycle::Branch

Included in:
Gitcycle
Defined in:
lib/gitcycle/branch.rb

Instance Method Summary collapse

Instance Method Details

#branch(*args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gitcycle/branch.rb', line 4

def branch(*args)
  url = args.detect { |arg| arg =~ /^https?:\/\// }
  title = args.detect { |arg| arg =~ /\s/ }

  exec_git(:branch, args) unless url || title

  require_git && require_config

  params = {
    'branch[source]' => branches(:current => true)
  }

  if url && url.include?('lighthouseapp.com/')
    params.merge!('branch[lighthouse_url]' => url)
  elsif url && url.include?('github.com/')
    params.merge!('branch[issue_url]' => url)
  elsif url
    puts "Gitcycle only supports Lighthouse or Github Issue URLs.".red
    exit ERROR[:unrecognized_url]
  elsif title
    params.merge!(
      'branch[name]' => title,
      'branch[title]' => title
    )
  else
    exec_git(:branch, args)
  end

  unless yes?("\nYour work will eventually merge into '#{params['branch[source]']}'. Is this correct?")
    params['branch[source]'] = q("What branch would you like to eventually merge into?")
  end

  source = params['branch[source]']

  puts "\nRetrieving branch information from gitcycle.\n".green
  branch = get('branch', params)
  name = branch['name']

  begin
    owner, repo = branch['repo'].split(':')
    branch['home'] ||= @git_login

    unless yes?("Would you like to name your branch '#{name}'?")
      name = q("\nWhat would you like to name your branch?")
      name = name.gsub(/[\s\W]/, '-')
    end

    checkout_remote_branch(
      :owner => owner,
      :repo => repo,
      :branch => branch['source'],
      :target => name
    )

    puts "Sending branch information to gitcycle.".green
    get('branch',
        'branch[home]' => branch['home'],
        'branch[name]' => branch['name'],
        'branch[rename]' => name != branch['name'] ? name : nil,
        'branch[source]' => branch['source']
    )
  rescue SystemExit, Interrupt
    puts "\nDeleting branch from gitcycle.\n".green
    get('branch',
      'branch[name]' => branch['name'],
      'create' => 0,
      'reset' => 1
    )
  end

  puts "\n"
end