mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-03-27 18:38:52 +00:00
204 lines
6.7 KiB
YAML
204 lines
6.7 KiB
YAML
name: API - Pull Request
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- "master"
|
|
- "v5.*"
|
|
paths:
|
|
- ".github/workflows/api-pull-request.yml"
|
|
- "api/**"
|
|
pull_request:
|
|
branches:
|
|
- "master"
|
|
- "v5.*"
|
|
paths:
|
|
- "api/**"
|
|
|
|
env:
|
|
POSTGRES_HOST: localhost
|
|
POSTGRES_PORT: 5432
|
|
POSTGRES_ADMIN_USER: prowler
|
|
POSTGRES_ADMIN_PASSWORD: S3cret
|
|
POSTGRES_USER: prowler_user
|
|
POSTGRES_PASSWORD: prowler
|
|
POSTGRES_DB: postgres-db
|
|
VALKEY_HOST: localhost
|
|
VALKEY_PORT: 6379
|
|
VALKEY_DB: 0
|
|
API_WORKING_DIR: ./api
|
|
IMAGE_NAME: prowler-api
|
|
|
|
jobs:
|
|
test:
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
matrix:
|
|
python-version: ["3.12"]
|
|
|
|
# Service containers to run with `test`
|
|
services:
|
|
# Label used to access the service container
|
|
postgres:
|
|
image: postgres
|
|
env:
|
|
POSTGRES_HOST: ${{ env.POSTGRES_HOST }}
|
|
POSTGRES_PORT: ${{ env.POSTGRES_PORT }}
|
|
POSTGRES_USER: ${{ env.POSTGRES_USER }}
|
|
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
|
|
POSTGRES_DB: ${{ env.POSTGRES_DB }}
|
|
# Set health checks to wait until postgres has started
|
|
ports:
|
|
- 5432:5432
|
|
options: >-
|
|
--health-cmd pg_isready
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
valkey:
|
|
image: valkey/valkey:7-alpine3.19
|
|
env:
|
|
VALKEY_HOST: ${{ env.VALKEY_HOST }}
|
|
VALKEY_PORT: ${{ env.VALKEY_PORT }}
|
|
VALKEY_DB: ${{ env.VALKEY_DB }}
|
|
# Set health checks to wait until postgres has started
|
|
ports:
|
|
- 6379:6379
|
|
options: >-
|
|
--health-cmd "valkey-cli ping"
|
|
--health-interval 10s
|
|
--health-timeout 5s
|
|
--health-retries 5
|
|
|
|
steps:
|
|
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
|
|
- name: Test if changes are in not ignored paths
|
|
id: are-non-ignored-files-changed
|
|
uses: tj-actions/changed-files@ed68ef82c095e0d48ec87eccea555d944a631a4c # v46.0.5
|
|
with:
|
|
files: api/**
|
|
files_ignore: |
|
|
api/.github/**
|
|
api/docs/**
|
|
api/permissions/**
|
|
api/README.md
|
|
api/mkdocs.yml
|
|
|
|
- name: Replace @master with current branch in pyproject.toml
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME}}"
|
|
echo "Using branch: $BRANCH_NAME"
|
|
sed -i "s|@master|@$BRANCH_NAME|g" pyproject.toml
|
|
|
|
- name: Install poetry
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
python -m pip install --upgrade pip
|
|
pipx install poetry==2.1.1
|
|
|
|
- name: Update poetry.lock after the branch name change
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry lock
|
|
|
|
- name: Set up Python ${{ matrix.python-version }}
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
|
|
with:
|
|
python-version: ${{ matrix.python-version }}
|
|
cache: "poetry"
|
|
|
|
- name: Install dependencies
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry install --no-root
|
|
poetry run pip list
|
|
VERSION=$(curl --silent "https://api.github.com/repos/hadolint/hadolint/releases/latest" | \
|
|
grep '"tag_name":' | \
|
|
sed -E 's/.*"v([^"]+)".*/\1/' \
|
|
) && curl -L -o /tmp/hadolint "https://github.com/hadolint/hadolint/releases/download/v${VERSION}/hadolint-Linux-x86_64" \
|
|
&& chmod +x /tmp/hadolint
|
|
|
|
- name: Poetry check
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry check --lock
|
|
|
|
- name: Lint with ruff
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run ruff check . --exclude contrib
|
|
|
|
- name: Check Format with ruff
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run ruff format --check . --exclude contrib
|
|
|
|
- name: Lint with pylint
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run pylint --disable=W,C,R,E -j 0 -rn -sn src/
|
|
|
|
- name: Bandit
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run bandit -q -lll -x '*_test.py,./contrib/' -r .
|
|
|
|
- name: Safety
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run safety check --ignore 70612,66963,74429
|
|
|
|
- name: Vulture
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run vulture --exclude "contrib,tests,conftest.py" --min-confidence 100 .
|
|
|
|
- name: Hadolint
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
/tmp/hadolint Dockerfile --ignore=DL3013
|
|
|
|
- name: Test with pytest
|
|
working-directory: ./api
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
run: |
|
|
poetry run pytest --cov=./src/backend --cov-report=xml src/backend
|
|
|
|
- name: Upload coverage reports to Codecov
|
|
if: steps.are-non-ignored-files-changed.outputs.any_changed == 'true'
|
|
uses: codecov/codecov-action@ad3126e916f78f00edff4ed0317cf185271ccc2d # v5.4.2
|
|
env:
|
|
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
|
with:
|
|
flags: api
|
|
test-container-build:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2 # v3.10.0
|
|
- name: Build Container
|
|
uses: docker/build-push-action@14487ce63c7a62a4a324b0bfb37086795e31c6c1 # v6.16.0
|
|
with:
|
|
context: ${{ env.API_WORKING_DIR }}
|
|
push: false
|
|
tags: ${{ env.IMAGE_NAME }}:latest
|
|
outputs: type=docker
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|