Class: Backend

Inherits:
Object
  • Object
show all
Includes:
GitHubPrOperations
Defined in:
lib/gitarro/backend.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from GitHubPrOperations

#commit_is_unreviewed?, #context_present?, #create_status, #failed_status?, #pending_pr?, #pr_last_update_less_than, #success_status?

Constructor Details

#initialize(option = nil) ⇒ Backend

public method of backend



161
162
163
164
165
166
167
168
169
170
171
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



157
158
159
# File 'lib/gitarro/backend.rb', line 157

def client
  @client
end

#gbexecObject (readonly)

Returns the value of attribute gbexec.



157
158
159
# File 'lib/gitarro/backend.rb', line 157

def gbexec
  @gbexec
end

#optionsObject

Returns the value of attribute options.



156
157
158
# File 'lib/gitarro/backend.rb', line 156

def options
  @options
end

Instance Method Details

#force_run_test(pr) ⇒ Object

public forcing to run the test



203
204
205
206
207
208
209
# 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_prsObject

public method for get prs opened and matching the changed_since condition



194
195
196
197
198
199
200
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
# 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_prsObject

public method retrieve pull request to process



174
175
176
177
178
# 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) ⇒ Object

public for retrigger the test



212
213
214
215
216
217
218
219
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/gitarro/backend.rb', line 275

def retriggered_by_checkbox?(pr, context)
  return false if pr.body.nil? || !pr.body.match(/\[x\]\s+Re-run\s+test\s+"#{context}"/i)

  # In check mode, don't uncheck the box as that would prevent a next gitarro run to actually run the test
  return true if @check

  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) ⇒ Boolean

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

Returns:

  • (Boolean)


259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/gitarro/backend.rb', line 259

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) ⇒ Boolean

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
# File 'lib/gitarro/backend.rb', line 246

def reviewed_pr?(comm_st, pr)
  # if PR status is not set we dont run the tests
  return false if context_present?(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?Boolean

public tests against the pr number passed by parameter

Returns:

  • (Boolean)


222
223
224
225
226
227
228
229
230
# 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) ⇒ Boolean

Returns:

  • (Boolean)


232
233
234
235
236
237
238
239
240
241
242
243
244
# 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