From 0a1cfb5094e665d37c7e2c8f980d8387ebd7ae64 Mon Sep 17 00:00:00 2001 From: lambeb <141648830+lambeb@users.noreply.github.com> Date: Wed, 29 Jan 2025 16:38:46 +0000 Subject: [PATCH] added github workflows and removed some unused code --- .github/workflows/codeql-analysis.yml | 38 ++++++++++++++ .github/workflows/test.py.yaml | 71 +++++++++++++++++++++++++++ Makefile | 1 + main.py | 4 +- poetry.lock | 9 ++-- services/validation_service.py | 58 +--------------------- tests/__init__.py | 0 utilities/__init__.py | 0 8 files changed, 119 insertions(+), 62 deletions(-) create mode 100644 .github/workflows/codeql-analysis.yml create mode 100644 .github/workflows/test.py.yaml create mode 100644 tests/__init__.py create mode 100644 utilities/__init__.py diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000..d23c35c --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,38 @@ +name: "CodeQL" + +on: + push: + branches: [ main, preprod, prod ] + pull_request: + branches: [ main ] + schedule: + - cron: '35 10 * * 6' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'python' ] + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/test.py.yaml b/.github/workflows/test.py.yaml new file mode 100644 index 0000000..b8f5ec7 --- /dev/null +++ b/.github/workflows/test.py.yaml @@ -0,0 +1,71 @@ +name: Test coverage report + +on: + pull_request: + branches: [ main ] + push: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [ 3.9 ] + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install poetry + poetry install + - name: Run pytest + run: | + poetry run python -m pytest --cov=./ + poetry run python -m coverage xml -i + - name: Codecov + uses: codecov/codecov-action@v1.5.0 + with: + token: ${{ secrets.CODECOV_TOKEN }} + fail_ci_if_error: true + + lint: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: [ 3.9 ] + steps: + - uses: actions/checkout@v2 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + pip install poetry + poetry install + - name: Run black + run: poetry run black --check . + - name: Run isort + run: poetry run isort --check . + +# check-types: +# runs-on: ubuntu-latest +# strategy: +# matrix: +# python-version: [ 3.9 ] +# steps: +# - uses: actions/checkout@v2 +# - name: Set up Python ${{ matrix.python-version }} +# uses: actions/setup-python@v2 +# with: +# python-version: ${{ matrix.python-version }} +# - name: Install dependencies +# run: | +# pip install poetry +# poetry install +# - name: Run mypy +# run: poetry run mypy . diff --git a/Makefile b/Makefile index 7bff073..884be47 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ lint: @poetry run black --check . @poetry run isort --check . @poetry run flake8 --ignore=E501 . + @poetry run mypy --config-file ${mkfile_dir}mypy.ini . .PHONY: test ## Run unit tests diff --git a/main.py b/main.py index 72afff8..5108229 100644 --- a/main.py +++ b/main.py @@ -86,4 +86,6 @@ def process_zip_file(data, _context): if __name__ == "__main__": - process_zip_file({"bucket": "ons-blaise-v2-dev-rr3-ingest", "name": "IPS2501A.zip"}) + process_zip_file( + {"bucket": "ons-blaise-v2-dev-rr3-ingest", "name": "IPS2501A.zip"}, None + ) diff --git a/poetry.lock b/poetry.lock index 1f177c1..38400e3 100644 --- a/poetry.lock +++ b/poetry.lock @@ -526,13 +526,13 @@ grpc = ["grpcio (>=1.38.0,<2.0dev)", "grpcio-status (>=1.38.0,<2.0.dev0)"] [[package]] name = "google-cloud-logging" -version = "3.11.3" +version = "3.11.4" description = "Stackdriver Logging API client library" optional = false python-versions = ">=3.7" files = [ - {file = "google_cloud_logging-3.11.3-py2.py3-none-any.whl", hash = "sha256:b8ec23f2998f76a58f8492db26a0f4151dd500425c3f08448586b85972f3c494"}, - {file = "google_cloud_logging-3.11.3.tar.gz", hash = "sha256:0a73cd94118875387d4535371d9e9426861edef8e44fba1261e86782d5b8d54f"}, + {file = "google_cloud_logging-3.11.4-py2.py3-none-any.whl", hash = "sha256:1d465ac62df29fb94bba4d6b4891035e57d573d84541dd8a40eebbc74422b2f0"}, + {file = "google_cloud_logging-3.11.4.tar.gz", hash = "sha256:32305d989323f3c58603044e2ac5d9cf23e9465ede511bbe90b4309270d3195c"}, ] [package.dependencies] @@ -544,7 +544,8 @@ google-cloud-core = ">=2.0.0,<3.0.0dev" grpc-google-iam-v1 = ">=0.12.4,<1.0.0dev" opentelemetry-api = ">=1.9.0" proto-plus = [ - {version = ">=1.22.2,<2.0.0dev", markers = "python_version >= \"3.11\""}, + {version = ">=1.25.0,<2.0.0dev", markers = "python_version >= \"3.13\""}, + {version = ">=1.22.2,<2.0.0dev", markers = "python_version >= \"3.11\" and python_version < \"3.13\""}, {version = ">=1.22.0,<2.0.0dev", markers = "python_version < \"3.11\""}, ] protobuf = ">=3.20.2,<4.21.0 || >4.21.0,<4.21.1 || >4.21.1,<4.21.2 || >4.21.2,<4.21.3 || >4.21.3,<4.21.4 || >4.21.4,<4.21.5 || >4.21.5,<6.0.0dev" diff --git a/services/validation_service.py b/services/validation_service.py index 5684eac..4bebf34 100644 --- a/services/validation_service.py +++ b/services/validation_service.py @@ -1,11 +1,9 @@ import logging -import re import blaise_restapi -import flask from appconfig.config import Config -from utilities.custom_exceptions import BlaiseError, ConfigError, RequestError +from utilities.custom_exceptions import BlaiseError, ConfigError from utilities.logging import function_name @@ -13,60 +11,6 @@ class ValidationService: def __init__(self) -> None: self.request_json = None - def get_valid_request_values_for_ingest_service( - self, request: flask.request - ) -> tuple[str, str, str]: - self.validate_request_is_json(request) - self.validate_request_values_are_not_empty() - self.validate_questionnaire_name() - - return ( - self.request_json["serverParkName"], - self.request_json["questionnaireName"], - self.request_json["tempFilePath"], - ) - - def validate_request_is_json(self, request): - try: - self.request_json = request.get_json() - except Exception as e: - error_message = ( - f"Exception raised in {function_name()}. " - f"Error getting json from request '{request}': {e}" - ) - logging.error(error_message) - raise RequestError(error_message) - - def validate_request_values_are_not_empty(self): - missing_values = [] - questionnaire_name = self.request_json["questionnaire_name"] - server_park = self.request_json["server_park"] - - if questionnaire_name is None or questionnaire_name == "": - missing_values.append("questionnaire_name") - - if server_park is None or server_park == "": - missing_values.append("server_park") - - if missing_values: - error_message = f"Missing required values from request: {missing_values}" - logging.error(error_message) - raise RequestError(error_message) - - def validate_questionnaire_name(self): - result = re.match( - r"^[A-Za-z]{3}\d{4}.*$", self.request_json["questionnaire_name"] - ) - if not result: - error_message = ( - f"{self.request_json['questionnaire_name']} is not a valid " - "questionnaire name format. " - "Questionnaire name must start with 3 letters, followed by 4 numbers" - ) - - logging.error(error_message) - raise RequestError(error_message) - @staticmethod def validate_config(config): missing_configs = [] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utilities/__init__.py b/utilities/__init__.py new file mode 100644 index 0000000..e69de29