Skip to content

Commit 85c3c70

Browse files
committed
Gitea
1 parent 542e2ec commit 85c3c70

File tree

9 files changed

+92
-96
lines changed

9 files changed

+92
-96
lines changed

.env.sample

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ POSTGRES_SCHEMA="public"
22
DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=${POSTGRES_SCHEMA}"
33

44

5-
# GitHub Webhook Configuration
6-
GITHUB_WEBHOOK_SECRET="your_webhook_secret_here"
5+
# Gitea Webhook Configuration
6+
GITEA_WEBHOOK_SECRET="your_webhook_secret_here"
77

88
# Kafka Configuration
99
KAFKA_BROKERS=localhost:9092
Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# GitHub Webhook Integration Setup and Testing Guide
1+
# Gitea Webhook Integration Setup and Testing Guide
22

33
## Overview
44

5-
The Topcoder Review API includes a secure GitHub webhook integration that receives webhook events from GitHub repositories, validates them using HMAC-SHA256 signature verification, and stores them in the database for audit and future processing.
5+
The Topcoder Review API includes a secure Gitea webhook integration that receives webhook events from Gitea repositories, validates them using HMAC-SHA256 signature verification, and stores them in the database for audit and future processing.
66

77
## Table of Contents
88

99
1. [Quick Start](#quick-start)
1010
2. [Environment Setup](#environment-setup)
11-
3. [GitHub Repository Configuration](#github-repository-configuration)
11+
3. [Gitea Repository Configuration](#Gitea-repository-configuration)
1212
4. [Local Development Setup](#local-development-setup)
1313
5. [Testing the Integration](#testing-the-integration)
1414
6. [API Endpoint Reference](#api-endpoint-reference)
@@ -23,7 +23,7 @@ For immediate setup, follow these steps:
2323

2424
1. Generate a secure webhook secret
2525
2. Configure environment variables
26-
3. Set up GitHub webhook in repository settings
26+
3. Set up Gitea webhook in repository settings
2727
4. Test with a sample event
2828

2929
## Environment Setup
@@ -34,22 +34,24 @@ Add the following environment variable to your application configuration:
3434

3535
```bash
3636
# .env file
37-
GITHUB_WEBHOOK_SECRET=your_generated_secret_here
37+
GITEA_WEBHOOK_SECRET=your_generated_secret_here
3838
```
3939

4040
### Generate Webhook Secret
4141

4242
**Using OpenSSL:**
43+
4344
```bash
4445
openssl rand -hex 32
4546
```
4647

4748
**Example Output:**
49+
4850
```
4951
a1b2c3d4e5f6789012345678901234567890abcdef1234567890abcdef123456
5052
```
5153

52-
⚠️ **Important:** Store this secret securely and use the same value in both your application environment and GitHub webhook configuration.
54+
⚠️ **Important:** Store this secret securely and use the same value in both your application environment and Gitea webhook configuration.
5355

5456
### Database Setup
5557

@@ -59,11 +61,11 @@ The webhook integration requires the `gitWebhookLog` table. If not already creat
5961
npx prisma migrate dev
6062
```
6163

62-
## GitHub Repository Configuration
64+
## Gitea Repository Configuration
6365

6466
### Step 1: Access Repository Settings
6567

66-
1. Navigate to your GitHub repository
68+
1. Navigate to your Gitea repository
6769
2. Click on the **Settings** tab (requires admin permissions)
6870
3. In the left sidebar, click **Webhooks**
6971
4. Click **Add webhook**
@@ -73,25 +75,30 @@ npx prisma migrate dev
7375
#### Payload URL
7476

7577
**Production/Staging Environment:**
78+
7679
```
77-
https://your-api-domain.com/v6/review/webhooks/git
80+
https://your-api-domain.com/v6/review/webhooks/gitea
7881
```
7982

8083
**Development Environment:**
84+
8185
```
82-
https://your-dev-domain.com/webhooks/git
86+
https://your-dev-domain.com/webhooks/gitea
8387
```
8488

8589
Note: The `/v6/review` prefix is only added in production when `NODE_ENV=production`.
8690

8791
#### Content Type
92+
8893
- Select `application/json`
8994

9095
#### Secret
96+
9197
- Enter the webhook secret you generated earlier
92-
- This must exactly match your `GITHUB_WEBHOOK_SECRET` environment variable
98+
- This must exactly match your `GITEA_WEBHOOK_SECRET` environment variable
9399

94100
#### SSL Verification
101+
95102
- Keep **Enable SSL verification** checked (recommended for production)
96103
- For development with proper HTTPS setup, this should remain enabled
97104

@@ -100,10 +107,12 @@ Note: The `/v6/review` prefix is only added in production when `NODE_ENV=product
100107
Choose one of the following options:
101108

102109
**Option A: Send Everything (Recommended for Testing)**
103-
- Select "Send me everything" to receive all GitHub event types
110+
111+
- Select "Send me everything" to receive all Gitea event types
104112

105113
**Option B: Select Individual Events**
106114
Common events for development workflows:
115+
107116
- **Pushes** - Code pushes to repository
108117
- **Pull requests** - PR creation, updates, merges
109118
- **Issues** - Issue creation, updates, comments
@@ -116,18 +125,20 @@ Common events for development workflows:
116125

117126
1. Ensure **Active** checkbox is checked
118127
2. Click **Add webhook**
119-
3. GitHub will automatically send a `ping` event to test the webhook
128+
3. Gitea will automatically send a `ping` event to test the webhook
120129

121130
## Local Development Setup
122131

123-
Since GitHub webhooks require a publicly accessible URL, local development requires exposing your local server to the internet.
132+
Since Gitea webhooks require a publicly accessible URL, local development requires exposing your local server to the internet.
124133

125134
**Install ngrok:**
135+
126136
```bash
127137
npm install -g ngrok
128138
```
129139

130140
**Setup process:**
141+
131142
```bash
132143
# 1. Start your local API server
133144
pnpm run start:dev
@@ -138,16 +149,17 @@ ngrok http 3000
138149
# 3. Copy the HTTPS URL from ngrok output
139150
# Example: https://abc123.ngrok.io
140151

141-
# 4. Use this URL in GitHub webhook settings
142-
# https://abc123.ngrok.io/webhooks/git
152+
# 4. Use this URL in Gitea webhook settings
153+
# https://abc123.ngrok.io/webhooks/gitea
143154
```
155+
144156
## Testing the Integration
145157

146158
### Manual Testing
147159

148160
#### 1. Verify Initial Setup
149161

150-
After creating the webhook, GitHub automatically sends a `ping` event:
162+
After creating the webhook, Gitea automatically sends a `ping` event:
151163

152164
1. Go to your repository's webhook settings
153165
2. Click on your webhook
@@ -157,6 +169,7 @@ After creating the webhook, GitHub automatically sends a `ping` event:
157169
#### 2. Trigger Test Events
158170

159171
**Create a Push Event:**
172+
160173
```bash
161174
# Make a small change
162175
echo "webhook test" >> test-webhook.txt
@@ -166,16 +179,18 @@ git push origin main
166179
```
167180

168181
**Create an Issue:**
169-
1. Go to your repository on GitHub
182+
183+
1. Go to your repository on Gitea
170184
2. Click **Issues** tab
171185
3. Click **New issue**
172186
4. Create a test issue
173187

174188
**Create a Pull Request:**
189+
175190
1. Create a new branch: `git checkout -b test-webhook`
176191
2. Make changes and commit
177192
3. Push branch: `git push origin test-webhook`
178-
4. Open pull request on GitHub
193+
4. Open pull request on Gitea
179194

180195
### Testing with curl
181196

@@ -185,7 +200,7 @@ You can test the webhook endpoint directly using curl with proper signature gene
185200
#!/bin/bash
186201

187202
# Configuration
188-
WEBHOOK_URL="http://localhost:3000/webhooks/git" # Adjust for your environment
203+
WEBHOOK_URL="http://localhost:3000/webhooks/gitea" # Adjust for your environment
189204
WEBHOOK_SECRET="your_webhook_secret_here"
190205
PAYLOAD='{"test": "data", "repository": {"name": "test-repo"}}'
191206
DELIVERY_ID="test-delivery-$(date +%s)"
@@ -197,8 +212,8 @@ SIGNATURE="sha256=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$WEBHOOK_SE
197212
# Send test webhook
198213
curl -X POST "$WEBHOOK_URL" \
199214
-H "Content-Type: application/json" \
200-
-H "X-GitHub-Event: $EVENT_TYPE" \
201-
-H "X-GitHub-Delivery: $DELIVERY_ID" \
215+
-H "X-Gitea-Event: $EVENT_TYPE" \
216+
-H "X-Gitea-Delivery: $DELIVERY_ID" \
202217
-H "X-Hub-Signature-256: $SIGNATURE" \
203218
-d "$PAYLOAD"
204219
```
@@ -207,24 +222,28 @@ curl -X POST "$WEBHOOK_URL" \
207222

208223
### Webhook Endpoint
209224

210-
**URL:** `POST /webhooks/git` (development) or `POST /v6/review/webhooks/git` (production)
225+
**URL:** `POST /webhooks/gitea` (development) or `POST /v6/review/webhooks/gitea` (production)
211226

212227
**Required Headers:**
228+
213229
- `Content-Type: application/json`
214-
- `X-GitHub-Event: {event_type}` - GitHub event type (push, pull_request, etc.)
215-
- `X-GitHub-Delivery: {delivery_id}` - Unique delivery identifier from GitHub
230+
- `X-Gitea-Event: {event_type}` - Gitea event type (push, pull_request, etc.)
231+
- `X-Gitea-Delivery: {delivery_id}` - Unique delivery identifier from Gitea
216232
- `X-Hub-Signature-256: sha256={signature}` - HMAC-SHA256 signature for verification
217233

218234
**Request Body:**
219-
- GitHub webhook payload (varies by event type)
235+
236+
- Gitea webhook payload (varies by event type)
220237

221238
**Response Codes:**
239+
222240
- `200 OK` - Webhook processed successfully
223241
- `400 Bad Request` - Missing required headers or invalid payload
224242
- `403 Forbidden` - Invalid signature verification
225243
- `500 Internal Server Error` - Processing error or configuration issue
226244

227245
**Success Response:**
246+
228247
```json
229248
{
230249
"success": true,
@@ -233,13 +252,14 @@ curl -X POST "$WEBHOOK_URL" \
233252
```
234253

235254
**Error Response:**
255+
236256
```json
237257
{
238258
"statusCode": 403,
239259
"message": "Invalid signature",
240260
"error": "Forbidden",
241261
"timestamp": "2024-01-01T00:00:00.000Z",
242-
"path": "/webhooks/git"
262+
"path": "/webhooks/gitea"
243263
}
244264
```
245265

@@ -254,7 +274,7 @@ CREATE TABLE "gitWebhookLog" (
254274
"event" TEXT NOT NULL,
255275
"eventPayload" JSONB NOT NULL,
256276
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
257-
277+
258278
CONSTRAINT "gitWebhookLog_pkey" PRIMARY KEY ("id")
259279
);
260280

@@ -267,43 +287,46 @@ CREATE INDEX "gitWebhookLog_createdAt_idx" ON "gitWebhookLog"("createdAt");
267287
### Query Examples
268288

269289
**View recent webhook events:**
290+
270291
```sql
271-
SELECT
292+
SELECT
272293
id,
273294
"eventId",
274295
event,
275296
"createdAt"
276-
FROM "gitWebhookLog"
277-
ORDER BY "createdAt" DESC
297+
FROM "gitWebhookLog"
298+
ORDER BY "createdAt" DESC
278299
LIMIT 10;
279300
```
280301

281302
**Filter by event type:**
303+
282304
```sql
283-
SELECT * FROM "gitWebhookLog"
284-
WHERE event = 'push'
305+
SELECT * FROM "gitWebhookLog"
306+
WHERE event = 'push'
285307
ORDER BY "createdAt" DESC;
286308
```
287309

288310
**View specific webhook payload:**
311+
289312
```sql
290-
SELECT
313+
SELECT
291314
event,
292315
"eventPayload"
293-
FROM "gitWebhookLog"
316+
FROM "gitWebhookLog"
294317
WHERE "eventId" = 'your-delivery-id';
295318
```
296319

297320
## Security Considerations
298321

299322
### Signature Verification
300323

301-
The webhook implementation uses GitHub's recommended security practices:
324+
The webhook implementation uses Gitea's recommended security practices:
302325

303326
1. **HMAC-SHA256 Signature:** All incoming webhooks are verified using HMAC-SHA256
304327
2. **Timing-Safe Comparison:** Uses `crypto.timingSafeEqual()` to prevent timing attacks
305328
3. **Secret Protection:** Webhook secrets are stored as environment variables
306-
4. **Header Validation:** Validates all required GitHub headers
329+
4. **Header Validation:** Validates all required Gitea headers
307330

308331
### Best Practices
309332

@@ -315,7 +338,7 @@ The webhook implementation uses GitHub's recommended security practices:
315338

316339
### Environment Security
317340

318-
- Store `GITHUB_WEBHOOK_SECRET` securely using your deployment platform's secret management
341+
- Store `GITEA_WEBHOOK_SECRET` securely using your deployment platform's secret management
319342
- Never commit secrets to version control
320343
- Use different secrets for different environments
321344
- Implement proper secret rotation procedures
@@ -326,25 +349,11 @@ Key log messages to monitor:
326349

327350
```
328351
# Successful webhook processing
329-
[WebhookController] Successfully processed GitHub webhook
352+
[WebhookController] Successfully processed Gitea webhook
330353
331354
# Signature validation failures
332-
[GitHubSignatureGuard] Invalid webhook signature for delivery
355+
[GiteaSignatureGuard] Invalid webhook signature for delivery
333356
334357
# Configuration errors
335-
[GitHubSignatureGuard] GITHUB_WEBHOOK_SECRET environment variable is not configured
336-
```
337-
338-
Example
339-
340-
```
341-
[2025-08-02T01:06:48.312Z] [LOG] [Bootstrap] Server started on port 3000
342-
[2025-08-02T01:07:15.700Z] [LOG] [HttpRequest] {"type":"request","method":"POST","url":"/webhooks/git","ip":"::1","userAgent":"GitHub-Hookshot/4f8bd7a"}
343-
[2025-08-02T01:07:15.739Z] [LOG] [GitHubSignatureGuard] Valid webhook signature verified for delivery 0722d0bc-6f3d-11f0-8a2d-6cc18966c098, event push
344-
[2025-08-02T01:07:15.740Z] [LOG] [WebhookController] {"message":"Received GitHub webhook","delivery":"0722d0bc-6f3d-11f0-8a2d-6cc18966c098","event":"push","timestamp":"2025-08-02T01:07:15.740Z"}
345-
[2025-08-02T01:07:15.740Z] [LOG] [WebhookService] {"message":"Processing GitHub webhook event","eventId":"0722d0bc-6f3d-11f0-8a2d-6cc18966c098","event":"push","timestamp":"2025-08-02T01:07:15.740Z"}
346-
[2025-08-02T01:07:15.804Z] [LOG] [WebhookService] {"message":"Successfully stored webhook event","eventId":"0722d0bc-6f3d-11f0-8a2d-6cc18966c098","event":"push","storedId":"9aHvEgDYPCYYnU","createdAt":"2025-08-02T01:07:15.747Z"}
347-
[2025-08-02T01:07:15.804Z] [LOG] [WebhookService] {"message":"Event-specific processing placeholder","event":"push","payloadSize":7979}
348-
[2025-08-02T01:07:15.804Z] [LOG] [WebhookController] {"message":"Successfully processed GitHub webhook","delivery":"0722d0bc-6f3d-11f0-8a2d-6cc18966c098","event":"push","success":true}
349-
[2025-08-02T01:07:15.804Z] [LOG] [HttpRequest] {"type":"response","statusCode":200,"method":"POST","url":"/webhooks/git","responseTime":"104ms"}
358+
[GiteaSignatureGuard] Gitea_WEBHOOK_SECRET environment variable is not configured
350359
```
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
-- DropForeignKey
2-
ALTER TABLE "reviewApplication" DROP CONSTRAINT "reviewApplication_opportunityId_fkey";
3-
4-
-- AlterTable
5-
ALTER TABLE "reviewApplication" ALTER COLUMN "opportunityId" SET DATA TYPE TEXT,
6-
ALTER COLUMN "updatedAt" DROP DEFAULT;
7-
8-
-- AlterTable
9-
ALTER TABLE "reviewOpportunity" ALTER COLUMN "updatedAt" DROP DEFAULT;
10-
111
-- CreateTable
122
CREATE TABLE "gitWebhookLog" (
133
"id" VARCHAR(14) NOT NULL DEFAULT nanoid(),
@@ -27,6 +17,3 @@ CREATE INDEX "gitWebhookLog_event_idx" ON "gitWebhookLog"("event");
2717

2818
-- CreateIndex
2919
CREATE INDEX "gitWebhookLog_createdAt_idx" ON "gitWebhookLog"("createdAt");
30-
31-
-- AddForeignKey
32-
ALTER TABLE "reviewApplication" ADD CONSTRAINT "reviewApplication_opportunityId_fkey" FOREIGN KEY ("opportunityId") REFERENCES "reviewOpportunity"("id") ON DELETE CASCADE ON UPDATE CASCADE;

0 commit comments

Comments
 (0)