-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathgh_utils.py
100 lines (80 loc) · 2.86 KB
/
gh_utils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Standard library
import inspect
import logging
import os
import re
import sys
# Third-party
from github import Github
from github.GithubException import BadCredentialsException
# First-party/Local
from ccos import log
GITHUB_ORGANIZATION = "creativecommons"
GITHUB_USERNAME = "cc-creativecommons-github-io-bot"
GITHUB_TOKEN = None
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
logger = logging.getLogger(log_name)
log.reset_handler()
def set_up_github_client():
global GITHUB_TOKEN
try:
GITHUB_TOKEN = os.environ["ADMIN_GITHUB_TOKEN"]
except KeyError:
logger.critical("missing ADMIN_GITHUB_TOKEN environment variable")
sys.exit(1)
logger.log(logging.INFO, "Setting up GitHub client...")
github_client = Github(GITHUB_TOKEN)
logger.log(log.SUCCESS, "done.")
return github_client
def get_cc_organization(github_client):
logger.log(logging.INFO, "Getting CC's GitHub organization...")
try:
cc = github_client.get_organization(GITHUB_ORGANIZATION)
except BadCredentialsException as e:
logger.critical(
f"{e.status} {e.data['message']} (see"
f" {e.data['documentation_url']})"
)
sys.exit(1)
logger.log(log.SUCCESS, "done.")
return cc
def get_team_slug_name(project_name, role):
"""
Get the team name and team slug based on GitHub's naming scheme. By
convention, teams are named in a well-defined .
team name schema
CT: <project name> <role, pluralized>
team slug schema
ct-<project_name_slug>-<role_slug>
@param project_name: the name of the project to which the team belongs
@param role: the role held by folks in the team
@return: the slug and name of the team
"""
sanitized_role = pluralized(role).replace("Project ", "")
team_name = f"CT: {project_name} {sanitized_role}"
team_slug = slugified(team_name)
return team_slug, team_name
def pluralized(word):
"""
Get the plural of the given word. Contains a dictionary for non-standard
plural forms. If the word ends in one of 5 known endings, appends an 'es'
to the end. By default, just appends an 's' to the given word.
@param word: the word to pluralize
@return: the plural form of the noun
"""
defined_plurals = {"person": "people"}
if word in defined_plurals:
return defined_plurals[word]
es_endings = ["s", "sh", "ch", "x", "z"]
if any([word.endswith(ending) for ending in es_endings]):
return f"{word}es"
else:
return f"{word}s"
def slugified(text):
"""
Get the slug generated from the given text. Replaces all non-alphanumeric
characters with hyphens. Coalesces successive hyphens into one.
@param text: the text to slugify
@return: the slug made from the given text
"""
return re.sub("-+", "-", re.sub(r"\W", "-", text.lower()))