Class: GitLab::MergeRequest

Inherits:
Object
  • Object
show all
Includes:
TtyIntegration
Defined in:
lib/GitLab/merge_request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TtyIntegration

#bar, #cmd, #error, #green, #pastel, #prompt, #red, #success, #table, #yellow

Constructor Details

#initialize(params = {}) ⇒ MergeRequest

Returns a new instance of MergeRequest.



10
11
12
13
14
15
16
17
18
19
# File 'lib/GitLab/merge_request.rb', line 10

def initialize(params = {})
  @source_branch = params[:source_branch]
  @target_branch = params[:target_branch]
  @title      = params[:title]
  @labels     = params[:labels]
  @issue_iid  = params[:issue_iid]
  @type       = params[:type]
  @description = params[:description]
  @options = params[:options]
end

Instance Attribute Details

#assignee_idObject

Returns the value of attribute assignee_id.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def assignee_id
  @assignee_id
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def description
  @description
end

#issue_iidObject

Returns the value of attribute issue_iid.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def issue_iid
  @issue_iid
end

#labelsObject

Returns the value of attribute labels.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def labels
  @labels
end

#obj_gitlabObject

Returns the value of attribute obj_gitlab.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def obj_gitlab
  @obj_gitlab
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def options
  @options
end

#source_branchObject

Returns the value of attribute source_branch.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def source_branch
  @source_branch
end

#target_branchObject

Returns the value of attribute target_branch.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def target_branch
  @target_branch
end

#titleObject

Returns the value of attribute title.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def title
  @title
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/GitLab/merge_request.rb', line 6

def type
  @type
end

Instance Method Details

#createObject



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
# File 'lib/GitLab/merge_request.rb', line 21

def create
  # if type != 'hotfix'
  #   users = GitLab::User.all
  #   print "Users list:\n\n".yellow
  #   print "----------------------------\n".blue
  #   print "#{"0".ljust(10)} - Empty\n".blue
  #   users.each do |user|
  #     print "#{user['id'].to_s.ljust(10)} - #{user['name']}\n".blue
  #   end
  #   print "----------------------------\n".blue
  #   print "Choice user ID for assignee:\n".yellow
  #   assignee_id = STDIN.gets.chomp
  #   print "\n#{assignee_id}, "
  #   print "ok!\n".green
  # end
  users_list = GitLab::User.all.map { |u| "#{u['id']} - #{u['name']}" }
  user_selected = prompt.select('Quem vai aprovar o MR?', users_list, symbols: { marker: '>' }, filter: true)
  assignee_id = user_selected.delete("\n").split('-')[0].strip

  url = "projects/#{$GITLAB_PROJECT_ID}/merge_requests"

  labels = ['merge_request']
  labels << type if type
  @obj_gitlab = GitLab.request_post(url, {
                                      source_branch: @source_branch,
                                      target_branch: @target_branch,
                                      title: @title,
                                      labels: labels.join(','),
                                      description: @description,
                                      assignee_id: assignee_id.to_i,
                                      squash: true,
                                      squash_on_merge: true

                                    })

  prompt.say(pastel.cyan("Merge request criado com sucesso!\n\n"))
end

#create_code_reviewObject



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
# File 'lib/GitLab/merge_request.rb', line 59

def create_code_review
  # print "Create merge request for code review: ".yellow
  # print "#{@source_branch} into #{@target_branch}\n\n".green
  # users = GitLab::User.all
  # print "Users list:\n\n".yellow
  # print "----------------------------\n".blue
  # print "#{"0".ljust(10)} - Empty\n".blue
  # users.each do |user|
  #  print "#{user['id'].to_s.ljust(10)} - #{user['name']}\n".blue
  # end
  # print "----------------------------\n".blue
  # print "Choice user ID for assignee code review:\n".yellow
  # assignee_id = STDIN.gets.chomp
  # print "\n#{assignee_id}, "
  # print "ok!\n".green

  users = GitLab::User.all
  assignee_id = prompt.select('Quem vai fazer o codereview?', symbols: { marker: '>' }, filter: true) do |menu|
    users.each do |user|
      menu.choice  user['name'], user['id']
    end
  end

  url = "projects/#{$GITLAB_PROJECT_ID}/merge_requests"
  title = "Draft: ##{@issue_iid} - Code review #{@source_branch}"
  description = 'Este Merge Request de codereview foi criado pelo gitsflow'
  @obj_gitlab = GitLab.request_post(url, {
                                      source_branch: @source_branch,
                                      target_branch: @target_branch,
                                      title: title,
                                      description: description,
                                      labels: @labels,
                                      assignee_id: GitLab::User.me['id'].to_i,
                                      reviewer_ids: [assignee_id.to_i]
                                    })

  return error(@obj_gitlab.dig('message').join('.')) if @obj_gitlab.has_key? 'message'

  success("Code Review criado com sucesso!\n\n")

  # print "Merge request for Code Review created with success!\n\n".green
end