Skip to content

Instantly share code, notes, and snippets.

@cthoyt
Created July 13, 2021 19:04
Show Gist options
  • Save cthoyt/ef0feab83e67891b478c40f5477e4e8a to your computer and use it in GitHub Desktop.
Save cthoyt/ef0feab83e67891b478c40f5477e4e8a to your computer and use it in GitHub Desktop.
Check if the given GitHub repository has the given topic.
from typing import Optional
import requests
def has_topic(owner: str, repo: str, topic: str, token: Optional[str] = None) -> bool:
"""Check if the given GitHub repository has the given topic.
:param owner: The name of the owner/organization for the repository.
:param repo: The name of the repository.
:param topic: The topic to check if the repository has. Case and punctuation sensitive.
:param token: The GitHub OAuth token. Not required, but if given, will let
you make many more queries before getting rate limited.
:return: If the repository has the given topic.
"""
headers = {
'Accept': "application/vnd.github.mercy-preview+json",
}
if token:
headers['Authorization'] = f"token {token}",
res = requests.get(
f'https://api.github.com/repos/{owner}/{repo}/topics',
headers=headers,
)
res_json = res.json()
return topic in res_json['names']
if __name__ == '__main__':
print(has_topic('obophenotype', 'dicty-phenotype-ontology', topic='obofoundry'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment