Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/github_actions/step-security/hard…
Browse files Browse the repository at this point in the history
…en-runner-6d3c2fe731c8f225990c8018cb71c337c0d9dfcd
  • Loading branch information
seyLu authored Jun 17, 2024
2 parents bb07c61 + eab9d6e commit bd00a5a
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/auto-update-pre-commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
egress-policy: audit

- name: Checkout repository
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v3.5.3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.5.3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v4.7.0
Expand All @@ -45,7 +45,7 @@ jobs:
run: pre-commit autoupdate

- name: Create PR
uses: peter-evans/create-pull-request@8500972a1322d52aebd0e3050bc201599f71661f # v5.0.2
uses: peter-evans/create-pull-request@ce008085c89860856de52c613ad894311f83c931 # v5.0.2
with:
token: ${{ secrets.SEYLUBOT_PAT }}
committer: GitHub <noreply@github.com>
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
egress-policy: audit

- name: Checkout repository
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v3.5.3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.5.3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
egress-policy: audit

- name: Checkout repository
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v3.5.3
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v3.5.3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v4.7.0
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
- prettier-plugin-toml@1.0.1

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
rev: v0.4.4
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
dependencies = [
"typer[all]==0.12.0",
"requests==2.31.0",
"requests==2.32.3",
"python-dotenv==1.0.1",
"PyYAML==6.0.1",
]
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# core
typer[all]==0.12.0
requests==2.31.0
requests==2.32.3
python-dotenv==1.0.1
PyYAML==6.0.1

# packaging
hatchling==1.22.5
hatchling==1.24.2
build==1.2.1
twine==5.0.0
twine==5.1.0
50 changes: 50 additions & 0 deletions src/ghlabel/__logger__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import logging
import os
from logging import Logger
from logging.config import fileConfig

import rich

from ghlabel.config import is_ghlabel_debug_mode

GHLABEL_LOGS_DIR: str = os.path.join("logs")


class GhlabelLogger:
def __init__(self) -> None:
self.logger: GhlabelLogger

if not os.path.isdir(GHLABEL_LOGS_DIR):
os.makedirs(GHLABEL_LOGS_DIR)
fileConfig(os.path.join(os.path.dirname(__file__), "logging.ini"))

def init(self, module_name: str) -> "GhlabelLogger":
"""NOTE: pass in __name__ as module name"""
logger: Logger = logging.getLogger(module_name)

if is_ghlabel_debug_mode():
logger.setLevel(level=logging.DEBUG)
else:
logger.setLevel(level=logging.ERROR)

self.logger = logger # type: ignore[assignment]
return self.logger

def exception(self, ex: Exception) -> None:
rich.print(
f"\nSomething went wrong. See logs ([blue underline]{GHLABEL_LOGS_DIR}[/blue underline]) for more details.\n"
)
rich.print(" [[red]Exception[/red]]:", str(ex))
self.logger.exception(ex)

def error(self, message: str) -> None:
self.logger.error(message)

def warning(self, message: str) -> None:
self.logger.warning(message)

def info(self, message: str) -> None:
self.logger.info(message)


ghlabel_logger: GhlabelLogger = GhlabelLogger()
21 changes: 12 additions & 9 deletions src/ghlabel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
__status__ = "Prototype"

import json
import logging
import os
import time
from enum import Enum
Expand All @@ -21,11 +20,9 @@
from rich.progress import Progress, SpinnerColumn, TextColumn

from ghlabel.__about__ import __version__
from ghlabel.utils.dump_label import DumpLabel
from ghlabel.utils.github_api import GithubApi
from ghlabel.config import set_ghlabel_debug_mode
from ghlabel.utils.github_api_types import GithubLabel
from ghlabel.utils.helpers import clear_screen, validate_env
from ghlabel.utils.setup_github_label import SetupGithubLabel


def parse_remove_labels(label_names: str | None) -> set[str] | None:
Expand Down Expand Up @@ -70,6 +67,7 @@ class RemoveAllChoices(str, Enum):
context_settings={
"help_option_names": ["-h", "--help"],
},
no_args_is_help=True,
)


Expand Down Expand Up @@ -153,6 +151,9 @@ def setup_labels( # noqa: PLR0913
),
] = False,
) -> None:
from ghlabel.utils.github_api import GithubApi
from ghlabel.utils.setup_github_label import SetupGithubLabel

if not token:
token = validate_env("GITHUB_TOKEN")
if not repo_owner:
Expand Down Expand Up @@ -244,6 +245,8 @@ def app_dump(
),
] = AppChoices.app.value, # type: ignore[assignment]
) -> None:
from ghlabel.utils.dump_label import DumpLabel

clear_screen()
with Progress(
SpinnerColumn(),
Expand All @@ -255,7 +258,9 @@ def app_dump(
DumpLabel.dump(labels_dir=labels_dir, new=new, ext=ext.value, app=app.value)
time.sleep(0.5)

rich.print(f"[green]Successfully[/green] dumped labels config to `{labels_dir}`.")
rich.print(
f"[green]Successfully[/green] dumped labels config to {os.path.join(os.getcwd(), labels_dir)}"
)


@app.callback() # type: ignore[misc]
Expand All @@ -275,15 +280,13 @@ def app_callback(
typer.Option(
"--debug",
"-D",
is_eager=True,
help="Enable debug mode and show logs.",
),
] = False,
) -> None:
"""Setup Github Labels from a yaml/json config file."""

if not debug:
logger = logging.getLogger("root")
logger.setLevel(logging.ERROR)
set_ghlabel_debug_mode(debug)


if __name__ == "__main__":
Expand Down
11 changes: 11 additions & 0 deletions src/ghlabel/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
g_GHLABEL_DEBUG_MODE = False


def set_ghlabel_debug_mode(is_debug: bool) -> None:
global g_GHLABEL_DEBUG_MODE # noqa: PLW0603
g_GHLABEL_DEBUG_MODE = is_debug


def is_ghlabel_debug_mode() -> bool:
global g_GHLABEL_DEBUG_MODE # noqa: PLW0602
return g_GHLABEL_DEBUG_MODE
2 changes: 1 addition & 1 deletion src/ghlabel/logging.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
keys=root

[logger_root]
level=DEBUG
level=INFO
handlers=screen,file

[formatters]
Expand Down
13 changes: 6 additions & 7 deletions src/ghlabel/utils/dump_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@
__status__ = "Prototype"

import json
import logging
import os
from dataclasses import dataclass
from logging.config import fileConfig
from pathlib import Path
from typing import TypedDict

import yaml

Path("logs").mkdir(exist_ok=True)
fileConfig(os.path.join(os.path.dirname(__file__), "../logging.ini"))
from ghlabel.__logger__ import GhlabelLogger, ghlabel_logger

logger: GhlabelLogger = ghlabel_logger.init(__name__)


@dataclass(frozen=True)
Expand Down Expand Up @@ -267,7 +266,7 @@ class GameDevLabels(Labels):
class DumpLabel:
@staticmethod
def _init_labels_dir(labels_dir: str = "labels") -> None:
logging.info(f"Initializing {labels_dir} dir.")
logger.info(f"Initializing {os.path.join(os.getcwd(), labels_dir)} dir.")
Path(labels_dir).mkdir(exist_ok=True)
for filename in os.listdir(labels_dir):
file_path: str = os.path.join(labels_dir, filename)
Expand All @@ -293,7 +292,7 @@ def dump(
label_file: str = os.path.join(labels_dir, f"{field.lower()}_labels.{ext}")

with open(label_file, "w+") as f:
logging.info(f"Dumping to {f.name}.")
logger.info(f"Dumping to {f.name}.")

if ext == "yaml":
print(
Expand All @@ -307,7 +306,7 @@ def dump(
elif ext == "json":
json.dump(labels, f, indent=2)

logging.info("Finished dumping of labels.")
logger.info("Finished dumping of labels.")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit bd00a5a

Please sign in to comment.