diff --git a/Dockerfile b/Dockerfile index 7072ecb..38d718e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ FROM python:3.8-slim-buster +LABEL org.opencontainers.image.source https://github.com/github/stale_repos WORKDIR /action/workspace COPY requirements.txt stale_repos.py /action/workspace/ diff --git a/requirements.txt b/requirements.txt index afd6006..bb78d4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ github3.py==4.0.1 python-dotenv==1.0.0 +python_dateutil==2.8.2 diff --git a/stale_repos.py b/stale_repos.py index 11c15d1..cc07420 100755 --- a/stale_repos.py +++ b/stale_repos.py @@ -1,10 +1,12 @@ #!/usr/bin/env python +""" Find stale repositories in a GitHub organization. """ import os from datetime import datetime from os.path import dirname, join import github3 +from dateutil.parser import parse from dotenv import load_dotenv @@ -31,12 +33,15 @@ def main(): ghe = os.getenv("GH_ENTERPRISE_URL", default="").strip() token = os.getenv("GH_TOKEN") if ghe and token: - gh = github3.github.GitHubEnterprise(ghe, token=token) + github_connection = github3.github.GitHubEnterprise(ghe, token=token) elif token: - gh = github3.login(token=os.getenv("GH_TOKEN")) + github_connection = github3.login(token=os.getenv("GH_TOKEN")) else: raise ValueError("GH_TOKEN environment variable not set") + if not github_connection: + raise ValueError("Unable to authenticate to GitHub") + # Set the threshold for inactive days inactive_days_threshold = os.getenv("INACTIVE_DAYS") if not inactive_days_threshold: @@ -49,14 +54,14 @@ def main(): # Iterate over repos in the org, acquire inactive days, # and print out the repo url and days inactive if it's over the threshold (inactive_days) - for repo in gh.repositories_by(organization): - last_push_str = repo.pushed_at + for repo in github_connection.repositories_by(organization): + last_push_str = repo.pushed_at # type: ignore if last_push_str is None: continue - last_push = datetime.fromisoformat(last_push_str[:-1]) + last_push = parse(last_push_str) days_inactive = (datetime.now() - last_push).days if days_inactive > int(inactive_days_threshold): - print(f"{repo.html_url}: {days_inactive} days inactive") + print(f"{repo.html_url}: {days_inactive} days inactive") # type: ignore if __name__ == "__main__":