Skip to content

Commit

Permalink
Treat Redirected Go Packages as Removed (#3358)
Browse files Browse the repository at this point in the history
* set removed for redirected go packages

* use simpler http stubs for check status specs
  • Loading branch information
mikeyoung85 authored Apr 11, 2024
1 parent de98ce6 commit d1800b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def check_status
response = Typhoeus.get(url)
if platform.downcase == "packagist" && [302, 404].include?(response.response_code)
update_attribute(:status, "Removed")
elsif platform.downcase == "go" && [400, 404].include?(response.response_code)
elsif platform.downcase == "go" && [302, 400, 404].include?(response.response_code)
# pkg.go.dev can be 404 on first-hit for a new package (or alias for the package), so ensure that the package existed in the past
# by ensuring its age is old enough to not be just uncached by pkg.go.dev yet.
update_attribute(:status, "Removed") if created_at < 1.week.ago
Expand Down
48 changes: 34 additions & 14 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,29 +210,49 @@
end

context "a go project missing from upstream" do
let(:check_status_url) { PackageManager::Go.check_status_url(project) }

context "recently created" do
let!(:project) { create(:project, platform: "Go", name: "github.com/some-nonexistent-fake/pkg", status: nil, created_at: 1.hour.ago) }

it "should not mark it as Removed" do
VCR.use_cassette("project/check_status/go") do
project.check_status
project.reload
expect(project.status).to eq(nil)
expect(project.status_checked_at).to eq(DateTime.current)
end
it "should not mark it as Removed for a 404" do
WebMock.stub_request(:get, check_status_url).to_return(status: 404)

project.check_status
project.reload
expect(project.status).to eq(nil)
expect(project.status_checked_at).to eq(DateTime.current)
end

it "should not mark it as Removed for a 302" do
WebMock.stub_request(:get, check_status_url).to_return(status: 302)

project.check_status
project.reload
expect(project.status).to eq(nil)
expect(project.status_checked_at).to eq(DateTime.current)
end
end

context "not recently created" do
let!(:project) { create(:project, platform: "Go", name: "github.com/some-nonexistent-fake/pkg", status: nil, created_at: 1.month.ago) }

it "should mark it as Removed" do
VCR.use_cassette("project/check_status/go") do
project.check_status
project.reload
expect(project.status).to eq("Removed")
expect(project.status_checked_at).to eq(DateTime.current)
end
it "should mark it as Removed for a 404" do
WebMock.stub_request(:get, check_status_url).to_return(status: 404)

project.check_status
project.reload
expect(project.status).to eq("Removed")
expect(project.status_checked_at).to eq(DateTime.current)
end

it "should mark it as Removed for a 302" do
WebMock.stub_request(:get, check_status_url).to_return(status: 302)

project.check_status
project.reload
expect(project.status).to eq("Removed")
expect(project.status_checked_at).to eq(DateTime.current)
end
end
end
Expand Down

0 comments on commit d1800b0

Please sign in to comment.