-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
executable file
·68 lines (54 loc) · 2.29 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/env python3
from github import Github
from github.GithubException import BadCredentialsException
from sys import exit
import argparse
import os
def write_topics(repo, topiclist, dry_run):
if not dry_run:
repo.replace_topics(topiclist)
parser = argparse.ArgumentParser(
description="Quickly add/remove the 'hacktoberfest' topic to all of your public Github projects"
)
parser.add_argument("action", type=str, help="'add' or 'remove' the topic", choices=["add", "remove"])
parser.add_argument("--organization", "-o", help="Modify topics for an organization, not your personal projects")
parser.add_argument("--dry-run", action="store_true", help="Don't actually modify the topics")
args = parser.parse_args()
try:
token = os.environ["GITHUB_TOKEN"]
except KeyError:
print("You need to provide an GitHub Token via the env variable 'GITHUB_TOKEN'")
exit(1)
g = Github(token)
try:
print("Getting list of repositories")
if args.organization:
repositories = [r for r in g.get_user().get_repos(affiliation = "organization_member", visibility = "public") if r.organization.login == args.organization]
else:
repositories = list(g.get_user().get_repos(affiliation = "owner", visibility = "public"))
user_id = g.get_user().id
except BadCredentialsException:
print("Token is invalid or has the wrong permissions")
exit(1)
for repo in repositories:
print(f"> Checking {repo.full_name}:")
if repo.fork or repo.archived:
print("Repo is a fork or archived. Skipping...")
continue
if not args.organization and repo.organization is not None:
print("Repo belongs to a organization. Skipping...")
continue
if not repo.permissions.admin:
print("You don't have admin permissions on the repo. Skipping...")
continue
topic_list = repo.get_topics()
if "hacktoberfest" in topic_list and args.action == "remove":
print("Removing topic from repo")
topic_list.remove("hacktoberfest")
write_topics(repo, topic_list, args.dry_run)
elif "hacktoberfest" not in topic_list and args.action == "add":
print("Adding topic to repo")
new_topics = topic_list + ["hacktoberfest"]
write_topics(repo, new_topics, args.dry_run)
else:
print("Repo in correct state")