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 crash in Folder, GNU and Crates backends #837

Merged
merged 1 commit into from
Sep 6, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions anitya/lib/backends/crates.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def _get_versions(cls, project):
try:
req = cls.call_url(url, last_change=last_change)
req.raise_for_status()
# Not modified
if req.status_code == 304:
return []
data = req.json()
except requests.RequestException as e:
raise AnityaPluginException(
Expand Down
8 changes: 4 additions & 4 deletions anitya/lib/backends/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ def get_versions(cls, project):

versions = []

# Not modified
if req.status_code == 304:
return versions

if not isinstance(req, six.string_types):
# Not modified
if req.status_code == 304:
return versions

req = req.text

try:
Expand Down
9 changes: 6 additions & 3 deletions anitya/lib/backends/gnu.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from anitya.lib.backends import BaseBackend, REGEX, get_versions_by_regex_for_text
from anitya.lib.exceptions import AnityaPluginException

import six


DEFAULT_REGEX = 'href="([0-9][0-9.]*)/"'

Expand Down Expand Up @@ -83,9 +85,10 @@ def get_versions(cls, project):
)

versions = []
# Not modified
if req.status_code == 304:
return versions
if not isinstance(req, six.string_types):
# Not modified
if req.status_code == 304:
return versions

try:
regex = REGEX % {"name": project.name}
Expand Down
13 changes: 13 additions & 0 deletions anitya/tests/lib/backends/test_crates.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ def test__get_versions_no_json(self, mock_call_url):
crates.CratesBackend._get_versions(project)
self.assertIn("Failed to decode JSON", str(context_manager.exception))

def test_get_versions_not_modified(self):
"""Assert that not modified response is handled correctly"""
pid = 1
project = models.Project.get(self.session, pid)
exp_url = "https://crates.io/api/v1/crates/itoa/versions"

with mock.patch("anitya.lib.backends.BaseBackend.call_url") as m_call:
m_call.return_value = mock.Mock(status_code=304)
versions = crates.CratesBackend.get_versions(project)

m_call.assert_called_with(exp_url, last_change=None)
self.assertEqual(versions, [])


if __name__ == "__main__":
unittest.main(verbosity=2)