Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc changes in GitHub client #286

Merged
merged 5 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 43 additions & 35 deletions lib/auxiliary/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
module AutoHCK
# github class
class Github
GITHUB_API_RETRIES = 5
GITHUB_API_RETRY_SLEEP = 30

def initialize(config, logger, url, tag, commit)
@api_connected = false
@pr_closed = nil

@logger = logger
@target_url = url
Expand All @@ -29,54 +31,43 @@ def connect
nil
end

def connected?
@api_connected
end

def pr_closed?
@pr_closed
end

def check_closed_pr
pr = @github.pulls(@repo, state: 'closed').find { |x| x['head']['sha'] == @commit }
def connected? = @api_connected

return false if pr.nil?
def find_pr = _find_pr || _find_pr('closed')

@pr_closed = true
if pr.merged_at?
@logger.warn("PR ##{pr['number']}: #{pr['title']} - already merged. Skipping CI.")
else
@logger.warn("PR ##{pr['number']}: #{pr['title']} - closed. Skipping CI.")
end

true
def pr_closed?(pr_object = find_pr)
pr_object.state == 'closed'
end

def find_pr
pr = @github.pulls(@repo).find { |x| x['head']['sha'] == @commit }
if pr.nil?
unless check_closed_pr
@logger.warn('Pull request commit hash not valid, disconnecting github.')
@api_connected = false
end

return nil
def log_pr(pr_object = find_pr)
if pr_object.merged_at?
@logger.info("PR ##{pr_object['number']}: #{pr_object['title']} - already merged")
elsif pr_object.state == 'closed'
@logger.info("PR ##{pr_object['number']}: #{pr_object['title']} - closed")
else
@logger.info("PR ##{pr_object['number']}: #{pr_object['title']}")
end

@logger.info("PR ##{pr['number']}: #{pr['title']}")
@logger.info(pr['html_url'])

pr
@logger.info(pr_object['html_url'])
end

def create_status(state, description)
retries ||= 0

options = { 'context' => @context,
'description' => description,
'target_url' => @target_url }
begin
@github.create_status(@repo, @commit, state, options)
rescue Faraday::ConnectionFailed, Octokit::BadGateway
@logger.warn('Github server connection error')
rescue Faraday::ConnectionFailed, Faraday::TimeoutError,
Octokit::BadGateway, Octokit::InternalServerError => e
@logger.warn("Github server connection error: #{e.message}")
# we can continue even if can't get update PR status
return unless (retries += 1) < GITHUB_API_RETRIES

sleep GITHUB_API_RETRY_SLEEP
@logger.info('Trying again execute github.create_status')
retry
end
@logger.info('Github status updated')
end
Expand Down Expand Up @@ -125,5 +116,22 @@ def handle_error
description = 'An error occurred while running HCK-CI'
create_status(state, description)
end

def _find_pr(state = nil)
retries ||= 0

@github.pulls(@repo, state:).find { _1['head']['sha'] == @commit }
rescue Faraday::ConnectionFailed, Faraday::TimeoutError,
Octokit::BadGateway, Octokit::InternalServerError => e
@logger.warn("Github server connection error: #{e.message}")
# we should fail if can't get updated PR information
raise unless (retries += 1) < GITHUB_API_RETRIES

sleep GITHUB_API_RETRY_SLEEP
@logger.info('Trying again execute github.pulls')
retry
end

private :_find_pr
end
end
2 changes: 1 addition & 1 deletion lib/auxiliary/multi_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def close
@loggers.each { |logger| logger.send(level.downcase, *args, &block) }
end

define_method("#{level.downcase}?".to_sym) do
define_method(:"#{level.downcase}?") do
@level <= Logger::Severity.const_get(level)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_parser(sub_parser)
define_options(parser)
parser.on_tail('-h', '--help', 'Show this message') do
puts parser
sub_parser&.each do |_k, v|
sub_parser&.each_value do |v|
puts v
end
exit
Expand Down
2 changes: 1 addition & 1 deletion lib/engines/hcktest/hcktest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def run_studio(scope, run_opts = {})

def run_clients(scope, run_opts = {})
@clients = {}
@platform['clients'].each do |_name, client|
@platform['clients'].each_value do |client|
@clients[client['name']] = @project.setup_manager.run_hck_client(scope, @studio, client['name'], run_opts)

break if @project.options.test.svvp
Expand Down
9 changes: 7 additions & 2 deletions lib/engines/hcktest/tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,11 @@ def check_new_finished_tests
handle_finished_tests(new_done)
end

def handle_test_running(running = nil)
until all_tests_finished?
def handle_test_running
running = nil

until all_tests_finished? || @project.run_terminated
@project.check_run_termination
reset_clients_to_ready_state
check_new_finished_tests
check_test_queued_time
Expand Down Expand Up @@ -420,6 +423,8 @@ def run
queue_test(test, wait: true)
list_tests
handle_test_running

break if @project.run_terminated
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class AutoHCKError < StandardError; end
class AutoHCKInterrupt < Exception; end
# rubocop:enable Lint/InheritException

# A custom GithubCommitInvalid error exception
class GithubCommitInvalid < AutoHCKError; end
# A custom GithubInitializationError error exception
class GithubInitializationError < AutoHCKError; end

# A custom Could not open json file exception
class OpenJsonError < StandardError; end
Expand Down
35 changes: 30 additions & 5 deletions lib/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Project

attr_reader :config, :logger, :timestamp, :setup_manager, :engine, :id,
:workspace_path, :github, :result_uploader,
:engine_type, :options, :extra_sw_manager
:engine_type, :options, :extra_sw_manager, :run_terminated

CONFIG_JSON = 'config.json'

Expand Down Expand Up @@ -115,6 +115,7 @@ def init_class_variables
@config = Json.read_json(CONFIG_JSON, @logger)
@timestamp = create_timestamp
@engine_type = @config["#{@options.mode}_engine"]
@run_terminated = false
end

def assign_id
Expand Down Expand Up @@ -151,17 +152,41 @@ def github_handling(commit)
url = @result_uploader.html_url || @result_uploader.url
@github = Github.new(@config, @logger, url, github_handling_context,
commit)
raise GithubCommitInvalid unless @github.connected?
raise GithubInitializationError unless @github.connected?

@github.find_pr
return false if @github.pr_closed?
pr = @github.find_pr

raise GithubCommitInvalid unless @github.connected?
if pr.nil?
@logger.warn('Pull request commit hash not valid, terminating CI')
# Do not raise an exception. If the commit is not valid
# it can be because PR was force-pushed. Just exit from
# CI with the corresponding message.
return false
end

@github.log_pr(pr)

return false if @github.pr_closed?(pr)

@github.create_status('pending', 'Tests session initiated')
true
end

def check_run_termination
return if @github.nil?

pr = @github.find_pr

# PR is nil when it was force-pushed
# PR is closed when it was closed or merged
@run_terminated = pr.nil? || @github.pr_closed?(pr)

return unless @run_terminated

@logger.warn('Pull request changed, terminating CI')
@github.handle_cancel
end

def create_timestamp
Time.now.strftime('%Y_%m_%d_%H_%M_%S')
end
Expand Down
2 changes: 1 addition & 1 deletion lib/setupmanagers/setupmanager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(project)
@project = project
@logger = project.logger
@type = project.config['setupmanager'].downcase.to_sym
super setupmanager_create
super(setupmanager_create)
end

def setupmanager_create
Expand Down
Loading