class Backend

this the main public class is the backend of gitarro, were we execute the tests and so on

Attributes

client[R]
gbexec[R]
options[RW]

Public Class Methods

new(option = nil) click to toggle source

public method of backend

# File lib/gitarro/backend.rb, line 161
def initialize(option = nil)
  @options = option.nil? ? OptParser.new.cmdline_options : option
  # each options will generate a object variable dinamically
  @options.each do |key, value|
    instance_variable_set("@#{key}", value)
    self.class.send(:attr_accessor, key)
  end
  Octokit.auto_paginate = true
  @client = Octokit::Client.new(netrc: true)
  @gbexec = TestExecutor.new(@options)
end

Public Instance Methods

force_run_test(pr) click to toggle source

public forcing to run the test

# File lib/gitarro/backend.rb, line 203
def force_run_test(pr)
  return false unless @force_test && defined?(@pr_number)

  print_test_required
  gbexec.export_pr_data(pr)
  launch_test_and_setup_status(pr)
end
open_newer_prs() click to toggle source

public method for get prs opened and matching the changed_since condition

# File lib/gitarro/backend.rb, line 194
def open_newer_prs
  prs = @client.pull_requests(@repo, state: 'open').select do |pr|
    pr_last_update_less_than(pr, @options[:changed_since])
  end
  print_pr_resume(prs)
  prs
end
pr_equal_specific_branch?(pr) click to toggle source

public method for check if pr belong to user specified branch if the pr belong to the branch continue tests otherwise just skip tests without setting any status

# File lib/gitarro/backend.rb, line 183
def pr_equal_specific_branch?(pr)
   return true if @branch.nil?
   return true if @branch == pr.base.ref 

   puts "branch \"#{pr.base.ref}\" should match github-branch \"#{@branch}\" (given) !!!"
   puts 'skipping tests !!!'
   false
end
required_prs() click to toggle source

public method retrieve pull request to process

# File lib/gitarro/backend.rb, line 174
def required_prs
  return open_newer_prs if @options[:pr_number].nil?

  [@client.pull_request(@repo, @options[:pr_number])]
end
retrigger_check(pr) click to toggle source

public for retrigger the test

# File lib/gitarro/backend.rb, line 212
def retrigger_check(pr)
  return false unless retrigger_needed?(pr)

  print_test_required
  gbexec.export_pr_data(pr)
  exit 0 if @check
  launch_test_and_setup_status(pr)
end
retriggered_by_checkbox?(pr, context) click to toggle source

this function will check if the PR contains a checkbox for retrigger all the tests.

# File lib/gitarro/backend.rb, line 277
def retriggered_by_checkbox?(pr, context)
  return false unless pr.body.match(/\[x\]\s+Re-run\s+test\s+"#{context}"/i)

  skipped = ''
  unless empty_files_changed_by_pr?(pr)
    skipped = '(Test skipped, there are no changes to test)'
  end

  puts "Re-run test \"#{context}\" #{skipped}"
  new_pr_body = pr.body.gsub("[x] Re-run test \"#{context}\"",
                             "[ ] Re-run test \"#{context}\" #{skipped}")
  new_pr_body = new_pr_body.gsub("#{skipped} #{skipped}", skipped) unless skipped.empty?
                  
  @client.update_pull_request(@repo, pr.number, body: new_pr_body)
  true
end
retriggered_by_comment?(pr, context) click to toggle source

this function will check if the PR contains in comment the magic word # for retrigger all the tests.

# File lib/gitarro/backend.rb, line 261
def retriggered_by_comment?(pr, context)
  magic_word_trigger = "gitarro rerun #{context} !!!"
  # a pr contain always a comments, cannot be nil
  @client.issue_comments(@repo, pr.number).each do |com|
    # delete comment otherwise it will be retrigger infinetely
    next unless com.body.include? magic_word_trigger

    puts "Re-run test \"#{context}\""
    @client.delete_comment(@repo, com.id)
    return true
  end
  false
end
reviewed_pr?(comm_st, pr) click to toggle source
# File lib/gitarro/backend.rb, line 246
def reviewed_pr?(comm_st, pr)
  # if PR status is not on pending and the context is not set,
  #  we dont run the tests
  return false unless context_present?(comm_st) == false ||
                      pending_pr?(comm_st)
  return false unless pr_all_files_type(pr.number, @file_type).any?

  print_test_required
  gbexec.export_pr_data(pr)
  exit(0) if @check
  launch_test_and_setup_status(pr)
end
triggered_by_pr_number?() click to toggle source

public tests against the pr number passed by parameter

# File lib/gitarro/backend.rb, line 222
def triggered_by_pr_number?
  return false if @pr_number.nil?

  # Note that the test will only run if it pass the checks on unreviewed_new_pr
  pr_on_number = @client.pull_request(@repo, @pr_number)
  puts "Got triggered by PR_NUMBER OPTION, PR: #{@pr_number}"
  comm_st = @client.status(@repo, pr_on_number.head.sha)
  unreviewed_new_pr?(pr_on_number, comm_st)
end
unreviewed_new_pr?(pr, comm_st) click to toggle source
# File lib/gitarro/backend.rb, line 232
def unreviewed_new_pr?(pr, comm_st)
  return unless commit_is_unreviewed?(comm_st)

  return true if empty_files_changed_by_pr?(pr)

  # gb.check is true when there is a job running as scheduler
  # which doesn't execute the test but trigger another job
  print_test_required
  gbexec.export_pr_data(pr)
  return false if @check

  launch_test_and_setup_status(pr)
end

Private Instance Methods

empty_files_changed_by_pr?(pr) click to toggle source

control if the pr change add any files, specified it can be also a dir

# File lib/gitarro/backend.rb, line 352
def empty_files_changed_by_pr?(pr)
  pr_all_files_type(pr.number, @file_type).any?
end
filter_files_by_type(files, type) click to toggle source

by default type is 'notype', which imply we get all files modified by a Pull Request otherwise we filter the file '.rb' type or fs ''

# File lib/gitarro/backend.rb, line 339
def filter_files_by_type(files, type)
  # ff: filtered files array
  ff = []
  if type == 'notype'
    ff = files
  else
    files.each { |f| ff.push(f.filename) if f.filename.include? type }
  end
  ff
end
launch_test_and_setup_status(pr) click to toggle source

this function setup first pending to PR, then execute the tests then set the status according to the results of script executed. pr_head = is the PR branch base = is a the upstream branch, where the pr targets it return a string 'success', 'failure' (github status)

# File lib/gitarro/backend.rb, line 319
def launch_test_and_setup_status(pr)
  # pending
  create_status(pr, 'pending')
  # do tests
  test_status = gbexec.pr_test(pr)
  # set status
  create_status(pr, test_status)
  # return status for other functions
  test_status == 'success' ? exit(0) : exit(1)
end
pr_all_files_type(pr_number, type) click to toggle source

check all files of a Prs Number if they are a specific type EX: Pr 56, we check if files are '.rb'

# File lib/gitarro/backend.rb, line 332
def pr_all_files_type(pr_number, type)
  filter_files_by_type(@client.pull_request_files(@repo, pr_number), type)
end
print_pr_resume(prs) click to toggle source

Show a message stating if there are opened PRs or not

print_test_required() click to toggle source
retrigger_needed?(pr) click to toggle source
# File lib/gitarro/backend.rb, line 356
def retrigger_needed?(pr)
  # we want redo sometimes tests
  return false unless retriggered_by_checkbox?(pr, @context) ||
                      retriggered_by_comment?(pr, @context)

  # if check is set, the comment in the trigger job will be del.
  # so setting it to pending, it will be remembered
  empty_files_changed_by_pr?(pr)
end