Skip to content

Instantly share code, notes, and snippets.

@cthoyt
Last active June 8, 2021 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cthoyt/95694a05190fc23b997fae62f12f96f1 to your computer and use it in GitHub Desktop.
Save cthoyt/95694a05190fc23b997fae62f12f96f1 to your computer and use it in GitHub Desktop.
Add issues to OBO Foundry repositories to add `obofoundry` topic
"""
This script blasts all of the OBO Foundry Repositories with a given issue.
Use sparingly.
______________________________
/ with great power comes great \
\ responsibility /
------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
"""
from textwrap import dedent
import pandas as pd
import pystow
import requests
import yaml
from tqdm import tqdm
URL = 'https://raw.githubusercontent.com/OBOFoundry/OBOFoundry.github.io/master/_config.yml'
PREFIX = 'https://github.com/'
PATH = pystow.join('obofoundry', name='obofoundry_topics.tsv')
TITLE = 'Add obofoundry topic to repo metadata'
BODY = dedent(f'''\
The OBO Foundry has recently created a topics page at https://github.com/topics/obofoundry.
It would be great if you could add the `obofoundry` topic to your repository, since
it's currently listed as active in the OBO Foundry. If you're not sure how to do that,
follow the instructions [here](https://docs.github.com/en/github/administering-a-repository/managing-repository-settings/classifying-your-repository-with-topics).
The main issue for this task is at https://github.com/OBOFoundry/OBOFoundry.github.io/issues/1538
in case you want some more context or to join the larger discussion.
''').replace('\n', ' ')
# Load the GitHub access token via PyStow. We'll
# need it so we don't hit the rate limit
TOKEN = pystow.get_config('github', 'access_token')
def main():
df = make_topic_sheet()
# print(tabulate(df.values, headers=list(df.columns), tablefmt='github'))
post_issues(df)
def post_issues(df):
for owner, repo in df.loc[~df.has_obofoundry_topic, ['owner', 'repo']].values:
res = requests.get(
f'https://api.github.com/repos/{owner}/{repo}/issues',
headers={
'Accept': "application/vnd.github.mercy-preview+json",
'Authorization': f"token {TOKEN}",
},
).json()
# An issue has already been sent. Don't make duplicates
if any(issue['title'] == TITLE for issue in res):
continue
tqdm.write(f'Creating issue for {owner}/{repo}')
from github import Github
g = Github(TOKEN)
repo = g.get_repo(f'{owner}/{repo}')
issue = repo.create_issue(title=TITLE, body=BODY)
tqdm.write(f'success: {issue}')
# probably enough time to avoid the abuse detection trigger
time.sleep(2)
def make_topic_sheet(force: bool = False) -> pd.DataFrame:
if PATH.is_file() and not force:
df = pd.read_csv(PATH, sep='\t')
df['topics'] = df['topics'].map(lambda x: x if pd.notna(x) else '')
return df
# Get the
res = requests.get(URL)
res = yaml.safe_load(res.content)
rows = []
for record in tqdm(res['ontologies']):
tracker = record.get('tracker')
# All active ontologies have trackers. Most of them
# have GitHub, but not all. Don't consider the non-GitHub
# ones
if not tracker or not tracker.startswith(PREFIX):
continue
# Since we assume it's a github link, slice out the prefix then
# parse the owner and repository out of the path
owner, repo, *_ = tracker[len(PREFIX):].split('/')
# Get all topics from the repository. See more information on the GH docs:
# https://docs.github.com/en/rest/reference/repos#get-all-repository-topics
topics = requests.get(
f'https://api.github.com/repos/{owner}/{repo}/topics',
headers={
'Accept': "application/vnd.github.mercy-preview+json",
'Authorization': f"token {TOKEN}",
},
).json()['names']
rows.append((
record['id'],
record['title'],
owner,
repo,
','.join(topics),
'obofoundry' in topics
))
columns = ['ontology', 'title', 'owner', 'repo', 'topics', 'has_obofoundry_topic']
df = pd.DataFrame(rows, columns=columns)
df.to_csv(PATH, sep='\t', index=False)
return df
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment