Skip to content

Commit

Permalink
Better support around bundler changelogs (#9429)
Browse files Browse the repository at this point in the history
* Support bundler changelogs that use a version in their changelog url

* Add tests for when the changelog source points to a file or directory

* Assign and return in one line
  • Loading branch information
Nishnha committed Apr 4, 2024
1 parent d53fce0 commit 005dd73
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
Expand Up @@ -128,6 +128,15 @@ def changelog_from_suggested_url
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
@changelog_from_suggested_url =
tmp_files.find { |f| f.name == filename }
rescue Octokit::NotFound, Octokit::UnavailableForLegalReasons
Expand Down
Expand Up @@ -157,6 +157,14 @@
end

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)
end

let(:finder) do
described_class.new(
source: source,
Expand All @@ -165,44 +173,72 @@
suggested_changelog_url: suggested_changelog_url
)
end
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" })
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

it "gets the right URL" do
expect(subject)
.to eq(
"https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
)
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

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

context "that can't be found" do
context "but it does not point to a specific directory" 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: 404)
.to_return(status: 200,
body: suggested_github_response,
headers: { "Content-Type" => "application/json" })
end

it "falls back to looking for the changelog as usual" do
it "gets the right URL" do
expect(subject)
.to eq(
"https://github.com/gocardless/business/" \
"blob/master/CHANGELOG.md"
"https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
)
end
end

context "that can't be found" do
before do
suggested_github_url =
"https://api.github.com/repos/mperham/sidekiq/contents/"
stub_request(:get, suggested_github_url)
.to_return(status: 404)

suggested_github_url_with_tag =
"https://api.github.com/repos/mperham/sidekiq/contents/?ref=v1.4.0"
stub_request(:get, suggested_github_url_with_tag)
.to_return(status: 404)
end

it "returns nil for the changelog url" do
expect(subject).to eq(nil)
end
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions common/spec/fixtures/github/contents_sidekiq_changelog.json
@@ -0,0 +1,16 @@
{
"name": "Pro-Changes.md",
"path": "Pro-Changes.md",
"sha": "0c50ae70b0d731329c7be800f9e25fd1434ae0c9",
"size": 20276,
"url": "https://api.github.com/repos/mperham/sidekiq/contents/Pro-Changes.md?ref=master",
"html_url": "https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md",
"git_url": "https://api.github.com/repos/mperham/sidekiq/git/blobs/0c50ae70b0d731329c7be800f9e25fd1434ae0c9",
"download_url": "https://raw.githubusercontent.com/mperham/sidekiq/master/Pro-Changes.md",
"type": "file",
"_links": {
"self": "https://api.github.com/repos/mperham/sidekiq/contents/Pro-Changes.md?ref=master",
"git": "https://api.github.com/repos/mperham/sidekiq/git/blobs/0c50ae70b0d731329c7be800f9e25fd1434ae0c9",
"html": "https://github.com/mperham/sidekiq/blob/master/Pro-Changes.md"
}
}

0 comments on commit 005dd73

Please sign in to comment.