Skip to content

Commit

Permalink
Add command-line options to the command-line interface
Browse files Browse the repository at this point in the history
These options will allow one to provide all required values as command-
line options. These options will also be the preferred source with any
environment variables being fallback options. This results in no impact
to how this project is currently run but allows for more convenient
usage on a local machine.
  • Loading branch information
mcdonnnj committed Jul 10, 2023
1 parent 8d10aaf commit 0bd9aba
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 11 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def get_version(version_file):
include_package_data=True,
install_requires=[
"Babel",
"docopt",
"PyGithub",
"python-dateutil",
"pytimeparse",
Expand Down
100 changes: 89 additions & 11 deletions src/apb/entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
"""GitHub Action to rebuild respositories that haven't been built in a while."""
"""Tool to rebuild repositories that haven't been built in a while.
This tool allows you to use environment variables to provide necessary information. Any
options provided on the command-line will be used instead of any environment variables
that have been set.
Usage:
apb [--log-level <level>] [--github-token <token>] [--repo-query <query>] [--workflow-name <workflow>] [--build-age <age>] [--event-type >event>] [--max-rebuilds <limit>] [--output-dir <dir>] [--output-file <file>]
Options:
--github-token <token> The GitHub Personal Access Token to use for authentication.
--repo-query <query> The query used to find GitHub repositories to check.
--workflow-name <workflow> The name of the workflow that should be checked by the
tool.
--build-age <age> The maximum age of the last workflow run before a repository
dispatch is created.
--event-type >event> The type of event to send when a repository dispatch is
created.
--max-rebuilds <limit> The maximum number of rebuilds dispatches to create in a
single run of the tool.
--output-dir <dir> The directory that should house the created state file.
--output-file <file> The name to use when creating the state file.
--log-level <level> The log level to use if specified. Valid values are
"debug", "info", "warning", "error", and "critical".
[default: info]
"""

# Standard Python Libraries
from datetime import datetime
Expand All @@ -7,16 +32,19 @@
import os
from pathlib import Path
import sys
from typing import Generator, Optional
from typing import Dict, Generator, Optional

# Third-Party Libraries
from babel.dates import format_timedelta
from dateutil.parser import isoparse
from dateutil.relativedelta import relativedelta
import docopt
from github import Github, Repository
import pytimeparse
import requests

from ._version import __version__


def get_repo_list(
g: Github, repo_query: str
Expand Down Expand Up @@ -50,18 +78,68 @@ def get_last_run(

def main() -> None:
"""Parse evironment and perform requested actions."""
args: Dict[str, str] = docopt.docopt(__doc__, version=__version__)
# Get the list of logging levels supported by the logging library. Filter out
# values that are deprecated or not actual logging levels.
log_levels = [
level.lower()
for level in list(logging._nameToLevel.keys() - ["NOTSET", "WARN"])
]
if args["--log-level"].lower() not in log_levels:
print(
"Possible values for --log-level are "
+ ", ".join(log_levels[:-1])
+ ", and "
+ log_levels[-1],
file=sys.stderr,
)
sys.exit(-1)
# Set up logging
logging.basicConfig(format="%(levelname)s %(message)s", level="INFO")
logging.basicConfig(
format="%(levelname)s %(message)s", level=args["--log-level"].upper()
)

# Get inputs from the environment
access_token: Optional[str] = os.environ.get("INPUT_ACCESS_TOKEN")
build_age: Optional[str] = os.environ.get("INPUT_BUILD_AGE")
event_type: Optional[str] = os.environ.get("INPUT_EVENT_TYPE")
github_workspace_dir: Optional[str] = os.environ.get("GITHUB_WORKSPACE")
max_rebuilds: int = int(os.environ.get("INPUT_MAX_REBUILDS", 10))
repo_query: Optional[str] = os.environ.get("INPUT_REPO_QUERY")
workflow_id: Optional[str] = os.environ.get("INPUT_WORKFLOW_ID")
write_filename: Optional[str] = os.environ.get("INPUT_WRITE_FILENAME", "apb.json")
access_token: Optional[str] = (
args["--github-token"]
if args["--github-token"] is not None
else os.environ.get("INPUT_ACCESS_TOKEN")
)
build_age: Optional[str] = (
args["--build-age"]
if args["--build-age"] is not None
else os.environ.get("INPUT_BUILD_AGE")
)
event_type: Optional[str] = (
args["--event-type"]
if args["--event-type"] is not None
else os.environ.get("INPUT_EVENT_TYPE")
)
github_workspace_dir: Optional[str] = (
args["--output-dir"]
if args["--output-dir"] is not None
else os.environ.get("GITHUB_WORKSPACE")
)
max_rebuilds: int = int(
args["--max-rebuilds"]
if args["--max-rebuilds"] is not None
else os.environ.get("INPUT_MAX_REBUILDS", "10")
)
repo_query: Optional[str] = (
args["--repo-query"]
if args["--repo-query"] is not None
else os.environ.get("INPUT_REPO_QUERY")
)
workflow_id: Optional[str] = (
args["--workflow-name"]
if args["--workflow-name"] is not None
else os.environ.get("INPUT_WORKFLOW_ID")
)
write_filename: Optional[str] = (
args["--output-file"]
if args["--output-file"] is not None
else os.environ.get("INPUT_WRITE_FILENAME", "apb.json")
)

# sanity checks
if access_token is None:
Expand Down

0 comments on commit 0bd9aba

Please sign in to comment.