Skip to content
Open
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

source 'https://rubygems.org'

ruby '3.1.2'
ruby '>= 3.1.2'

# Token
gem 'jwt'
Expand Down
14 changes: 12 additions & 2 deletions lib/github/parsers/pull_request_commit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
14 changes: 13 additions & 1 deletion lib/github/re_run/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 64 additions & 2 deletions spec/lib/github/re_run/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down