Created
March 26, 2025 15:30
-
-
Save flodolo/227605c4fcf4f6fb09a0ad11dfcd3edf to your computer and use it in GitHub Desktop.
Re-enable disabled GitHub workflows in an org
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Requires PyGithub | |
from github import Github | |
import configparser | |
import os | |
import requests | |
import sys | |
def get_repos(gh, organization): | |
""" | |
Get all repos for an organization. | |
""" | |
repos = [] | |
org_repos = gh.get_organization(organization).get_repos() | |
valid_repos = [r for r in org_repos if not r.fork and not r.archived] | |
repos += valid_repos | |
return repos | |
def get_disabled_workflows(repos): | |
""" | |
Get all the workflows with a `disabled_inactivity` state. | |
""" | |
disabled_workflows = [] | |
for repo in repos: | |
workflows_to_enable = [ | |
w for w in repo.get_workflows() if w.state == "disabled_inactivity" | |
] | |
disabled_workflows += workflows_to_enable | |
return disabled_workflows | |
def enable_workflows(token, workflows): | |
""" | |
Enable all the workflows. | |
""" | |
for workflow in workflows: | |
print(f"Enabling workflow {workflow.url}") | |
enable_url = f"{workflow.url}/enable" | |
header = {"Authorization": f"Bearer {token}"} | |
result = requests.put(enable_url, headers=header) | |
if result.content: | |
print(f"Result: {result.content}") | |
def main(): | |
# Read config file in the parent folder | |
config_file = os.path.join( | |
os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)), | |
"api_config.env", | |
) | |
config = configparser.ConfigParser() | |
config.read(config_file) | |
github_token = config.get("KEYS", "GITHUB_TOKEN") | |
organization = "mozilla-l10n" | |
gh = Github(login_or_token=github_token) | |
repos = get_repos(gh, organization) | |
if not repos: | |
sys.exit("No repos found") | |
print(f"Found {len(repos)} repositories in {organization}") | |
disabled_workflows = get_disabled_workflows(repos) | |
if not disabled_workflows: | |
sys.exit("No disabled workflows found") | |
print(f"Found {len(disabled_workflows)} disabled workflows") | |
enable_workflows(github_token, disabled_workflows) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment