From f6cf3c537c8799b5454b07c52dc4c5dd0f31302f Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Tue, 5 Aug 2025 11:08:21 -0300 Subject: [PATCH 1/2] Update Ruby version requirement to allow for newer versions --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 442d185..c62c672 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ source 'https://rubygems.org' -ruby '3.1.2' +ruby '>= 3.1.2' # Token gem 'jwt' From 546251328b27086dd1977fc86553ec173878b7ef Mon Sep 17 00:00:00 2001 From: Rodrigo Nardi Date: Tue, 5 Aug 2025 11:08:31 -0300 Subject: [PATCH 2/2] Handle invalid PullRequestCommit and improve error feedback --- lib/github/parsers/pull_request_commit.rb | 14 ++++- lib/github/re_run/comment.rb | 14 ++++- spec/lib/github/re_run/comment_spec.rb | 66 ++++++++++++++++++++++- 3 files changed, 89 insertions(+), 5 deletions(-) diff --git a/lib/github/parsers/pull_request_commit.rb b/lib/github/parsers/pull_request_commit.rb index 1e922eb..950f3dd 100644 --- a/lib/github/parsers/pull_request_commit.rb +++ b/lib/github/parsers/pull_request_commit.rb @@ -20,9 +20,13 @@ def initialize(repo, pr_id) @repo = repo @pr_id = pr_id - pull_request = PullRequest.find_by(github_pr_id: pr_id) + @pull_request = PullRequest.find_by(github_pr_id: pr_id) - @github_check = Github::Check.new(pull_request.check_suites.last) + created_github_check unless invalid? + end + + def invalid? + @pull_request.nil? end # Finds a commit by its SHA. @@ -66,6 +70,12 @@ def last_commit_in_pr last_commit end + + private + + def created_github_check + @github_check = Github::Check.new(@pull_request.check_suites.last) + end end end end diff --git a/lib/github/re_run/comment.rb b/lib/github/re_run/comment.rb index daeba8a..cbdab38 100644 --- a/lib/github/re_run/comment.rb +++ b/lib/github/re_run/comment.rb @@ -54,6 +54,9 @@ def sha256_or_comment? def comment_flow commit = fetch_last_commit_or_sha256 + + return if commit.nil? + github_check = fetch_github_check pull_request_info = github_check.pull_request_info(pr_id, repo) pull_request = fetch_or_create_pr(pull_request_info) @@ -138,7 +141,16 @@ def fetch_last_commit_or_sha256 end def fetch_last_commit - Github::Parsers::PullRequestCommit.new(repo, pr_id).last_commit_in_pr + pull_request_commit = Github::Parsers::PullRequestCommit.new(repo, pr_id) + + if pull_request_commit.invalid? + github_check = Github::Check.new(nil) + github_check.comment_reaction_thumb_down(repo, comment_id) + + return nil + end + + pull_request_commit.last_commit_in_pr end def github_reaction_feedback(comment_id) diff --git a/spec/lib/github/re_run/comment_spec.rb b/spec/lib/github/re_run/comment_spec.rb index 044b057..557170b 100644 --- a/spec/lib/github/re_run/comment_spec.rb +++ b/spec/lib/github/re_run/comment_spec.rb @@ -50,7 +50,7 @@ let(:fake_translation) { create(:stage_configuration) } context 'when receives a valid command' do - let(:check_suite) { create(:check_suite, :with_running_ci_jobs) } + let(:check_suite) { create(:check_suite, :with_running_ci_jobs, pull_request: pull_request) } let(:ci_jobs) do [ { name: 'First Test', job_ref: 'UNIT-TEST-FIRST-1', stage: fake_translation.bamboo_stage_name }, @@ -229,7 +229,7 @@ end context 'when you receive an comment' do - let(:check_suite) { create(:check_suite, :with_running_ci_jobs) } + let(:check_suite) { create(:check_suite, :with_running_ci_jobs, pull_request: pull_request) } let(:check_suite_rerun) { CheckSuite.find_by(commit_sha_ref: check_suite.commit_sha_ref, re_run: true) } let(:ci_jobs) do @@ -296,6 +296,68 @@ expect(check_suite_rerun).not_to be_nil end end + + context 'when PullRequestCommit is invalid' do + let(:fake_pull_request_commit) { Github::Parsers::PullRequestCommit.new(pull_request.repository, 1) } + let(:check_suite) { create(:check_suite, pull_request: pull_request) } + let(:ci_jobs) do + [ + { name: 'First Test', job_ref: 'UNIT-TEST-FIRST-1', stage: fake_translation.bamboo_stage_name }, + { name: 'Checkout', job_ref: 'CHK-01', stage: fake_translation.bamboo_stage_name } + ] + end + + let(:payload) do + { + 'action' => 'created', + 'comment' => { 'body' => "CI:rerun 000000 ##{check_suite.commit_sha_ref}", 'id' => 1 }, + 'repository' => { 'full_name' => check_suite.pull_request.repository }, + 'issue' => { 'number' => check_suite.pull_request.github_pr_id } + } + end + + let(:pull_request_commits) do + [ + { sha: check_suite.commit_sha_ref, date: Time.now } + ] + end + + let(:pull_request_info) do + { + head: { + ref: 'master' + }, + base: { + ref: 'test', + sha: check_suite.base_sha_ref + } + } + end + + before do + 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(:pull_request_commits).and_return(pull_request_commits, []) + + allow(Github::Check).to receive(:new).and_return(fake_github_check) + allow(fake_github_check).to receive(:create).and_return(check_suite) + allow(fake_github_check).to receive(:add_comment) + allow(fake_github_check).to receive(:cancelled) + allow(fake_github_check).to receive(:queued) + allow(fake_github_check).to receive(:pull_request_info).and_return(pull_request_info) + allow(fake_github_check).to receive(:fetch_username).and_return({}) + allow(fake_github_check).to receive(:check_runs_for_ref).and_return({}) + allow(fake_github_check).to receive(:comment_reaction_thumb_down).and_return({}) + + allow(Github::Parsers::PullRequestCommit).to receive(:new).and_return(fake_pull_request_commit) + allow(fake_pull_request_commit).to receive(:invalid?).and_return(true) + end + + it 'must returns an error' do + expect(rerun.start).to eq([404, 'Failed to create a check suite']) + end + end end describe 'alternative scenarios' do