Skip to content

Commit

Permalink
Add a script to list repos
Browse files Browse the repository at this point in the history
  • Loading branch information
bsweger committed Aug 21, 2024
1 parent 74652f1 commit d9208bc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ repos:
- id: detect-aws-credentials
args: [--allow-missing-credentials]
- id: detect-private-key
- repo: https://github.com/pre-commit/mirrors-mypy
rev: 'v1.10.1' # Use the sha / tag you want to point at
hooks:
- id: mypy
additional_dependencies: [types-all]
- repo: https://github.com/codespell-project/codespell
rev: v2.3.0
hooks:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dev = [
[project.entry-points."console_scripts"]
add_default_rulesets = "reichlab_repo_utils.add_repo_rulesets:main"
archive_repos = "reichlab_repo_utils.archive_repos:main"
list_repos = "reichlab_repo_utils.list_repos:main"

[build-system]
# Minimum requirements for the build system to execute.
Expand Down
7 changes: 4 additions & 3 deletions src/reichlab_repo_utils/archive_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ def archive_repo(org_name: str, session: requests.Session):

# Get all repositories in the organization
repos = get_all_repos(org_name, session)
# Only archive repos that are on our list and are not already archived
repos_to_update = [repo for repo in repos if (repo["name"] in ARCHIVE_REPO_LIST and repo["archived"] is False)]

# payload for updating the repo to archive status
repo_updates = {
"archived": True,
}

# Only archive repos that are on our list and are not already archived
repos_to_update = [repo for repo in repos if (repo["name"] in ARCHIVE_REPO_LIST and repo["archived"] is False)]

update_count = 0
for repo in repos_to_update:
repo_name = repo["name"]
Expand Down
87 changes: 87 additions & 0 deletions src/reichlab_repo_utils/list_repos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import os
from collections import namedtuple
from itertools import zip_longest

import requests
import structlog
from rich.console import Console
from rich.style import Style
from rich.table import Table

from reichlab_repo_utils.util.logs import setup_logging
from reichlab_repo_utils.util.repo import get_all_repos
from reichlab_repo_utils.util.session import get_session

setup_logging()
logger = structlog.get_logger()

GITHUB_ORG = "reichlab"


def list_repos(org_name: str, session: requests.Session):
"""
Archive repositories in the organization.
:param org_name: Name of the GitHub organization
:param session: Requests session for interacting with the GitHub API
"""

# Settings for the output columns when listing repo information
output_column_list = ["name", "created_at", "archived", "visibility", "id"]
output_column_colors = ["green", "magenta", "cyan", "blue", "yellow"]
OutputColumns = namedtuple(
"OutputColumns",
output_column_list,
)

# Create the output table and columns
console = Console()
table = Table(
title=f"Repositories in the {org_name} GitHub organization",
)
for col, color in zip_longest(output_column_list, output_column_colors, fillvalue="cyan"):
# add additional attributes, depending on the column
style_kwargs = {}
col_kwargs = {}
if col == "name":
col_kwargs = {"ratio": 4}
style_kwargs = {"link": True}

style = Style(color=color, **style_kwargs)
table.add_column(col, style=style, **col_kwargs)

repos = get_all_repos(org_name, session)
repo_count = len(repos)

for repo in repos:
r = OutputColumns(
name=f"[link={repo.get('html_url')}]{repo.get('name')}[/link]",
created_at=str(repo.get("created_at", "")),
archived=str(repo.get("archived", "")),
visibility=str(repo.get("visibility", "")),
id=str(repo.get("id", "")),
)
try:
table.add_row(*r)
except Exception as e:
logger.error(f"Error adding row for repo {r.name}: {e}")

logger.info("Repository report complete", count=repo_count)

console.print(table)


def main():
org_name = GITHUB_ORG
token = os.getenv("GITHUB_TOKEN")
if not token:
logger.error("GITHUB_TOKEN environment variable is required")
return

session = get_session(token)

list_repos(org_name, session)


if __name__ == "__main__":
main()

0 comments on commit d9208bc

Please sign in to comment.