Module: Abak::Flow::CommandsExtension

Defined in:
lib/abak-flow/hub_extensions.rb

Instance Method Summary collapse

Instance Method Details

#pull_request(args) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/abak-flow/hub_extensions.rb', line 17

def pull_request(args)
  args.shift
  options = { }
  force = explicit_owner = false
  base_project = local_repo.main_project
  head_project = local_repo.current_project

  from_github_ref = lambda do |ref, context_project|
    if ref.index(':')
      owner, ref = ref.split(':', 2)
      project = github_project(context_project.name, owner)
    end
    [project || context_project, ref]
  end

  while arg = args.shift
    case arg
    when '-f'
      force = true
    when '-b'
      base_project, options[:base] = from_github_ref.call(args.shift, base_project)
    when '-h'
      head = args.shift
      explicit_owner = !!head.index(':')
      head_project, options[:head] = from_github_ref.call(head, head_project)
    when '-i'
      options[:issue] = args.shift
    when '-d'
      options[:body] = args.shift
    else
      if url = resolve_github_url(arg) and url.project_path =~ /^issues\/(\d+)/
        options[:issue] = $1
        base_project = url.project
      elsif !options[:title] then options[:title] = arg
      else
        abort "invalid argument: #{arg}"
      end
    end
  end

  options[:project] = base_project
  options[:base] ||= master_branch.short_name

  if tracked_branch = options[:head].nil? && current_branch.upstream
    if base_project == head_project and tracked_branch.short_name == options[:base]
      $stderr.puts "Aborted: head branch is the same as base (#{options[:base].inspect})"
      warn "(use `-h <branch>` to specify an explicit pull request head)"
      abort
    end
  end
  options[:head] ||= (tracked_branch || current_branch).short_name

  # when no tracking, assume remote branch is published under active user's fork
  user = github_user(true, head_project.host)
  if head_project.owner != user and !tracked_branch and !explicit_owner
    head_project = head_project.owned_by(user)
  end

  remote_branch = "#{head_project.remote}/#{options[:head]}"
  options[:head] = "#{head_project.owner}:#{options[:head]}"

  if !force and tracked_branch and local_commits = git_command("rev-list --cherry #{remote_branch}...")
    $stderr.puts "Aborted: #{local_commits.split("\n").size} commits are not yet pushed to #{remote_branch}"
    warn "(use `-f` to force submit a pull request anyway)"
    abort
  end

  if args.noop?
    puts "Would reqest a pull to #{base_project.owner}:#{options[:base]} from #{options[:head]}"
    exit
  end

  unless options[:title] or options[:issue]
    base_branch = "#{base_project.remote}/#{options[:base]}"
    changes = git_command "log --no-color --pretty=medium --cherry %s...%s" %
      [base_branch, remote_branch]

    options[:title], options[:body] = pullrequest_editmsg(changes) { |msg|
      msg.puts "# Requesting a pull to #{base_project.owner}:#{options[:base]} from #{options[:head]}"
      msg.puts "#"
      msg.puts "# Write a message for this pull request. The first block"
      msg.puts "# of text is the title and the rest is description."
    }
  end

  pull = create_pullrequest(options)

  args.executable = 'echo'
  args.replace [pull['html_url']]
rescue HTTPExceptions
  display_http_exception("creating pull request", $!.response)
  exit 1
end