Skip to content

Commit

Permalink
feat: add github action base project with golang
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo-Lopes-Estevao committed Dec 1, 2023
1 parent bc7c3b2 commit d437b76
Show file tree
Hide file tree
Showing 26 changed files with 408 additions and 159 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

.github/workflows/test_**.yml
79 changes: 77 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# ci generator

CI generator is a tool to generate CI configuration files for your project.
It's a command line tool that can be used to generate CI configuration files for your project. It's written in Python and uses Jinja2 templates to generate the files.
It's a command line tool that can be used to generate CI configuration files for your project. It's written in Python and uses templates to generate the files.

Possible CI systems are:

- [ ] Github Actions
- [x] Github Actions
- [ ] Jenkins
- [ ] Docker
- [ ] Gitlab CI
Expand All @@ -31,6 +32,80 @@ pip install ci-generator
cigen --help
```

### Github Actions

```bash
cigen github-actions --help
```

github-actions subcommand can be used to generate Build and Test Github Actions configuration files.

github-actions Golang example:

```bash
cigen github-actions go -n myproject -b push main -a 1 -v 1.21.1
```

github-actions Python example:

```bash
cigen github-actions python -n myproject -b push main -a 1 -v 3.9.6
```

### Jenkins

```bash
cigen jenkins --help
```

jenkins subcommand can be used to generate Build and Test Jenkins configuration files.

jenkins Golang example:

```bash
cigen jenkins go -n myproject -b push main -a 1 -v 1.21.1
```

jenkins Python example:

```bash
cigen jenkins python -n myproject -b push main -a 1 -v 3.9.6
```

### Docker

```bash
cigen docker --help
```

docker subcommand can be used to generate Docker configuration files.

docker example:

```bash
cigen docker -n dockerfile -i golang -v 1.21.1 -s multi
```

### Gitlab CI

```bash
cigen gitlab-ci --help
```

gitlab-ci subcommand can be used to generate Build and Test Gitlab CI configuration files.

gitlab-ci Golang example:

```bash
cigen gitlab-ci go -n myproject -b push main -a 1 -v 1.21.1
```

gitlab-ci Python example:

```bash
cigen gitlab-ci python -n myproject -b push main -a 1 -v 3.9.6
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions cigen/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from cigen.cmd.command import cli


if __name__ == '__main__':
cli()
File renamed without changes.
Empty file added cigen/adapter/input/__init__.py
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions cigen/adapter/input/docker_command/docker_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import click


@click.group()
def docker():
"""
This is the main command for the Docker
"""
pass
Empty file.
13 changes: 13 additions & 0 deletions cigen/adapter/input/github_command/github_action_group.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import click
from cigen.adapter.input.github_command.go_command import action_go


@click.group()
def github_action():
"""
This is the main command for the GitHub Actions
"""
pass


github_action.add_command(action_go)
226 changes: 226 additions & 0 deletions cigen/adapter/input/github_command/go_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
import pprint

import click

from cigen.adapter.output.github_out.file_action import generate_action
from cigen.core.github.github_action import OnEventFactory
from cigen.core.github.go_action import GoActionBuilderImpl, ActionCIGenGolang

ACTION_DOC_LONG = """
Actions:
0 - base 2 - version_list 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
"""


def action_params_valid(action_ciGen_golang, action):
action_ciGen = None
lastElement = action.split(" ")[len(action.split(" ")) - 1]

dropLastElement = action.split(" ")
if len(dropLastElement) > 1:
dropLastElement.pop()
dropLastElement = dropLastElement[0].split(",")

elements = dropLastElement

if "2" in elements or "1" in elements:
if len(elements) > 1:
click.echo("""
The action after the value 1 or 2 is [optional] the last value after is a base action of a simple code.
The action 1 or 2 cannot be followed by any value separated by a comma. the last parameter is [optional] base.
Example: 1 or 2 - is valid
Example: 1 0 or 2 1 - is valid
Example: 1,2 0 or 2,3 1 - is invalid
""")
return
else:
if len(action.split(",")) == 1:
click.echo("Action required 2 parameters! Example: 3,4,6 0")
return

for actions in elements:
if actions.isnumeric():
actions = int(actions)
action_ciGen = action_builder(action_ciGen_golang, actions)

if lastElement == "0" or elements[0] == "1":
click.echo(pprint.pprint(action_ciGen.action_build_base()))
return action_ciGen.action_build_base()
elif lastElement == "1" or elements[0] == "2":
click.echo(pprint.pprint(action_ciGen.action_build_base_with_version_list()))
return action_ciGen.action_build_base_with_version_list()
else:
click.echo("Action not found")
return


def on_events(listEvent: list[str]) -> dict:
OnEventsName = {}
branchesName = {}

if len(listEvent) == 1:
names = listEvent[0].split(" ")
branchesName['branches'] = [names[1]]
OnEventsName[names[0]] = branchesName
return OnEventsName

if len(listEvent) == 2:
branchesName['branches'] = listEvent[1].split(",")
OnEventsName[listEvent[0]] = branchesName
return OnEventsName

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

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

return OnEventsName


@click.command("go", help="Generate action github actions for go", epilog=ACTION_DOC_LONG)
@click.option("-n", "--name", nargs=1, help="Name of the action", prompt="Name of the action", required=True)
@click.option("-b", "--branch_name", help="Branch to add", prompt="Name of the branch", required=True)
@click.option("-a", "--action", help="Action to add", prompt="Action to add", required=True)
@click.option("-v", "--version", nargs=1, help="Version of go [default=1.17]", type=click.STRING, default="1.17",
required=False)
def action_go(name, branch_name, action, version):
version = version.split(",")
if len(version) == 1:
version = [version[0]]

if len(branch_name.split(" ")) == 1:
click.echo("Branch required 2 parameters! Example: push main,master")
return

on_event = OnEventFactory.create_events(on_events(branch_name.split(",")))
action_ciGen_golang = ActionCIGenGolang()
action_ciGen_golang.builder = GoActionBuilderImpl(name, version, on_event)

ciGen = action_params_valid(action_ciGen_golang, action)

confirm = click.confirm("Do you want to generate the action?")
if confirm:
print("Generating action...")
nameFile = name.replace(" ", "_")
pathFile = ".github/workflows/{}.yml".format(nameFile.lower())
generate_action(path=pathFile, content=ciGen)
else:
click.echo("Aborted!")


def action_builder(action_ciGen_golang, actions):
action_mapping = {
"build_base": {
"action_id": 1,
"steps": [
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,
]
},
"build_base_with_version_list": {
"action_id": 2,
"steps": [
action_ciGen_golang.builder.step_checkout,
action_ciGen_golang.builder.step_setup_go_with_versions_matrix,
action_ciGen_golang.builder.step_run_build,
action_ciGen_golang.builder.step_run_tests
]
},
"checkout": {
"action_id": 3,
"steps": [
action_ciGen_golang.builder.step_checkout
]
},
"setup_go": {
"action_id": 4,
"steps": [
action_ciGen_golang.builder.step_setup_go
]
},
"setup_go_with_version_list": {
"action_id": 5,
"steps": [
action_ciGen_golang.builder.step_setup_go_with_versions_matrix
]
},
"build": {
"action_id": 6,
"steps": [
action_ciGen_golang.builder.step_run_build
]
},
"cache": {
"action_id": 7,
"steps": [
action_ciGen_golang.builder.step_run_cache
]
},
"install_dependencies": {
"action_id": 8,
"steps": [
action_ciGen_golang.builder.step_run_install_dependencies
]
},
"tests": {
"action_id": 9,
"steps": [
action_ciGen_golang.builder.step_run_tests
]
},
"tests_and_coverage": {
"action_id": 10,
"steps": [
action_ciGen_golang.builder.step_run_tests_and_coverage
]
},
"tests_and_coverage_with_coverage": {
"action_id": 11,
"steps": [
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage
]
},
"tests_and_coverage_with_coverage_and_html": {
"action_id": 12,
"steps": [
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage_and_html
]
},
"tests_and_coverage_with_coverage_and_html_and_upload": {
"action_id": 13,
"steps": [
action_ciGen_golang.builder.step_run_tests_and_coverage_with_coverage_and_html_and_upload
]
}
}

if isinstance(actions, int):
matching_action = next((key for key, value in action_mapping.items() if value["action_id"] == actions),
None)
if matching_action:
action_data = action_mapping[matching_action]
for step in action_data["steps"]:
step()
else:
print("Action ID not found")
elif actions in action_mapping:
action_data = action_mapping[actions]
for step in action_data["steps"]:
step()
else:
print("Action not found")

return action_ciGen_golang
21 changes: 21 additions & 0 deletions cigen/adapter/input/github_command/go_command_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest

from cigen.adapter import go_command
from click.testing import CliRunner


class GoCommandTestCase(unittest.TestCase):
runner = CliRunner()

def test_something(self):
self.assertEqual(True, True) # add assertion here

def test_action_build_base(self):
result = self.runner.invoke(go_command.action_go, ["-n", "Go Action", "-b", "push main", "-a", "1"], input="y")

self.assertEqual(result.exit_code, 0)
self.assertIn("'name': 'Go Action'", result.output)


if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit d437b76

Please sign in to comment.