Skip to content

Commit 770bba6

Browse files
committed
reorganize push_data_to_ccos and improve error handling
1 parent 557dfb7 commit 770bba6

File tree

14 files changed

+238
-598
lines changed

14 files changed

+238
-598
lines changed

Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ ipython = "*"
1010
isort = "*"
1111

1212
[packages]
13+
asana = "*"
14+
emoji = "*"
15+
GitPython = "*"
1316
PyGithub = "*"
1417
PyYAML = "*"
1518

Pipfile.lock

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,87 @@ These are scripts used to maintain various pieces of CC's open source community
44
infrastructure.
55

66

7-
## Scripts
7+
## Workflows
8+
89

9-
### `normalize_repos.py`
10+
### Normalize Repos
1011

12+
- Workflow
13+
- Status: [![Normalize Repos][norm_badge]][norm_link]
14+
- Schedule: Hourly at 45 minutes past the hour (`**:45`)
15+
- YAML: [`normalize_repos.yml`][norm_pr_yml]
1116
- Script: `normalize_repos.py`
1217
- Common Modules: [`ccos/`](ccos/)
1318
- Specific Modules: [`ccos/norm/`](ccos/norm/)
14-
- Workflow
15-
- Status: [![Add Community PRs to Project][b1]][l1]
16-
- Schedule: Hourly at 45 minutes past the hour
17-
- YAML: [`normalize_repos.yml`][norm_pr_yml]
1819
- Required Environment Variables:
1920
- `ADMIN_GITHUB_TOKEN`
2021

21-
This script ensures that all active repositories in the creativecommons GitHub
22-
organization are consistent in the following ways:
22+
This workflow ensures that all active repositories in the creativecommons
23+
GitHub organization are consistent in the following ways:
2324
- They have all the labels defined in `labels.yml` present.
2425
- They have standard branch protections set up (with some exceptions).
2526

2627
This script will only update color and description of existing labels or create
2728
new labels. It will never delete labels.
2829

2930
[norm_pr_yml]: .github/workflows/normalize_repos.yml
30-
[b2]: https://github.com/creativecommons/ccos-scripts/actions/workflows/normalize_repos.yml/badge.svg
31-
[l2]: https://github.com/creativecommons/ccos-scripts/actions/workflows/normalize_repos.yml
31+
[norm_badge]: https://github.com/creativecommons/ccos-scripts/actions/workflows/normalize_repos.yml/badge.svg
32+
[norm_link]: https://github.com/creativecommons/ccos-scripts/actions/workflows/normalize_repos.yml
3233

3334

34-
## Environment Variables
35+
### Push data to CC Open Source
36+
37+
- Workflow
38+
- Status: [![Push data to CC Open Source][data_badge]][data_link]
39+
- Schedule: Daily at midnight:15 (`00:15`)
40+
- YAML: [`push_data_to_ccos.yml`][push_ccos_yml]
41+
- Script: `push_data_to_ccos.py`
42+
- Common Modules: [`ccos/`](ccos/)
43+
- Specific Modules: [`ccos/data/`](ccos/data/)
44+
- Required Environment Variables:
45+
- `ADMIN_ASANA_TOKEN`
46+
- `ADMIN_GITHUB_TOKEN`
3547

48+
This workflow retreives data from Asana, formats it as a lektor databag, and
49+
pushes it to CC Open Source website source repository:
50+
- Data Source: [Community Team Tracking - Asana][asana] (limited access)
51+
- Data Destination:
52+
- [`creativecommons.github.io-source`][ccos_source]
53+
- [`databags/community_team_members.json`][db_community]
54+
- [`databags/repos.json`][db_repos]
3655

37-
### `ADMIN_GITHUB_TOKEN`
56+
The destination data is used by the following pages:
57+
- [Community Team Members — Creative Commons Open Source][ctlistpage]
58+
- [Open Source Projects — Creative Commons Open Source][osproj]
3859

39-
GitHub token with admin permissions to the `creativecommons` GitHub
40-
organization.
60+
[data_badge]: https://github.com/creativecommons/ccos-scripts/actions/workflows/push_data_to_ccos.yml/badge.svg
61+
[data_link]: https://github.com/creativecommons/ccos-scripts/actions/workflows/push_data_to_ccos.yml
62+
[ctlistpage]: https://opensource.creativecommons.org/community/community-team/members/
63+
[osproj]: https://opensource.creativecommons.org/contributing-code/projects/
64+
[asana]: https://app.asana.com/0/1172465506923657/list
65+
[ccos_source]: https://github.com/creativecommons/creativecommons.github.io-source
66+
[db_community]: https://github.com/creativecommons/creativecommons.github.io-source/blob/main/databags/community_team_members.json
67+
[db_repos]: https://github.com/creativecommons/creativecommons.github.io-source/blob/main/databags/repos.json
4168

4269

43-
## Workflows
70+
## Environment Variables
71+
72+
- `ADMIN_ASANA_TOKEN`: Asana token with access to the Creative Commons Asana
73+
organization
74+
- `ADMIN_GITHUB_TOKEN`: GitHub token with admin permissions to the
75+
`creativecommons` GitHub organization
76+
77+
78+
## Old Workflows Documentation
4479

4580
| Workflow Name/Status | YML File Name | Workflow Purpose |
4681
| -------------------- | ------------- | ---------------- |
4782
| [![Add Community PRs to Project][b1]][l1] | [`add_community_pr.yml`][community_pr_yml] | Runs hourly at 5 minutes past every hour UTC and adds new Vocabulary issues to [Vocabulary: In Progress][vocab_in_progress] |
48-
| [![Push data to CC Open Source][b3]][l3] | [`push_data_to_ccos.yml`][push_ccos_yml] | Runs daily at 00:00 UTC and whenever someone pushes to the main branch and uses [`push_data_to_ccos`][push_to_ccos] |
4983
| [![Sync Community Teams with GitHub][b4]][l4] | [`sync_community_team.yml`][sync_team_yml] | Runs daily at 00:30 UTC and whenever someone pushes to the main branch and uses [`sync_community_team`][sync_team] |
5084
| [![Track new issues in backlog][b5]][l5] | [`track_backlog.yml`][track_backlog] | Runs hourly at 45 minutes past every hour UTC and adds PRs to [Active Sprint: Code Review][active_sprint] and new issues to [Backlog: Pending Review][backlog_pending]. Uses [dhruvkb/issue-projector][issue-projector]. |
5185

5286
[b1]: https://github.com/creativecommons/ccos-scripts/actions/workflows/add_community_pr.yml/badge.svg
5387
[l1]: https://github.com/creativecommons/ccos-scripts/actions/workflows/add_community_pr.yml
54-
55-
[b3]: https://github.com/creativecommons/ccos-scripts/actions/workflows/push_data_to_ccos.yml/badge.svg
56-
[l3]: https://github.com/creativecommons/ccos-scripts/actions/workflows/push_data_to_ccos.yml
5788
[b4]: https://github.com/creativecommons/ccos-scripts/actions/workflows/sync_community_team.yml/badge.svg
5889
[l4]: https://github.com/creativecommons/ccos-scripts/actions/workflows/sync_community_team.yml
5990
[b5]: https://github.com/creativecommons/ccos-scripts/actions/workflows/track_backlog.yml/badge.svg

push_data_to_ccos/get_community_team_data.py renamed to ccos/data/get_community_team_data.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,46 @@
66
"""
77

88
# Standard library
9+
import inspect
910
import logging
1011
import os
12+
import sys
1113

1214
# Third-party
1315
import asana
1416

1517
# First-party/Local
16-
import log
18+
from ccos import log
1719

18-
ASANA_CLIENT = asana.Client.access_token(os.environ["ADMIN_ASANA_TOKEN"])
20+
ASANA_WORKSPACE_GID = "133733285600979"
1921
ASANA_PROJECT_GID = "1172465506923661"
2022

21-
log.set_up_logging()
22-
logger = logging.getLogger("push_data_to_ccos")
23+
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
24+
logger = logging.getLogger(log_name)
2325
log.reset_handler()
2426

2527

26-
def generate_databag():
28+
def setup_asana_client():
29+
logger.log(logging.INFO, "Setting up Asana client...")
30+
try:
31+
asana_token = os.environ["ADMIN_ASANA_TOKEN"]
32+
except KeyError:
33+
logger.critical("missin ADMIN_ASANA_TOKEN environment variable")
34+
sys.exit(1)
35+
asana_client = asana.Client.access_token(asana_token)
36+
try:
37+
# Perform simple API operation to test authentication
38+
asana_client.workspaces.get_workspace(ASANA_WORKSPACE_GID)
39+
except asana.error.NoAuthorizationError as e:
40+
logger.critical(
41+
f"{e.status} {e.message} (is ADMIN_ASANA_TOKEN valid?)"
42+
)
43+
sys.exit(1)
44+
logger.log(logging.INFO, "done.")
45+
return asana_client
46+
47+
48+
def generate_databag(asana_client):
2749
"""
2850
This method pulls the team members from Asana and
2951
loads them into the databag after a little
@@ -57,10 +79,11 @@ def generate_databag():
5779
]
5880
}
5981
"""
82+
6083
logger.log(logging.INFO, "Pulling from Asana and generating databag...")
6184
databag = {"projects": [], "community_builders": []}
6285

63-
members = ASANA_CLIENT.tasks.find_by_section(
86+
members = asana_client.tasks.find_by_section(
6487
ASANA_PROJECT_GID, opt_fields=["name", "custom_fields"]
6588
)
6689
logger.log(logging.INFO, "Team members pulled.")
@@ -164,5 +187,5 @@ def get_custom_field(task, field_name):
164187
return field["text_value"]
165188

166189

167-
def get_community_team_data():
168-
return prune_databag(sort_databag(generate_databag()))
190+
def get_community_team_data(asana_client):
191+
return prune_databag(sort_databag(generate_databag(asana_client)))

push_data_to_ccos/get_repo_data.py renamed to ccos/data/get_repo_data.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
# vim: set fileencoding=utf-8:
33

44
# Standard library
5+
import inspect
56
import logging
7+
import os.path
68

79
# Third-party
810
import emoji
@@ -11,13 +13,13 @@
1113
from github.GithubException import GithubException, UnknownObjectException
1214

1315
# First-party/Local
14-
import log
15-
from push_data_via_git import GITHUB_ORGANIZATION, GITHUB_TOKEN
16+
from ccos import log
17+
from ccos.data.push_data_via_git import GITHUB_ORGANIZATION, GITHUB_TOKEN
1618

1719
CC_METADATA_FILE_NAME = ".cc-metadata.yml"
1820

19-
log.set_up_logging()
20-
logger = logging.getLogger("push_data_to_ccos")
21+
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
22+
logger = logging.getLogger(log_name)
2123
log.reset_handler()
2224

2325

push_data_to_ccos/push_data_via_git.py renamed to ccos/data/push_data_via_git.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# vim: set fileencoding=utf-8:
33

44
# Standard library
5+
import inspect
56
import json
67
import logging
78
import os
@@ -10,7 +11,7 @@
1011
import git
1112

1213
# First-party/Local
13-
import log
14+
from ccos import log
1415

1516
GIT_USER_NAME = "CC creativecommons.github.io Bot"
1617
GIT_USER_EMAIL = "cc-creativecommons-github-io-bot@creativecommons.org"
@@ -29,8 +30,8 @@
2930
GIT_WORKING_DIRECTORY = f"{WORKING_DIRECTORY}/{GITHUB_REPO_NAME}"
3031
JSON_FILE_DIRECTORY = f"{GIT_WORKING_DIRECTORY}/databags"
3132

32-
log.set_up_logging()
33-
logger = logging.getLogger("push_data_to_ccos")
33+
log_name = os.path.basename(os.path.splitext(inspect.stack()[-1].filename)[0])
34+
logger = logging.getLogger(log_name)
3435
log.reset_handler()
3536

3637

0 commit comments

Comments
 (0)