Skip to content

Commit

Permalink
Merge pull request #68 from RodrigoMNardi/bug/watchdog/finished_ci_error
Browse files Browse the repository at this point in the history
Watchdog - Updating query to use stages instead jobs
  • Loading branch information
RodrigoMNardi authored May 31, 2024
2 parents 14667fe + 8bc98d8 commit 231617f
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .simplecov
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ SimpleCov.start do
enable_coverage :branch
primary_coverage :branch
add_filter %r{^/spec/}
add_filter 'database_loader.rb'
add_group 'Models', 'lib/models'
add_group 'GitHub Functions', 'lib/github'
add_group 'Bamboo CI Functions', 'lib/bamboo_ci'
add_group 'Helpers', 'lib/helpers'
add_group 'Others', %w[app/ database_loader.rb]
add_group 'Others', %w[app/]

minimum_coverage_by_file line: 90, branch: 90
end
2 changes: 1 addition & 1 deletion app/github_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def sinatra_logger_level
when 'issue_comment'
halt 404, 'Action not found' if payload.nil? or payload['comment'].nil?

case payload.dig('comment', 'body')
case payload.dig('comment', 'body').to_s.downcase
when /ci:retry/
halt Github::Retry::Comment.new(payload, logger_level: GithubApp.sinatra_logger_level).start
when /ci:rerun/
Expand Down
1 change: 1 addition & 0 deletions database_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
OTR::ActiveRecord.db_dir = 'db'
OTR::ActiveRecord.migrations_paths = ['db/migrate']
OTR::ActiveRecord.configure_from_file! 'config/database.yml'
ActiveRecord::Base.logger&.level = Logger::WARN

Dir['lib/models/*.rb'].each { |model| require_relative model }

Expand Down
1 change: 1 addition & 0 deletions lib/github/build/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require_relative '../../bamboo_ci/download'
require_relative '../../bamboo_ci/running_plan'
require_relative '../../helpers/github_logger'
require_relative '../../bamboo_ci/result'

module Github
module Build
Expand Down
7 changes: 5 additions & 2 deletions lib/github/check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def fetch_check_runs
end

def installation_id
@authenticate_app.find_app_installations.first['id'].to_i
@authenticate_app.find_app_installations(accept: 'application/vnd.github.v3+json').first['id'].to_i
end

def signature
Expand Down Expand Up @@ -198,7 +198,10 @@ def authenticate(jwt)

return if installation_id.zero?

token = @authenticate_app.create_app_installation_access_token(installation_id)[:token]
token =
@authenticate_app
.create_app_installation_access_token(installation_id, accept: 'application/vnd.github.v3+json')[:token]

@app = Octokit::Client.new(bearer_token: token)
end
end
Expand Down
15 changes: 15 additions & 0 deletions lib/github/plan_execution/finished.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def finished

check_stages
clear_deleted_jobs
update_all_stages

[200, 'Finished']
end
Expand All @@ -57,6 +58,18 @@ def in_progress?(build_status)

private

def update_all_stages
last_stage =
Stage
.joins(:configuration)
.where(check_suite: @check_suite)
.max_by { |stage| stage.configuration.position }

return if last_stage.nil? or last_stage.jobs.last.nil?

build_summary(last_stage.jobs.last)
end

# This method will move all tests that no longer exist in BambooCI to the skipped state,
# because there are no executions for them.
def clear_deleted_jobs
Expand Down Expand Up @@ -143,6 +156,8 @@ def slack_notify_cancelled(job)
def check_stages
github_check = Github::Check.new(@check_suite)
@logger.info ">>> @result: #{@result.inspect}"
return if @result.nil? or @result.empty? or @result['status-code']&.between?(400, 500)

@result.dig('stages', 'stage').each do |stage|
stage.dig('results', 'result').each do |result|
ci_job = CiJob.find_by(job_ref: result['buildResultKey'], check_suite_id: @check_suite.id)
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/check_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,11 @@
create_list(:ci_job, 5, check_suite: check_suite, stage: stage, status: 1)
end
end

trait :with_stages do
after(:create) do |check_suite|
create(:stage, check_suite: check_suite)
end
end
end
end
50 changes: 50 additions & 0 deletions spec/lib/github/plan_execution/finished_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,56 @@
end

describe 'Finishing execution' do
context 'when receives a valid payload - but stages does not have tests' do
let(:check_suite) { create(:check_suite, :with_stages) }
let(:status) { 200 }
let(:body) do
{
'stages' => {
'stage' => [
'results' => {
'result' =>
check_suite.ci_jobs.map { |job| { 'buildResultKey' => job.job_ref, 'state' => 'Successful' } }
}
]
}
}
end
let(:payload) { { 'bamboo_ref' => check_suite.bamboo_ci_ref } }

it 'must returns error' do
expect(pla_exec.finished).to eq([200, 'Finished'])
end
end

context 'when receives a valid payload - but result has error' do
let(:status) { 200 }
let(:body) do
{
'status-code' => 400
}
end
let(:payload) { { 'bamboo_ref' => check_suite.bamboo_ci_ref } }

before do
stub_request(:get, url)
.with(
headers: {
'Accept' => %w[*/* application/json],
'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Authorization' => 'Basic dXNlcjpwYXNzd29yZA==',
'Host' => '127.0.0.1',
'User-Agent' => 'Ruby'
}
)
.to_return(status: status, body: body.to_json, headers: {})
end

it 'must returns error' do
expect(pla_exec.finished).to eq([200, 'Finished'])
end
end

context 'when receives a valid payload - Successful' do
let(:status) { 200 }
let(:body) do
Expand Down
4 changes: 2 additions & 2 deletions workers/watch_dog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def check_suites

def check_suites_fetch_map
CheckSuite
.joins(:ci_jobs)
.where(ci_jobs: { status: %w[queued in_progress] }, created_at: [..Time.now])
.joins(:stages)
.where(stages: { status: %w[queued in_progress] }, created_at: [..Time.now])
.map(&:id)
.uniq
end
Expand Down

0 comments on commit 231617f

Please sign in to comment.