Skip to content

Commit aa6854e

Browse files
feat: allow PORT override for GitLab webhook server (#2157)
1 parent da0c992 commit aa6854e

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/docs/installation/gitlab.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ GITLAB__SHARED_SECRET=<shared_secret>
8888
GITLAB__URL=https://gitlab.com
8989
GITLAB__AUTH_TYPE=oauth_token # Use "private_token" for older GitLab versions
9090
OPENAI__KEY=<your_openai_api_key>
91+
PORT=3000 # Optional: override the webhook server port
9192
```
9293

9394
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`.
@@ -152,4 +153,4 @@ AWS_SECRETS_MANAGER__SECRET_ARN=arn:aws:secretsmanager:us-east-1:123456789012:se
152153

153154
**Important**: When using Secrets Manager, GitLab's webhook secret must be the Secrets Manager secret name.
154155

155-
5. Add IAM permission `secretsmanager:GetSecretValue` to your Lambda execution role
156+
5. Add IAM permission `secretsmanager:GetSecretValue` to your Lambda execution role

pr_agent/servers/gitlab_webhook.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import json
3+
import os
34
import re
45
from datetime import datetime
56

@@ -309,7 +310,23 @@ async def root():
309310

310311

311312
def start():
312-
uvicorn.run(app, host="0.0.0.0", port=3000)
313+
"""
314+
Start the GitLab webhook server.
315+
316+
The server port can be configured via the PORT environment variable.
317+
Defaults to 3000 if PORT is not set or invalid.
318+
"""
319+
raw_port = os.environ.get("PORT")
320+
try:
321+
port = int(raw_port) if raw_port else 3000
322+
if not (1 <= port <= 65535):
323+
raise ValueError(f"Port {port} is out of valid range")
324+
if raw_port:
325+
get_logger().info(f"Using custom PORT from environment: {port}")
326+
except ValueError as e:
327+
get_logger().warning(f"Invalid PORT environment variable ({e}), using default port 3000")
328+
port = 3000
329+
uvicorn.run(app, host="0.0.0.0", port=port)
313330

314331

315332
if __name__ == '__main__':
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import os
2+
from unittest import mock
3+
4+
os.environ.setdefault("GITLAB__URL", "https://gitlab.example.com")
5+
import pr_agent.servers.gitlab_webhook as gitlab_webhook
6+
7+
8+
def test_start_uses_port_env(monkeypatch):
9+
monkeypatch.setenv("PORT", "4567")
10+
11+
with mock.patch.object(gitlab_webhook.uvicorn, "run") as mock_run:
12+
gitlab_webhook.start()
13+
14+
_, kwargs = mock_run.call_args
15+
assert kwargs["port"] == 4567
16+
assert kwargs["host"] == "0.0.0.0"
17+
18+
19+
def test_start_invalid_port_env(monkeypatch):
20+
monkeypatch.setenv("PORT", "not-a-number")
21+
22+
with mock.patch.object(gitlab_webhook.uvicorn, "run") as mock_run:
23+
gitlab_webhook.start()
24+
25+
_, kwargs = mock_run.call_args
26+
assert kwargs["port"] == 3000
27+
28+
29+
def test_start_default_port(monkeypatch):
30+
monkeypatch.delenv("PORT", raising=False)
31+
32+
with mock.patch.object(gitlab_webhook.uvicorn, "run") as mock_run:
33+
gitlab_webhook.start()
34+
35+
_, kwargs = mock_run.call_args
36+
assert kwargs["port"] == 3000
37+
38+
39+
def test_start_invalid_port_range(monkeypatch):
40+
monkeypatch.setenv("PORT", "70000")
41+
42+
with mock.patch.object(gitlab_webhook.uvicorn, "run") as mock_run:
43+
gitlab_webhook.start()
44+
45+
_, kwargs = mock_run.call_args
46+
assert kwargs["port"] == 3000

0 commit comments

Comments
 (0)