From cb6865b0a826a9225a918c5ffaf99f4579d7797c Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Sun, 3 Jan 2021 10:12:14 +0100 Subject: [PATCH] thor updates:check group major/minor updates --- lib/docs/core/doc.rb | 11 +++++---- lib/tasks/updates.thor | 43 +++++++++++++++------------------- test/lib/docs/core/doc_test.rb | 20 ++++++++-------- 3 files changed, 35 insertions(+), 39 deletions(-) diff --git a/lib/docs/core/doc.rb b/lib/docs/core/doc.rb index 979e2c41..5eae27aa 100644 --- a/lib/docs/core/doc.rb +++ b/lib/docs/core/doc.rb @@ -184,7 +184,7 @@ module Docs raise NotImplementedError end - # Returns whether or not this scraper is outdated. + # Returns whether or not this scraper is outdated ("Outdated major version", "Outdated minor version" or 'Up-to-date'). # # The default implementation assumes the documentation uses a semver(-like) approach when it comes to versions. # Patch updates are ignored because there are usually little to no documentation changes in bug-fix-only releases. @@ -195,18 +195,19 @@ module Docs # 1 -> 2 = outdated # 1.1 -> 1.2 = outdated # 1.1.1 -> 1.1.2 = not outdated - def is_outdated(scraper_version, latest_version) + def outdated_state(scraper_version, latest_version) scraper_parts = scraper_version.to_s.split(/[-.]/).map(&:to_i) latest_parts = latest_version.to_s.split(/[-.]/).map(&:to_i) # Only check the first two parts, the third part is for patch updates [0, 1].each do |i| break if i >= scraper_parts.length or i >= latest_parts.length - return true if latest_parts[i] > scraper_parts[i] - return false if latest_parts[i] < scraper_parts[i] + return 'Outdated major version' if i == 0 and latest_parts[i] > scraper_parts[i] + return 'Outdated minor version' if i == 1 and latest_parts[i] > scraper_parts[i] + return 'Up-to-date' if latest_parts[i] < scraper_parts[i] end - false + 'Up-to-date' end private diff --git a/lib/tasks/updates.thor b/lib/tasks/updates.thor index 1b37536f..62dcf20c 100644 --- a/lib/tasks/updates.thor +++ b/lib/tasks/updates.thor @@ -68,7 +68,7 @@ class UpdatesCLI < Thor name: doc.name, scraper_version: format_version(scraper_version), latest_version: format_version(latest_version), - is_outdated: instance.is_outdated(scraper_version, latest_version) + outdated_state: instance.outdated_state(scraper_version, latest_version) } rescue NotImplementedError logger.warn("Couldn't check #{doc.name}, get_latest_version is not implemented") @@ -95,28 +95,27 @@ class UpdatesCLI < Thor end def process_results(results) - successful_results = results.select {|result| result.key?(:is_outdated)} + successful_results = results.select {|result| result.key?(:outdated_state)} + grouped_results = successful_results.group_by {|result| result[:outdated_state]} failed_results = results.select {|result| result.key?(:error)} - up_to_date_results = successful_results.select {|result| !result[:is_outdated]} - outdated_results = successful_results.select {|result| result[:is_outdated]} - - log_results(outdated_results, up_to_date_results, failed_results) - upload_results(outdated_results, up_to_date_results, failed_results) if options[:upload] + log_results(grouped_results, failed_results) + upload_results(grouped_results, failed_results) if options[:upload] end # # Result logging methods # - def log_results(outdated_results, up_to_date_results, failed_results) + def log_results(grouped_results, failed_results) if options[:markdown] - puts all_results_to_markdown(outdated_results, up_to_date_results, failed_results) + puts all_results_to_markdown(grouped_results, failed_results) return end log_failed_results(failed_results) unless failed_results.empty? - log_successful_results('Up-to-date', up_to_date_results) unless up_to_date_results.empty? - log_successful_results('Outdated', outdated_results) unless outdated_results.empty? + grouped_results.each do |label, results| + log_successful_results(label, results) + end end def log_successful_results(label, results) @@ -141,7 +140,7 @@ class UpdatesCLI < Thor # Upload methods # - def upload_results(outdated_results, up_to_date_results, failed_results) + def upload_results(grouped_results, failed_results) # We can't create issues without a GitHub token unless options.key?(:github_token) logger.error("Please specify a GitHub token with the public_repo permission for #{UPLOAD_USER} with the --github-token parameter") @@ -163,7 +162,7 @@ class UpdatesCLI < Thor issue = { title: "Documentation versions report for #{Date.today.strftime('%B %Y')}", - body: all_results_to_markdown(outdated_results, up_to_date_results, failed_results) + body: all_results_to_markdown(grouped_results, failed_results) } created_issue = github_post("/repos/#{UPLOAD_REPO}/issues", issue) @@ -197,14 +196,14 @@ class UpdatesCLI < Thor end end - def all_results_to_markdown(outdated_results, up_to_date_results, failed_results) - results = [ - successful_results_to_markdown('Outdated', outdated_results), - successful_results_to_markdown('Up-to-date', up_to_date_results), - failed_results_to_markdown(failed_results) - ] + def all_results_to_markdown(grouped_results, failed_results) + all_results = [] + grouped_results.each do |label, results| + all_results.push(successful_results_to_markdown(label, results)) + end + all_results.push(failed_results_to_markdown(failed_results)) - results_str = results.select {|result| !result.nil?}.join("\n\n") + results_str = all_results.select {|result| !result.nil?}.join("\n\n") travis_str = ENV['TRAVIS'].nil? ? '' : "\n\nThis issue was created by Travis CI build [##{ENV['TRAVIS_BUILD_NUMBER']}](#{ENV['TRAVIS_BUILD_WEB_URL']})." body = <<-MARKDOWN @@ -216,10 +215,6 @@ Maintainers can close this issue when all documentations are up-to-date. The iss ## Results -The #{outdated_results.length + up_to_date_results.length + failed_results.length} documentations are divided as follows: -- #{outdated_results.length} that #{outdated_results.length == 1 ? 'is' : 'are'} outdated -- #{up_to_date_results.length} that #{up_to_date_results.length == 1 ? 'is' : 'are'} up-to-date (patch updates are ignored) -- #{failed_results.length} that could not be checked MARKDOWN body.strip + "\n\n" + results_str end diff --git a/test/lib/docs/core/doc_test.rb b/test/lib/docs/core/doc_test.rb index c0ea718c..b188ff0d 100644 --- a/test/lib/docs/core/doc_test.rb +++ b/test/lib/docs/core/doc_test.rb @@ -390,16 +390,16 @@ class DocsDocTest < MiniTest::Spec it "compares versions" do instance = doc.versions.first.new - assert !instance.is_outdated('1', '1') - assert !instance.is_outdated('1.2', '1.2') - assert !instance.is_outdated('1.2.2', '1.2.2') - assert !instance.is_outdated('1.2.2', '1.2.3') - assert instance.is_outdated('1', '2') - assert instance.is_outdated('1.2', '1.3') - assert instance.is_outdated('9', '10') - assert instance.is_outdated('99', '101') - assert !instance.is_outdated('2006-01-02', '2006-01-03') - assert instance.is_outdated('2006-01-02', '2006-02-03') + assert_equal 'Up-to-date', instance.outdated_state('1', '1') + assert_equal 'Up-to-date', instance.outdated_state('1.2', '1.2') + assert_equal 'Up-to-date', instance.outdated_state('1.2.2', '1.2.2') + assert_equal 'Up-to-date', instance.outdated_state('1.2.2', '1.2.3') + assert_equal "Outdated major version", instance.outdated_state('1', '2') + assert_equal "Outdated minor version", instance.outdated_state('1.2', '1.3') + assert_equal "Outdated major version", instance.outdated_state('9', '10') + assert_equal "Outdated major version", instance.outdated_state('99', '101') + assert_equal 'Up-to-date', instance.outdated_state('2006-01-02', '2006-01-03') + assert_equal "Outdated minor version", instance.outdated_state('2006-01-02', '2006-02-03') end end end