Skip to content

Commit 4cca519

Browse files
committed
Raise BranchNotFound error when getting a branch's head commit fails
1 parent 34c0d85 commit 4cca519

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/dependabot/errors.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,28 @@
33
module Dependabot
44
class DependabotError < StandardError; end
55

6+
#####################
7+
# Repo leval errors #
8+
#####################
9+
10+
class BranchNotFound < DependabotError
11+
attr_reader :branch_name
12+
13+
def initialize(branch_name, msg = nil)
14+
@branch_name = branch_name
15+
super(msg)
16+
end
17+
18+
def file_name
19+
branch_name.split("/").last
20+
end
21+
22+
def directory
23+
# Directory should always start with a `/`
24+
branch_name.split("/")[0..-2].join("/").sub(%r{^/*}, "/")
25+
end
26+
end
27+
628
#####################
729
# File level errors #
830
#####################

lib/dependabot/file_fetchers/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def commit
4848
gitlab_client.branch(repo, branch).commit.id
4949
else raise "Unsupported host '#{host}'."
5050
end
51+
rescue Octokit::NotFound
52+
raise Dependabot::BranchNotFound, branch
5153
end
5254

5355
private

spec/dependabot/file_fetchers/base_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ def fetch_files
7777
end
7878

7979
it { is_expected.to eq("bb218f56b14c9653891f9e74264a383fa43fefbd") }
80+
81+
context "that can't be found" do
82+
before do
83+
stub_request(:get, url + "/git/refs/heads/my_branch").
84+
with(headers: { "Authorization" => "token token" }).
85+
to_return(status: 404,
86+
headers: { "content-type" => "application/json" })
87+
end
88+
89+
it "raises a custom error" do
90+
expect { file_fetcher_instance.files }.
91+
to raise_error(Dependabot::BranchNotFound) do |error|
92+
expect(error.branch_name).to eq("my_branch")
93+
end
94+
end
95+
end
8096
end
8197
end
8298

0 commit comments

Comments
 (0)