class Backend
this the main public class is the backend of gitarro, were we execute the tests and so on
Attributes
Public Class Methods
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
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
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
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
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
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
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
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
# 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
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
# 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
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
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
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
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
Show a message stating if there are opened PRs or not
# File lib/gitarro/backend.rb, line 297 def print_pr_resume(prs) if prs.any? puts "[PRS=#{prs.any?}] PRs opened. Analyzing them..." return end if @options[:changed_since] >= 0 puts "[PRS=#{prs.any?}] No Pull Requests opened or with changes " \ "newer than #{options[:changed_since]} seconds" else puts "[PRS=#{prs.any?}] No Pull Requests opened" end end
# File lib/gitarro/backend.rb, line 310 def print_test_required puts '[TESTREQUIRED=true] PR requires test' end
# 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