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



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/gitarro/backend.rb', line 166

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.



162
163
164
# File 'lib/gitarro/backend.rb', line 162

def client
  @client
end

#gbexecObject (readonly)

Returns the value of attribute gbexec.



162
163
164
# File 'lib/gitarro/backend.rb', line 162

def gbexec
  @gbexec
end

#optionsObject

Returns the value of attribute options.



161
162
163
# File 'lib/gitarro/backend.rb', line 161

def options
  @options
end

Instance Method Details

#force_run_test(pr) ⇒ Object

public forcing to run the test



208
209
210
211
212
213
214
215
# File 'lib/gitarro/backend.rb', line 208

def force_run_test(pr)
  return false unless @force_test && defined?(@pr_number)
  puts "Test run forced on PR #{@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



199
200
201
202
203
204
205
# File 'lib/gitarro/backend.rb', line 199

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)


188
189
190
191
192
193
194
195
# File 'lib/gitarro/backend.rb', line 188

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



179
180
181
182
183
# File 'lib/gitarro/backend.rb', line 179

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



218
219
220
221
222
223
224
225
226
# File 'lib/gitarro/backend.rb', line 218

def retrigger_check(pr)
  return false unless retrigger_needed?(pr)
  puts "Retrigger requested on PR #{@pr_number}"

  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)


288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/gitarro/backend.rb', line 288

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)


272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/gitarro/backend.rb', line 272

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)


253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/gitarro/backend.rb', line 253

def reviewed_pr?(comm_st, pr)
  # if PR status is not set we dont run the tests
  return false if context_present?(comm_st)

  matching_files = pr_all_files_type(pr.number, @file_type)
  if matching_files.empty?
    puts 'Found no file matching the type'
    return false
  end
  puts "Files matching the type: #{matching_files.join(', ')}"

  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)


229
230
231
232
233
234
235
236
237
# File 'lib/gitarro/backend.rb', line 229

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)


239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/gitarro/backend.rb', line 239

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