Skip to content

Commit

Permalink
Merge branch 'release/2.0.0b4'
Browse files Browse the repository at this point in the history
  • Loading branch information
nadouani committed Jan 20, 2023
2 parents 025a9a7 + 0220479 commit 1655154
Show file tree
Hide file tree
Showing 31 changed files with 689 additions and 37 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: CI
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: pip install --no-cache-dir -U pip .['dev']
- name: Lint check with flake8
run: scripts/ci.py lint
- name: Format check with black
run: scripts/ci.py format
- name: Type check with mypy
run: scripts/ci.py type
- name: CVE check with pip-audit
run: scripts/ci.py cve
- name: Security check with bandit
run: scripts/ci.py security
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# experimental (works from the terminal but not from vscode and possibly from other IDEs)
repos:
- repo: local
hooks:
- id: ci-checks
name: ci-checks
entry: scripts/ci.py
language: system
pass_filenames: false
always_run: true
stages: [push]
34 changes: 32 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<a href="https://pypi.org/project/thehive4py" target"_blank">
<img src="https://img.shields.io/pypi/dm/thehive4py" alt="Pypi page">
</a>
<a href="https://github.com/TheHive-Project/TheHive4py/actions/workflows/ci.yml" target"_blank">
<img src="https://github.com/TheHive-Project/TheHive4py/actions/workflows/ci.yml/badge.svg" alt="ci action badge">
</a>
</p>
</div>

Expand All @@ -26,15 +29,42 @@ Rebooted version of thehive4py for TheHive5! Stay tuned, more to come!

## Development

You can setup a venv (see the [official docs for this](https://docs.python.org/3/tutorial/venv.html)):
### Setting up a virtual environment (optional)

You can setup a venv (see the [official docs for this](https://docs.python.org/3/tutorial/venv.html):

```
# Create and activate venv
python3 -m venv <path_of_venv>
source <path_of_venv>/bin/activate
```

Install the dev dependencies for the project
### Install the package for development

To install the package with the dev dependencies one can run:

```
pip install -e '.[dev]'
```

### Run CI checks before pushing changes

To check the integrity of changes made one can run:

```
python scripts/ci.py
```

or to execute the checks automatically just install the pre-commit hooks come with the repo:

```
pre-commit install
```

### Run CD commands to build and publish

To build the package one can run:

```
python scripts/cd.py build
```
38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"

[project]
name = "thehive4py"
description = "Python client for TheHive5"
version = "2.0.0b4"
requires-python = ">=3.8"
dependencies = ["requests>=2.27"]
readme = "README.md"
keywords = ["thehive5", "api", "client"]
license = { text = "AGPL-V3" }
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Natural Language :: English",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"License :: OSI Approved :: GNU Affero General Public License v3",
]
authors = [{ name = "Szabolcs Antal", email = "[email protected]" }]

[project.optional-dependencies]
audit = ["bandit", "pip-audit"]
build = ["build", "twine"]
lint = ["black", "flake8", "mypy", "pre-commit"]
test = ["pytest"]
dev = ["thehive4py[audit, lint, test, build]"]

[tool.setuptools.packages.find]
include = ["thehive4py*"]

[tool.setuptools.package-data]
thehive4py = ["py.typed"]
104 changes: 104 additions & 0 deletions scripts/cd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env python
import argparse
import subprocess


def _run_subprocess(
args: str,
init_message: str,
success_message: str,
error_message: str,
verbose=False,
):
print(init_message)
proc = subprocess.run(args, shell=True, capture_output=True)

process_output = proc.stdout.decode() or proc.stderr.decode()
indented_process_output = "\n".join(
[f"\t{output_line}" for output_line in process_output.splitlines()]
)

if proc.returncode != 0:
exit_message = "\n".join([error_message, indented_process_output])
exit(exit_message)

if verbose:
print(indented_process_output)

print(success_message)


def run_all(verbose=False):
print("Run all deployment tasks...")
run_build(verbose=verbose)
run_publish(verbose=verbose)
print("All tasks succeeded!")


def run_build(verbose: bool):
_run_subprocess(
args=("rm -rf build/ dist/ && python -m build --sdist --wheel"),
init_message="Building the package with the build module...",
success_message="Package build succeeded!",
error_message="Package build failed due to:",
verbose=verbose,
)


def run_publish(verbose: bool):
_run_subprocess(
args=("echo 'Publish command is not implemented yet...' && exit 1 "),
init_message="Publishing the package with twine...",
success_message="Publish succeeded!",
error_message="Publish failed due to:",
verbose=verbose,
)


def parse_arguments():
main_parser = argparse.ArgumentParser(
prog="thehive4py-cd",
description="run all cd tasks or use sub commands to run cd tasks individually",
)
main_parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="generate verbose output",
)
main_parser.set_defaults(func=run_all)

subparsers = main_parser.add_subparsers(help="commands")
subparser_options = [
{
"name": "build",
"help": "task to build the package",
"default_func": run_build,
},
{
"name": "publish",
"help": "task to publish the package",
"default_func": run_publish,
},
]

for subparser_option in subparser_options:
_subparser = subparsers.add_parser(
name=subparser_option["name"],
help=subparser_option["help"],
parents=[main_parser],
add_help=False,
)
_subparser.set_defaults(func=subparser_option["default_func"])

return main_parser.parse_args()


def main():
args = parse_arguments()
args.func(verbose=args.verbose)


if __name__ == "__main__":
main()
142 changes: 142 additions & 0 deletions scripts/ci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env python
import argparse
import subprocess


def _run_subprocess(
args: str,
init_message: str,
success_message: str,
error_message: str,
verbose=False,
):
print(init_message)
proc = subprocess.run(args, shell=True, capture_output=True)

process_output = proc.stdout.decode() or proc.stderr.decode()
indented_process_output = "\n".join(
[f"\t{output_line}" for output_line in process_output.splitlines()]
)

if proc.returncode != 0:
exit_message = "\n".join([error_message, indented_process_output])
exit(exit_message)

if verbose:
print(indented_process_output)

print(success_message)


def check_all(verbose=False):
print("Run all checks...")
check_lint(verbose=verbose)
check_format(verbose=verbose)
check_type(verbose=verbose)
check_cve(verbose=verbose)
check_security(verbose=verbose)
print("All checks succeeded!")


def check_lint(verbose=False):
_run_subprocess(
args="flake8 thehive4py/ tests/",
init_message="Run lint checks with flake8...",
success_message="Lint checks succeeded!",
error_message="Lint checks failed due to:",
verbose=verbose,
)


def check_format(verbose=False):
_run_subprocess(
args="black --diff thehive4py/ tests/",
init_message="Run format checks with black...",
success_message="Format checks succeeded!",
error_message="Lint checks failed due to:",
verbose=verbose,
)


def check_type(verbose=False):
_run_subprocess(
args="mypy --install-types --non-interactive thehive4py/",
init_message="Run type checks with mypy...",
success_message="Type checks succeeded!",
error_message="Type checks failed due to:",
verbose=verbose,
)


def check_cve(verbose=False):
_run_subprocess(
args="pip-audit .",
init_message="Run CVE checks with pip-audit...",
success_message="CVE checks succeeded!",
error_message="CVE checks failed due to:",
verbose=verbose,
)


def check_security(verbose=False):
_run_subprocess(
args="bandit -r thehive4py/",
init_message="Run security checks with bandit...",
success_message="Security checks succeeded!",
error_message="Security checks failed due to:",
verbose=verbose,
)


def parse_arguments():
main_parser = argparse.ArgumentParser(
prog="thehive4py-ci",
description=(
"run all ci checks or use sub commands to run ci checks individually"
),
)
main_parser.add_argument(
"-v",
"--verbose",
action="store_true",
default=False,
help="generate verbose output",
)
main_parser.set_defaults(func=check_all)

subparsers = main_parser.add_subparsers(help="commands")
subparser_options = [
{"name": "lint", "help": "run lint checks only", "default_func": check_lint},
{
"name": "format",
"help": "run format checks only",
"default_func": check_format,
},
{"name": "type", "help": "run type checks only", "default_func": check_type},
{"name": "cve", "help": "run cve checks only", "default_func": check_cve},
{
"name": "security",
"help": "run security checks",
"default_func": check_security,
},
]

for subparser_option in subparser_options:
_subparser = subparsers.add_parser(
name=subparser_option["name"],
help=subparser_option["help"],
parents=[main_parser],
add_help=False,
)
_subparser.set_defaults(func=subparser_option["default_func"])

return main_parser.parse_args()


def main():
args = parse_arguments()
args.func(verbose=args.verbose)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion .flake8 → setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ max-line-length=88
per-file-ignores =
thehive4py/__init__.py:F401
thehive4py/endpoints/__init__.py:F401
thehive4py/query/__init__.py:F401
thehive4py/query/__init__.py:F401
Loading

0 comments on commit 1655154

Please sign in to comment.