Module: MrBump

Defined in:
lib/mr_bump.rb,
lib/mr_bump/slack.rb,
lib/mr_bump/change.rb,
lib/mr_bump/config.rb,
lib/mr_bump/git_api.rb,
lib/mr_bump/version.rb,
lib/mr_bump/git_config.rb,
lib/mr_bump/regex_template.rb

Overview

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at mozilla.org/MPL/2.0/.

Defined Under Namespace

Classes: Change, Config, GitApi, GitConfig, RegexTemplate, Slack, Version

Class Method Summary collapse

Class Method Details

.all_tagged_versionsObject



86
87
88
89
90
91
92
93
94
# File 'lib/mr_bump.rb', line 86

def self.all_tagged_versions
  all_tags.map do |tag|
    begin
      MrBump::Version.new(tag)
    rescue
      nil
    end
  end.compact
end

.all_tagsObject



82
83
84
# File 'lib/mr_bump.rb', line 82

def self.all_tags
  `git tag -l`.each_line.map(&:strip)
end

.change_log_items_for_range(rev, head) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mr_bump.rb', line 113

def self.change_log_items_for_range(rev, head)
  ignored_branch = Regexp.new("^(#{release_branch_regex}|master|develop)$")
  make_change = lambda do |title, comment = []|
    change = MrBump::Change.from_gitlog(config_file, title, comment)
    change unless ignored_branch.match(change.branch_name)
  end

  chunked_log = merge_logs(rev, head).chunk { |change| change[/^Merge/].nil? }
  chunked_log.each_slice(2).map do |merge_str, comment|
    begin
      no_comment_changes = merge_str[1][0..-2].map(&make_change)
      commented_changes = make_change.call(merge_str[1][-1], comment.nil? ? [] : comment[1])
      no_comment_changes.push(commented_changes)
    rescue ArgumentError => e
      puts e
    end
  end.flatten.compact
end

.config_fileObject



143
144
145
# File 'lib/mr_bump.rb', line 143

def self.config_file
  @config_file ||= MrBump::Config.new.config
end

.current_branchObject



13
14
15
# File 'lib/mr_bump.rb', line 13

def self.current_branch
  @current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip.freeze
end

.current_masterObject



101
102
103
104
# File 'lib/mr_bump.rb', line 101

def self.current_master
  uat = current_uat_major
  all_tagged_versions.select { |ver| ver < uat }.max
end

.current_uatObject



96
97
98
99
# File 'lib/mr_bump.rb', line 96

def self.current_uat
  uat = current_uat_major
  all_tagged_versions.select { |ver| ver.major == uat.major && ver.minor == uat.minor }.max
end

.current_uat_majorObject



73
74
75
76
# File 'lib/mr_bump.rb', line 73

def self.current_uat_major
  branches = `git branch -r`.each_line.map(&:strip)
  latest_release_from_list(branches)
end

.file_prepend(file, str) ⇒ Object



132
133
134
135
136
137
138
139
140
141
# File 'lib/mr_bump.rb', line 132

def self.file_prepend(file, str)
  new_contents = ''
  File.open(file, 'r') do |fd|
    contents = fd.read
    new_contents = str + contents
  end
  File.open(file, 'w') do |fd|
    fd.write(new_contents)
  end
end

.git_configObject



153
154
155
# File 'lib/mr_bump.rb', line 153

def self.git_config
  @git_config ||= MrBump::GitConfig.from_current_path
end

.last_releaseObject



46
47
48
49
50
51
52
53
54
# File 'lib/mr_bump.rb', line 46

def self.last_release
  if on_release_branch? || (on_master_branch? && release_stale?)
    MrBump.current_uat
  elsif on_master_branch?
    MrBump.current_master
  elsif on_develop_branch?
    MrBump.current_uat_major
  end
end

.latest_release_from_list(branches) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/mr_bump.rb', line 65

def self.latest_release_from_list(branches)
  regex = Regexp.new("^origin/#{release_branch_regex}$")
  branches.map do |branch|
    matches = regex.match(branch.force_encoding("UTF-8"))
    MrBump::Version.new(matches[1]) if matches
  end.compact.max || MrBump::Version.new('0.0.0')
end

.merge_logs(rev, head) ⇒ Object



106
107
108
109
110
111
# File 'lib/mr_bump.rb', line 106

def self.merge_logs(rev, head)
  git_cmd = "git log --pretty='format:%B' --grep " \
            "'(^Merge pull request | into #{current_branch}$)' --merges -E"
  log = `#{git_cmd} #{rev}..#{head}`
  log.each_line.map(&:strip).select { |str| !(str.nil? || str == '' || str[0] == '#') }
end

.next_releaseObject



56
57
58
59
60
61
62
63
# File 'lib/mr_bump.rb', line 56

def self.next_release
  return nil unless last_release
  if on_release_branch? || on_master_branch?
    last_release.bump_patch
  elsif on_develop_branch?
    last_release.bump_minor
  end
end

.on_develop_branch?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mr_bump.rb', line 38

def self.on_develop_branch?
  !MrBump.current_branch[/^develop$/].nil?
end

.on_master_branch?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/mr_bump.rb', line 34

def self.on_master_branch?
  !MrBump.current_branch[/^master$/].nil?
end

.on_release_branch?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/mr_bump.rb', line 29

def self.on_release_branch?
  regex = Regexp.new("^#{release_branch_regex}$")
  !MrBump.current_branch[regex].nil?
end

.release_branch_for_version(ver) ⇒ Object



23
24
25
26
27
# File 'lib/mr_bump.rb', line 23

def self.release_branch_for_version(ver)
  prefix = config_file['release_prefix']
  suffix = config_file['release_suffix']
  "#{prefix}#{ver.major}.#{ver.minor}.0#{suffix}"
end

.release_branch_regexObject



17
18
19
20
21
# File 'lib/mr_bump.rb', line 17

def self.release_branch_regex
  prefix = Regexp.escape(config_file['release_prefix'])
  suffix = Regexp.escape(config_file['release_suffix'])
  "#{prefix}(\\d+\\.\\d+)(\\.\\d+)?#{suffix}"
end

.release_stale?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/mr_bump.rb', line 42

def self.release_stale?
  !`git branch master --contains #{MrBump.current_uat_major}`.strip.empty?
end

.slack_notifier(version, changelog) ⇒ Object



147
148
149
150
151
# File 'lib/mr_bump.rb', line 147

def self.slack_notifier(version, changelog)
  if config_file.key? 'slack'
    MrBump::Slack.new(git_config, config_file).bump(version, changelog)
  end
end

.uat_branchObject



78
79
80
# File 'lib/mr_bump.rb', line 78

def self.uat_branch
  release_branch_for_version(current_uat_major)
end