Remaining files
- User
Remaining files
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Remaining files
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here are the remaining files needed to complete the working prototype implementation:
import requests
class GitLabClient:
def __init__(self, private_token):
self.base_url = "https://gitlab.com/api/v4"
self.headers = {
"Private-Token": private_token,
"Content-Type": "application/json"
}
def get_mr_changes(self, project_id, mr_iid):
url = f"{self.base_url}/projects/{project_id}/merge_requests/{mr_iid}/changes"
response = requests.get(url, headers=self.headers)
response.raise_for_status()
return response.json().get("changes", [])
def post_mr_comment(self, project_id, mr_iid, body, line=None):
url = f"{self.base_url}/projects/{project_id}/merge_requests/{mr_iid}/notes"
data = {
"body": body,
"position": {
"base_sha": "",
"start_sha": "",
"head_sha": "",
"position_type": "text",
"new_path": "",
"old_path": "",
"new_line": line,
"old_line": line
} if line else {}
}
response = requests.post(url, headers=self.headers, json=data)
response.raise_for_status()
return response.json()
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "main.py"]
google-cloud-aiplatform==1.32.3
flask==2.3.2
requests==2.31.0
gitlab-client==1.0.0 # (or appropriate version)
provider "google" {
project = var.project_id
region = var.region
}
resource "google_pubsub_topic" "gitlab_mr_analysis" {
name = "gitlab-mr-analysis"
}
resource "google_cloudfunctions_function" "webhook_handler" {
name = "webhook-handler"
runtime = "python39"
entry_point = "handle_webhook"
available_memory = "256MB"
timeout = "60s"
source_archive_bucket = var.bucket_name
source_archive_object = "webhook-handler.zip"
environment_variables = {
GITLAB_WEBHOOK_TOKEN = var.gitlab_webhook_token
GCP_PROJECT = var.project_id
}
event_trigger {
event_type = "google.pubsub.topic.publish"
resource = google_pubsub_topic.gitlab_mr_analysis.id
failure_policy {}
}
}
resource "google_cloud_run_service" "code_analyzer" {
name = "code-analyzer"
location = var.region
template {
spec {
containers {
image = "gcr.io/${var.project_id}/code-analyzer:latest"
env {
name = "GITLAB_TOKEN"
value = var.gitlab_api_token
}
env {
name = "GCP_PROJECT"
value = var.project_id
}
}
}
}
traffic {
latest_revision_weight = 100
}
}
steps:
- name: 'gcr.io/cloud-builders/gcloud'
args: ['functions', 'deploy', 'webhook-handler',
'--source', 'src/webhook_handler',
'--runtime', 'python39',
'--trigger-topic', 'gitlab-mr-analysis',
'--set-env-vars', 'GITLAB_WEBHOOK_TOKEN=$_GITLAB_WEBHOOK_TOKEN',
'--set-env-vars', 'GCP_PROJECT=$_GCP_PROJECT']
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/code-analyzer:latest', '.' ]
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/code-analyzer:latest']
- name: 'gcr.io/cloud-builders/gcloud'
args: ['run', 'deploy', 'code-analyzer',
'--image', 'gcr.io/$PROJECT_ID/code-analyzer:latest',
'--platform', 'managed',
'--region', 'us-central1',
'--allow-unauthenticated',
'--set-env-vars', 'GITLAB_TOKEN=$_GITLAB_API_TOKEN',
'--set-env-vars', 'GCP_PROJECT=$_GCP_PROJECT']
include:
- project: 'your-namespace/gitlab-ai-reviewer'
file: '/ci-component/gitlab-ai-review.yml'
variables:
GCP_PROJECT: "your-gcp-project-id"
REGION: "us-central1"
GITLAB_WEBHOOK_TOKEN: "$GITLAB_WEBHOOK_TOKEN_SECRET"
GITLAB_API_TOKEN: "$GITLAB_API_TOKEN_SECRET"
stages:
- build
- deploy
- test
build-webhook-handler:
stage: build
image: google/cloud-sdk:latest
script:
- gcloud components install beta
- gcloud functions deploy webhook-handler \
--runtime python39 \
--trigger-http \
--allow-unauthenticated \
--entry-point handle_webhook \
--set-env-vars="GITLAB_WEBHOOK_TOKEN=${GITLAB_WEBHOOK_TOKEN},GCP_PROJECT=${GCP_PROJECT}"
only:
- main
deploy-code-analyzer:
stage: deploy
image: google/cloud-sdk:latest
script:
- gcloud builds submit --config cloudbuild.yaml .
only:
- main
test-end-to-end:
stage: test
image: google/cloud-sdk:latest
script:
- gcloud auth activate-service-account --key-file ${GOOGLE_APPLICATION_CREDENTIALS}
- curl -X POST https://webhook-handler-url.cloudfunctions.net/webhook \
-H "X-Gitlab-Token: ${GITLAB_WEBHOOK_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"object_kind": "merge_request", "project": {"id": "123"}, "object_attributes": {"iid": "456", "action": "open"}}'
only:
- main
GITLAB_WEBHOOK_TOKEN=your-gitlab-webhook-token
GITLAB_API_TOKEN=your-gitlab-api-token
GCP_PROJECT=your-gcp-project-id
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
# GitLab AI Code Review Assistant
## Setup
1. **Environment Variables**: Copy `.env.example` to `.env` and populate values.
2. **Google Cloud**: Enable required APIs (Cloud Functions, Cloud Run, Pub/Sub).
3. **GitLab**: Create a webhook pointing to your Cloud Function URL.
4. **Deploy**: Push to `main` branch to trigger CI/CD pipeline.
## Usage
1. Open a merge request in GitLab.
2. The AI reviewer will analyze changes and post comments automatically.
3. Review suggestions in the merge request UI.
## Demo

graph TD
A[GitLab Merge Request] -->|Webhook| B[Cloud Function (Webhook Handler)]
B --> C[Pub/Sub Topic]
C --> D[Cloud Run (Code Analyzer)]
D --> E[Vertex AI]
D --> F[GitLab API Comments]
Add these to your .gitlab-ci.yml for monitoring:
monitoring:
stage: test
image: google/cloud-sdk:latest
script:
- gcloud logging read "resource.type=cloud_run_revision" --format=json > logs.json
- gcloud functions logs read webhook-handler --limit 10 > function-logs.txt
This completes the full implementation. Deploy using the CI/CD pipeline and test by creating a merge request in GitLab. The AI will automatically analyze the code and post suggestions!
Not yet included in a published release.