Class: GitlabMrRelease::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab_mr_release/project.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint:, private_token:, project_name:) ⇒ Project

Returns a new instance of Project.

Parameters:

  • api_endpoint (String)
  • private_token (String)
  • project_name (String)


8
9
10
11
12
13
14
15
# File 'lib/gitlab_mr_release/project.rb', line 8

def initialize(api_endpoint:, private_token:, project_name:)
  Gitlab.configure do |config|
    config.endpoint      = api_endpoint
    config.private_token = private_token
  end
  @api_endpoint = api_endpoint
  @project_name = project_name
end

Instance Method Details

#api_versionObject



17
18
19
20
# File 'lib/gitlab_mr_release/project.rb', line 17

def api_version
  @api_endpoint =~ %r(/api/v([0-9]+)/?)
  $1.to_i
end

#apply_checkbox_statuses(new_desc, old_desc) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gitlab_mr_release/project.rb', line 72

def apply_checkbox_statuses(new_desc, old_desc)
  checked_iids = old_desc.split("\n").select{ |line| line.include?("[x]") }.map{ |line| /\!\d+/.match(line).to_a.first }.compact

  applied_lines = new_desc.split("\n").map do |line|
    iid = /\!\d+/.match(line).to_a.first
    if !iid.nil? && checked_iids.include?(iid)
      line.gsub(/^( *)(\d+?\.|-|\*) \[ \]/) { "#{$1}#{$2} [x]" }
    else
      line
    end
  end
  applied_lines.join("\n")
end

#create_merge_request(source_branch:, target_branch:, title:, template:, labels:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gitlab_mr_release/project.rb', line 43

def create_merge_request(source_branch:, target_branch:, title:, template:, labels:)
  iids = merge_request_iids_between(target_branch, source_branch)
  description = generate_description(iids, template)

  mr = find_current_release_mr(source_branch, target_branch)
  if mr.nil?
    options = {
      source_branch: source_branch,
      target_branch: target_branch,
      description:   description,
      labels:        labels,
    }
    Gitlab.create_merge_request(@project_name, title, options)
  else
    options = {
      title: title,
      description: apply_checkbox_statuses(description, mr.description)
    }
    Gitlab.update_merge_request(@project_name, mr.iid, options)
  end
end

#find_current_release_mr(source_branch, target_branch) ⇒ Object

find release mr already exists



66
67
68
69
70
# File 'lib/gitlab_mr_release/project.rb', line 66

def find_current_release_mr(source_branch, target_branch)
  Gitlab.merge_requests(@project_name, state: :opened).find do |mr|
    mr.source_branch == source_branch && mr.target_branch == target_branch
  end
end

#generate_description(iids, template) ⇒ Object



38
39
40
41
# File 'lib/gitlab_mr_release/project.rb', line 38

def generate_description(iids, template)
  merge_requests = Gitlab.merge_requests(@project_name, iids: iids)
  ERB.new(template, nil, "-").result(binding).strip
end

#merge_request_iids_between(from, to) ⇒ Array<Integer>

find merge requests between from…to

Parameters:

  • from (String)
  • to (String)

Returns:

  • (Array<Integer>)

    MergeRequest iids



30
31
32
33
34
35
36
# File 'lib/gitlab_mr_release/project.rb', line 30

def merge_request_iids_between(from, to)
  commits = Gitlab.repo_compare(@project_name, from, to).commits
  commits.map do |commit|
    commit["message"] =~ /^Merge branch .*See merge request .*\!(\d+)$/m
    $1
  end.compact.map(&:to_i)
end

#web_urlObject



22
23
24
# File 'lib/gitlab_mr_release/project.rb', line 22

def web_url
  @web_url ||= Gitlab.project(@project_name).web_url
end