Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 157bcc9a authored by Dorian Turba's avatar Dorian Turba
Browse files

logging rework

parent 7b4d03df
No related branches found
No related tags found
No related merge requests found
import functools
import pathlib
import typing
import gitlab.v4.objects
import rich.panel
import rich.table
import typer
import release_by_changelog.exc
import release_by_changelog.logging as logging
from release_by_changelog.app import app
from release_by_changelog.services.changelog import find_changelog_file, last_changelog
from release_by_changelog.services.gitlab import get_project as get_project_
from release_by_changelog.services.write import post
from release_by_changelog.typings_ import ChangelogEntry
err_console = rich.console.Console(stderr=True)
panel = functools.partial(
rich.panel.Panel,
title_align="left",
subtitle="release_by_changelog --help",
subtitle_align="left",
expand=False,
border_style="red",
)
@app.command()
def release(
......@@ -65,32 +52,27 @@ def release(
is_flag=True,
),
) -> None:
console = rich.console.Console()
token = get_token(ci_job_token, token)
changelog_path = get_changelog_path(
changelog_path, console, host, project, ref, token
)
changelog_entry = last_changelog(changelog_path, console)
project_, project_name = get_project(console, host, project, token)
changelog_path = get_changelog_path(changelog_path, host, project, ref, token)
changelog_entry = last_changelog(changelog_path)
project_, project_name = get_project(host, project, token)
if interact:
typer.confirm("Do you confirm release?", default=True, abort=True)
publish(changelog_entry, console, project_, project_name, ref, tag_only)
publish(changelog_entry, project_, project_name, ref, tag_only)
def get_project(
console: rich.console.Console,
host: str,
project: str,
token: str,
) -> tuple[gitlab.v4.objects.Project, str]:
try:
project_, project_name = get_project_(token, host, project, console)
project_, project_name = get_project_(token, host, project)
except release_by_changelog.exc.UnauthorizedError:
err_console.print(
panel(
logging.err_console.print(
logging.err_panel(
"Possible remediation:\n"
"\t- Check if the provided token is correct.\n"
"\t- Check if the provided token has the correct permissions and "
......@@ -100,8 +82,8 @@ def get_project(
)
raise typer.Exit(code=1)
except release_by_changelog.exc.ProjectNotFoundError:
err_console.print(
panel(
logging.err_console.print(
logging.err_panel(
"Possible remediation:\n"
"\t- Provide the project ID. To find the project ID, go to the project "
"page, click on the three vertical dot button on the top right corner "
......@@ -122,19 +104,16 @@ def get_project(
def get_changelog_path(
changelog_path: pathlib.Path,
console: rich.console.Console,
host: str,
project: str,
ref: str,
token: str,
) -> pathlib.Path:
try:
changelog_path = find_changelog_file(
changelog_path, token, host, project, ref, console
)
changelog_path = find_changelog_file(changelog_path, token, host, project, ref)
except release_by_changelog.exc.UnauthorizedError:
err_console.print(
panel(
logging.err_console.print(
logging.err_panel(
"Possible remediation:\n"
"\t- Check if the provided token is correct.\n"
"\t- Check if the provided token has the correct permissions and "
......@@ -144,8 +123,8 @@ def get_changelog_path(
)
raise typer.Exit(code=1)
except release_by_changelog.exc.ProjectNotFoundError:
err_console.print(
panel(
logging.err_console.print(
logging.err_panel(
"Possible remediation:\n"
"\t- Provide the project ID. To find the project ID, go to the project "
"page, click on the three vertical dot button on the top right corner "
......@@ -176,8 +155,8 @@ def get_token(ci_job_token: str | None, token: str | None) -> str:
if ci_job_token is not None:
return ci_job_token
err_console.print(
panel(
logging.err_console.print(
logging.err_panel(
"You need to provide a PRIVATE_TOKEN or a CI_JOB_TOKEN",
title="Error: Missing token",
)
......@@ -187,13 +166,12 @@ def get_token(ci_job_token: str | None, token: str | None) -> str:
def publish(
changelog_entry: ChangelogEntry,
console: rich.console.Console,
project_: gitlab.v4.objects.Project,
project_name: str,
ref: str,
tag_only: bool,
) -> None:
console.print(
logging.info(
f"Creating release [bold cyan]{changelog_entry.version}[/bold cyan] for "
f"project [bold cyan]{project_name}[/bold cyan]"
)
......@@ -205,8 +183,8 @@ def publish(
project_.tags if tag_only else project_.releases, changelog_entry, ref
)
except release_by_changelog.exc.UnauthorizedError:
err_console.print(
panel(
logging.error(
logging.err_panel(
"Possible remediation:\n"
"\t- Check if the provided token is correct.\n"
"\t- Check if the provided token has the correct permissions and "
......@@ -216,8 +194,8 @@ def publish(
)
raise typer.Exit(code=1)
except release_by_changelog.exc.TargetAlreadyExistError:
err_console.print(
panel(
logging.error(
logging.err_panel(
f"It looks like the {target} [bold cyan]{changelog_entry.version}"
f"[/bold cyan] already exists.\n"
"Possible remediation: Bump the version in the changelog file.",
......@@ -225,7 +203,7 @@ def publish(
)
)
raise typer.Exit(code=1)
console.print(f"Release created: {result}", style="bold green")
logging.success(f"Release created: {result}")
if __name__ == "__main__":
......
import functools
import rich.panel
import rich.table
_console = rich.console.Console()
success = functools.partial(_console.print, style="bold green")
info = functools.partial(_console.print)
warn = functools.partial(_console.print, style="yellow")
_err_console = rich.console.Console(stderr=True)
err_panel = functools.partial(
rich.panel.Panel,
title_align="left",
subtitle="release_by_changelog --help",
subtitle_align="left",
expand=False,
border_style="red",
)
error = functools.partial(_err_console.print, style="bold red")
......@@ -8,12 +8,19 @@ import re
import typing
import gitlab.v4.objects
import rich.console
import typer
from release_by_changelog import logging
from release_by_changelog.services.gitlab import get_project
from release_by_changelog.typings_ import ChangelogEntry
regex: typing.Final = re.compile(
r"^## \[(?P<version>(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]"
r"\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*"
r"|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA"
r"-Z-]+)*))?)\]"
)
def _extract_last_version(f: typing.TextIO) -> str:
for lines in f:
......@@ -26,18 +33,13 @@ def _extract_last_version(f: typing.TextIO) -> str:
raise ValueError("No changelog entry found")
def last_changelog(
changelog_path: pathlib.Path, console: rich.console.Console
) -> ChangelogEntry:
def last_changelog(changelog_path: pathlib.Path) -> ChangelogEntry:
"""Extract the last changelog entry from the changelog file."""
console.print(f"Processing [bold cyan]{changelog_path}[/bold cyan]")
logging.info(f"Processing [bold cyan]{changelog_path}[/bold cyan]")
with changelog_path.open() as f:
version = _extract_last_version(f)
body = _extract_body(f)
console.print(
"[bold green]Found changelog entry:[/bold green] "
f"[bold cyan]{version}[/bold cyan]"
)
logging.success(f"Found changelog entry: [bold cyan]{version}[/bold cyan]")
return ChangelogEntry(version=version, body=body)
......@@ -47,7 +49,6 @@ def find_changelog_file(
host: str,
project: str,
ref: str,
console: rich.console.Console,
) -> pathlib.Path:
"""
Find usable changelog file path.
......@@ -57,26 +58,22 @@ def find_changelog_file(
:raises NotImplementedError: If the OS is not supported.
"""
console.print(f"Look for local [bold cyan]{changelog_path}[/bold cyan]")
logging.info(f"Look for local [bold cyan]{changelog_path}[/bold cyan]")
if changelog_path.exists():
console.print(
f"[bold green]Found local[/bold green] "
f"[bold cyan]{changelog_path}[/bold cyan]"
)
logging.success(f"Found local [bold cyan]{changelog_path}[/bold cyan]")
return changelog_path
console.print(
f"[yellow]Local [bold cyan]{changelog_path}[/bold cyan] file not found, looking"
" for file in the remote project files[/yellow]"
logging.warn(
f"Local [bold cyan]{changelog_path}[/bold cyan] file not found, looking for "
"file in the remote project files"
)
project_, _ = get_project(
token,
host,
project,
console,
)
changelog_file = _get_remote_changelog_file(changelog_path, console, project_, ref)
changelog_file = _get_remote_changelog_file(changelog_path, project_, ref)
tmp_file = _save_remote_changelog_file(changelog_file, changelog_path)
return tmp_file
......@@ -102,7 +99,6 @@ def _save_remote_changelog_file(
def _get_remote_changelog_file(
changelog_path: pathlib.Path,
console: rich.console.Console,
project_: gitlab.v4.objects.Project,
ref: str,
) -> gitlab.v4.objects.ProjectFile:
......@@ -112,15 +108,14 @@ def _get_remote_changelog_file(
)
except gitlab.GitlabGetError as e:
if e.response_code == http.HTTPStatus.NOT_FOUND:
console.print(
logging.error(
f"[bold red]Changelog file {changelog_path} not found in the remote "
f"project files[/bold red]"
)
raise typer.Exit(code=1)
raise e
console.print(
f"[bold cyan]{changelog_path}[/bold cyan] [bold green]found in the remote "
f"project files[/bold green]"
logging.success(
f"[bold cyan]{changelog_path}[/bold cyan] found in the remote project files"
)
return changelog_file
......@@ -135,11 +130,3 @@ def _extract_body(f: typing.TextIO) -> str:
body.append(lines)
return "".join(body)
regex: typing.Final = re.compile(
r"^## \[(?P<version>(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]"
r"\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*"
r"|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA"
r"-Z-]+)*))?)\]"
)
......@@ -4,9 +4,9 @@ import functools
import http
import gitlab.v4.objects
import rich.console
import release_by_changelog.exc
from release_by_changelog import logging
@functools.cache
......@@ -14,9 +14,8 @@ def get_project(
token: str,
host: str,
project: str,
console: rich.console.Console,
) -> tuple[gitlab.v4.objects.Project, str]:
console.print(
logging.info(
f"Retrieving project [bold cyan]{project}[/bold cyan] from "
f"[bold cyan]{host}[/bold cyan]"
)
......@@ -33,7 +32,5 @@ def get_project(
raise release_by_changelog.exc.ProjectNotFoundError from e
raise e
project_name = f"{project_.namespace.get('full_path')}/{project_.name}"
console.print(
f"[bold green]Project found:[/bold green] [bold cyan]{project_name}[bold cyan]"
)
logging.success(f"Project found: [bold cyan]{project_name}[bold cyan]")
return project_, project_name
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment