Skip to content

Commit

Permalink
ci cd
Browse files Browse the repository at this point in the history
  • Loading branch information
gaarutyunov committed Nov 10, 2024
1 parent ff07601 commit 856b56e
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 21 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI/CD

on:
push:

jobs:
build:
name: Test and Build
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3
- name: "Setup Python, Poetry and Dependencies"
uses: packetcoders/action-setup-cache-python-poetry@main
with:
python-version: 3.12
poetry-version: 1.8
- name: Run tests
run: |
poetry run pytest
- name: Build
run: |
poetry build
12 changes: 10 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ It can be a local file or a file stored somewhere, e.g. git repository.
Running the Pipeline
--------------------

If the pipeline configuration is stored locally, run `pype --local config.yml`.
If the pipeline configuration is stored locally, run:

If it is stored in a repository, run `pype --source github --repository gaarutyunov/pype --filename examples/config.yml --ref main`.
.. code:: bash
pype --local config.yml
If it is stored in a repository, run:

.. code:: bash
pype --source github --repository gaarutyunov/pype --file examples/config.yml --ref main
2 changes: 1 addition & 1 deletion examples/logger/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .strategy import LoggerStrategy
from .commands import Echo
from .commands import Echo
3 changes: 2 additions & 1 deletion examples/logger/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from pype import Command


class Echo(Command):
logger: logging.Logger | None = None

Expand All @@ -11,4 +12,4 @@ def __init__(self, log_level: str = "ERROR"):

def configure(self, **kwargs: str):
self.logger = logging.getLogger(self.name)
self.logger.setLevel(self.log_level)
self.logger.setLevel(self.log_level)
2 changes: 1 addition & 1 deletion examples/logger/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def configure_echo(self, cmd: Echo, **kwargs: dict[str, str]):
cmd.log_level = "INFO"

def echo(self, cmd: Echo, params: dict[str, str]):
cmd.logger.info(params["MESSAGE"])
cmd.logger.info(params["MESSAGE"])
2 changes: 0 additions & 2 deletions pype/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import pype.gitlab # noqa: import for side effects
import pype.github # noqa: import for side effects
from pype.cli import cli

if __name__ == "__main__":
Expand Down
7 changes: 6 additions & 1 deletion pype/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import click
import yaml

import pype.gitlab # noqa: import for side effects
import pype.github # noqa: import for side effects

from pype import Pipeline
from pype.registry import REGISTRY
from pype.vcs import get_factory, RepositoryFileClient


@click.command()
@click.option("-s", "--source", help="Configuration remote source")
@click.option("-l", "--local", type=click.File(lazy=True), help="Path to local configuration file")
@click.option(
"-l", "--local", type=click.File(lazy=True), help="Path to local configuration file"
)
@click.option("-p", "--repository", type=str, help="Repository full name")
@click.option("-r", "--ref", type=str, help="Reference (branch or tag)")
@click.option("-f", "--file", type=str, help="Config file name")
Expand Down
1 change: 1 addition & 0 deletions pype/core/command.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Command:
"""Command to be executed"""

def __init__(self, name: str):
"""
:param name: name of the command (is used to get method from Strategy)
Expand Down
1 change: 1 addition & 0 deletions pype/core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Pipeline:
"""Entrypoint for the pipeline to be executed"""

def __init__(self, stages: list[Stage]):
"""
:param stages: list of stages to be run inside pipeline
Expand Down
1 change: 1 addition & 0 deletions pype/core/stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Stage:
"""A list of commands to be handled by a Strategy one by one"""

def __init__(self, name: str, strategy: Strategy, commands: list[Command]):
"""
:param name: Name of the stage
Expand Down
1 change: 1 addition & 0 deletions pype/core/strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Strategy:
"""Class that implements commands handlers"""

def run(self, command: Command, params: dict[str, str]):
if hasattr(self, command.name):
getattr(self, command.name)(command, params)
Expand Down
13 changes: 10 additions & 3 deletions pype/github/vcs_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,23 @@
from github import Github, Auth
from github.Consts import DEFAULT_BASE_URL

from pype.vcs import VCSAdapter
from pype.vcs import VCSAdapter, register_factory


@register_factory("github")
def github_factory() -> VCSAdapter:
return GithubAdapter(os.environ.get("GITHUB_TOKEN"), os.environ.get("GITHUB_URL", DEFAULT_BASE_URL))
return GithubAdapter(
os.environ.get("GITHUB_TOKEN"), os.environ.get("GITHUB_URL", DEFAULT_BASE_URL)
)


class GithubAdapter:
def __init__(self, token: str, url: str = DEFAULT_BASE_URL):
self.client = Github(auth=Auth.Token(token), base_url=url)

def download(self, repository: str, filename: str, ref: str) -> io.TextIOBase:
return io.StringIO(self.client.get_repo(repository).get_contents(filename, ref=ref).decoded_content.decode('utf-8'))
return io.StringIO(
self.client.get_repo(repository)
.get_contents(filename, ref=ref)
.decoded_content.decode("utf-8")
)
7 changes: 6 additions & 1 deletion pype/gitlab/vcs_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ def __init__(self, url: str, token: str):
self.client = Gitlab(url, token)

def download(self, repository: str, filename: str, ref: str) -> io.TextIOBase:
return io.StringIO(self.client.projects.get(repository).files.get(filename, ref=ref).decode().decode("utf-8"))
return io.StringIO(
self.client.projects.get(repository)
.files.get(filename, ref=ref)
.decode()
.decode("utf-8")
)
1 change: 1 addition & 0 deletions pype/vcs/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

FACTORIES = {}


def register_factory(name):
def register(factory: Callable[[], VCSAdapter]):
FACTORIES[name] = factory
Expand Down
3 changes: 1 addition & 2 deletions pype/vcs/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@


class RepositoryFileClient(Protocol):
def download(self, repository: str, filename: str, ref: str) -> io.TextIOBase:
...
def download(self, repository: str, filename: str, ref: str) -> io.TextIOBase: ...


VCSAdapter: TypeAlias = RepositoryFileClient
29 changes: 22 additions & 7 deletions tests/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pathlib
import sys

from click.testing import CliRunner

Expand All @@ -9,11 +8,27 @@
def test_pipeline_local(caplog):
runner = CliRunner()
root_path = pathlib.Path(__file__).parent.parent
config_path = root_path / 'examples/logger/config.yml'
config_path = root_path / "examples/logger/config.yml"
msg = "test message"
result = runner.invoke(cli, ['--local', str(config_path)], env={
"MESSAGE": msg,
"LOG_LEVEL": "INFO"
})
result = runner.invoke(
cli, ["--local", str(config_path)], env={"MESSAGE": msg, "LOG_LEVEL": "INFO"}
)
assert result.exit_code == 0
assert msg in caplog.messages
assert msg in caplog.messages


def test_pipeline_github(caplog):
runner = CliRunner()
msg = "test message"
result = runner.invoke(
cli,
[
"--source", "github",
"--repository", "gaarutyunov/pype",
"--file", "examples/logger/config.yml",
"--ref", "main",
],
env={"MESSAGE": msg, "LOG_LEVEL": "INFO"},
)
assert result.exit_code == 0
assert msg in caplog.messages

0 comments on commit 856b56e

Please sign in to comment.