Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.venv
__pycache__
*.pyc
.pytest_cache
.git
tests
tools/eslint/node_modules
.env
13 changes: 13 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,16 @@ DB_PASSWORD=postgres
# exists on that broker.
# -----------------------------------------------------------------------
RABBITMQ_URL=amqp://guest:guest@localhost:5672

# -----------------------------------------------------------------------
# CORS — origin allowed to read analysis results directly (web-interface)
# -----------------------------------------------------------------------
FRONTEND_ORIGIN=http://localhost:3000

# AI review agents (optional). Without it, reviews are skipped.
OPENAI_API_KEY=

# Private repositories: shared with integration-service (see its .env.example).
# Without it, only public repositories can be analyzed.
INTEGRATION_SERVICE_URL=http://localhost:5001
INTERNAL_SERVICE_TOKEN=
98 changes: 98 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: CI

on:
push:
branches: ["**"]
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read

jobs:
check:
name: Tests
runs-on: ubuntu-latest

# Part of the suite is marked `integration` and stores a real analysis
# result through the real schema, because the JSONB columns are exactly
# where a silent coercion would hide. Those tests need a database, and
# they create their own schema, so an empty Postgres is enough.
services:
postgres:
image: postgres:16-alpine
env:
POSTGRES_USER: ci
POSTGRES_PASSWORD: ci
POSTGRES_DB: analysis_engine_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

steps:
- uses: actions/checkout@v4

- uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: pip

# This service analyses JavaScript as well as Python, and part of the
# suite runs the real ESLint rather than a mock (the `integration`
# marker in pyproject.toml). That toolchain is this service's own,
# never the analysed repository's.
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
cache-dependency-path: tools/eslint/package-lock.json

- name: Install ESLint toolchain
run: npm ci
working-directory: tools/eslint

- name: Install
run: pip install -e ".[dev]"

# Settings has no defaults for the database fields, on purpose: the
# service refuses to start half-configured. That check runs at import
# time, so the suite needs them present even though no unit test
# opens a connection — locally they come from .env, which is not in
# the repository.
#
# No OPENAI_API_KEY is set, deliberately. The agent tests stub the
# provider; if one ever reaches the real API it will fail here rather
# than quietly spending money on every push.
- name: Tests
run: pytest -q
env:
DB_HOST: localhost
DB_NAME: analysis_engine_test
DB_USER: ci
DB_PASSWORD: ci

image:
name: Container image
needs: check
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: docker/setup-buildx-action@v3

- name: Build image
uses: docker/build-push-action@v6
with:
context: .
push: false
tags: analysis-engine:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
78 changes: 78 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
name: Deploy

# Only main, and only after CI has passed on it: a red build must never
# reach the running platform.
on:
workflow_run:
workflows: ["CI"]
branches: [main]
types: [completed]
# So a deploy can be re-run without pushing an empty commit.
workflow_dispatch:

concurrency:
group: deploy
cancel-in-progress: false

permissions:
contents: read
id-token: write # required to request the OIDC token AWS trusts

jobs:
deploy:
name: Build, push and roll out
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success'

steps:
- uses: actions/checkout@v4

# No access key anywhere: GitHub signs a short-lived token, AWS
# trusts it for this repository on main only, and the credentials
# expire when the job ends.
- name: Assume the deploy role
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::814454905947:role/codepulse-github-deploy
aws-region: us-east-1

- uses: aws-actions/amazon-ecr-login@v2
id: ecr

- uses: docker/setup-buildx-action@v3

- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: |
${{ steps.ecr.outputs.registry }}/codepulse/analysis-engine-service:${{ github.sha }}
${{ steps.ecr.outputs.registry }}/codepulse/analysis-engine-service:current
cache-from: type=gha
cache-to: type=gha,mode=max

# The host pulls what was just pushed and restarts this one service.
# Sent through SSM because the instance has no SSH port and no key.
- name: Roll out on the application host
run: |
COMMAND_ID=$(aws ssm send-command \
--instance-ids i-059cdefb4c573f592 \
--document-name AWS-RunShellScript \
--comment "deploy analysis-engine-service ${GITHUB_SHA::7}" \
--parameters 'commands=["aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 814454905947.dkr.ecr.us-east-1.amazonaws.com && cd /opt/codepulse && docker compose --env-file .env pull analysis && docker compose --env-file .env up -d analysis && docker compose --env-file .env ps analysis"]' \
--query 'Command.CommandId' --output text)

for _ in $(seq 1 60); do
STATUS=$(aws ssm get-command-invocation --command-id "$COMMAND_ID" \
--instance-id i-059cdefb4c573f592 --query Status --output text 2>/dev/null || echo Pending)
case "$STATUS" in Success|Failed|TimedOut|Cancelled) break ;; esac
sleep 5
done

aws ssm get-command-invocation --command-id "$COMMAND_ID" \
--instance-id i-059cdefb4c573f592 --query StandardOutputContent --output text

# The job must fail when the rollout does, or a broken deploy
# looks green.
[ "$STATUS" = "Success" ]
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ venv/
dist/
build/
.pytest_cache/

# ESLint's own toolchain — not source, installed via tools/eslint's own
# npm install (see README.md). package-lock.json IS committed
# (reproducible installs), only node_modules/ itself is ignored.
tools/eslint/node_modules/

# Evaluation runs (analysis_engine.evaluation) — local comparison data
evaluation-results/
47 changes: 47 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Reproducible runtime for both toolchains this service shells out to:
# - Python: Pylint/Radon/Bandit, installed as pinned dependencies via
# pyproject.toml (see analyzers/python/README.md for exact versions).
# - Node.js: ESLint, installed via tools/eslint/'s own package.json (see
# analyzers/README.md) — never the analyzed repository's own toolchain,
# for either language.
#
# git is required too: workspace/git_client.py shells out to the real git
# binary to clone/checkout the analyzed repository.
FROM python:3.12-slim

RUN apt-get update \
&& apt-get install -y --no-install-recommends git curl gnupg \
&& curl -fsSL https://deb.nodesource.com/setup_22.x | bash - \
&& apt-get install -y --no-install-recommends nodejs \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

# Python dependencies (FastAPI, asyncpg, aio-pika, Pylint, Radon, Bandit —
# see pyproject.toml for exact pins) — installed before copying the rest
# of the source so this layer only rebuilds when dependencies change.
#
# The empty package is not decoration: setuptools resolves
# [tool.setuptools.packages.find] where = ["src"] while working out what an
# editable install points at, and with no src/ at all the build fails
# outright ("Getting requirements to build editable did not run
# successfully"). A placeholder is enough to satisfy it; COPY . . below
# replaces it with the real source, which the editable install already
# points to.
COPY pyproject.toml ./
RUN mkdir -p src/analysis_engine \
&& touch src/analysis_engine/__init__.py \
&& pip install --no-cache-dir -e ".[dev]"

# ESLint's own dedicated toolchain (never the analyzed repository's).
COPY tools/eslint/package.json tools/eslint/package-lock.json tools/eslint/
RUN cd tools/eslint && npm ci

COPY . .
# Re-run install now that the real source is present (editable install
# above only needed pyproject.toml to resolve dependencies).
RUN pip install --no-cache-dir -e ".[dev]"

EXPOSE 8000

CMD ["uvicorn", "analysis_engine.main:app", "--app-dir", "src", "--host", "0.0.0.0", "--port", "8000"]
Loading
Loading