Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix fragment in suggested_changelog_url causing exceptions #9604

Merged
merged 2 commits into from Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 3 additions & 10 deletions common/lib/dependabot/metadata_finders/base/changelog_finder.rb
Expand Up @@ -53,6 +53,8 @@ def initialize(source:, dependency:, credentials:,
@dependency = dependency
@credentials = credentials
@suggested_changelog_url = suggested_changelog_url
# strip fragment from URL, if present
@suggested_changelog_url = @suggested_changelog_url&.split("#")&.first

@new_version = T.let(nil, T.nilable(String))
@changelog_from_suggested_url = T.let(nil, T.untyped)
Expand Down Expand Up @@ -127,16 +129,7 @@ def changelog_from_suggested_url
suggested_source_client = github_client_for_source(T.must(suggested_source))
tmp_files = T.unsafe(suggested_source_client).contents(suggested_source&.repo, opts)

filename = T.must(T.must(suggested_changelog_url).split("/").last).split("#").first

# If the suggested source points to a specific directory
# then we will receive a hash for just the changelog file
if suggested_source&.directory && tmp_files[:name] == filename
return @changelog_from_suggested_url = tmp_files
end

# Otherwise we will get back an array of hashes representing the files
# in the root directory and we need to find the changelog
filename = T.must(T.must(suggested_changelog_url).split("/").last)
@changelog_from_suggested_url =
tmp_files.find { |f| f.name == filename }
rescue Octokit::NotFound, Octokit::UnavailableForLegalReasons
Expand Down
Expand Up @@ -158,60 +158,35 @@

context "when given a suggested_changelog_url" do
let(:suggested_changelog_url) do
"github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
end

let(:source) do
Dependabot::Source.from_url(suggested_changelog_url)
"https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
end

let(:finder) do
described_class.new(
source: source,
source: nil,
credentials: credentials,
dependency: dependency,
suggested_changelog_url: suggested_changelog_url
)
end

context "and it points to a specific directory" do
let(:suggested_changelog_url) do
"github.com/mperham/sidekiq/blob/master/dir/Pro-Changes.md"
end

before do
sidekiq_changelog =
fixture("github", "contents_sidekiq_changelog.json")
suggested_github_url =
"https://api.github.com/repos/mperham/sidekiq/contents/dir?ref=master"
stub_request(:get, suggested_github_url)
.to_return(status: 200,
body: sidekiq_changelog,
headers: { "Content-Type" => "application/json" })
end
before do
stub_request(:get, "https://api.github.com/repos/mperham/sidekiq/contents/")
.to_return(status: 200,
body: fixture("github", "contents_sidekiq.json"),
headers: { "Content-Type" => "application/json" })
end

it "gets the right URL" do
expect(subject)
.to eq(
"https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
)
end
it "gets the right URL" do
expect(subject)
.to eq(
"https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
)
end

context "but it does not point to a specific directory" do
context "and the URL has a fragment" do
let(:suggested_changelog_url) do
"github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
end

before do
suggested_github_response =
fixture("github", "contents_sidekiq.json")
suggested_github_url =
"https://api.github.com/repos/mperham/sidekiq/contents/"
stub_request(:get, suggested_github_url)
.to_return(status: 200,
body: suggested_github_response,
headers: { "Content-Type" => "application/json" })
"https:/github.com/mperham/sidekiq/blob/master/Pro-Changes.md#v2.8.6"
end

it "gets the right URL" do
Expand Down