From e97542b89ce8cd73c4792dabd264aceaee2345c7 Mon Sep 17 00:00:00 2001 From: Manuel Meurer Date: Mon, 16 Sep 2024 16:15:53 +0200 Subject: [PATCH] trigger github checks via github api instead of commits --- app/services/check_out_worker_repo.rb | 67 --------------------------- app/services/compats/check.rb | 54 +++++++-------------- app/services/external/github.rb | 6 +++ 3 files changed, 23 insertions(+), 104 deletions(-) delete mode 100644 app/services/check_out_worker_repo.rb diff --git a/app/services/check_out_worker_repo.rb b/app/services/check_out_worker_repo.rb deleted file mode 100644 index 6ce902d..0000000 --- a/app/services/check_out_worker_repo.rb +++ /dev/null @@ -1,67 +0,0 @@ -class CheckOutWorkerRepo < Baseline::Service - REPO = "git@github.com:railsbump/checker.git" - TMP = Rails.root.join("tmp") - - WorkerRepoCheckedOut = Class.new(Error) - - def call - hostname = Socket.gethostname - if hostname.blank? - raise Error, "Could not determine hostname." - end - cache_key = [ - :worker_repo_checked_out_since, - hostname - ].join(":") - if worker_repo_checked_out_since = Kredis.redis.get(cache_key)&.then { Time.parse _1 } - if worker_repo_checked_out_since < 10.minutes.ago - ReportError.call "Worker repo seems to be checked out for a long time already.", - worker_repo_checked_out_since: worker_repo_checked_out_since - end - if Sidekiq.server? - unless @_call_args && @_call_kwargs - raise "@_call_args or @_call_kwargs are nil." - end - self.class.call_in 30.seconds, *@_call_args, **@_call_kwargs - end - raise WorkerRepoCheckedOut - else - Kredis.redis.set(cache_key, Time.current.iso8601) - end - - begin - dir = TMP.join("railsbump_checker_#{SecureRandom.hex(3)}") - end while dir.exist? - - ssh_key = ENV["SSH_KEY"]&.dup - if ssh_key.present? - ssh_key_file = TMP.join("ssh_key") - unless ssh_key_file.exist? - unless ssh_key[-1] == "\n" - ssh_key << "\n" - end - File.write ssh_key_file, ssh_key - File.chmod 0600, ssh_key_file - end - ENV["GIT_SSH_COMMAND"] = "ssh -o StrictHostKeyChecking=no -i #{ssh_key_file}" - end - - git = Octopoller.poll retries: 5 do - Git.clone REPO, dir - rescue Git::GitExecuteError - :re_poll - end - - git.config "user.name", "RailsBump" - git.config "user.email", "hello@railsbump.org" - - git.checkout "main" - - yield git - ensure - Kredis.redis.del cache_key - if git && File.exist?(git.dir.path) - FileUtils.rm_rf git.dir.path - end - end -end diff --git a/app/services/compats/check.rb b/app/services/compats/check.rb index f5a9586..34d8d0c 100644 --- a/app/services/compats/check.rb +++ b/app/services/compats/check.rb @@ -161,44 +161,24 @@ def check_with_bundler_locally def check_with_bundler_github return unless @compat.pending? && Rails.env.production? - branch = @compat.id.to_s - - # Delete branch if it exists - External::Github.delete_branch(branch) - - CheckOutWorkerRepo.call do |git| - git.branch(branch).checkout - - action_file = File.join(git.dir.path, ".github", "workflows", "check.yml") - action_content = File.read(action_file) - .gsub("RUBY_VERSION", @compat.rails_release.compatible_ruby_version.to_s) - .gsub("BUNDLER_VERSION", @compat.rails_release.compatible_bundler_version.to_s) - File.write action_file, action_content - - dependencies = @compat.dependencies.dup - dependencies.transform_values! do |contraints| - contraints.split(/\s*,\s*/) - end - dependencies["rails"] ||= [] - dependencies["rails"] << "#{@compat.rails_release.version.approximate_recommendation}.0" + dependencies = @compat + .dependencies + .dup + .transform_values { + _1.split(/\s*,\s*/) + }.then { + _1["rails"] ||= [] + _1["rails"] << "#{@compat.rails_release.version.approximate_recommendation}.0" + } - gemfile = File.join(git.dir.path, "Gemfile") - gemfile_content = dependencies - .map do |gem, constraints_group| - "gem '#{gem}', #{constraints_group.map { "'#{_1}'" }.join(", ")}" - end - .unshift("source 'https://rubygems.org'") - .join("\n") - File.write gemfile, gemfile_content - - git.add [action_file, gemfile] - git.commit @compat.to_s - Octopoller.poll retries: 5 do - git.push "origin", branch - rescue Git::GitExecuteError - :re_poll - end - end + External::Github.dispatch_workflow \ + "railsbump/checker", + "check.yml", + :main, + compat_id: @compat.id.to_s, + ruby_version: @compat.rails_release.compatible_ruby_version.to_s, + bundler_version: @compat.rails_release.compatible_bundler_version.to_s, + dependencies: dependencies.to_json end end end diff --git a/app/services/external/github.rb b/app/services/external/github.rb index 32dd0b2..009ab37 100644 --- a/app/services/external/github.rb +++ b/app/services/external/github.rb @@ -19,6 +19,12 @@ class Github < Baseline::ExternalService false end + add_method :dispatch_workflow do |repo, id, ref, **inputs| + client.workflow_dispatch \ + repo, id, ref, + inputs: inputs + end + private def client