Skip to content

Commit

Permalink
Add: Add GitHub API for updating repository topics
Browse files Browse the repository at this point in the history
Allow to update repository topics
  • Loading branch information
bjoernricks committed Sep 27, 2023
1 parent cf5f21e commit c9328eb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions pontos/github/api/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
22 changes: 22 additions & 0 deletions tests/github/api/test_repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

0 comments on commit c9328eb

Please sign in to comment.