Class: Gl::MergeRequests

Inherits:
Thor
  • Object
show all
Defined in:
lib/gl/cli/merge_requests.rb

Instance Method Summary collapse

Instance Method Details

#approve(iid = nil) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/gl/cli/merge_requests.rb', line 44

def approve(iid = nil)
  iid ||= merge_request_dialogue
  begin
    Gitlab.approve_merge_request(Gl.current_project, iid)
  rescue Gitlab::Error::Unauthorized
    TTY::Prompt.new.error('Unable to approve merge request')
  end
end

#label(label) ⇒ Object



59
60
61
# File 'lib/gl/cli/merge_requests.rb', line 59

def label(label)
  label_action(label)
end

#list(project = nil) ⇒ Object



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
# File 'lib/gl/cli/merge_requests.rb', line 7

def list(project = nil)
  if options[:i]
    id = merge_request_dialogue(project)
    mr = merge_requests.select { |merge_request| merge_request.iid == id.to_i }.first
    box = TTY::Box.frame(
      width: TTY::Screen.width,
      height: mr.description.lines.count + 4,
      padding: 1,
      title: { top_center: mr.title[0..TTY::Screen.width - 3],
               bottom_left: "State: #{mr.state}",
               bottom_right: "by #{mr.author.name}" }
    ) do
      mr.description
    end
    puts box

    choice = TTY::Prompt.new(interrupt: :exit).select('What to do next?') do |menu|
      menu.choice :open
      menu.choice :merge
      menu.choice :approve
      menu.choice :exit
    end

    case choice.to_sym
    when :open then open_mr_in_browser(mr.iid)
    when :merge then merge(mr.iid)
    when :approve then approve(mr.iid)
    when :exit then exit
    end
  else
    table = TTY::Table.new(%w[IID Author Title],
                           merge_requests_list(project))
    puts table.render(:ascii, padding: [0, 1])
  end
end

#merge(iid = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gl/cli/merge_requests.rb', line 71

def merge(iid = nil)
  iid ||= merge_request_dialogue.split(' - ')[0]
  Gitlab.accept_merge_request(Gl.current_project,
                              iid,
                              squash: options['squash'],
                              should_remove_source_branch: options['delete-branch'])
rescue Gitlab::Error::MethodNotAllowed => e
  TTY::Prompt.new.error(
    case e.response_status
    when 405
      'Unable to accept merge request ' \
      '(ie: Work in Progress, Closed, Pipeline Pending Completion, ' \
      'or Failed while requiring Success)'
    else
      "Could not merge due to unknown error: #{e.response_status}"
    end
  )
end

#open(iid = nil) ⇒ Object



54
55
56
# File 'lib/gl/cli/merge_requests.rb', line 54

def open(iid = nil)
  open_mr_in_browser(iid)
end

#unlabel(label) ⇒ Object



64
65
66
# File 'lib/gl/cli/merge_requests.rb', line 64

def unlabel(label)
  label_action(label)
end