get_canonical_repo_name: Better handle HTTP errors

Signed-off-by: Stephen Gallagher <sgallagh@redhat.com>
This commit is contained in:
Stephen Gallagher 2024-07-10 08:50:57 -04:00
parent a35f1acdfd
commit 8c605fd2ee

View file

@ -18,8 +18,9 @@ import re
import requests import requests
import sys import sys
from datetime import date, datetime from datetime import date, datetime
from http import HTTPStatus
from pyrpkg import rpkgError from pyrpkg import rpkgError
from requests.exceptions import ConnectionError from requests.exceptions import ConnectionError, HTTPError
from configparser import NoOptionError, NoSectionError from configparser import NoOptionError, NoSectionError
from urllib.parse import quote_plus, urlparse from urllib.parse import quote_plus, urlparse
@ -229,6 +230,15 @@ def get_canonical_repo_name(config, repo_url):
rv_json = rv.json() rv_json = rv.json()
canonical_repo_name = rv_json['forked_from_project']['name'] canonical_repo_name = rv_json['forked_from_project']['name']
except HTTPError as e:
# We got a 4xx or 5xx error code from the URL lookup
if e.response.status_code == HTTPStatus.FORBIDDEN:
raise rpkgError("Insufficient Gitlab API permissions. Missing token?")
# Other errors are unexpected, so re-raise them
raise
except KeyError as e: except KeyError as e:
# There was no 'forked_from_project' key, likely meaning the # There was no 'forked_from_project' key, likely meaning the
# user lacked permissions to read the API. Usually this means # user lacked permissions to read the API. Usually this means