Class: Releasinator::Validator

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

Defined Under Namespace

Classes: Submodule

Instance Method Summary collapse

Constructor Details

#initialize(releasinator_config) ⇒ Validator

Returns a new instance of Validator.



15
16
17
# File 'lib/validator.rb', line 15

def initialize(releasinator_config)
  @releasinator_config = releasinator_config
end

Instance Method Details

#line_match_in_file?(contains_string, filename) ⇒ Boolean

Returns:

  • (Boolean)


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

def line_match_in_file?(contains_string, filename)
  File.open("#{filename}", "r") do |f|
    f.each_line do |line|
      if line.match /^#{Regexp.escape(contains_string)}$/
        Printer.success("#{filename} contains #{contains_string}")
        return true
      end
    end
  end
  false
end

#validate_base_dir(base_dir) ⇒ Object



211
212
213
214
215
216
# File 'lib/validator.rb', line 211

def validate_base_dir(base_dir)
  if !File.exist? base_dir
    Printer.fail("Directory specified by base_docs_dir '#{base_dir}' not found.  Please fix the config, or add this directory.")
    abort()
  end
end

#validate_branches(version) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/validator.rb', line 300

def validate_branches(version)
  GitUtil.fetch()
  current_branch = GitUtil.get_current_branch()

  if @releasinator_config.use_git_flow()
    expected_release_branch = "release/#{version}"

    unless current_branch == expected_release_branch || current_branch == "develop"
      Printer.fail("git flow expects the current branch to be either 'develop' or 'release/#{version}'.  Current branch is '#{current_branch}'")
      abort()
    end

    # validate that master is up to date, because git flow requires this.
    validate_local_matches_remote("master")
  else
    unless current_branch == "master"
      Printer.fail("non-git flow expects releases to come from the master branch.  Current branch is '#{current_branch}'")
      abort()
    end
  end

  validate_local_matches_remote(current_branch)
end

#validate_changelog(base_dir, downstream_dir) ⇒ Object



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

def validate_changelog(base_dir, downstream_dir)
  validate_base_dir base_dir
  validate_exist(base_dir, "CHANGELOG.md", downstream_dir, ["release_notes.md"])

  changelog_contents = get_changelog_contents(base_dir)
  ValidatorChangelog.new(@releasinator_config).validate_changelog_contents(changelog_contents)
end

#validate_clean_gitObject



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
# File 'lib/validator.rb', line 218

def validate_clean_git
  untracked_files = GitUtil.untracked_files
  diff = GitUtil.diff
  diff_cached = GitUtil.cached

  if '' != untracked_files
    puts untracked_files.red if @releasinator_config[:verbose]
    error = true
    Printer.fail("Untracked files found.")
  else
    Printer.success("No untracked files found.")
  end

  if '' != diff
    puts diff.red if @releasinator_config[:verbose]
    error = true
    Printer.fail("Unstaged changes found.")
  else
    Printer.success("No unstaged changes found.")
  end

  if '' != diff_cached
    puts diff_cached.red if @releasinator_config[:verbose]
    error = true
    Printer.fail("Uncommitted changes found.")
  else
    Printer.success("No uncommitted changes found.")
  end

  abort() if error
end

#validate_configObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/validator.rb', line 99

def validate_config()
  validate_required_configatron_key(:product_name)
  validate_required_configatron_key(:prerelease_checklist_items)
  validate_required_configatron_key(:build_method)
  validate_required_configatron_key(:publish_to_package_manager_method)
  validate_required_configatron_key(:wait_for_package_manager_method)
  validate_required_configatron_key(:release_to_github)

  validate_method_convention(@releasinator_config)

  if @releasinator_config.has_key? :downstream_repos
    @releasinator_config[:downstream_repos].each do |downsteam_repo|
      validate_is_type downsteam_repo, DownstreamRepo

      validate_method_convention(downsteam_repo.options)
    end
  end
end

#validate_exist(base_dir, expected_file_name, downstream_dir, alternate_names = []) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/validator.rb', line 185

def validate_exist(base_dir, expected_file_name, downstream_dir, alternate_names=[])
  validate_base_dir base_dir
  Dir.chdir(base_dir) do
    if !File.exist?(expected_file_name)
      puts "#{base_dir}/#{expected_file_name} not found. Searching for similar files.".yellow
    
      # search for files that are somewhat similar to the file being searched, ignoring case
      filename_prefix = expected_file_name[0,5]
      similar_files = CommandProcessor.command("find . -type f -not -path \"./#{downstream_dir}/*\" -iname '#{filename_prefix}*'| sed 's|./||'").strip
      num_similar_files = similar_files.split.count
      puts similar_files
      if num_similar_files == 1
        Printer.check_proceed("Found a single similar file: #{similar_files}.  Do you want to rename this to the expected #{expected_file_name}?","Please place #{base_dir}/#{expected_file_name}")
        rename_file(similar_files, expected_file_name)
      elsif num_similar_files > 1
        Printer.fail("Found more than 1 file similar to #{expected_file_name}.  Please rename one, and optionally remove the others to not confuse users.")
        abort()
      elsif !rename_alternate_name(expected_file_name, alternate_names)
        Printer.fail("Please place #{base_dir}/#{expected_file_name}.") 
        abort()
      end
    end
    Printer.success("#{base_dir}/#{expected_file_name} found.")
  end
end

#validate_git_versionObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/validator.rb', line 43

def validate_git_version
  version_output = CommandProcessor.command("git version")
  # version where the parallel git fetch features were added
  expected_git_version = "2.8.0"
  actual_git_version = version_output.split[2]

  if Gem::Version.new(expected_git_version) > Gem::Version.new(actual_git_version)
    Printer.fail("Actual git version " + actual_git_version.bold + " is smaller than expected git version " + expected_git_version.bold)
    abort()
  else
    Printer.success("Git version " + actual_git_version.bold + " found, and is higher than or equal to expected git version " + expected_git_version.bold)
  end
end

#validate_github_permissions(repo_url) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/validator.rb', line 118

def validate_github_permissions(repo_url)
  github_repo = GitHubRepo.new(repo_url)
  github_client = github_repo.client

  begin
    # get the list of collaborators.
    puts "Checking collaborators on #{repo_url}." if @releasinator_config[:verbose]
    github_collaborators = github_client.collaborators "#{github_repo.org}/#{github_repo.repo}"
    if ! github_collaborators
      Printer.fail("request failed with code:#{res.code}\nbody:#{res.body}")
      abort()
    end
    puts github_collaborators.inspect if @releasinator_config[:trace]
    Printer.success("User has push permissions on #{repo_url}.")
  rescue => error
    #This will fail if the user does not have push permissions.
    Printer.fail(error.inspect)
    abort()
  end
end

#validate_gitignore_contents(line) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/validator.rb', line 139

def validate_gitignore_contents(line)
  if !line_match_in_file?(line, ".gitignore")
    is_git_already_clean = GitUtil.is_clean_git?
    File.open('.gitignore', 'a') do |f|
      f.puts "# #{@releasinator_config[:releasinator_name]}"
      f.puts line
    end

    if is_git_already_clean
      CommandProcessor.command("git add . && git commit -m \"#{@releasinator_config[:releasinator_name]}: add downstream dir to .gitignore\"")
      Printer.success("Added downstream dir to .gitignore file.")
    else
      Printer.fail("Added downstream dir to .gitignore file, but there are other changes in the workspace.  Please commit and continue.")
      abort()
    end
  end
end

#validate_in_path(executable) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/validator.rb', line 34

def validate_in_path(executable)
  if "" == CommandProcessor.command("which #{executable} | cat")
    Printer.fail(executable.bold + " not found on path.")
    abort()
  else
    Printer.success(executable.bold + " found on path.")
  end
end

#validate_is_type(obj, type) ⇒ Object



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

def validate_is_type(obj, type)
  if !obj.is_a? type
    Printer.fail("#{obj} is not a #{type}.")
    abort()
  end 
end

#validate_method_convention(hash) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/validator.rb', line 72

def validate_method_convention(hash)
  hash.each do |key, value|
    if key.to_s.end_with? "_methods" 
      # validate that anything ending in _methods is a list of methods
      if !value.respond_to? :each
        Printer.fail("#{key} is not a list.")
        abort()
      end
      value.each do |list_item|
        validate_is_type list_item, Method
      end
    elsif key.to_s.end_with? "_method"
      # anything ending in _method is a method
      validate_is_type value, Method
    else
      # ignore everything else
    end
  end
end

#validate_referenced_in_readme(base_dir, filename) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/validator.rb', line 169

def validate_referenced_in_readme(base_dir, filename)
  validate_base_dir base_dir
  Dir.chdir(base_dir) do
    File.open("README.md", "r") do |f|
      f.each_line do |line|
        if line.include? "(#{filename})"
          Printer.success("#{filename} referenced in #{base_dir}/README.md")
          return
        end
      end
    end
  end
  Printer.fail("Please link to the #{filename} file somewhere in #{base_dir}/README.md.")
  abort()
end

#validate_releasinator_versionObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/validator.rb', line 19

def validate_releasinator_version
  uri = URI('https://rubygems.org/api/v1/gems/releasinator.json')
  latest_version = JSON.parse(Net::HTTP.get(uri))["version"]
  current_version = Releasinator::VERSION

  if Gem::Version.new(latest_version) > Gem::Version.new(current_version)
    Printer.fail("Please upgrade to the latest releasinator version:" + latest_version.bold + ".  Current version:" + current_version.bold)
    abort()
  elsif Gem::Version.new(latest_version) < Gem::Version.new(current_version)
    Printer.success("Releasinator version: " + current_version.bold + " is newer than one from rubygems.org: " + latest_version.bold + ".  You're probably testing a development version.")
  else
    Printer.success("Releasinator version: " + latest_version.bold + " is the latest from rubygems.org.")
  end
end

#validate_required_configatron_key(key) ⇒ Object



92
93
94
95
96
97
# File 'lib/validator.rb', line 92

def validate_required_configatron_key(key)
  if !@releasinator_config.has_key?(key)
    Printer.fail("No #{key} found in configatron.")
    abort()
  end
end

#validate_submodulesObject



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/validator.rb', line 251

def validate_submodules
  if File.exist?(".gitmodules")
    submodules = Array.new

    current_name = nil
    current_path = nil
    current_url = nil
    File.open(".gitmodules", "r") do |f|
      f.each_line do |line|

        if line.include? "\""
          current_name = line.strip.split(' ').last.to_s.split("\"").at(1)
        elsif line.include? "path = "
          current_path = line.strip.split(' ').last.to_s
        elsif line.include? "url = "
          current_url = line.strip.split(' ').last.to_s
          submodules << Submodule.new(current_name, current_path, current_url)
        end
      end
    end

    Printer.success("Found " + submodules.count.to_s.bold + " submodules in .gitmodules.")
    submodules.each do |submodule|
      Dir.chdir(submodule.path) do
        if GitUtil.detached?
          local_sha1 = GitUtil.get_local_head_sha1()
        else
          local_branch_name = GitUtil.get_current_branch
          local_sha1 = GitUtil.get_local_branch_sha1(local_branch_name)
        end

        origin_master_sha1 = GitUtil.get_remote_branch_sha1("master")

        if local_sha1 != origin_master_sha1
          abort_string = "Submodule #{Dir.pwd} not on latest master.  Currently at #{local_sha1}, but origin/master is at #{origin_master_sha1}."\
          "\nYou should update this submodule to the latest in origin/master."
          Printer.fail(abort_string)
          abort()
        else
          Printer.success("Submodule #{Dir.pwd} matches origin/master.")
        end
        validate_clean_git()
      end
    end
  else
    Printer.success("No submodules found.")
  end
end