Skip to content

Commit d9822ab

Browse files
committed
Add check for version_url to API
The API doesn't check if the version_url is filled when the backend requires it. The project created this way will be removed after 1000 failed checks, but we still should prevent this by checking if the version_url is filled. So this PR is adding new attribute to backends, which is checked when creating new project using API. Fixes #1921
1 parent d8a845e commit d9822ab

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

anitya/api_v2.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from anitya import authentication
2020
from anitya.db import Session, models
21-
from anitya.lib import utilities
21+
from anitya.lib import plugins, utilities
2222
from anitya.lib.exceptions import AnityaException, ProjectExists
2323

2424
_log = logging.getLogger(__name__)
@@ -494,6 +494,15 @@ def post(self):
494494
else:
495495
args = parser.parse(user_args, request, location="json")
496496

497+
if not args["version_url"]:
498+
backend = plugins.get_plugin(args["backend"])
499+
if backend.required_version_url:
500+
response = (
501+
jsonify("Chosen backend requires version_url"),
502+
400,
503+
)
504+
return response
505+
497506
try:
498507
project = utilities.create_project(
499508
Session,

anitya/lib/backends/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class BaseBackend(object):
7878
is used.
7979
check_interval (`datetime.timedelta`): Interval which is used for periodic
8080
checking for new versions. This could be overriden by backend plugin.
81+
required_version_url (bool): This flag will let us know if the version URL
82+
is required field on project backend. Default is False as most of the
83+
backends don't require it.
8184
"""
8285

8386
name: str
@@ -86,6 +89,7 @@ class BaseBackend(object):
8689
more_info: str
8790
default_version_scheme = GLOBAL_DEFAULT
8891
check_interval = timedelta(hours=1)
92+
required_version_url: bool = False
8993

9094
@classmethod
9195
def expand_subdirs(cls, url, last_change=None, glob_char="*"):

anitya/lib/backends/custom.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CustomBackend(BaseBackend):
3131
"user-guide.html#regular-expressions</a>"
3232
)
3333
default_regex = REGEX % {"name": "{project name}"}
34+
required_version_url = True
3435

3536
@classmethod
3637
def get_version_url(cls, project):

anitya/lib/backends/folder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class FolderBackend(BaseBackend):
2828
"https://ftp.gnu.org/pub/gnu/gnash/",
2929
"https://subsurface-divelog.org/downloads/",
3030
]
31+
required_version_url = True
3132

3233
@classmethod
3334
def get_version_url(cls, project):

anitya/tests/test_flask_api_v2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,6 +1211,26 @@ def test_conflicting_request(self):
12111211
)
12121212
self.assertEqual("requests", data["requested_project"]["name"])
12131213

1214+
def test_required_version_url_for_backend(self):
1215+
"""
1216+
Assert that backend with required version url will return 400
1217+
if version url is missing.
1218+
"""
1219+
request_data = {
1220+
"backend": "custom",
1221+
"homepage": "http://python-requests.org",
1222+
"name": "requests",
1223+
}
1224+
1225+
output = self.app.post(
1226+
"/api/v2/projects/", headers=self.auth, data=request_data
1227+
)
1228+
self.assertEqual(output.status_code, 400)
1229+
1230+
# Error details should report conflicting fields.
1231+
data = _read_json(output)
1232+
self.assertIn("Chosen backend requires version_url", data)
1233+
12141234
def test_valid_request(self):
12151235
"""Test valid request"""
12161236
request_data = {

news/1921.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix: Should not allow projects with no way to check versions

0 commit comments

Comments
 (0)