Skip to content

Commit

Permalink
feat: base command
Browse files Browse the repository at this point in the history
- cli
- file action
update requirements
  • Loading branch information
Paulo-Lopes-Estevao committed Nov 27, 2023
1 parent 2aaea35 commit bc7c3b2
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 8 deletions.
Empty file added cigen/adapter/input/command.py
Empty file.
142 changes: 142 additions & 0 deletions cigen/adapter/input/github_command_in/go_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import pprint

import click

from cigen.core.github.github_action import OnEventFactory
from cigen.core.github.go_action import GoActionBuilderImpl, ActionCIGenGolang


class AddCommand:
def __init__(self):
pass

@staticmethod
def list_steps():
return [
"build_base",
"build_base_with_version_list",
"checkout",
"setup_go",
"setup_go_with_version_list",
"build",
"cache",
"install_dependencies",
"tests",
"tests_and_coverage",
"tests_and_coverage_with_coverage",
"tests_and_coverage_with_coverage_and_html",
"tests_and_coverage_with_coverage_and_html_and_upload",
]

@staticmethod
def on_events(listEvent: list[str]) -> dict:
branchs = {}
brancheName = {}
if len(listEvent) == 2:
brancheName['branches'] = listEvent[1].split(",")
branchs[listEvent[0]] = brancheName
return branchs

countElements = 0
for i in range(len(listEvent)):
if countElements >= len(listEvent):
break

brancheName['branches'] = listEvent[countElements + 1].split(",")
branchs[listEvent[countElements]] = brancheName
countElements += 2

return branchs

@click.command()
@click.option("--step", "-s", nargs=-1, type=click.Choice(list_steps()), help="Steps list", required=False)
def steps(self, steps):
click.echo(pprint.pprint(self.list_steps()))

@click.command()
@click.option("-n", "--name", nargs=1, help="Name of the action", prompt="Name of the action", required=True)
@click.option("-b", "--branch_name", nargs=-1, help="Branch to add", prompt="Name of the branch", required=True)
@click.option("-a", "--action", nargs=-1, help="Action to add", default="go", required=True)
@click.option("-v", "--version", nargs=1, help="Version of go", type=click.STRING, default="1.17", required=False)
def action(self, name, branch_name, action, version):
"""
:param name:
:param branch_name:
:param action:
:param version:
:return:
Actions:
0 - base
1 - version_list
2 - build_base
3 - build_base_with_version_list
4 - checkout
5 - setup_go
6 - setup_go_with_version_list
7 - build
8 - cache
9 - install_dependencies
10 - tests
11 - tests_and_coverage
12 - tests_and_coverage_with_coverage
13 - tests_and_coverage_with_coverage_and_html
14 - tests_and_coverage_with_coverage_and_html_and_upload
base ou version_list 0 ou 1 is required like last parameter
:example: 2,3,5 0
"""

on_events = OnEventFactory.create_events(self.on_events(branch_name))
action_ciGen_golang = ActionCIGenGolang()
action_ciGen_golang.builder = GoActionBuilderImpl(name, version, on_events)
for actions in action:
self.action_builder(action_ciGen_golang, actions)

@staticmethod
def action_builder(action_ciGen_golang, actions):
if actions == "base" or actions == 0:
action_ciGen_golang.action_build_base()
elif actions == "version_list" or actions == 1:
action_ciGen_golang.action_build_base_with_version_list()
if actions == "build_base" or actions == 2:
action_ciGen_golang.builder.step_checkout()
action_ciGen_golang.builder.step_setup_go()
action_ciGen_golang.builder.step_run_build()
action_ciGen_golang.builder.step_run_tests()
action_ciGen_golang.action_build_base()
elif actions == "build_base_with_version_list" or actions == 3:
action_ciGen_golang.builder.step_checkout()
action_ciGen_golang.builder.step_setup_go()
action_ciGen_golang.builder.step_run_build()
action_ciGen_golang.builder.step_run_tests()
action_ciGen_golang.action_build_base_with_version_list()
elif actions == "checkout" or actions == 4:
action_ciGen_golang.builder.step_checkout()
elif actions == "setup_go" or actions == 5:
action_ciGen_golang.builder.step_setup_go()
elif actions == "setup_go_with_version_list" or actions == 6:
action_ciGen_golang.builder.step_setup_go_with_version_list()
elif actions == "build" or actions == 7:
action_ciGen_golang.builder.step_run_build()
elif actions == "cache" or actions == 8:
action_ciGen_golang.builder.step_run_cache()
elif actions == "install_dependencies" or actions == 9:
action_ciGen_golang.builder.step_run_install_dependencies()
elif actions == "tests" or actions == 10:
action_ciGen_golang.builder.step_run_tests()
elif actions == "tests_and_coverage" or actions == 11:
action_ciGen_golang.builder.step_run_tests_and_coverage()
elif actions == "tests_and_coverage_with_coverage" or actions == 12:
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage()
elif actions == "tests_and_coverage_with_coverage_and_html" or actions == 13:
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage_and_html()
elif actions == "tests_and_coverage_with_coverage_and_html_and_upload" or actions == 14:
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage_and_html_and_upload()
else:
click.echo("Action not found")
return
40 changes: 40 additions & 0 deletions cigen/adapter/output/github_out/file_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
from pathlib import Path

import click
import yaml


def verify_folder_exists(path: Path) -> bool:
return path.exists()


def create_folder(path: Path) -> None:
print("Creating folder...", path)
path.mkdir(parents=True, exist_ok=True)


def create_file(path: Path, content) -> None:
content_yml = yaml.dump(content, sort_keys=False, default_flow_style=False).replace('\'on\':', 'on:')
path.write_text(content_yml)


def verify_file_exists(path: Path, content) -> None:
if path.exists():
confirm = click.confirm("Do you want to overwrite the file?")
if confirm:
print("Overwriting file...")
path.unlink()
create_file(path, content)
else:
click.echo("Aborted!")
return


def generate_action(path, content) -> None:
pathFile = Path(path)
if verify_folder_exists(pathFile):
verify_file_exists(pathFile, content)
else:
create_folder(Path(os.path.dirname(path)))
create_file(pathFile, content)
Empty file added cigen/cmd/cli.py
Empty file.
43 changes: 40 additions & 3 deletions cigen/core/github/go_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def step_setup_go(self) -> None:
def step_setup_go_with_version_list(self) -> None:
pass

@abstractmethod
def step_setup_go_with_versions_matrix(self) -> None:
pass

@abstractmethod
def step_run_cache(self) -> None:
pass
Expand Down Expand Up @@ -154,6 +158,9 @@ def step_setup_go(self):
def step_setup_go_with_version_list(self):
self.step.add(self._steps.step_setup_go_with_version_list())

def step_setup_go_with_versions_matrix(self):
self.step.add_at(self._steps.step_setup_go_with_versions_matrix(), 1)

def step_run_cache(self):
self.step.add(self._steps.step_run_cache())

Expand Down Expand Up @@ -197,9 +204,11 @@ def __init__(self, name, version, on, steps: Steps, env=None) -> None:
self.env = env

def base(self):
print(self.on)

return {
'name': self.name,
'on': self.on,
"on": self.on,
'jobs': {
'build': {
'name': 'Build',
Expand All @@ -210,6 +219,9 @@ def base(self):
}

def base_version_list(self):
if self.version == [] or self.version == "" or self.version is None:
self.version = ['1.19', '1.20', '1.21.x']

return {
'name': self.name,
'on': self.on,
Expand All @@ -227,6 +239,20 @@ def base_version_list(self):
}
}

def order_json(self, json_obj, ordem):
ordered_json = {key: json_obj[key] for key in ordem if key in json_obj}

remaining_keys = [key for key in json_obj if key not in ordem]
ordered_json.update({key: json_obj[key] for key in remaining_keys})

for key, value in ordered_json.items():
if isinstance(value, dict):
ordered_json[key] = self.order_json(value, ordem)
elif isinstance(value, list):
ordered_json[key] = [self.order_json(item, ordem) if isinstance(item, dict) else item for item in value]

return ordered_json

def base_to_yaml(self):
return yaml.dump(self.base())

Expand Down Expand Up @@ -284,6 +310,16 @@ def step_setup_go_with_version_list(self):
]
}

@staticmethod
def step_setup_go_with_versions_matrix():
return {
'name': 'Setup Go',
'uses': 'actions/setup-go@v4',
'with': {
'go-version': '${{ matrix.go-version }}'
}
}

@staticmethod
def step_run_cache():
return {
Expand Down Expand Up @@ -369,11 +405,12 @@ def builder(self, builder: GoActionBuilder) -> None:
def action_build_base(self):
return self.builder.base()

def action_build_base_version_list(self):
def action_build_base_with_version_list(self):
return self.builder.base_version_list()

def _list_steps(self):
return self.builder.list_steps()

def action_steps_run_build(self):
def _action_steps_run_build(self):
self.builder.add_steps(self.builder.step_run_build())

63 changes: 62 additions & 1 deletion cigen/core/github/go_action_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import unittest

from cigen.adapter.output.github_out.file_action import generate_action
from cigen.core.github.go_action import GoAction, GoActionSteps, ActionCIGenGolang, GoActionBuilderImpl
from cigen.core.github.github_action import On, Steps, Push, PullRequest, OnEventFactory

Expand Down Expand Up @@ -32,7 +34,7 @@ def test_base(self):
)

self.assertEqual(go_action.base(), {
'name': 'Go Action',
'name': 'Go Actions',
'on': {
'push': {
'branches': ['main']
Expand Down Expand Up @@ -120,6 +122,8 @@ def test_go_runVersionWithRange(self):
steps,
)

print(go_action.base_version_list())

self.assertEqual(go_action.base_version_list(), {
'name': 'Go Action',
'on': {
Expand Down Expand Up @@ -241,6 +245,63 @@ def test_action_ci_base_push_and_pull_request(self):
}
})

def test_action_ci_build_base_with_version_list_push_and_pull_request(self):
action_ciGen_golang = ActionCIGenGolang()

on_events = OnEventFactory.create_events({
'push': {
'branches': ['main', 'master']
},
'pull_request': {
'branches': ['main', 'master']
}
})

action_ciGen_golang.builder = GoActionBuilderImpl('Go Action', ['1.17', '1.18', '1.19'], on_events)
action_ciGen_golang.builder.step_checkout()
action_ciGen_golang.builder.step_setup_go_with_versions_matrix()
action_ciGen_golang.builder.step_run_build()

self.assertEqual(action_ciGen_golang.action_build_base_with_version_list(), {
'name': 'Go Action',
'on': {
'push': {
'branches': ['main', 'master']
},
'pull_request': {
'branches': ['main', 'master']
}
},
'jobs': {
'build': {
'name': 'Build',
'runs-on': 'ubuntu-latest',
'strategy': {
'matrix': {
'go-version': ['1.17', '1.18', '1.19']
}
},
'steps': [
{
'name': 'Checkout',
'uses': 'actions/checkout@v4'
},
{
'name': 'Setup Go',
'uses': 'actions/setup-go@v4',
'with': {
'go-version': '${{ matrix.go-version }}'
}
},
{
'name': 'Build',
'run': 'go build ./...'
}
]
}
}
})


if __name__ == '__main__':
unittest.main()
18 changes: 14 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
pip~=23.0.1
wheel~=0.37.0
setuptools~=58.0.4
future~=0.18.2
pip==23.3.1
wheel==0.37.1
PyYAML==6.0.1
build==1.0.3
click==8.1.7
setuptools==58.0.4
## The following requirements were added by pip freeze:
markdown-it-py==3.0.0
mdurl==0.1.2
packaging==23.2
pip-tools==7.3.0
Pygments==2.17.1
pyproject_hooks==1.0.0
rich==13.7.0

0 comments on commit bc7c3b2

Please sign in to comment.