Skip to content

Commit

Permalink
Add files
Browse files Browse the repository at this point in the history
  • Loading branch information
wvanheemstra committed Oct 31, 2024
1 parent 5c1c4ae commit 53707bb
Show file tree
Hide file tree
Showing 33 changed files with 866 additions and 1 deletion.
14 changes: 14 additions & 0 deletions 200/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# 200 - Requirements

We use [pipx](https://github.com/pypa/pipx?tab=readme-ov-file#install-pipx) instead of ```pip```, for isolation.
1. Install pipx:
see https://github.com/pypa/pipx?tab=readme-ov-file#install-pipx for platform (Linux, macOS, Windows) specifics.
2. Install python with pipx:
```bash
$ pipx install python
```

We use [hatch](https://hatch.pypa.io/) for project management.
2. Install hatch:
```bash
$ pipx install hatch
```
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,61 @@ threagile-builder

**Executive Summary**

Text
Getting your code up and running on your own system.
1. Installation process:
```bash
cd threagile-builder
pip install -r requirements.txt
hatch run src/threagile_builder/app
```
2. Software dependencies
3. Latest releases
4. API references

# Build and Test
To build your code, use:

```bash
$ cd threagile-builder
$ hatch build
```

To run the application, use:

Linux:
```bash
export SECRET_KEY="secret"
```

Windows:
```bash
setx SECRET_KEY secret
```

Then:

```bash
$ cd threagile-builder
# Without hatch: $ python src/threagile_builder/app.py
$ hatch run src/threagile_builder/app
```

Then, navigate to `http://127.0.0.1:5000/` in your web browser.

To run tests, use:

```bash
$ cd threagile-builder
$ pytest tests/
```

# API Documentation

Navigate to `http://127.0.0.1:5000/docs` in your web browser, or download the openapi.json from `http://127.0.0.1:5000/openapi.json`.

# Metrics

Let a tool like Prometheus scrape `http://127.0.0.1:5000/metrics`.

## 100 - Introduction

Expand Down
21 changes: 21 additions & 0 deletions threagile-builder/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Threagile Builder

[![PyPI - Version](https://img.shields.io/pypi/v/threagile-builder.svg)](https://pypi.org/project/threagile-builder)
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/threagile-builder.svg)](https://pypi.org/project/threagile-builder)

-----

## Table of Contents

- [Installation](#installation)
- [License](#license)

## Installation

```console
pip install threagile-builder
```

## License

`threagile-builder` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
Empty file added threagile-builder/dist/.keepgit
Empty file.
111 changes: 111 additions & 0 deletions threagile-builder/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# threagile-builder/pyproject.toml
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "threagile-builder"
dynamic = ["version"]
description = 'A Threagile Builder.'
readme = "README.md"
requires-python = ">=3.8"
license = "MIT"
keywords = []
authors = [
{ name = "Willem van Heemstra", email = "[email protected]" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"Flask",
"Flask-SQLAlchemy",
"Flask-WTF",
"apiflask",
"black",
"opentelemetry-api",
"opentelemetry-sdk",
"opentelemetry-instrumentation-flask",
"opentelemetry-instrumentation-sqlalchemy",
"opentelemetry-exporter-prometheus",
"prefect",
"prometheus_client",
"python-dotenv"
]

[project.urls]
Documentation = "https://github.com/vanHeemstraSystems/threagile-builder#readme"
Issues = "https://github.com/vanHeemstraSystems/threagile-builder/issues"
Source = "https://github.com/vanHeemstraSystems/threagile-builder"

[tool.hatch.version]
path = "src/threagile_builder/__about__.py"

[tool.hatch.envs.default]
dependencies = [
"coverage[toml]>=6.5",
"pytest",
"flask"
]
[tool.hatch.envs.default.scripts]
app = "python src/threagile_builder/app.py"

[tool.hatch.envs.types]
extra-dependencies = [
"mypy>=1.0.0",
]

[tool.hatch.envs.types.scripts]
check = "mypy --install-types --non-interactive {args:src/threagile_builder tests}"

[tool.hatch.envs.test]
dependencies = [
"pytest",
"pytest-cov",
"pytest-watcher"
]

[tool.hatch.envs.test.scripts]
test = "pytest {args:tests}"
test-cov = "coverage run -m pytest {args:tests}"
cov-report = [
"- coverage combine",
"coverage report",
]
cov = [
"test-cov",
"cov-report",
]

[[tool.hatch.envs.test.matrix]]
python = ["3.10", "3.11"]

[[tool.hatch.envs.all.matrix]]
python = ["3.12"]

[tool.coverage.run]
source_pkgs = ["threagile_builder", "tests"]
branch = true
parallel = true
omit = [
"src/threagile_builder/__about__.py",
]

[tool.coverage.paths]
threagile_builder = ["src/threagile_builder", "*/threagile-builder/src/threagile_builder"]
tests = ["tests", "*/threagile-builder/tests"]

[tool.coverage.report]
exclude_lines = [
"no cov",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
]
14 changes: 14 additions & 0 deletions threagile-builder/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# threagile-builder/requirements.txt
Flask==3.0.3
Flask-SQLAlchemy==3.1.1
Flask-WTF==1.2.2
apiflask==2.2.1
black==24.10.0
opentelemetry-api==1.27.0
opentelemetry-sdk==1.27.0
opentelemetry-instrumentation-flask==0.48b0
opentelemetry-instrumentation-sqlalchemy==0.48b0
opentelemetry-exporter-prometheus==0.48b0
prefect==2.20.10
prometheus_client==0.15.0
python-dotenv==1.0.1
2 changes: 2 additions & 0 deletions threagile-builder/src/threagile_builder/__about__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# threagile-builder/scr/threagile_builder/__about__.py
__version__ = "0.0.1"
1 change: 1 addition & 0 deletions threagile-builder/src/threagile_builder/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# threagile-builder/scr/threagile_builder/__init__.py
7 changes: 7 additions & 0 deletions threagile-builder/src/threagile_builder/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# threagile-builder/src/threagile_builder/app.py
from utils import create_app

app = create_app()

if __name__ == "__main__":
app.run(debug=True)
2 changes: 2 additions & 0 deletions threagile-builder/src/threagile_builder/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# threagile-builder/scr/threagile_builder/config/__init__.py
from .config import Config
11 changes: 11 additions & 0 deletions threagile-builder/src/threagile_builder/config/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# threagile-builder/scr/threagile_builder/config/config.py
import os
from dotenv import load_dotenv

load_dotenv()


class Config:
SECRET_KEY = os.getenv("SECRET_KEY") or "secret"
SQLALCHEMY_DATABASE_URI = os.getenv("SQLALCHEMY_DATABASE_URI") or "sqlite:///app.db"
SQLALCHEMY_TRACK_MODIFICATIONS = os.getenv("SQLALCHEMY_TRACK_MODIFICATIONS")
2 changes: 2 additions & 0 deletions threagile-builder/src/threagile_builder/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# threagile-builder/scr/threagile_builder/forms/__init__.py
from .build_form import BuildForm
9 changes: 9 additions & 0 deletions threagile-builder/src/threagile_builder/forms/build_form.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# threagile-builder/scr/threagile_builder/forms/build_form_.py
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired


class BuildForm(FlaskForm):
title = StringField("Build Title", validators=[DataRequired()])
submit = SubmitField("Create Build")
2 changes: 2 additions & 0 deletions threagile-builder/src/threagile_builder/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# threagile-builder/scr/threagile_builder/models/__init__.py
from .build_model import Build
7 changes: 7 additions & 0 deletions threagile-builder/src/threagile_builder/models/build_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# threagile-builder/scr/threagile_builder/models/build_model.py
from utils.db_utils import db


class Build(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
3 changes: 3 additions & 0 deletions threagile-builder/src/threagile_builder/routes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# threagile-builder/scr/threagile_builder/routes/__init__.py
from .build_routes import build_bp
from .main_routes import main_bp
68 changes: 68 additions & 0 deletions threagile-builder/src/threagile_builder/routes/build_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# threagile-builder/scr/threagile_builder/routes/build_routes.py
import json
import logging
from apiflask import APIBlueprint
from flask import request, jsonify
from models import Build
from utils.db_utils import db
from workflows import build_workflow


build_bp = APIBlueprint("build", __name__)


# Create a Build
@build_bp.route("/builds", methods=["POST"])
def create_build():
data = request.json
new_build = Build(title=data["title"])
db.session.add(new_build)
db.session.commit()
return jsonify({"id": new_build.id, "title": new_build.title}), 201


# Read all Builds
@build_bp.route("/builds", methods=["GET"])
def get_builds():
builds = Build.query.all()
return jsonify([{"id": build.id, "title": build.title} for build in builds]), 200


# Read a single Build by ID
@build_bp.route("/builds/<int:id>", methods=["GET"])
def get_build(id):
build = Build.query.get_or_404(id)
return jsonify({"id": build.id, "title": build.title}), 200


# Update a Build by ID
@build_bp.route("/builds/<int:id>", methods=["PUT"])
def update_build(id):
build = Build.query.get_or_404(id)
data = request.json
build.title = data["title"]
db.session.commit()
return jsonify({"id": build.id, "title": build.title}), 200


# Delete a Build by ID
@build_bp.route("/builds/<int:id>", methods=["DELETE"])
def delete_build(id):
build = Build.query.get_or_404(id)
db.session.delete(build)
db.session.commit()
return jsonify({"message": "Build deleted successfully."}), 204


@build_bp.route("/run-build", methods=["POST"])
def run_build():
logging.info("Received request to run the Build's workflow.")
try:
data = json.loads(request.data)
logging.debug(f"Workflow data: {data}")
result = build_workflow()
logging.info("Build's workflow executed successfully.")
return result, 200
except Exception as e:
logging.error(f"Error occurred while running the workflow: {e}")
return {"error": "An error occurred while processing your request."}, 500
Loading

0 comments on commit 53707bb

Please sign in to comment.