improve anitya util

This commit is contained in:
Anton Medvedev 2025-05-15 16:11:34 +02:00
parent 2de83ef8cf
commit 0a2f768848
3 changed files with 17 additions and 20 deletions

View file

@ -255,7 +255,7 @@ ping_comment = "This request wants to skip bugzilla validation! {maintainers} co
oidc_distgit_token = "OIDC token used to push git changes using pagure_user"
# Anitya access token and endpoint for managing project in release-monitoring
anitya_access_token = "API token for Anitya"
anitya_endpoint = "https://release-monitoring.org/"
anitya_endpoint = "https://release-monitoring.org"
# Pagure mapping to bugzilla

View file

@ -793,10 +793,10 @@ class SCMRequestProcessor(ToddlerBase):
"Anitya project is accessible by this link \n`{0}`\n "
"you can modify it manually.".format(anitya_project_url)
)
package_response = self.anitya.does_package_exists_in_anitya(
package_exists = self.anitya.does_package_exists_in_anitya(
repo, project_name, distribution
)
if package_response != 2:
if not package_exists:
response_msg = self.anitya.create_package_in_anitya(
repo, project_name, distribution, upstreamurl
)

View file

@ -33,9 +33,6 @@ def set_anitya(config):
return Anitya(config)
ANITYA_PROJECT_BASE_URL = "https://release-monitoring.org/project/"
class Anitya(object):
"""
Object that works with Anitya.
@ -68,7 +65,7 @@ class Anitya(object):
self._requests_session = requests.make_session(timeout=300)
def does_project_exists_in_anitya(self, project_name: str):
def does_project_exists_in_anitya(self, project_name: str) -> Optional[str]:
"""
Check if project exists in Anitya.
@ -97,14 +94,14 @@ class Anitya(object):
try:
project_id = response_json["items"][0]["id"]
project_url = "{0}/{1}".format(ANITYA_PROJECT_BASE_URL, project_id)
project_url = "{0}/project/{1}".format(self._anitya_endpoint, project_id)
return project_url
except (KeyError, IndexError):
return None
def does_package_exists_in_anitya(
self, package_name: str, distribution: str, project_name: str
):
) -> bool:
"""
Check if package exists in Anitya.
@ -114,9 +111,9 @@ class Anitya(object):
project_name (str): The name of the project.
Returns:
0 if package dont exist
1 if package exist but his project is different from provided project name
2 if package exist and his project is correct
False if package dont exist
False if package exist but his project is different from provided project name
True if package exist and his project is correct
"""
packages_endpoint = self._anitya_endpoint + "/api/v2/packages/"
packages_params = {
@ -128,25 +125,25 @@ class Anitya(object):
)
if packages_response.status_code != 200:
log.info("Package '{0}' not found in Anitya.".format(package_name))
return 0 # Not able to find package
return False # Not able to find package
response_json = packages_response.json()
item_count = response_json["total_items"]
if item_count < 1:
log.info("Package '{0}' not found in Anitya.".format(package_name))
return 0
return False
package_json = response_json["items"][0]
package_project_name = package_json["project"]
if package_project_name != project_name:
return 1 # Expected and actual project name are different
return False # Expected and actual project name are different
else:
return 2 # Expected and actual project name are the same
return True # Expected and actual project name are the same
def create_project_in_anitya(
self,
name: str,
homepage: str,
backend: str,
):
) -> Optional[str]:
"""
Create a new project in Anitya.
@ -156,7 +153,7 @@ class Anitya(object):
backend (str): The name of the backend.
Returns:
The project ID if successful, otherwise None.
The project URL if successful, otherwise None.
"""
headers = {"Authorization": "token " + self._anitya_token}
endpoint = self._anitya_endpoint + "/api/v2/projects/"
@ -172,7 +169,7 @@ class Anitya(object):
if response.status_code == 201:
project_json = response.json()
project_id = project_json["id"]
project_url = "{0}/{1}".format(ANITYA_PROJECT_BASE_URL, project_id)
project_url = "{0}/project/{1}".format(self._anitya_endpoint, project_id)
return project_url
else:
return None
@ -183,7 +180,7 @@ class Anitya(object):
project_name: str,
distribution: str,
project_ecosystem: str,
):
) -> Optional[str]:
"""
Create a new package in Anitya.