Skip to content

Commit 8f5786e

Browse files
authored
Merge pull request #232 from creativecommons/enable-workflows
Enable workflows
2 parents 3f8c162 + 2017d33 commit 8f5786e

File tree

5 files changed

+160
-12
lines changed

5 files changed

+160
-12
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Enable workflows
2+
on:
3+
schedule:
4+
- cron: '22 22 1 * *' # Monthly on the 1st at 22:22
5+
workflow_dispatch:
6+
7+
jobs:
8+
normalize_repos:
9+
runs-on: ubuntu-latest
10+
steps:
11+
12+
# https://github.com/actions/setup-python
13+
- name: Setup Python 3.11
14+
uses: actions/setup-python@v5
15+
with:
16+
python-version: '3.11'
17+
18+
- name: Install system dependencies
19+
run: |
20+
python -m pip install --upgrade pip
21+
python -m pip install pipenv
22+
23+
# https://github.com/actions/checkout
24+
- uses: actions/checkout@v4
25+
with:
26+
token: ${{ secrets.ADMIN_GITHUB_TOKEN }}
27+
28+
- name: Install app dependencies
29+
run: pipenv sync --system
30+
31+
- name: Run script with token in env
32+
run: ./enable_workflows.py \
33+
ccos_scripts
34+
env:
35+
ADMIN_GITHUB_TOKEN: ${{ secrets.ADMIN_GITHUB_TOKEN }}

.github/workflows/normalize_repos.yml

-7
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,3 @@ jobs:
3939
with:
4040
name: invalid-issue-report
4141
path: /tmp/invalid_issues.yml
42-
43-
# # https://github.com/gautamkrishnar/keepalive-workflow
44-
# - uses: gautamkrishnar/keepalive-workflow@v2
45-
# with:
46-
# committer_username: cc-open-source-bot
47-
# committer_email: opensource@creativecommons.org
48-
# time_elapsed: 46

README.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ infrastructure.
1010
- [![Manage issues and pull requests in projects][issues_badge]][issues_link]
1111
- [![Normalize Repos][norm_badge]][norm_link]
1212
- [![Push data to CC Open Source][data_badge]][data_link]
13+
- [![Enable workflows][enable_badge]][enable_link]
1314

1415
[teams_badge]: https://github.com/creativecommons/ccos-scripts/actions/workflows/sync_community_teams.yml/badge.svg
1516
[teams_link]: #sync-community-teams-with-github
@@ -19,6 +20,8 @@ infrastructure.
1920
[norm_link]: #normalize-repos
2021
[data_badge]: https://github.com/creativecommons/ccos-scripts/actions/workflows/push_data_to_ccos.yml/badge.svg
2122
[data_link]: #push-data-to-cc-open-source
23+
[enable_badge]: https://github.com/creativecommons/ccos-scripts/actions/workflows/enable_workflows.yml/badge.svg
24+
[enable_link]: #enable-workflows
2225

2326

2427
## Code of conduct
@@ -113,8 +116,6 @@ in a GitHub project:
113116
| | File: | [`normalize_repos.py`][norm_file] |
114117
| | Common Modules: | [`ccos/`](ccos/) |
115118
| | Specific Modules: | [`ccos/norm/`](ccos/norm/) |
116-
| **Action** | | |
117-
| | | [gautamkrishnar/keepalive-workflow][keepalive] |
118119
| **Env** | | |
119120
| | Required: | `ADMIN_GITHUB_TOKEN` |
120121

@@ -128,7 +129,6 @@ new labels. It will never delete labels.
128129

129130
[norm_pr_yml]: .github/workflows/normalize_repos.yml
130131
[norm_file]: normalize_repos.py
131-
[keepalive]: https://github.com/gautamkrishnar/keepalive-workflow
132132

133133

134134
### Push data to CC Open Source
@@ -167,6 +167,28 @@ The destination data is used by the following pages:
167167
[db_repos]: https://github.com/creativecommons/ccos-website-source/blob/main/databags/repos.json
168168

169169

170+
### Eanble Workflows
171+
172+
| **Workflow** | | |
173+
| -- | --: | --- |
174+
| | Schedule: | Monthly on the 1st at 22:22 |
175+
| | YAML: | [`enable_workflows.yml`][enable_workflows_yml] |
176+
| **Script** | | |
177+
| | File: | [`enable_workflows.py`][enable_workflows_file] |
178+
| | Common Modules: | [`ccos/`](ccos/) |
179+
| **Env** | | |
180+
| | Required: | `ADMIN_GITHUB_TOKEN` |
181+
182+
Enable GitHub Action workflows for specified repositories (ensures that they
183+
are not disabled due to inactivity).
184+
185+
For more information, see [Prevent scheduled GitHub Actions from becoming disabled - Stack Overflow][prevent_scheduled].
186+
187+
[enable_workflows_yml]: .github/workflows/enable_workflows.yml
188+
[enable_workflows_file]: enable_workflows.py
189+
[prevent_scheduled]: https://stackoverflow.com/questions/67184368/prevent-scheduled-github-actions-from-becoming-disabled
190+
191+
170192
## Environment Variables
171193

172194
- `ADMIN_ASANA_TOKEN`: Asana token with access to the Creative Commons Asana

enable_workflows.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env python3
2+
# vim: set fileencoding=utf-8:
3+
"""
4+
Enable GitHub Action workflows for specified repositories (ensures that they
5+
are not disabled due to inactivity).
6+
"""
7+
8+
# Standard library
9+
import argparse
10+
import sys
11+
import traceback
12+
13+
# First-party/Local
14+
import ccos.log
15+
from ccos import gh_utils
16+
17+
LOG = ccos.log.setup_logger()
18+
19+
20+
class ScriptError(Exception):
21+
def __init__(self, message, code=None):
22+
self.code = code if code else 1
23+
message = "({}) {}".format(self.code, message)
24+
super(ScriptError, self).__init__(message)
25+
26+
27+
def setup():
28+
"""
29+
Instantiate and configure argparse and logging.
30+
31+
Return argsparse namespace.
32+
"""
33+
ap = argparse.ArgumentParser(description=__doc__)
34+
ap.add_argument(
35+
"-n",
36+
"--dryrun",
37+
action="store_true",
38+
help="dry run: do not make any changes",
39+
)
40+
ap.add_argument(
41+
"repos",
42+
nargs="*",
43+
help="repository to act on (multiple repositories may be specified)",
44+
metavar="REPOSITORY",
45+
)
46+
args = ap.parse_args()
47+
if args.dryrun:
48+
args.dryrun = "dryrun (no-op): "
49+
else:
50+
args.dryrun = ""
51+
if not args.repos:
52+
raise ap.error("at least one (1) REPOSITORY must be specified")
53+
return args
54+
55+
56+
def get_workflows(args, repos):
57+
workflows = []
58+
for repo in repos:
59+
for workflow in repo.get_workflows():
60+
workflow.repo_name = repo.name
61+
workflows.append(workflow)
62+
return workflows
63+
64+
65+
def enable_workflows(args, workflows):
66+
for workflow in workflows:
67+
LOG.info(
68+
f"{args.dryrun}Enabling {workflow.repo_name}:"
69+
f' "{workflow.name}"'
70+
)
71+
if not args.dryrun:
72+
workflow.enable()
73+
74+
75+
def main():
76+
args = setup()
77+
github_client = gh_utils.setup_github_rest_client()
78+
gh_org_cc = gh_utils.get_cc_organization(github_client)
79+
repos = gh_utils.get_select_repos(args, gh_org_cc)
80+
workflows = get_workflows(args, repos)
81+
enable_workflows(args, workflows)
82+
83+
84+
if __name__ == "__main__":
85+
try:
86+
main()
87+
except SystemExit as e:
88+
sys.exit(e.code)
89+
except KeyboardInterrupt:
90+
LOG.info("Halted via KeyboardInterrupt.")
91+
sys.exit(130)
92+
except ScriptError:
93+
error_type, error_value, error_traceback = sys.exc_info()
94+
LOG.critical(f"{error_value}")
95+
sys.exit(error_value.code)
96+
except Exception:
97+
LOG.error(f"Unhandled exception: {traceback.format_exc()}")
98+
sys.exit(1)

normalize_repos.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def update_branch_protection(repo):
110110
config = load_branch_protection_config()
111111
exempt_repositories = config["EXEMPT_REPOSITORIES"]
112112
required_status_check_map = config["REQUIRED_STATUS_CHECK_MAP"]
113-
113+
114114
exempt_users = config.get("EXEMPT_USERS", {}).get(repo.name, [])
115115

116116
if repo.name not in exempt_repositories and is_engineering_project(repo):
@@ -143,7 +143,7 @@ def update_branch_protection(repo):
143143
def update_branches(args, repos):
144144
if args.skip_branches:
145145
return
146-
146+
147147
LOG.info("Evaluting repositories for branch protections...")
148148
for repo in repos:
149149
update_branch_protection(repo)

0 commit comments

Comments
 (0)