diff --git a/gitarro.rb b/gitarro.rb index 9028200..bb37f75 100755 --- a/gitarro.rb +++ b/gitarro.rb @@ -8,7 +8,14 @@ require_relative 'lib/gitarro/backend' b = Backend.new -prs = b.open_prs +prs = b.open_newer_prs + +# Exit without errors if no PRs were found +if prs.empty? + puts 'There are no Pull Requests opened or with changes newer than ' \ + "#{b.options[:change_newer]} minutes" + exit(0) +end prs.each do |pr| puts '=' * 30 + "\n" + "TITLE_PR: #{pr.title}, NR: #{pr.number}\n" + '=' * 30 diff --git a/lib/gitarro/backend.rb b/lib/gitarro/backend.rb index 4b8a0d7..6d166bd 100755 --- a/lib/gitarro/backend.rb +++ b/lib/gitarro/backend.rb @@ -2,6 +2,7 @@ require 'octokit' require 'optparse' +require 'time' require 'English' require_relative 'opt_parser' require_relative 'git_op' @@ -63,11 +64,16 @@ def initialize(option = nil) @gbexec = TestExecutor.new(@options) end - # public method for get prs opens - # given a repo - def open_prs - prs = @client.pull_requests(@repo, state: 'open') - puts 'no Pull request OPEN on the REPO!' unless prs.any? + # public method for get prs opened and matching the change_newer + # condition + def open_newer_prs + prs = [] + @client.pull_requests(@repo, state: 'open').each do |pr| + if Time.now.utc - pr.updated_at < @options[:change_newer] * 60 || + @options[:change_newer] < 0 + prs << pr + end + end prs end @@ -84,8 +90,7 @@ def retrigger_check(pr) # public always rerun tests against the pr number if this exists def trigger_by_pr_number(pr) - return false if @pr_number.nil? - return false if @pr_number != pr.number + return false if @pr_number.nil? || @pr_number != pr.number puts "Got triggered by PR_NUMBER OPTION, rerunning on #{@pr_number}" launch_test_and_setup_status(@repo, pr) true @@ -117,8 +122,7 @@ def reviewed_pr_test(comm_st, pr) pr_all_files_type(@repo, pr.number, @file_type) return true if changelog_active(pr, comm_st) return false unless @pr_files.any? - exit 1 if @check - launch_test_and_setup_status(@repo, pr) + @check ? exit(1) : launch_test_and_setup_status(@repo, pr) true end @@ -145,9 +149,8 @@ def launch_test_and_setup_status(repo, pr) # # for retrigger all the tests. def magicword(repo, pr_number, context) magic_word_trigger = "gitarro rerun #{context} !!!" - pr_comment = @client.issue_comments(repo, pr_number) # a pr contain always a comments, cannot be nil - pr_comment.each do |com| + @client.issue_comments(repo, pr_number).each do |com| # delete comment otherwise it will be retrigger infinetely if com.body.include? magic_word_trigger @client.delete_comment(repo, com.id) @@ -160,8 +163,7 @@ def magicword(repo, pr_number, context) # check all files of a Prs Number if they are a specific type # EX: Pr 56, we check if files are '.rb' def pr_all_files_type(repo, pr_number, type) - files = @client.pull_request_files(repo, pr_number) - files.each do |file| + @client.pull_request_files(repo, pr_number).each do |file| @pr_files.push(file.filename) if file.filename.include? type end end @@ -259,8 +261,7 @@ def do_changelog_test(repo, pr) def changelog_changed(repo, pr, comm_st) return false unless @changelog_test # only execute 1 time, don"t run if test is failed, or ok - return false if failed_status?(comm_st) - return false if success_status?(comm_st) + return false if failed_status?(comm_st) || success_status?(comm_st) do_changelog_test(repo, pr) end diff --git a/lib/gitarro/opt_parser.rb b/lib/gitarro/opt_parser.rb index 69d2c8a..3fce2ab 100755 --- a/lib/gitarro/opt_parser.rb +++ b/lib/gitarro/opt_parser.rb @@ -91,6 +91,14 @@ def pr_number(opt) end end + def change_newer(opt) + change_newer_desc = 'If present, will only check PRs with a change in ' \ + 'the last X minutes' + opt.on("--change_newer 'MINUTES'", change_newer_desc) do |change_newer| + @options[:change_newer] = Integer(change_newer) + end + end + def optional_options(opt) opt.separator '' opt.separator 'Optional options:' @@ -100,6 +108,7 @@ def optional_options(opt) url_opt(opt) pr_number(opt) https_opt(opt) + change_newer(opt) end end diff --git a/tests/unit_tests/t_backend.rb b/tests/unit_tests/t_backend.rb index 058797f..55e6b9e 100755 --- a/tests/unit_tests/t_backend.rb +++ b/tests/unit_tests/t_backend.rb @@ -5,10 +5,13 @@ Dir.chdir Dir.pwd # Test the option parser class BackendTest2 < Minitest::Test - def test_full_option_import2 + def setup @full_hash = { repo: 'gino/gitarro', context: 'python-t', description: 'functional', test_file: 'gino.sh', file_type: '.sh', - git_dir: 'gitty' } + git_dir: 'gitty', change_newer: -1 } + end + + def test_full_option_import2 gitarro = Backend.new(@full_hash) puts gitarro.j_status gitarro.j_status = 'foo' @@ -25,9 +28,7 @@ def gitarro_assert(gitarro) end def test_run_script - @full_hash = { repo: 'gino/gitarro', context: 'python-t', description: - 'functional', test_file: 'test_data/script_ok.sh', - file_type: '.sh', git_dir: 'gitty' } + @full_hash[:test_file] = 'test_data/script_ok.sh' gbex = TestExecutor.new(@full_hash) ck_files(gbex) test_file = 'nofile.txt' @@ -48,4 +49,19 @@ def assert_file_non_ex(gbex, test_file) assert_equal("'#{test_file}\' doesn't exists.Enter valid file, -t option", ex.message) end + + def test_get_all_prs + @full_hash[:repo] = 'openSUSE/gitbot' + gitbot = Backend.new(@full_hash) + prs = gitbot.open_newer_prs + assert(true, prs.any?) + end + + def test_get_no_prs + @full_hash[:repo] = 'openSUSE/gitbot' + @full_hash[:change_newer] = 0 + gitbot = Backend.new(@full_hash) + prs = gitbot.open_newer_prs + assert(0, prs.count) + end end