Skip to content

Commit

Permalink
prevent trying to get a commit that can't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman committed Oct 26, 2022
1 parent 6da4ead commit 3bbe65b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion common/lib/dependabot/file_fetchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ def commit
def clone_repo_contents
@clone_repo_contents ||=
_clone_repo_contents(target_directory: repo_contents_path)
rescue Dependabot::SharedHelpers::HelperSubprocessFailed
rescue Dependabot::SharedHelpers::HelperSubprocessFailed => e
if e.message.include?("fatal: Remote branch #{target_branch} not found in upstream origin")
raise Dependabot::BranchNotFound, target_branch
end

raise Dependabot::RepoNotFound, source
end

Expand Down
10 changes: 10 additions & 0 deletions common/spec/dependabot/file_fetchers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,16 @@ def fetch_files
expect { subject }.to raise_error(Dependabot::RepoNotFound)
end
end

context "when the branch can't be found" do
let(:branch) do
"notfound"
end

it "raises a not found error" do
expect { subject }.to raise_error(Dependabot::BranchNotFound)
end
end
end
end
end
4 changes: 4 additions & 0 deletions updater/lib/dependabot/file_fetcher_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def clone_repo_contents
return unless job.clone?

file_fetcher.clone_repo_contents
rescue Dependabot::RepoNotFound, Dependabot::BranchNotFound
# base_commit_sha can't be reached, don't attempt to fetch it in the rescue
@base_commit_sha = "unknown"
raise
end

def base64_dependency_files
Expand Down
21 changes: 21 additions & 0 deletions updater/spec/dependabot/file_fetcher_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@
expect(Dir.exist?(Dependabot::Environment.repo_contents_path)).to be_truthy
expect(Dir.empty?(Dependabot::Environment.repo_contents_path)).to be_truthy
end

context "when the fetcher raises a BranchNotFound error while cloning" do
before do
allow_any_instance_of(Dependabot::GoModules::FileFetcher).
to receive(:clone_repo_contents).
and_raise(Dependabot::BranchNotFound, "my_branch")
end

it "tells the backend about the error (and doesn't re-raise it)" do
expect(api_client).
to receive(:record_update_job_error).
with(
job_id,
error_details: { "branch-name": "my_branch" },
error_type: "branch_not_found"
)
expect(api_client).to receive(:mark_job_as_processed)

expect { perform_job }.to output(/Error during file fetching; aborting/).to_stdout_from_any_process
end
end
end

context "when the connectivity check is enabled", vcr: true do
Expand Down

0 comments on commit 3bbe65b

Please sign in to comment.