Changeset View
Changeset View
Standalone View
Standalone View
swh/lister/gogs/lister.py
Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
class GogsLister(Lister[GogsListerState, GogsListerPage]): | class GogsLister(Lister[GogsListerState, GogsListerPage]): | ||||
"""List origins from the Gogs | """List origins from the Gogs | ||||
Gogs API documentation: https://github.com/gogs/docs-api | Gogs API documentation: https://github.com/gogs/docs-api | ||||
The API is protected behind authentication so credentials/API tokens | The API may be protected behind authentication so credentials/API tokens can be | ||||
are mandatory. It supports pagination and provides next page URL | provided. | ||||
through the 'next' value of the 'Link' header. The default value for | |||||
page size ('limit') is 10 but the maximum allowed value is 50. | The lister supports pagination and provides next page URL through the 'next' value | ||||
of the 'Link' header. The default value for page size ('limit') is 10 but the | |||||
maximum allowed value is 50. | |||||
Api can usually be found at the location: https://<host>/api/v1/repos/search | |||||
""" | """ | ||||
LISTER_NAME = "gogs" | LISTER_NAME = "gogs" | ||||
VISIT_TYPE = "git" | VISIT_TYPE = "git" | ||||
REPO_LIST_PATH = "repos/search" | REPO_LIST_PATH = "repos/search" | ||||
Show All 20 Lines | ): | ||||
self.api_token = api_token | self.api_token = api_token | ||||
if self.api_token is None: | if self.api_token is None: | ||||
if len(self.credentials) > 0: | if len(self.credentials) > 0: | ||||
cred = random.choice(self.credentials) | cred = random.choice(self.credentials) | ||||
username = cred.get("username") | username = cred.get("username") | ||||
self.api_token = cred["password"] | self.api_token = cred["password"] | ||||
logger.info("Using authentication credentials from user %s", username) | logger.info("Using authentication credentials from user %s", username) | ||||
else: | |||||
# Raises an error on Gogs, or a warning on Gitea | |||||
self.on_anonymous_mode() | |||||
self.session.headers.update({"Accept": "application/json"}) | self.session.headers.update({"Accept": "application/json"}) | ||||
if self.api_token: | if self.api_token: | ||||
self.session.headers["Authorization"] = f"token {self.api_token}" | self.session.headers["Authorization"] = f"token {self.api_token}" | ||||
else: | |||||
def on_anonymous_mode(self): | logger.warning( | ||||
raise ValueError("No credentials or API token provided") | "No authentication token set in configuration, using anonymous mode" | ||||
) | |||||
anlambert: We should remove that method in that lister but also in the gitea one (which simply inherits… | |||||
Done Inline Actionsright, done! ardumont: right, done! | |||||
Not Done Inline ActionsYou forgot to commit the swh/lister/gitea/lister.py file I think. anlambert: You forgot to commit the `swh/lister/gitea/lister.py` file I think. | |||||
def state_from_dict(self, d: Dict[str, Any]) -> GogsListerState: | def state_from_dict(self, d: Dict[str, Any]) -> GogsListerState: | ||||
return GogsListerState(**d) | return GogsListerState(**d) | ||||
def state_to_dict(self, state: GogsListerState) -> Dict[str, Any]: | def state_to_dict(self, state: GogsListerState) -> Dict[str, Any]: | ||||
return asdict(state) | return asdict(state) | ||||
def page_request( | def page_request( | ||||
self, url: str, params: Dict[str, Any] | self, url: str, params: Dict[str, Any] | ||||
Show All 35 Lines | def get_pages(self) -> Iterator[GogsListerPage]: | ||||
body, links = self.page_request( | body, links = self.page_request( | ||||
next_link, {**self.query_params, "page": page_id} | next_link, {**self.query_params, "page": page_id} | ||||
) | ) | ||||
while next_link is not None: | while next_link is not None: | ||||
repos = self.extract_repos(body) | repos = self.extract_repos(body) | ||||
assert len(links) > 0, "API changed: no Link header found" | |||||
if "next" in links: | if "next" in links: | ||||
next_link = links["next"]["url"] | next_link = links["next"]["url"] | ||||
else: | else: | ||||
next_link = None # Happens for the last page | next_link = None # Happens for the last page | ||||
yield GogsListerPage(repos=repos, next_link=next_link) | yield GogsListerPage(repos=repos, next_link=next_link) | ||||
if next_link is not None: | if next_link is not None: | ||||
▲ Show 20 Lines • Show All 42 Lines • Show Last 20 Lines |
We should remove that method in that lister but also in the gitea one (which simply inherits from the gogs lister) and move the warning in the __init__ method instead.