Skip to content

Commit 7f54b14

Browse files
authored
Merge pull request #1874 from yoohya/feat/support-gitlab-lambda-webhooks
feat: Support GitLab webhooks in Lambda functions
2 parents 938ab9a + 235df73 commit 7f54b14

File tree

5 files changed

+106
-12
lines changed

5 files changed

+106
-12
lines changed

docker/Dockerfile.lambda

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM public.ecr.aws/lambda/python:3.12
1+
FROM public.ecr.aws/lambda/python:3.12 AS base
22

33
RUN dnf update -y && \
44
dnf install -y gcc python3-devel git && \
@@ -9,4 +9,10 @@ RUN pip install --no-cache-dir . && rm pyproject.toml
99
RUN pip install --no-cache-dir mangum==0.17.0
1010
COPY pr_agent/ ${LAMBDA_TASK_ROOT}/pr_agent/
1111

12-
CMD ["pr_agent.servers.serverless.serverless"]
12+
FROM base AS github_lambda
13+
CMD ["pr_agent.servers.github_lambda_webhook.lambda_handler"]
14+
15+
FROM base AS gitlab_lambda
16+
CMD ["pr_agent.servers.gitlab_lambda_webhook.lambda_handler"]
17+
18+
FROM github_lambda

docs/docs/installation/github.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,15 @@ For example: `GITHUB.WEBHOOK_SECRET` --> `GITHUB__WEBHOOK_SECRET`
187187
2. Build a docker image that can be used as a lambda function
188188
189189
```shell
190-
docker buildx build --platform=linux/amd64 . -t codiumai/pr-agent:serverless -f docker/Dockerfile.lambda
190+
# Note: --target github_lambda is optional as it's the default target
191+
docker buildx build --platform=linux/amd64 . -t codiumai/pr-agent:github_lambda --target github_lambda -f docker/Dockerfile.lambda
191192
```
192193

193194
3. Push image to ECR
194195

195196
```shell
196-
docker tag codiumai/pr-agent:serverless <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:serverless
197-
docker push <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:serverless
197+
docker tag codiumai/pr-agent:github_lambda <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:github_lambda
198+
docker push <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:github_lambda
198199
```
199200

200201
4. Create a lambda function that uses the uploaded image. Set the lambda timeout to be at least 3m.

docs/docs/installation/gitlab.md

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ git clone https://github.com/qodo-ai/pr-agent.git
6161
```
6262

6363
5. Prepare variables and secrets. Skip this step if you plan on setting these as environment variables when running the agent:
64-
1. In the configuration file/variables:
65-
- Set `config.git_provider` to "gitlab"
64+
1. In the configuration file/variables:
65+
- Set `config.git_provider` to "gitlab"
6666

67-
2. In the secrets file/variables:
68-
- Set your AI model key in the respective section
69-
- In the [gitlab] section, set `personal_access_token` (with token from step 2) and `shared_secret` (with secret from step 3)
67+
2. In the secrets file/variables:
68+
- Set your AI model key in the respective section
69+
- In the [gitlab] section, set `personal_access_token` (with token from step 2) and `shared_secret` (with secret from step 3)
7070

7171
6. Build a Docker image for the app and optionally push it to a Docker repository. We'll use Dockerhub as an example:
7272

@@ -88,3 +88,63 @@ OPENAI__KEY=<your_openai_api_key>
8888
8. Create a webhook in your GitLab project. Set the URL to `http[s]://<PR_AGENT_HOSTNAME>/webhook`, the secret token to the generated secret from step 3, and enable the triggers `push`, `comments` and `merge request events`.
8989

9090
9. Test your installation by opening a merge request or commenting on a merge request using one of PR Agent's commands.
91+
92+
## Deploy as a Lambda Function
93+
94+
Note that since AWS Lambda env vars cannot have "." in the name, you can replace each "." in an env variable with "__".<br>
95+
For example: `GITLAB.PERSONAL_ACCESS_TOKEN` --> `GITLAB__PERSONAL_ACCESS_TOKEN`
96+
97+
1. Follow steps 1-5 from [Run a GitLab webhook server](#run-a-gitlab-webhook-server).
98+
2. Build a docker image that can be used as a lambda function
99+
100+
```shell
101+
docker buildx build --platform=linux/amd64 . -t codiumai/pr-agent:gitlab_lambda --target gitlab_lambda -f docker/Dockerfile.lambda
102+
```
103+
104+
3. Push image to ECR
105+
106+
```shell
107+
docker tag codiumai/pr-agent:gitlab_lambda <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:gitlab_lambda
108+
docker push <AWS_ACCOUNT>.dkr.ecr.<AWS_REGION>.amazonaws.com/codiumai/pr-agent:gitlab_lambda
109+
```
110+
111+
4. Create a lambda function that uses the uploaded image. Set the lambda timeout to be at least 3m.
112+
5. Configure the lambda function to have a Function URL.
113+
6. In the environment variables of the Lambda function, specify `AZURE_DEVOPS_CACHE_DIR` to a writable location such as /tmp. (see [link](https://github.com/Codium-ai/pr-agent/pull/450#issuecomment-1840242269))
114+
7. Go back to steps 8-9 of [Run a GitLab webhook server](#run-a-gitlab-webhook-server) with the function url as your Webhook URL.
115+
The Webhook URL would look like `https://<LAMBDA_FUNCTION_URL>/webhook`
116+
117+
### Using AWS Secrets Manager
118+
119+
For production Lambda deployments, use AWS Secrets Manager instead of environment variables:
120+
121+
1. Create individual secrets for each GitLab webhook with this JSON format (e.g., secret name: `project-webhook-secret-001`)
122+
123+
```json
124+
{
125+
"gitlab_token": "glpat-xxxxxxxxxxxxxxxxxxxxxxxx",
126+
"token_name": "project-webhook-001"
127+
}
128+
```
129+
130+
2. Create a main configuration secret for common settings (e.g., secret name: `pr-agent-main-config`)
131+
132+
```json
133+
{
134+
"openai.key": "sk-proj-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
135+
}
136+
```
137+
138+
3. Set these environment variables in your Lambda:
139+
140+
```bash
141+
CONFIG__SECRET_PROVIDER=aws_secrets_manager
142+
AWS_SECRETS_MANAGER__SECRET_ARN=arn:aws:secretsmanager:us-east-1:123456789012:secret:pr-agent-main-config-AbCdEf
143+
```
144+
145+
4. In your GitLab webhook configuration, set the **Secret Token** to the **Secret name** created in step 1:
146+
- Example: `project-webhook-secret-001`
147+
148+
**Important**: When using Secrets Manager, GitLab's webhook secret must be the Secrets Manager secret name.
149+
150+
5. Add IAM permission `secretsmanager:GetSecretValue` to your Lambda execution role

pr_agent/servers/serverless.py renamed to pr_agent/servers/github_lambda_webhook.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
handler = Mangum(app, lifespan="off")
2424

2525

26-
def serverless(event, context):
27-
return handler(event, context)
26+
def lambda_handler(event, context):
27+
return handler(event, context)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from fastapi import FastAPI
2+
from mangum import Mangum
3+
from starlette.middleware import Middleware
4+
from starlette_context.middleware import RawContextMiddleware
5+
6+
from pr_agent.servers.gitlab_webhook import router
7+
8+
try:
9+
from pr_agent.config_loader import apply_secrets_manager_config
10+
apply_secrets_manager_config()
11+
except Exception as e:
12+
try:
13+
from pr_agent.log import get_logger
14+
get_logger().debug(f"AWS Secrets Manager initialization failed, falling back to environment variables: {e}")
15+
except:
16+
# Fail completely silently if log module is not available
17+
pass
18+
19+
middleware = [Middleware(RawContextMiddleware)]
20+
app = FastAPI(middleware=middleware)
21+
app.include_router(router)
22+
23+
handler = Mangum(app, lifespan="off")
24+
25+
26+
def lambda_handler(event, context):
27+
return handler(event, context)

0 commit comments

Comments
 (0)