Class: Changelog

Inherits:
WorkflowAction show all
Includes:
ReleaseManager::Git::Utilities, ReleaseManager::Logger, ReleaseManager::VCSManager
Defined in:
lib/release_manager/changelog.rb

Instance Attribute Summary collapse

Attributes included from ReleaseManager::VCSManager

#vcs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReleaseManager::VCSManager

adapter_instance, adapter_types, default_instance

Methods included from ReleaseManager::Logger

#color, #log_level, #logger

Methods included from ReleaseManager::Git::Utilities

#add_all, #add_file, #add_remote, #apply_diff, #apply_patch, #author, #author_email, #author_name, #branch_exist?, #changed_files, #checkout_branch, #cherry_pick, #cli_create_commit, #clone, #create_branch, #create_commit, #create_diff, #create_diff_obj, #create_local_tag, #credentials, #current_branch, #current_branch?, #delete_branch, #fetch, #fetch_cli, #find_or_create_remote, #find_ref, #find_tag, #get_content, #git_command, #git_url?, #push_branch, #push_tags, #rebase_branch, #ref_exists?, #remote_exists?, #remote_from_name, #remote_from_url, #remote_url_matches?, #remove_file, #repo, #tag_exists?, #tags, #transports, #up2date?

Constructor Details

#initialize(module_path, version, options = {}) ⇒ Changelog

Returns a new instance of Changelog.



20
21
22
23
24
# File 'lib/release_manager/changelog.rb', line 20

def initialize(module_path, version, options = {})
  @options = options
  @root_dir = module_path
  @version = version
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/release_manager/changelog.rb', line 14

def options
  @options
end

#root_dirObject (readonly)

Returns the value of attribute root_dir.



14
15
16
# File 'lib/release_manager/changelog.rb', line 14

def root_dir
  @root_dir
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/release_manager/changelog.rb', line 14

def version
  @version
end

Class Method Details

.check_requirements(path) ⇒ Object

checks to make sure the unreleased line is valid, and the file exists



129
130
131
132
# File 'lib/release_manager/changelog.rb', line 129

def self.check_requirements(path)
  log = new(path, nil)
  log.unreleased_index
end

Instance Method Details

#already_released?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/release_manager/changelog.rb', line 79

def already_released?
  !!changelog_lines.each_index.find {|index| changelog_lines[index] =~ /\A\s*\#{2}\s*Version #{version}/i }
end

#changelog_fileObject



53
54
55
# File 'lib/release_manager/changelog.rb', line 53

def changelog_file
  options[:file] || File.join(root_dir, 'CHANGELOG.md')
end

#changelog_linesObject



58
59
60
61
62
63
64
# File 'lib/release_manager/changelog.rb', line 58

def changelog_lines
  unless @changelog_lines
    @changelog_lines = File.exists?(changelog_file) ?
        File.readlines(changelog_file) : empty_changelog_contents.lines
  end
  @changelog_lines
end

#commit_changelog(msg = nil, remote = false, branch = 'master') ⇒ String

Returns the oid of the commit that was created.

Returns:

  • (String)

    the oid of the commit that was created



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/release_manager/changelog.rb', line 112

def commit_changelog(msg = nil, remote = false, branch = 'master')
  message = msg || "[ReleaseManager] - bump changelog to version #{version}"
  if remote
    actions = [{
       action: 'update',
       file_path: changelog_file.split(repo.workdir).last,
       content: new_content
    }]
    obj = vcs_create_commit(source, branch, message, actions)
    obj.id if obj
  else
    add_file(changelog_file)
    create_commit(message)
  end
end

#empty_changelog_contentsObject



26
27
28
# File 'lib/release_manager/changelog.rb', line 26

def empty_changelog_contents
  "# #{module_name}\n\n## Unreleased\n"
end

#get_unreleased_contentArray

Returns - array of lines of the unreleased content, text between unreleased and next version.

Returns:

  • (Array)
    • array of lines of the unreleased content, text between unreleased and next version



84
85
86
87
88
# File 'lib/release_manager/changelog.rb', line 84

def get_unreleased_content
  unreleased_index
  start_content = changelog_lines.slice((unreleased_index + 1), changelog_lines.count)
  start_content.slice_when {|a,b| b.downcase.start_with?('## version') }.map {|d| d}.first
end

#get_version_content(version) ⇒ Array

Note:
  • returns empty string if version is not found

Returns - array of lines of the specified version content, text between specified version and next version.

Parameters:

  • - (String)

    the version of content you want

Returns:

  • (Array)
    • array of lines of the specified version content, text between specified version and next version



93
94
95
96
97
98
# File 'lib/release_manager/changelog.rb', line 93

def get_version_content(version)
  start_index = changelog_lines.find_index {|line| line.downcase.include?("version #{version}") }
  return nil unless start_index
  start_content = changelog_lines.slice((start_index + 1), changelog_lines.count)
  start_content.slice_when {|a,b| b.downcase.start_with?('## version') }.map {|d| d}.first
end

#module_nameObject



30
31
32
# File 'lib/release_manager/changelog.rb', line 30

def module_name
  ['name']
end

#new_contentObject



107
108
109
# File 'lib/release_manager/changelog.rb', line 107

def new_content
  update_unreleased.join
end

#pathObject



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

def path
  @root_dir
end

#run(remote = false, branch = 'master') ⇒ String

Create the changelog entries and commit

Parameters:

  • remote (Boolean) (defaults to: false)
    • if the commit is a remote git on the vcs server

Returns:

  • (String)
    • sha of the commit



41
42
43
44
45
46
47
48
49
50
# File 'lib/release_manager/changelog.rb', line 41

def run(remote = false, branch = 'master')
  if already_released?
    logger.fatal "Version #{version} had already been released, did you bump the version manually?"
    exit 1
  end
  File.write(changelog_file, new_content) unless remote
  id = commit_changelog(nil, remote, branch) if options[:commit]
  logger.info "The changelog has been updated to version #{version}"
  id
end

#unreleased_indexObject

Raises:



67
68
69
70
71
72
73
74
75
76
# File 'lib/release_manager/changelog.rb', line 67

def unreleased_index
  begin
    linenum = changelog_lines.each_index.find {|index| changelog_lines[index] =~ /\A\s*\#{2}\s*Unreleased/i }
  rescue ArgumentError => e
    logger.fatal "Error with CHANGELOG.md #{e.message}"
    exit 1
  end
  raise NoUnreleasedLine unless linenum
  linenum
end

#update_unreleasedObject



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

def update_unreleased
  time = Time.now.strftime("%B %d, %Y")
  changelog_lines.insert(unreleased_index + 1, "\n## Version #{version}\nReleased: #{time}\n")
end