-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathutils.py
74 lines (56 loc) · 2.1 KB
/
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
# Standard library
import os
import re
# Third-party
from github import Github
GITHUB_ORGANIZATION = "creativecommons"
GITHUB_USERNAME = "cc-creativecommons-github-io-bot"
GITHUB_TOKEN = os.environ["ADMIN_GITHUB_TOKEN"]
def set_up_github_client():
print("Setting up GitHub client...")
github_client = Github(GITHUB_TOKEN)
return github_client
def get_cc_organization(github_client):
print("Getting CC's GitHub organization...")
cc = github_client.get_organization(GITHUB_ORGANIZATION)
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()))