Skip to content

Commit

Permalink
Merge pull request #98 from RodrigoMNardi/bug/github/check_run/failed…
Browse files Browse the repository at this point in the history
…_to_update_ci

GitHub CI Sync
  • Loading branch information
RodrigoMNardi authored Sep 19, 2024
2 parents 3c1d6bf + 869e387 commit 2479a84
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
SimpleCov.start do
enable_coverage :branch
primary_coverage :branch
add_filter %r{^/spec/}
add_filter %r{^/(spec|config)/}
add_filter 'database_loader.rb'
add_group 'Models', 'lib/models'
add_group 'GitHub Functions', 'lib/github'
Expand Down
6 changes: 5 additions & 1 deletion config/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class << self
end
end

DELAYED_JOB_TIMER = 5
DELAYED_JOB_TIMER = 30

Rails.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO)
ActiveRecord::Base.logger = GithubLogger.instance.create('delayed_job.log', Logger::INFO)
Expand All @@ -34,5 +34,9 @@ class << self
Delayed::Worker.max_attempts = 5
Delayed::Worker.max_run_time = 5.minutes

Delayed::Job.delete_all unless ENV.fetch('RAILS_ENV', 'test') == 'test'

# Load the database configuration

config = YAML.load_file('config/database.yml')[ENV.fetch('RACK_ENV', 'development')]
ActiveRecord::Base.establish_connection(config)
5 changes: 1 addition & 4 deletions lib/github/build/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def initialize(job, logger_level: Logger::INFO, agent: 'Github')
def build_summary
current_stage = @job.stage
current_stage = fetch_parent_stage if current_stage.nil?
current_stage.refresh_reference(@github)

logger(Logger::INFO, "build_summary: #{current_stage.inspect}")
msg = "Github::Build::Summary - #{@job.inspect}, #{current_stage.inspect}, bamboo info: #{bamboo_info}"
Expand Down Expand Up @@ -81,8 +80,6 @@ def must_cancel_next_stages(current_stage)
.where(check_suite: @check_suite)
.where(configuration: { position: [(current_stage.configuration.position + 1)..] })
.each do |stage|
next if stage.cancelled?

cancelling_next_stage(stage)
end
end
Expand All @@ -98,7 +95,7 @@ def must_continue_next_stage(current_stage)
.where(configuration: { position: current_stage.configuration.position + 1 })
.first

return if next_stage.nil? or next_stage.cancelled?
return if next_stage.nil? or next_stage.finished?

update_summary(next_stage)
end
Expand Down
49 changes: 34 additions & 15 deletions lib/github/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,29 @@ def basic_status(check_ref, status, output)
def completed(check_ref, status, conclusion, output)
return if check_ref.nil?

opts = {
status: status,
conclusion: conclusion
}
retry_count = 0

opts[:output] = output unless output.empty?
begin
opts = {
status: status,
conclusion: conclusion

resp =
@app.update_check_run(
@check_suite.pull_request.repository,
check_ref,
opts
).to_h
}

@logger.info("completed: #{check_ref}, status: #{status}, conclusion: #{conclusion} -> resp: #{resp}")
opts[:output] = output unless output.empty?

resp
rescue Octokit::NotFound
@logger.error "#{check_ref} not found at GitHub"
send_update(check_ref, opts, conclusion)
rescue Octokit::NotFound, RuntimeError
retry_count += 1

sleep retry_count * 5

retry if retry_count <= 3

@logger.error "#{check_ref} not found at GitHub"

{}
end
end

def authenticate_app
Expand Down Expand Up @@ -190,5 +194,20 @@ def authenticate(jwt)

@app = Octokit::Client.new(bearer_token: token)
end

def send_update(check_ref, opts, conclusion)
resp =
@app.update_check_run(
@check_suite.pull_request.repository,
check_ref,
opts
).to_h

raise 'GitHub failed to update status' if resp[:conclusion] != conclusion

@logger.info("completed: #{check_ref}, conclusion: #{conclusion} -> resp: #{resp}")

resp
end
end
end
13 changes: 2 additions & 11 deletions lib/github/update_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,17 @@ def update_status
def insert_new_delayed_job
queue = @job.check_suite.pull_request.github_pr_id % 10

if can_add_new_job?
return CiJobStatus
.delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, queue: queue)
.update(@job.check_suite.id, @job.id)
end

delete_and_create_delayed_job(queue)
end

def delete_and_create_delayed_job(queue)
fetch_delayed_job.destroy_all
fetch_delayed_job&.destroy_all

CiJobStatus
.delay(run_at: DELAYED_JOB_TIMER.seconds.from_now, queue: queue)
.update(@job.check_suite.id, @job.id)
end

def can_add_new_job?
fetch_delayed_job.empty?
end

def fetch_delayed_job
Delayed::Job.where('handler LIKE ?', "%method_name: :update\nargs:\n- #{@job.check_suite.id}%")
end
Expand Down
32 changes: 14 additions & 18 deletions lib/models/stage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,39 @@ def update_output(github, output: output_in_progress)
end

def cancelled(github, output: {}, agent: 'Github')
return if cancelled?
check_run = refresh_reference(github)
github.cancelled(check_run.id, output)

create_github_check(github)
github.cancelled(check_ref, output)
update(status: :cancelled)
update(status: :cancelled, check_ref: check_run.id)
AuditStatus.create(auditable: self, status: :cancelled, agent: agent, created_at: Time.now)
notification
end

def failure(github, output: {}, agent: 'Github')
return if failure?
check_run = refresh_reference(github)
github.failure(check_run.id, output)

create_github_check(github)
github.failure(check_ref, output)
update(status: :failure)
update(status: :failure, check_ref: check_run.id)
AuditStatus.create(auditable: self, status: :failure, agent: agent, created_at: Time.now)
notification
end

def success(github, output: {}, agent: 'Github')
return if success?
check_run = refresh_reference(github)
github.success(check_run.id, output)

create_github_check(github)
github.success(check_ref, output)
update(status: :success)
reload
update(status: :success, check_ref: check_run.id)
AuditStatus.create(auditable: self, status: :success, agent: agent, created_at: Time.now)
notification
end

def refresh_reference(github, agent: 'Github')
check_run = github.create(github_stage_full_name(name))
update(check_ref: check_run.id)
AuditStatus.create(auditable: self, status: :refresh, agent: agent, created_at: Time.now)
end

private

def refresh_reference(github)
github.create(github_stage_full_name(name))
end

def in_progress_notification
SlackBot.instance.stage_in_progress_notification(self)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/github/build/unavailable_jobs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
allow(Octokit::Client).to receive(:new).and_return(fake_client)
allow(fake_client).to receive(:find_app_installations).and_return([{ 'id' => 1 }])
allow(fake_client).to receive(:create_app_installation_access_token).and_return({ 'token' => 1 })
allow(fake_client).to receive(:update_check_run)
allow(fake_client).to receive(:update_check_run).and_return({ conclusion: 'skipped' })
allow(File).to receive(:read).and_return('')
allow(OpenSSL::PKey::RSA).to receive(:new).and_return(OpenSSL::PKey::RSA.new(2048))
end
Expand Down
46 changes: 36 additions & 10 deletions spec/lib/github/check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
context 'when call create' do
let(:pr_id) { 1 }
let(:name) { 'test' }
let(:pr_info) { { name: name } }
let(:pr_info) { { name: name, conclusion: 'success' } }

before do
allow(fake_client).to receive(:create_check_run)
Expand Down Expand Up @@ -141,7 +141,7 @@
context 'when call in_progress' do
let(:id) { 1 }
let(:status) { 'in_progress' }
let(:pr_info) { { status: status } }
let(:pr_info) { { status: status, conclusion: status } }
let(:output) { { title: 'Title', summary: 'Summary' } }

before do
Expand Down Expand Up @@ -173,11 +173,11 @@
status: status,
conclusion: conclusion
})
.and_return({})
.and_return({ conclusion: conclusion })
end

it 'must returns success' do
expect(check.cancelled(id)).to eq({})
expect(check.cancelled(id)).to eq({ conclusion: conclusion })
end
end

Expand All @@ -194,11 +194,11 @@
status: status,
conclusion: conclusion
})
.and_return({})
.and_return({ conclusion: conclusion })
end

it 'must returns success' do
expect(check.success(id)).to eq({})
expect(check.success(id)).to eq({ conclusion: conclusion })
end
end

Expand Down Expand Up @@ -238,11 +238,11 @@
conclusion: conclusion,
output: output
})
.and_return({})
.and_return({ conclusion: conclusion })
end

it 'must returns success' do
expect(check.failure(id, output)).to eq({})
expect(check.failure(id, output)).to eq({ conclusion: conclusion })
end
end

Expand All @@ -259,11 +259,11 @@
status: status,
conclusion: conclusion
})
.and_return({})
.and_return({ conclusion: conclusion })
end

it 'must returns success' do
expect(check.skipped(id)).to eq({})
expect(check.skipped(id)).to eq({ conclusion: conclusion })
end
end

Expand Down Expand Up @@ -350,4 +350,30 @@
end
end
end

describe 'retry send_update ' do
let(:id) { 1 }
let(:status) { 'completed' }
let(:conclusion) { 'success' }

context 'when send_update returns error' do
before do
allow(fake_client).to receive(:update_check_run).and_raise(Octokit::NotFound)
end

it 'must returns raise' do
expect(check.skipped(id)).to eq({})
end
end

context 'when send_update returns error and success' do
before do
allow(fake_client).to receive(:update_check_run).and_return({}, { conclusion: conclusion })
end

it 'must returns a valid data' do
expect(check.skipped(id)).to eq({})
end
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def app
end

config.before(:each) do
allow_any_instance_of(Object).to receive(:sleep)
Delayed::Worker.delay_jobs = false
end

Expand Down

0 comments on commit 2479a84

Please sign in to comment.