Class: Release

Inherits:
Object
  • Object
show all
Includes:
ReleaseManager::Logger
Defined in:
lib/release_manager/release.rb

Direct Known Subclasses

RemoteRelease

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ReleaseManager::Logger

#color, #log_level, #logger

Constructor Details

#initialize(path = Dir.getwd, options = {}) ⇒ Release

Returns a new instance of Release.



8
9
10
11
# File 'lib/release_manager/release.rb', line 8

def initialize(path = Dir.getwd, options = {})
  @path = path || Dir.getwd
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/release_manager/release.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/release_manager/release.rb', line 5

def path
  @path
end

Instance Method Details

#add_upstream_remoteObject



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/release_manager/release.rb', line 157

def add_upstream_remote
  if auto_release?
    puppet_module.add_upstream_remote
    return
  end
  answer = nil
  while answer !~ /y|n/
    print "Ok to change your upstream remote from #{puppet_module.upstream}\n to #{puppet_module.source}? (y/n): "
    answer = gets.downcase.chomp
  end
  puppet_module.add_upstream_remote if answer == 'y'
end

#auto_release?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/release_manager/release.rb', line 90

def auto_release?
  options[:auto] || ENV['AUTO_RELEASE'] == 'true'
end

#bumpObject

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/release_manager/release.rb', line 51

def bump
  if dry_run?
    logger.info "Would have just bumped the version to #{version}"
    return
  end
  raise TagExists.new("Tag v#{version} already exists") if puppet_module.tag_exists?("v#{next_version}", options[:remote])
  if puppet_module.respond_to?("bump_#{level}_version".to_sym)
    version = puppet_module.public_send("bump_#{level}_version".to_sym) unless options[:bump]
  end
  # save the update version to the metadata file, then commit
  puppet_module.(options[:remote])
end

#bump_logString

Returns - sha of the commit.

Returns:

  • (String)
    • sha of the commit



69
70
71
72
73
74
75
# File 'lib/release_manager/release.rb', line 69

def bump_log
  if dry_run?
    logger.info "Would have just bumped the CHANGELOG to version #{version}"
    return
  end
  changelog.run(options[:remote], puppet_module.src_branch)
end

#changelogObject



64
65
66
# File 'lib/release_manager/release.rb', line 64

def changelog
  @changelog ||= Changelog.new(puppet_module.path, version, {:commit => true})
end

#check_requirementsObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/release_manager/release.rb', line 94

def check_requirements
  @loop_count = @loop_count.to_i + 1
  begin
    PuppetModule.check_requirements(puppet_module.path)
    raise AlreadyReleased.new("No new changes, skipping release") if puppet_module.already_latest?
    Changelog.check_requirements(puppet_module.path)
  rescue NoUnreleasedLine
    logger.fatal "No Unreleased line in the CHANGELOG.md file, please add a Unreleased line and retry"
    return false
  rescue UpstreamSourceMatch
    logger.warn "The upstream remote url does not match the source url in the metadata.json source"
    add_upstream_remote
    return false if @loop_count > 2
    check_requirements
  rescue InvalidMetadataSource
    logger.fatal "The puppet module's metadata.json source field must be a git url: ie. [email protected]:devops/module.git"
    return false
  rescue NoChangeLogFile
    logger.fatal "CHANGELOG.md does not exist, please create one"
    return false
  end
end

#dry_run?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/release_manager/release.rb', line 86

def dry_run?
  options[:dry_run] == true
end

#levelObject



29
30
31
# File 'lib/release_manager/release.rb', line 29

def level
   options[:level] || 'patch'
end

#next_versionObject



33
34
35
# File 'lib/release_manager/release.rb', line 33

def next_version
  puppet_module.next_version(level)
end

#puppet_moduleObject



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

def puppet_module
  @puppet_module ||= PuppetModule.new(path, {upstream: upstream_repo,
                                             src_branch: options[:src_branch]})
end

#push(id = nil) ⇒ Object

Parameters:

  • id (String) (defaults to: nil)
    • a ref spec to push



78
79
80
81
82
83
84
# File 'lib/release_manager/release.rb', line 78

def push(id = nil)
  if dry_run?
    logger.info "Would have just pushed the code and tag to #{puppet_module.source}"
    return
  end
  puppet_module.push_to_upstream(id)
end

#releaseObject

runs all the required steps to release the software currently this must be done manually by a release manager



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
151
152
153
154
155
# File 'lib/release_manager/release.rb', line 120

def release
  begin
    unless auto_release?
      print "Have you merged your code?  Did you fetch and rebase against the upstream?  Want to continue (y/n)?: ".yellow
      answer = gets.downcase.chomp
      if answer == 'n'
        return false
      end
    end

    # updates the metadata.json file to the next version
    bump
    # updates the changelog to the next version based on the metadata file
    id = bump_log
    # tags the r10k-module with the version found in the metadata.json file
    tag(id)
    # pushes the updated code and tags to the upstream repo
    if auto_release?
     push(id)
     return
    end
    print "Ready to release version #{version} to #{puppet_module.source}\n and forever change history(y/n)?: ".yellow
    answer = gets.downcase.chomp
    if answer == 'y'
      push(id)
      $?.success?
    else
      puts "Nah, forget it, this release wasn't that cool anyways.".yellow
      false
    end
  rescue Rugged::TagError => e
    logger.fatal(e.message)
    logger.fatal("You might need to rebase your branch")
    exit 1
  end
end

#release_notesObject



37
38
39
40
# File 'lib/release_manager/release.rb', line 37

def release_notes
  notes = changelog.get_version_content(version) || changelog.get_unreleased_content || []
  notes.join(" ")
end

#runObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/release_manager/release.rb', line 174

def run
  begin
    exit -1 unless check_requirements
    puppet_module.create_src_branch
    value = release
    unless value
     exit 1
    end
    logger.info "Releasing Version #{version} to #{puppet_module.source}"
    logger.info "Version #{version} has been released successfully"
    puts "This was a dry run so nothing actually happen".green if dry_run?
    exit 0
  rescue Gitlab::Error::Forbidden => e
    logger.fatal(e.message)
    logger.fatal("You don't have access to modify the repository")
    exit -1
  rescue TagExists => e
    logger.fatal(e.message)
    exit -1
  rescue GitError => e
    logger.fatal "There was an issue when running a git command\n #{e.message}"
  rescue InvalidMetadataSource
    logger.fatal "The puppet module's metadata.json source field must be a git url: ie. [email protected]:devops/module.git"
    exit -1
  rescue AlreadyReleased => e
    logger.warn(e.message)
    exit 0
  rescue ModNotFoundException
    logger.fatal "Invalid module path for #{path}"
    exit -1
  end
end

#tag(id) ⇒ Object

Parameters:

  • id (String)
    • the commit id to tag to



43
44
45
46
47
48
49
# File 'lib/release_manager/release.rb', line 43

def tag(id)
  if dry_run?
    logger.info "Would have just tagged the module to #{version}"
    return
  end
  puppet_module.tag_module(options[:remote], id, release_notes)
end

#upstream_repoString

Returns - the url of the repository defined in the options or environment variable.

Returns:

  • (String)
    • the url of the repository defined in the options or environment variable



19
20
21
# File 'lib/release_manager/release.rb', line 19

def upstream_repo
  options[:repo] || ENV['UPSTREAM_REPO']
end

#verbose?Boolean

Returns:

  • (Boolean)


170
171
172
# File 'lib/release_manager/release.rb', line 170

def verbose?
  options[:verbose]
end

#versionObject



24
25
26
# File 'lib/release_manager/release.rb', line 24

def version
   dry_run? ? next_version : puppet_module.version
end