Class: GitlabAwesomeRelease::Project

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

Constant Summary collapse

PER_PAGE =
100

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint:, private_token:, project_name:, allow_tag_format:, merge_request_commit_format:, logger:) ⇒ Project

Returns a new instance of Project.

Parameters:

  • api_endpoint (String)
  • private_token (String)
  • project_name (String)
  • allow_tag_format (Regexp)
  • merge_request_commit_format (Regexp)


15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab_awesome_release/project.rb', line 15

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

Instance Method Details

#add_merge_request_label(mr, label) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/gitlab_awesome_release/project.rb', line 130

def add_merge_request_label(mr, label)
  labels = mr.labels
  labels << label

  Gitlab.update_merge_request(@project_name, mr.iid, labels: labels.uniq.join(","))
  @logger.info "Add [#{label}] to !#{mr.iid} #{mr.title}"
end

#all_tag_namesArray<String>

all tag names order by author date

Returns:

  • (Array<String>)


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gitlab_awesome_release/project.rb', line 32

def all_tag_names
  return @all_tag_names if @all_tag_names

  @logger.info "fetch git tags"
  repo_tags =
    with_paging do |params|
      Gitlab.repo_tags(@project_name, params)
    end

  # avoid SEGV on ruby 2.6.0
  # https://bugs.ruby-lang.org/issues/15489
  @all_tag_names = repo_tags.sort_by {|tag| tag.commit.authored_date }.map {|tag| tag.name }
end

#generate_change_log(oldest_tag, newest_tag) ⇒ String

Parameters:

  • oldest_tag (String)
  • newest_tag (String)

Returns:

  • (String)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/gitlab_awesome_release/project.rb', line 54

def generate_change_log(oldest_tag, newest_tag)
  release_notes = []
  release_notes << generate_heading(oldest_tag) if oldest_tag == release_tag_names.first
  release_tag_names.within(oldest_tag, newest_tag).each_cons(2) do |from, to|
    release_notes << generate_release_note(from, to)
  end
  release_notes << generate_release_note(newest_tag, "HEAD", title: "Unreleased") if newest_tag == release_tag_names.last

  changelog =
    release_notes.reverse.each_with_object("") do |release_note, str|
      str << release_note
      str << "\n"
    end

  changelog << <<-MARKDOWN
*This Change Log was automatically generated by [gitlab_awesome_release](https://gitlab.com/sue445/gitlab_awesome_release)*
  MARKDOWN

  changelog
end

#generate_heading(title) ⇒ Object



95
96
97
# File 'lib/gitlab_awesome_release/project.rb', line 95

def generate_heading(title)
  "## #{title}\n"
end

#generate_release_note(from, to, title: nil) ⇒ String

generate release note between from...to

Parameters:

  • from (String)
  • to (String)
  • title (String) (defaults to: nil)

Returns:

  • (String)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/gitlab_awesome_release/project.rb', line 80

def generate_release_note(from, to, title: nil)
  @logger.info "generate release note (#{from}...#{to})"

  title ||= to
  summary = merge_requests_summary_between(from, to)

  header = generate_heading(title)
  header << <<-MARKDOWN
[full changelog](#{web_url}/compare/#{from}...#{to})

  MARKDOWN

  header + summary
end

#merge_request(iid) ⇒ Object

find MergeRequest with iid



126
127
128
# File 'lib/gitlab_awesome_release/project.rb', line 126

def merge_request(iid)
  Gitlab.merge_request(@project_name, iid)
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



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gitlab_awesome_release/project.rb', line 103

def merge_request_iids_between(from, to)
  commits = Gitlab.repo_compare(@project_name, from, to).commits

  merge_request_iids =
    commits.map do |commit|
      commit["message"] =~ @merge_request_commit_format
      Regexp.last_match(1)
    end

  merge_request_iids.compact.map(&:to_i)
end

#merge_request_summary(iid) ⇒ String

Returns markdown text.

Parameters:

  • iid (Integer)

    MergeRequest iid

Returns:

  • (String)

    markdown text



117
118
119
120
121
122
123
# File 'lib/gitlab_awesome_release/project.rb', line 117

def merge_request_summary(iid)
  mr = merge_request(iid)
  return nil unless mr

  mr_url = "#{web_url}/merge_requests/#{iid}"
  "* #{mr.title} [!#{iid}](#{mr_url}) *@#{mr.author.username}*"
end

#merge_requests_summary_between(from, to) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/gitlab_awesome_release/project.rb', line 138

def merge_requests_summary_between(from, to)
  mr_iids = merge_request_iids_between(from, to)
  mr_iids.each_with_object("") do |iid, str|
    begin
      str << merge_request_summary(iid) + "\n"
    rescue Gitlab::Error::NotFound
      # NOTE: suppress error (e.g. other repo's iid)
    end
  end
end

#release_tag_namesArray<String>

Returns:

  • (Array<String>)


47
48
49
# File 'lib/gitlab_awesome_release/project.rb', line 47

def release_tag_names
  all_tag_names.find_all {|tag| tag =~ @allow_tag_format }
end

#web_urlObject



26
27
28
# File 'lib/gitlab_awesome_release/project.rb', line 26

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