Skip to content

Commit

Permalink
Add: New GitHub API for supporting actions #258
Browse files Browse the repository at this point in the history
Add: New GitHub API for supporting actions
  • Loading branch information
bjoernricks authored Jan 10, 2022
2 parents 166c31d + fc4499d commit 70339a1
Show file tree
Hide file tree
Showing 9 changed files with 1,207 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pontos/github/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2022 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
225 changes: 225 additions & 0 deletions pontos/github/actions/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
# Copyright (C) 2021 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import contextlib
import os

from typing import Optional


def _to_options(
name: str = None,
line: str = None,
end_line: str = None,
column: str = None,
end_column: str = None,
title: str = None,
):
options = []
if name:
options.append(f"file={name}")
if line:
options.append(f"line={line}")
if end_line:
options.append(f"endLine={end_line}")
if column:
options.append(f"col={column}")
if end_column:
options.append(f"endColumn={end_column}")
if title:
options.append(f"title={title}")
return ",".join(options)


def _message(
message_type: str,
message: str,
*,
name: str = None,
line: str = None,
end_line: str = None,
column: str = None,
end_column: str = None,
title: str = None,
):
options = _to_options(name, line, end_line, column, end_column, title)
print(f"::{message_type} {options}::{message}")


class Console:
"""
Class for printing messages to the action console
"""

@classmethod
@contextlib.contextmanager
def group(cls, title: str):
"""
ContextManager to display a foldable group
Args:
title: Title of the group
"""
cls.start_group(title)
yield
cls.end_group()

@staticmethod
def start_group(title: str):
"""
Start a new folable group
Args:
title: Title of the group
"""
print(f"::group::{title}")

@staticmethod
def end_group():
"""
End the last group
"""
print("::endgroup::")

@staticmethod
def warning(
message: str,
*,
name: str = None,
line: str = None,
end_line: str = None,
column: str = None,
end_column: str = None,
title: str = None,
):
"""
Print a warning message
This message will also be shown at the action summary
"""
_message(
"warning",
message,
name=name,
line=line,
end_line=end_line,
column=column,
end_column=end_column,
title=title,
)

@staticmethod
def error(
message: str,
*,
name: str = None,
line: str = None,
end_line: str = None,
column: str = None,
end_column: str = None,
title: str = None,
):
"""
Print an error message
This message will also be shown at the action summary
"""
_message(
"error",
message,
name=name,
line=line,
end_line=end_line,
column=column,
end_column=end_column,
title=title,
)

@staticmethod
def notice(
message: str,
*,
name: str = None,
line: str = None,
end_line: str = None,
column: str = None,
end_column: str = None,
title: str = None,
):
"""
Print a warning message
This message will also be shown at the action summary
"""
_message(
"notice",
message,
name=name,
line=line,
end_line=end_line,
column=column,
end_column=end_column,
title=title,
)

@staticmethod
def log(
message: str,
):
"""
Print a message to the console
"""
print(message)

@staticmethod
def debug(message: str):
# pylint: disable=line-too-long
"""
Print a debug message to the console
These messages are only shown if the secret ACTIONS_STEP_DEBUG is set to true.
See https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/enabling-debug-logging#enabling-step-debug-logging
"""
print(f"::debug::{message}")


class ActionIO:
@staticmethod
def output(name: str, value: str):
"""
Set action output
An action output can be consumed by another job
Args:
name: Name of the output variable
value: Value of the output variable
"""
print(f"::set-output name={name}::{value}")

@staticmethod
def input(name: str, default: Optional[str] = None) -> str:
"""
Get the value of an action input
Args:
name: Name of the input variable
default: Use as default if the is no value for the variable
"""
return os.environ.get(
f"INPUT_{name.replace(' ', '_').upper()}", default
)
79 changes: 79 additions & 0 deletions pontos/github/actions/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (C) 2021 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from pathlib import Path
from typing import Optional


class GitHubEnvironment:
"""
Class to handle values from the GitHub Environment
https://docs.github.com/en/actions/learn-github-actions/environment-variables
"""

@property
def workspace(self) -> Optional[Path]:
workspace = os.environ.get("GITHUB_WORKSPACE")
return Path(workspace) if workspace else None

@property
def repository(self) -> Optional[str]:
return os.environ.get("GITHUB_REPOSITORY")

@property
def sha(self) -> Optional[str]:
return os.environ.get("GITHUB_SHA")

@property
def ref(self) -> Optional[str]:
return os.environ.get("GITHUB_REF")

@property
def ref_name(self) -> Optional[str]:
return os.environ.get("GITHUB_REF_NAME")

@property
def event_path(self) -> Optional[Path]:
event_path = os.environ.get("GITHUB_EVENT_PATH")
return Path(event_path) if event_path else None

@property
def head_ref(self) -> Optional[str]:
return os.environ.get("GITHUB_HEAD_REF")

@property
def base_ref(self) -> Optional[str]:
return os.environ.get("GITHUB_BASE_REF")

@property
def api_url(self) -> Optional[str]:
return os.environ.get("GITHUB_API_URL")

@property
def actor(self) -> Optional[str]:
return os.environ.get("GITHUB_ACTOR")

@property
def run_id(self) -> Optional[str]:
return os.environ.get("GITHUB_RUN_ID")

@property
def action_id(self) -> Optional[str]:
return os.environ.get("GITHUB_ACTION")
Loading

0 comments on commit 70339a1

Please sign in to comment.