Skip to content

Commit e7c21b7

Browse files
committed
improve logging and code organization
- improve logging - use root logger - easier/less code - exposes debug messages of third-party libraries - add success() and change_indent() to root logger for easier invocation and fewer imports - fix indent level handling - reduce indent and use gray color to reduce distraction of non-esssential punctation - standardize "done." messags and log level - improve code organization - move asana into its own file - reduce duplication with gh_utils - remove detritus from when each script had their own environment - clean-up whitespace
1 parent 9c37348 commit e7c21b7

16 files changed

+244
-352
lines changed

ccos/data/asana.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Standard library
2+
import logging
3+
import os
4+
import sys
5+
6+
# Third-party
7+
import asana
8+
9+
# To see workspace GID, log into Asana and then view:
10+
# https://app.asana.com/api/1.0/workspaces
11+
ASANA_WORKSPACE_GID = "133733285600979"
12+
# To see project GIDs, log into Asana and then view:
13+
# https://app.asana.com/api/1.0/projects
14+
#
15+
# To see "Community Team Tracking" project section GIDs, log into Asana and
16+
# then view:
17+
# https://app.asana.com/api/1.0/projects/1172465506923657/sections
18+
ASANA_SECTION_GID = "1172465506923661"
19+
LOG = logging.root
20+
21+
22+
def setup_asana_client():
23+
LOG.info("Setting up Asana client...")
24+
try:
25+
asana_token = os.environ["ADMIN_ASANA_TOKEN"]
26+
except KeyError:
27+
LOG.critical("missin ADMIN_ASANA_TOKEN environment variable")
28+
sys.exit(1)
29+
asana_client = asana.Client.access_token(asana_token)
30+
asana_client.headers = {"asana-enable": "new_goal_memberships"}
31+
try:
32+
# Perform simple API operation to test authentication
33+
asana_client.workspaces.get_workspace(ASANA_WORKSPACE_GID)
34+
except asana.error.NoAuthorizationError as e:
35+
LOG.critical(f"{e.status} {e.message} (is ADMIN_ASANA_TOKEN valid?)")
36+
sys.exit(1)
37+
LOG.success("done.")
38+
return asana_client
39+
40+
41+
def get_asana_team_members(asana_client):
42+
LOG.info("Get Team Members...")
43+
team_members = asana_client.tasks.find_by_section(
44+
ASANA_SECTION_GID, opt_fields=["name", "custom_fields"]
45+
)
46+
LOG.success("done.")
47+
return team_members

ccos/data/get_community_team_data.py

+6-46
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,11 @@
11
# Standard library
2-
import inspect
32
import logging
4-
import os
53
import sys
64

7-
# Third-party
8-
import asana
5+
LOG = logging.root
96

10-
# First-party/Local
11-
import ccos.log
127

13-
ASANA_WORKSPACE_GID = "133733285600979"
14-
ASANA_PROJECT_GID = "1172465506923661"
15-
16-
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
17-
LOG = logging.getLogger(log_name)
18-
ccos.log.reset_handler()
19-
20-
21-
def setup_asana_client():
22-
LOG.info("Setting up Asana client...")
23-
try:
24-
asana_token = os.environ["ADMIN_ASANA_TOKEN"]
25-
except KeyError:
26-
LOG.critical("missin ADMIN_ASANA_TOKEN environment variable")
27-
sys.exit(1)
28-
asana_client = asana.Client.access_token(asana_token)
29-
asana_client.headers = {"asana-enable": "new_goal_memberships"}
30-
try:
31-
# Perform simple API operation to test authentication
32-
asana_client.workspaces.get_workspace(ASANA_WORKSPACE_GID)
33-
except asana.error.NoAuthorizationError as e:
34-
LOG.critical(f"{e.status} {e.message} (is ADMIN_ASANA_TOKEN valid?)")
35-
sys.exit(1)
36-
LOG.info("done.")
37-
return asana_client
38-
39-
40-
def generate_databag(asana_client):
8+
def generate_databag(team_members):
419
"""
4210
This method pulls the team members from Asana and
4311
loads them into the databag after a little
@@ -71,17 +39,9 @@ def generate_databag(asana_client):
7139
]
7240
}
7341
"""
74-
75-
LOG.info("Pulling from Asana and generating databag...")
7642
databag = {"projects": [], "community_builders": []}
77-
78-
members = asana_client.tasks.find_by_section(
79-
ASANA_PROJECT_GID, opt_fields=["name", "custom_fields"]
80-
)
81-
LOG.info("Team members pulled.")
82-
8343
LOG.info("Processing team members...")
84-
for member in members:
44+
for member in team_members:
8545
if member["name"] == "":
8646
continue # Sometimes blank names come up
8747
role = get_custom_field(member, "Role")
@@ -115,7 +75,7 @@ def generate_databag(asana_client):
11575
)
11676
break
11777

118-
LOG.info("Done.")
78+
LOG.success("done.")
11979
return databag
12080

12181

@@ -195,8 +155,8 @@ def get_custom_field(task, field_name):
195155
return field["text_value"]
196156

197157

198-
def get_community_team_data(asana_client, repo_names):
199-
databag = generate_databag(asana_client)
158+
def get_community_team_data(team_members, repo_names):
159+
databag = generate_databag(team_members)
200160
databag = prune_databag(databag)
201161
databag = verify_databag(databag, repo_names)
202162
databag = sort_databag(databag)

ccos/data/get_repo_data.py

+5-34
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
1-
#!/usr/bin/env python3
2-
# vim: set fileencoding=utf-8:
3-
41
# Standard library
5-
import inspect
62
import logging
7-
import os.path
83

94
# Third-party
105
import emoji
116
import yaml
12-
from github import Github
137
from github.GithubException import GithubException, UnknownObjectException
148

15-
# First-party/Local
16-
import ccos.log
17-
from ccos.data.push_data_via_git import GITHUB_ORGANIZATION, GITHUB_TOKEN
18-
199
CC_METADATA_FILE_NAME = ".cc-metadata.yml"
20-
21-
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
22-
LOG = logging.getLogger(log_name)
23-
ccos.log.reset_handler()
24-
25-
26-
def set_up_github_client():
27-
LOG.info("Setting up GitHub client...")
28-
github_client = Github(GITHUB_TOKEN)
29-
return github_client
30-
31-
32-
def get_cc_organization(github_client):
33-
LOG.info("Getting CC's GitHub organization...")
34-
cc = github_client.get_organization(GITHUB_ORGANIZATION)
35-
return cc
10+
LOG = logging.root
3611

3712

3813
def get_repositories(organization):
@@ -113,19 +88,15 @@ def get_repo_data_dict(repo_data_list):
11388
return {"repos": repo_data_list}
11489

11590

116-
def get_repo_data():
117-
github_client = set_up_github_client()
118-
cc = get_cc_organization(github_client)
119-
repos = get_repositories(cc)
91+
def get_repo_data(gh_org_cc):
92+
repos = get_repositories(gh_org_cc)
12093
repo_data_list = get_repo_data_list(repos)
12194
data = get_repo_data_dict(repo_data_list)
12295
return data
12396

12497

125-
def get_repo_names():
126-
github_client = set_up_github_client()
127-
cc = get_cc_organization(github_client)
128-
repos = get_repositories(cc)
98+
def get_repo_names(gh_org_cc):
99+
repos = get_repositories(gh_org_cc)
129100
names = []
130101
for repo in repos:
131102
names.append(repo.name)

ccos/data/push_data_via_git.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
#!/usr/bin/env python3
2-
# vim: set fileencoding=utf-8:
3-
41
# Standard library
5-
import inspect
62
import json
73
import logging
84
import os
@@ -12,33 +8,25 @@
128
import git
139

1410
# First-party/Local
15-
import ccos.log
11+
from ccos.gh_utils import GITHUB_ORGANIZATION, get_credentials
1612

17-
GIT_USER_NAME = "CC creativecommons.github.io Bot"
18-
GIT_USER_EMAIL = "cc-creativecommons-github-io-bot@creativecommons.org"
19-
20-
GITHUB_USERNAME = "cc-creativecommons-github-io-bot"
21-
GITHUB_ORGANIZATION = "creativecommons"
2213
GITHUB_REPO_NAME = "creativecommons.github.io-source"
23-
24-
GITHUB_TOKEN = os.environ["ADMIN_GITHUB_TOKEN"]
25-
GITHUB_REPO_URL_WITH_CREDENTIALS = (
26-
f"https://{GITHUB_USERNAME}:{GITHUB_TOKEN}"
27-
f"@github.com/{GITHUB_ORGANIZATION}/{GITHUB_REPO_NAME}.git"
28-
)
29-
14+
GIT_USER_EMAIL = "cc-creativecommons-github-io-bot@creativecommons.org"
15+
GIT_USER_NAME = "CC creativecommons.github.io Bot"
3016
JSON_FILE_DIR = "databags"
31-
32-
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
33-
LOG = logging.getLogger(log_name)
34-
ccos.log.reset_handler()
17+
LOG = logging.root
3518

3619

3720
def set_up_repo(git_working_dir):
21+
github_username, github_token = get_credentials()
22+
github_repo_url_with_credentials = (
23+
f"https://{github_username}:{github_token}"
24+
f"@github.com/{GITHUB_ORGANIZATION}/{GITHUB_REPO_NAME}.git"
25+
)
3826
if not os.path.isdir(git_working_dir):
3927
LOG.info("Cloning repo...")
4028
repo = git.Repo.clone_from(
41-
url=GITHUB_REPO_URL_WITH_CREDENTIALS, to_path=git_working_dir
29+
url=github_repo_url_with_credentials, to_path=git_working_dir
4230
)
4331
else:
4432
LOG.info("Setting up repo...")

ccos/gh_utils.py

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Standard library
2-
import inspect
32
import logging
43
import os
54
import re
@@ -9,15 +8,9 @@
98
from github import Github
109
from github.GithubException import BadCredentialsException
1110

12-
# First-party/Local
13-
import ccos.log
14-
1511
GITHUB_ORGANIZATION = "creativecommons"
1612
GITHUB_USERNAME_DEFAULT = "cc-creativecommons-github-io-bot"
17-
18-
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
19-
LOG = logging.getLogger(log_name)
20-
ccos.log.reset_handler()
13+
LOG = logging.root
2114

2215

2316
def get_credentials():
@@ -37,22 +30,22 @@ def set_up_github_client():
3730
_, github_token = get_credentials()
3831
LOG.info("Setting up GitHub client...")
3932
github_client = Github(github_token)
40-
LOG.log(ccos.log.SUCCESS, "done.")
33+
LOG.success("done.")
4134
return github_client
4235

4336

4437
def get_cc_organization(github_client):
4538
LOG.info("Getting CC's GitHub organization...")
4639
try:
47-
cc = github_client.get_organization(GITHUB_ORGANIZATION)
40+
gh_org_cc = github_client.get_organization(GITHUB_ORGANIZATION)
4841
except BadCredentialsException as e:
4942
LOG.critical(
5043
f"{e.status} {e.data['message']} (see"
5144
f" {e.data['documentation_url']})"
5245
)
5346
sys.exit(1)
54-
LOG.log(ccos.log.SUCCESS, "done.")
55-
return cc
47+
LOG.success("done.")
48+
return gh_org_cc
5649

5750

5851
def get_team_slug_name(project_name, role):

0 commit comments

Comments
 (0)