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.



12
13
14
# File 'lib/validator.rb', line 12

def initialize(releasinator_config)
  @releasinator_config = releasinator_config
end

Instance Method Details

#get_changelog_contents(base_dir) ⇒ Object



16
17
18
19
20
# File 'lib/validator.rb', line 16

def get_changelog_contents(base_dir)
  Dir.chdir(base_dir) do
    open('CHANGELOG.md').read
  end
end

#line_match_in_file?(contains_string, filename) ⇒ Boolean

Returns:

  • (Boolean)


150
151
152
153
154
155
156
157
158
159
160
# File 'lib/validator.rb', line 150

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

#rename_alternate_name(expected_file_name, alternate_names) ⇒ Object



313
314
315
316
317
318
319
320
321
322
# File 'lib/validator.rb', line 313

def rename_alternate_name(expected_file_name, alternate_names)
  alternate_names.each do |name|
    Dir.glob(name) do |entry|
      puts "Found similar file: #{name}."
      rename_file(name, expected_file_name)
      return true
    end
  end
  false
end

#rename_file(old_name, new_name) ⇒ Object



305
306
307
308
309
310
311
# File 'lib/validator.rb', line 305

def rename_file(old_name, new_name)
  puts "Renaming #{old_name} to expected filename: #{new_name}".yellow
  CommandProcessor.command("mv #{old_name} #{new_name}")
  # fix any references to file in readme
  replace_string("README.md", "(#{old_name})", "(#{new_name})")
  CommandProcessor.command("git add . && git commit -m \"#{@releasinator_config[:releasinator_name]}: rename #{old_name} to #{new_name}\"")
end

#validate_changelog(base_dir, downstream_dir) ⇒ Object



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

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

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

#validate_clean_gitObject



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

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/validator.rb', line 77

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/validator.rb', line 177

def validate_exist(base_dir, expected_file_name, downstream_dir, alternate_names=[])
  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



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

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



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

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(line, is_downstream_present) ⇒ Object



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

def validate_gitignore(line, is_downstream_present)
  if !File.exist?(".gitignore")
    is_git_already_clean = GitUtil.new().is_clean_git?
    FileUtils.touch('.gitignore')
    if is_git_already_clean
      CommandProcessor.command("git add . && git commit -m \"#{@releasinator_config[:releasinator_name]}: add .gitignore\"")
      Printer.success("Added .gitignore file.")
    else
      Printer.fail("Added .gitignore, but there are other changes in the workspace.  Please commit and continue.")
      abort()
    end
  end
  Printer.success(".gitignore found.")

  if is_downstream_present
    if !line_match_in_file?(line, ".gitignore")
      is_git_already_clean = GitUtil.new().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
end

#validate_is_type(obj, type) ⇒ Object



43
44
45
46
47
48
# File 'lib/validator.rb', line 43

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

#validate_matches_branch(branch_name, console_prefix = "Root") ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/validator.rb', line 276

def validate_matches_branch(branch_name, console_prefix="Root")
  current_dir = Dir.pwd

  # Don't fetch the submodules, as they should already be fetched by the initial recursive fetch.
  if console_prefix == "Root"
    puts "fetching #{current_dir}" if @releasinator_config[:verbose]

    # silently fails if it can't connect because sometimes we want to release even if 
    # corp GitHub is down.  
    `git fetch --recurse-submodules -j9`
  end

  validate_clean_git()

  head_sha1 = `git rev-parse --verify head`.strip
  origin_branch_sha1 = `git rev-parse --verify origin/#{branch_name}`.strip
  if head_sha1 != origin_branch_sha1
    abort_string = "#{console_prefix} #{current_dir} at #{head_sha1}, but origin/#{branch_name} is #{origin_branch_sha1}."\
    "\nIf you received this error on the root project, you may need to:"\
    "\n  1. pull the latest changes from the remote,"\
    "\n  2. push changes up to the remote,"\
    "\n  3. back out a current release in progress."
    Printer.fail(abort_string)
    abort()
  else
    Printer.success("#{console_prefix} #{current_dir} matches origin/#{branch_name}.")
  end
end

#validate_method_convention(hash) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/validator.rb', line 50

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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/validator.rb', line 162

def validate_referenced_in_readme(base_dir, filename)
  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_required_configatron_key(key) ⇒ Object



70
71
72
73
74
75
# File 'lib/validator.rb', line 70

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

#validate_submodulesObject



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
269
270
271
272
273
274
# File 'lib/validator.rb', line 244

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
        validate_matches_branch("master", "Submodule")
      end
    end
  else
    Printer.success("No submodules found.")
  end
end