Skip to content

github

GithubEnvVars

Bases: BaseModel

Environment variables required when running in Github.

Attributes:

Name Type Description
repo str

...

run_id str

...

run_number str

...

github_token str

...

git_branch str

...

git_sha str

...

triggered_by str

...

Source code in m/log/ci_tools/providers/github.py
class GithubEnvVars(BaseModel):
    """Environment variables required when running in Github."""

    repo: str = Field('GITHUB_REPOSITORY')
    run_id: str = Field('GITHUB_RUN_ID')
    run_number: str = Field('GITHUB_RUN_NUMBER')
    github_token: str = Field('GITHUB_TOKEN')
    git_branch: str = Field('GITHUB_REF')
    git_sha: str = Field('GITHUB_SHA')
    triggered_by: str = Field('GITHUB_ACTOR')

env_vars()

Read the environment variables from Github Actions.

Returns:

Type Description
OneOf[Issue, EnvVars]

An EnvVars instance or an Issue.

Source code in m/log/ci_tools/providers/github.py
def env_vars() -> OneOf[Issue, EnvVars]:
    """Read the environment variables from Github Actions.

    Returns:
        An `EnvVars` instance or an Issue.
    """
    server_url = 'https://github.com'
    return one_of(
        lambda: [
            EnvVars(
                **env.model_dump(),
                ci_env=True,
                server_url=server_url,
                run_url=run_url,
            )
            for env in mio.env_model(GithubEnvVars)
            for run_url in (
                f'{server_url}/{env.repo}/actions/runs/{env.run_id}',
            )
        ],
    ).flat_map_bad(hone('GH Actions env_vars failure'))

log_format(formatter, record, show_traceback, debug_python)

Format a log record using the functions provided in this module.

This function is meant to be used by a log formatter. See more info

Parameters:

Name Type Description Default
formatter Formatter

A log formatter instance.

required
record LogRecord

A log record.

required
show_traceback bool

If true, display the python stack trace.

required
debug_python bool

If true, display the location of the record's origin.

required

Returns:

Type Description
str

A formatted string.

Source code in m/log/ci_tools/providers/github.py
def log_format(
    formatter: logging.Formatter,
    record: logging.LogRecord,
    show_traceback: bool,
    debug_python: bool,
) -> str:
    """Format a log record using the functions provided in this module.

    This function is meant to be used by a log formatter. See more info


    Args:
        formatter: A log formatter instance.
        record: A log record.
        show_traceback: If true, display the python stack trace.
        debug_python: If true, display the location of the record's origin.

    Returns:
        A formatted string.
    """
    record_dict = record.__dict__

    open_b = record_dict.get('open_block')
    if open_b:
        return _format_block(*open_b)

    if record_dict.get('close_block'):
        return '::endgroup::'

    is_command = record.levelname in {'WARNING', 'ERROR'}

    indent_padding = 2 if is_command else 3
    indent = len(record.levelname) + indent_padding

    ci_info = record_dict.get('ci_info', Message(msg=record.msg))
    context = format_context(record, indent, show_traceback=show_traceback)
    if not record.msg:
        return context[1:]

    loc = (
        format_location([record.pathname, f'{record.lineno}'])
        if debug_python
        else ''
    )
    if is_command:
        msg = f'{loc} {record.msg}'.lstrip()
        return _gh_format(record.levelname.lower(), ci_info, msg, context)
    return default_record_fmt(
        record,
        formatter.formatTime(record, formatter.datefmt),
        loc,
        context,
    )