Skip to content

Commit

Permalink
Improve PR search when gitarro is running with cron-like tools to avo…
Browse files Browse the repository at this point in the history
…id exceeding GitHub API limits
  • Loading branch information
juliogonzalez committed Sep 28, 2017
1 parent e2718fe commit 12f4569
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
9 changes: 8 additions & 1 deletion gitarro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 16 additions & 15 deletions lib/gitarro/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'octokit'
require 'optparse'
require 'time'
require 'English'
require_relative 'opt_parser'
require_relative 'git_op'
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions lib/gitarro/opt_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand All @@ -100,6 +108,7 @@ def optional_options(opt)
url_opt(opt)
pr_number(opt)
https_opt(opt)
change_newer(opt)
end
end

Expand Down
26 changes: 21 additions & 5 deletions tests/unit_tests/t_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand All @@ -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

0 comments on commit 12f4569

Please sign in to comment.