diff --git a/pontos/github/api/repositories.py b/pontos/github/api/repositories.py index a02e65e7d..0aeda98d9 100644 --- a/pontos/github/api/repositories.py +++ b/pontos/github/api/repositories.py @@ -522,3 +522,38 @@ async def topics(self, repo: str) -> Iterable[str]: data: dict[str, Any] = response.json() return data.get("names", []) + + async def update_topics( + self, repo: str, new_topics: Iterable[str] + ) -> Iterable[str]: + """ + Replace all topics of a repository + + Args: + repo: GitHub repository (owner/name) to update the topics for + new_topics: Iterable of new topics to set on the repository + + Raises: + HTTPStatusError: A httpx.HTTPStatusError is raised if the request + failed. + + Returns: + An iterable of topics as string + + Example: + .. code-block:: python + + from pontos.github.api import GitHubAsyncRESTApi + + async with GitHubAsyncRESTApi(token) as api: + topics = await api.repositories.update_topics( + "foo/bar", ["foo", "bar"] + ) + """ + api = f"/repos/{repo}/topics" + data = {"names": list(new_topics)} + response = await self._client.put(api, data=data) # type: ignore[arg-type] # noqa: E501 + response.raise_for_status() + + data: dict[str, Any] = response.json() + return data.get("names", []) diff --git a/tests/github/api/test_repositories.py b/tests/github/api/test_repositories.py index a146438e6..e1ac5674d 100644 --- a/tests/github/api/test_repositories.py +++ b/tests/github/api/test_repositories.py @@ -785,3 +785,25 @@ async def test_get_topics(self): self.assertEqual(len(topics), 3) self.assertEqual(topics, ["foo", "bar", "baz"]) + + async def test_update_topics(self): + response = create_response() + response.json.return_value = {} + response.json.return_value = {"names": ["foo", "bar"]} + + self.client.put.return_value = response + + new_topics = await self.api.update_topics( + "foo/bar", + ( + "foo", + "bar", + ), + ) + + self.client.put.assert_awaited_once_with( + "/repos/foo/bar/topics", data={"names": ["foo", "bar"]} + ) + + self.assertEqual(len(new_topics), 2) + self.assertEqual(new_topics, ["foo", "bar"])