Class: Fastlane::Actions::ConventionalChangelogAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



281
282
283
284
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 281

def self.authors
  # So no one will ever forget your contribution to fastlane :) You are awesome btw!
  ["xotahal"]
end

.available_optionsObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 202

def self.available_options
  # Define all options your action supports.

  # Below a few examples
  [
    FastlaneCore::ConfigItem.new(
      key: :format,
      description: "You can use either markdown, slack or plain",
      default_value: "markdown",
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :title,
      description: "Title for release notes",
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :commit_url,
      description: "Uses as a link to the commit",
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :order,
      description: "You can change the order of groups in release notes",
      default_value: ["feat", "fix", "refactor", "perf", "chore", "test", "docs", "no_type"],
      type: Array,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :sections,
      description: "Map type to section title",
      default_value: {
        feat: "Features",
        fix: "Bug fixes",
        refactor: "Code refactoring",
        perf: "Performance improvements",
        chore: "Building system",
        test: "Testing",
        docs: "Documentation",
        no_type: "Other work"
      },
      type: Hash,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :display_author,
      description: "Whether you want to show the author of the commit",
      default_value: false,
      type: Boolean,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :display_title,
      description: "Whether you want to hide the title/header with the version details at the top of the changelog",
      default_value: true,
      type: Boolean,
      optional: true
    ),
    FastlaneCore::ConfigItem.new(
      key: :display_links,
      description: "Whether you want to display the links to commit IDs",
      default_value: true,
      type: Boolean,
      optional: true
    )
  ]
end


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 152

def self.build_commit_link(commit, commit_url, format)
  # formats the link according to the output format we need
  short_hash = commit[:short_hash]
  hash = commit[:hash]
  url = "#{commit_url}/#{hash}"

  case format
  when "slack"
    "<#{url}|#{short_hash}>"
  when "markdown"
    "[#{short_hash}](#{url})"
  else
    url
  end
end

.descriptionObject



194
195
196
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 194

def self.description
  "Get commits since last version and generates release notes"
end

.detailsObject



198
199
200
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 198

def self.details
  "Uses conventional commits. It groups commits by their types and generates release notes in markdown or slack format."
end

.get_commits_from_hash(params) ⇒ Object



10
11
12
13
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 10

def self.get_commits_from_hash(params)
  commits = Helper::SemanticReleaseHelper.git_log('%s|%b|%H|%h|%an|%at|>', params[:hash])
  commits.split("|>")
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
289
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 286

def self.is_supported?(platform)
  # you can do things like
  true
end

.note_builder(format, commits, version, commit_url, params) ⇒ Object



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
110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 41

def self.note_builder(format, commits, version, commit_url, params)
  sections = params[:sections]

  result = ""

  # Begining of release notes
  if params[:display_title] == true
    title = version
    title += " #{params[:title]}" if params[:title]
    title += " (#{Date.today})"

    result = style_text(title, format, "title").to_s
    result += "\n\n"
  end

  params[:order].each do |type|
    # write section only if there is at least one commit
    next if commits.none? { |commit| commit[:type] == type }

    result += style_text(sections[type.to_sym], format, "heading").to_s
    result += "\n"

    commits.each do |commit|
      next if commit[:type] != type || commit[:is_merge]

      result += "-"

      unless commit[:scope].nil?
        formatted_text = style_text("#{commit[:scope]}:", format, "bold").to_s
        result += " #{formatted_text}"
      end

      result += " #{commit[:subject]}"

      if params[:display_links] == true
        styled_link = build_commit_link(commit, commit_url, format).to_s
        result += " (#{styled_link})"
      end

      if params[:display_author]
        result += " - #{commit[:author_name]}"
      end

      result += "\n"
    end
    result += "\n"
  end

  if commits.any? { |commit| commit[:is_breaking_change] == true }
    result += style_text("BREAKING CHANGES", format, "heading").to_s
    result += "\n"

    commits.each do |commit|
      next unless commit[:is_breaking_change]
      result += "- #{commit[:breaking_change]}" # This is the only unique part of this loop

      if params[:display_links] == true
        styled_link = build_commit_link(commit, commit_url, format).to_s
        result += " (#{styled_link})"
      end

      if params[:display_author]
        result += " - #{commit[:author_name]}"
      end

      result += "\n"
    end

    result += "\n"
  end

  # Trim any trailing newlines
  result = result.rstrip!

  result
end

.outputObject



270
271
272
273
274
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 270

def self.output
  # Define the shared values you are going to provide
  # Example
  []
end

.parse_commits(commits) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 168

def self.parse_commits(commits)
  parsed = []
  # %s|%b|%H|%h|%an|%at
  commits.each do |line|
    splitted = line.split("|")

    commit = Helper::SemanticReleaseHelper.parse_commit(
      commit_subject: splitted[0],
      commit_body: splitted[1]
    )

    commit[:hash] = splitted[2]
    commit[:short_hash] = splitted[3]
    commit[:author_name] = splitted[4]
    commit[:commit_date] = splitted[5]

    parsed.push(commit)
  end

  parsed
end

.return_valueObject



276
277
278
279
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 276

def self.return_value
  # If your method provides a return value, you can describe here what it does
  "Returns generated release notes as a string"
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 15

def self.run(params)
  # Get next version number from shared values
  analyzed = lane_context[SharedValues::RELEASE_ANALYZED]

  # If analyze commits action was not run there will be no version in shared
  # values. We need to run the action to get next version number
  unless analyzed
    UI.user_error!("Release hasn't been analyzed yet. Run analyze_commits action first please.")
    # version = other_action.analyze_commits(match: params[:match])
  end

  last_tag_hash = lane_context[SharedValues::RELEASE_LAST_TAG_HASH]
  version = lane_context[SharedValues::RELEASE_NEXT_VERSION]

  # Get commits log between last version and head
  commits = get_commits_from_hash(hash: last_tag_hash)
  parsed = parse_commits(commits)

  commit_url = params[:commit_url]
  format = params[:format]

  result = note_builder(format, parsed, version, commit_url, params)

  result
end

.style_text(text, format, style) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fastlane/plugin/semantic_release/actions/conventional_changelog.rb', line 118

def self.style_text(text, format, style)
  # formats the text according to the style we're looking to use

  # Skips all styling
  case style
  when "title"
    if format == "markdown"
      "# #{text}"
    elsif format == "slack"
      "*#{text}*"
    else
      text
    end
  when "heading"
    if format == "markdown"
      "### #{text}"
    elsif format == "slack"
      "*#{text}*"
    else
      "#{text}:"
    end
  when "bold"
    if format == "markdown"
      "**#{text}**"
    elsif format == "slack"
      "*#{text}*"
    else
      text
    end
  else
    text # catchall, shouldn't be needed
  end
end