Initial commit: Plugin Directory with automation
This commit is contained in:
193
src/Validator.js
Normal file
193
src/Validator.js
Normal file
@@ -0,0 +1,193 @@
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
/**
|
||||
* Validator - Validates PR changes against plugin directory rules
|
||||
*/
|
||||
export class Validator {
|
||||
constructor() {
|
||||
this.requiredFields = ['id', 'name', 'version', 'description', 'author', 'repository'];
|
||||
this.optionalFields = ['downloadUrl', 'homepage', 'tags', 'addedDate', 'dependencies'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a PR's file changes
|
||||
*/
|
||||
async validatePR(files, prAuthor) {
|
||||
const errors = [];
|
||||
const added = [];
|
||||
const removed = [];
|
||||
|
||||
// Check that all files are in the plugins/ directory
|
||||
const invalidPaths = files.filter(f => !f.filename.startsWith('plugins/'));
|
||||
if (invalidPaths.length > 0) {
|
||||
errors.push(`Files outside plugins/ directory: ${invalidPaths.map(f => f.filename).join(', ')}`);
|
||||
}
|
||||
|
||||
// Check that all files are .json files
|
||||
const nonJsonFiles = files.filter(f =>
|
||||
f.filename.startsWith('plugins/') && !f.filename.endsWith('.json')
|
||||
);
|
||||
if (nonJsonFiles.length > 0) {
|
||||
errors.push(`Non-JSON files in plugins/: ${nonJsonFiles.map(f => f.filename).join(', ')}`);
|
||||
}
|
||||
|
||||
// Process each changed file
|
||||
for (const file of files) {
|
||||
if (!file.filename.startsWith('plugins/') || !file.filename.endsWith('.json')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const pluginId = file.filename.replace('plugins/', '').replace('.json', '');
|
||||
|
||||
if (file.status === 'added' || file.status === 'modified') {
|
||||
// Validate added/modified plugins
|
||||
try {
|
||||
const validation = await this.validatePluginFile(file, pluginId, prAuthor);
|
||||
|
||||
if (!validation.valid) {
|
||||
errors.push(...validation.errors.map(e => `${file.filename}: ${e}`));
|
||||
} else {
|
||||
added.push(pluginId);
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(`${file.filename}: ${error.message}`);
|
||||
}
|
||||
} else if (file.status === 'removed') {
|
||||
// Validate removed plugins (check ownership)
|
||||
try {
|
||||
const canRemove = await this.validateRemoval(pluginId, prAuthor);
|
||||
|
||||
if (!canRemove.valid) {
|
||||
errors.push(...canRemove.errors.map(e => `${file.filename}: ${e}`));
|
||||
} else {
|
||||
removed.push(pluginId);
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(`${file.filename}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Must have at least one change
|
||||
if (files.length === 0) {
|
||||
errors.push('PR has no file changes');
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors,
|
||||
added,
|
||||
removed
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a plugin JSON file
|
||||
*/
|
||||
async validatePluginFile(file, pluginId, prAuthor) {
|
||||
const errors = [];
|
||||
|
||||
try {
|
||||
// Parse the JSON content from the file patch
|
||||
const content = this.extractFileContent(file);
|
||||
const plugin = JSON.parse(content);
|
||||
|
||||
// Check required fields
|
||||
for (const field of this.requiredFields) {
|
||||
if (!plugin[field]) {
|
||||
errors.push(`Missing required field: ${field}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Check that ID matches filename
|
||||
if (plugin.id !== pluginId) {
|
||||
errors.push(`Plugin ID "${plugin.id}" does not match filename "${pluginId}.json"`);
|
||||
}
|
||||
|
||||
// Validate version format (semver-ish)
|
||||
if (plugin.version && !this.isValidVersion(plugin.version)) {
|
||||
errors.push(`Invalid version format: ${plugin.version} (expected: X.Y.Z)`);
|
||||
}
|
||||
|
||||
// Validate repository URL
|
||||
if (plugin.repository && !this.isValidUrl(plugin.repository)) {
|
||||
errors.push(`Invalid repository URL: ${plugin.repository}`);
|
||||
}
|
||||
|
||||
// Validate author matches PR author (for new plugins)
|
||||
if (file.status === 'added' && plugin.author !== prAuthor) {
|
||||
errors.push(`Author "${plugin.author}" must match PR author "${prAuthor}"`);
|
||||
}
|
||||
|
||||
// Validate tags if present
|
||||
if (plugin.tags && !Array.isArray(plugin.tags)) {
|
||||
errors.push('Tags must be an array');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
errors.push(`Invalid JSON: ${error.message}`);
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a user can remove a plugin
|
||||
*/
|
||||
async validateRemoval(pluginId, prAuthor) {
|
||||
const errors = [];
|
||||
|
||||
try {
|
||||
// Read the existing plugin file from disk
|
||||
const content = await readFile(`./plugins/${pluginId}.json`, 'utf-8');
|
||||
const plugin = JSON.parse(content);
|
||||
|
||||
// Check if the PR author is the plugin author
|
||||
if (plugin.author !== prAuthor) {
|
||||
errors.push(`Cannot remove plugin: you are not the author (author: ${plugin.author})`);
|
||||
}
|
||||
} catch (error) {
|
||||
// If file doesn't exist, it's probably already removed
|
||||
console.warn(`Could not validate removal of ${pluginId}:`, error.message);
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract file content from Gitea file object
|
||||
* In a real implementation, you'd fetch the raw content from the PR
|
||||
*/
|
||||
extractFileContent(file) {
|
||||
// This is a simplified version
|
||||
// In production, fetch the actual file content from the PR's head branch
|
||||
|
||||
// For now, return a placeholder that would need to be fetched
|
||||
throw new Error('File content extraction not fully implemented - fetch from PR branch');
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate semantic version format
|
||||
*/
|
||||
isValidVersion(version) {
|
||||
return /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/.test(version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate URL format
|
||||
*/
|
||||
isValidUrl(url) {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
232
src/ValidatorEnhanced.js
Normal file
232
src/ValidatorEnhanced.js
Normal file
@@ -0,0 +1,232 @@
|
||||
import { WebhookHandler } from './WebhookHandler.js';
|
||||
import fetch from 'node-fetch';
|
||||
|
||||
/**
|
||||
* Enhanced validator that fetches actual file content from PR
|
||||
*/
|
||||
export class Validator {
|
||||
constructor(webhookHandler) {
|
||||
this.handler = webhookHandler;
|
||||
this.requiredFields = ['id', 'name', 'version', 'description', 'author', 'repository'];
|
||||
this.optionalFields = ['downloadUrl', 'homepage', 'tags', 'addedDate', 'dependencies'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a PR's file changes
|
||||
*/
|
||||
async validatePR(files, prAuthor, prNumber) {
|
||||
const errors = [];
|
||||
const added = [];
|
||||
const removed = [];
|
||||
|
||||
// Check that all files are in the plugins/ directory
|
||||
const invalidPaths = files.filter(f => !f.filename.startsWith('plugins/'));
|
||||
if (invalidPaths.length > 0) {
|
||||
errors.push(`Files outside plugins/ directory: ${invalidPaths.map(f => f.filename).join(', ')}`);
|
||||
}
|
||||
|
||||
// Check that all files are .json files
|
||||
const nonJsonFiles = files.filter(f =>
|
||||
f.filename.startsWith('plugins/') && !f.filename.endsWith('.json')
|
||||
);
|
||||
if (nonJsonFiles.length > 0) {
|
||||
errors.push(`Non-JSON files in plugins/: ${nonJsonFiles.map(f => f.filename).join(', ')}`);
|
||||
}
|
||||
|
||||
// Process each changed file
|
||||
for (const file of files) {
|
||||
if (!file.filename.startsWith('plugins/') || !file.filename.endsWith('.json')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const pluginId = file.filename.replace('plugins/', '').replace('.json', '');
|
||||
|
||||
if (file.status === 'added' || file.status === 'modified') {
|
||||
// Fetch and validate the file content
|
||||
try {
|
||||
const content = await this.fetchPRFileContent(prNumber, file.filename);
|
||||
const validation = await this.validatePluginContent(content, pluginId, prAuthor, file.status);
|
||||
|
||||
if (!validation.valid) {
|
||||
errors.push(...validation.errors.map(e => `${file.filename}: ${e}`));
|
||||
} else {
|
||||
added.push(pluginId);
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(`${file.filename}: ${error.message}`);
|
||||
}
|
||||
} else if (file.status === 'removed') {
|
||||
// Validate removed plugins (check ownership)
|
||||
try {
|
||||
const canRemove = await this.validateRemoval(pluginId, prAuthor);
|
||||
|
||||
if (!canRemove.valid) {
|
||||
errors.push(...canRemove.errors.map(e => `${file.filename}: ${e}`));
|
||||
} else {
|
||||
removed.push(pluginId);
|
||||
}
|
||||
} catch (error) {
|
||||
errors.push(`${file.filename}: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Must have at least one change
|
||||
if (files.length === 0) {
|
||||
errors.push('PR has no file changes');
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors,
|
||||
added,
|
||||
removed
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch file content from a PR
|
||||
*/
|
||||
async fetchPRFileContent(prNumber, filename) {
|
||||
const pr = await this.getPRDetails(prNumber);
|
||||
const headSha = pr.head.sha;
|
||||
|
||||
const url = `${this.handler.giteaUrl}/api/v1/repos/${this.handler.repoOwner}/${this.handler.repoName}/contents/${filename}?ref=${headSha}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: { 'Authorization': `token ${this.handler.giteaToken}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch file: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const content = Buffer.from(data.content, 'base64').toString('utf-8');
|
||||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get PR details
|
||||
*/
|
||||
async getPRDetails(prNumber) {
|
||||
const url = `${this.handler.giteaUrl}/api/v1/repos/${this.handler.repoOwner}/${this.handler.repoName}/pulls/${prNumber}`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: { 'Authorization': `token ${this.handler.giteaToken}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch PR: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate plugin JSON content
|
||||
*/
|
||||
async validatePluginContent(content, pluginId, prAuthor, status) {
|
||||
const errors = [];
|
||||
|
||||
try {
|
||||
const plugin = JSON.parse(content);
|
||||
|
||||
// Check required fields
|
||||
for (const field of this.requiredFields) {
|
||||
if (!plugin[field]) {
|
||||
errors.push(`Missing required field: ${field}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Check that ID matches filename
|
||||
if (plugin.id !== pluginId) {
|
||||
errors.push(`Plugin ID "${plugin.id}" does not match filename "${pluginId}.json"`);
|
||||
}
|
||||
|
||||
// Validate version format (semver-ish)
|
||||
if (plugin.version && !this.isValidVersion(plugin.version)) {
|
||||
errors.push(`Invalid version format: ${plugin.version} (expected: X.Y.Z)`);
|
||||
}
|
||||
|
||||
// Validate repository URL
|
||||
if (plugin.repository && !this.isValidUrl(plugin.repository)) {
|
||||
errors.push(`Invalid repository URL: ${plugin.repository}`);
|
||||
}
|
||||
|
||||
// Validate author matches PR author (for new plugins)
|
||||
if (status === 'added' && plugin.author !== prAuthor) {
|
||||
errors.push(`Author "${plugin.author}" must match PR author "${prAuthor}"`);
|
||||
}
|
||||
|
||||
// Validate tags if present
|
||||
if (plugin.tags && !Array.isArray(plugin.tags)) {
|
||||
errors.push('Tags must be an array');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
errors.push(`Invalid JSON: ${error.message}`);
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a user can remove a plugin
|
||||
*/
|
||||
async validateRemoval(pluginId, prAuthor) {
|
||||
const errors = [];
|
||||
|
||||
try {
|
||||
// Read the existing plugin file from main branch
|
||||
const url = `${this.handler.giteaUrl}/api/v1/repos/${this.handler.repoOwner}/${this.handler.repoName}/contents/plugins/${pluginId}.json`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: { 'Authorization': `token ${this.handler.giteaToken}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
// If file doesn't exist on main, removal is fine
|
||||
return { valid: true, errors: [] };
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const content = Buffer.from(data.content, 'base64').toString('utf-8');
|
||||
const plugin = JSON.parse(content);
|
||||
|
||||
// Check if the PR author is the plugin author
|
||||
if (plugin.author !== prAuthor) {
|
||||
errors.push(`Cannot remove plugin: you are not the author (author: ${plugin.author})`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn(`Could not validate removal of ${pluginId}:`, error.message);
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate semantic version format
|
||||
*/
|
||||
isValidVersion(version) {
|
||||
return /^\d+\.\d+\.\d+(-[a-zA-Z0-9.-]+)?(\+[a-zA-Z0-9.-]+)?$/.test(version);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate URL format
|
||||
*/
|
||||
isValidUrl(url) {
|
||||
try {
|
||||
new URL(url);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
241
src/WebhookHandler.js
Normal file
241
src/WebhookHandler.js
Normal file
@@ -0,0 +1,241 @@
|
||||
import crypto from 'crypto';
|
||||
import fetch from 'node-fetch';
|
||||
import { readdir, readFile } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
import { Validator } from './ValidatorEnhanced.js';
|
||||
|
||||
/**
|
||||
* WebhookHandler - Processes Gitea webhooks for automated PR handling
|
||||
*/
|
||||
export class WebhookHandler {
|
||||
constructor(options) {
|
||||
this.giteaUrl = options.giteaUrl;
|
||||
this.giteaToken = options.giteaToken;
|
||||
this.webhookSecret = options.webhookSecret;
|
||||
this.repoOwner = options.repoOwner;
|
||||
this.repoName = options.repoName;
|
||||
this.validator = new Validator(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify webhook signature
|
||||
*/
|
||||
verifySignature(payload, signature) {
|
||||
if (!this.webhookSecret) return true; // Skip if no secret configured
|
||||
|
||||
const hmac = crypto.createHmac('sha256', this.webhookSecret);
|
||||
const digest = hmac.update(JSON.stringify(payload)).digest('hex');
|
||||
return signature === digest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle pull request webhook
|
||||
*/
|
||||
async handlePullRequest(payload) {
|
||||
const { action, pull_request, repository } = payload;
|
||||
|
||||
console.log(`PR #${pull_request.number}: ${action} by ${pull_request.user.login}`);
|
||||
|
||||
// Only process opened or synchronized PRs
|
||||
if (action !== 'opened' && action !== 'synchronize') {
|
||||
console.log('Skipping PR action:', action);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get PR details
|
||||
const prNumber = pull_request.number;
|
||||
const prAuthor = pull_request.user.login;
|
||||
const headBranch = pull_request.head.ref;
|
||||
const baseBranch = pull_request.base.ref;
|
||||
|
||||
// Get file changes in the PR
|
||||
const files = await this.getPRFiles(prNumber);
|
||||
|
||||
console.log(`PR files changed: ${files.map(f => `${f.filename} (${f.status})`).join(', ')}`);
|
||||
|
||||
// Validate the PR
|
||||
const validation = await this.validator.validatePR(files, prAuthor, prNumber);
|
||||
|
||||
if (!validation.valid) {
|
||||
console.log('Validation failed:', validation.errors);
|
||||
await this.commentOnPR(prNumber, this.buildErrorComment(validation.errors));
|
||||
await this.labelPR(prNumber, 'validation-failed');
|
||||
return;
|
||||
}
|
||||
|
||||
// PR is valid, auto-approve and merge
|
||||
console.log('✓ PR validation passed');
|
||||
await this.commentOnPR(prNumber, this.buildSuccessComment(validation));
|
||||
await this.labelPR(prNumber, 'auto-approved');
|
||||
await this.mergePR(prNumber);
|
||||
|
||||
console.log(`✓ Auto-merged PR #${prNumber}`);
|
||||
} catch (error) {
|
||||
console.error('Error handling PR:', error);
|
||||
await this.commentOnPR(pull_request.number,
|
||||
`❌ **Automation Error**\n\nFailed to process this PR: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get files changed in a PR
|
||||
*/
|
||||
async getPRFiles(prNumber) {
|
||||
const url = `${this.giteaUrl}/api/v1/repos/${this.repoOwner}/${this.repoName}/pulls/${prNumber}/files`;
|
||||
const response = await fetch(url, {
|
||||
headers: { 'Authorization': `token ${this.giteaToken}` }
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to fetch PR files: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Comment on a PR
|
||||
*/
|
||||
async commentOnPR(prNumber, body) {
|
||||
const url = `${this.giteaUrl}/api/v1/repos/${this.repoOwner}/${this.repoName}/issues/${prNumber}/comments`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `token ${this.giteaToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ body })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Failed to comment on PR:', response.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Label a PR
|
||||
*/
|
||||
async labelPR(prNumber, label) {
|
||||
const url = `${this.giteaUrl}/api/v1/repos/${this.repoOwner}/${this.repoName}/issues/${prNumber}/labels`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `token ${this.giteaToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ labels: [label] })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Failed to label PR:', response.statusText);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge a PR
|
||||
*/
|
||||
async mergePR(prNumber) {
|
||||
const url = `${this.giteaUrl}/api/v1/repos/${this.repoOwner}/${this.repoName}/pulls/${prNumber}/merge`;
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `token ${this.giteaToken}`,
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
Do: 'squash',
|
||||
MergeMessageField: 'Auto-merged plugin directory update',
|
||||
delete_branch_after_merge: true
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to merge PR: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build error comment for PR
|
||||
*/
|
||||
buildErrorComment(errors) {
|
||||
let comment = '❌ **Validation Failed**\n\nThis PR cannot be auto-merged due to the following issues:\n\n';
|
||||
|
||||
errors.forEach(error => {
|
||||
comment += `- ${error}\n`;
|
||||
});
|
||||
|
||||
comment += '\n**Rules:**\n';
|
||||
comment += '1. Only modify files in the `plugins/` directory\n';
|
||||
comment += '2. Only add or remove `.json` files\n';
|
||||
comment += '3. JSON files must match the plugin schema\n';
|
||||
comment += '4. You can only remove plugins you authored\n';
|
||||
comment += '5. Plugin ID must match the filename (without .json)\n';
|
||||
comment += '\nPlease fix these issues and push again.';
|
||||
|
||||
return comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build success comment for PR
|
||||
*/
|
||||
buildSuccessComment(validation) {
|
||||
let comment = '✅ **Validation Passed**\n\nThis PR has been automatically approved and will be merged.\n\n';
|
||||
|
||||
if (validation.added.length > 0) {
|
||||
comment += `**Added plugins:** ${validation.added.join(', ')}\n`;
|
||||
}
|
||||
|
||||
if (validation.removed.length > 0) {
|
||||
comment += `**Removed plugins:** ${validation.removed.join(', ')}\n`;
|
||||
}
|
||||
|
||||
comment += '\nTo undo this change, create a new PR that reverses these changes.';
|
||||
|
||||
return comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* List all plugins from the plugins directory
|
||||
*/
|
||||
async listPlugins() {
|
||||
const pluginsDir = './plugins';
|
||||
const files = await readdir(pluginsDir);
|
||||
const plugins = [];
|
||||
|
||||
for (const file of files) {
|
||||
if (file.endsWith('.json')) {
|
||||
try {
|
||||
const content = await readFile(join(pluginsDir, file), 'utf-8');
|
||||
const plugin = JSON.parse(content);
|
||||
plugins.push(plugin);
|
||||
} catch (error) {
|
||||
console.error(`Error reading plugin ${file}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific plugin
|
||||
*/
|
||||
async getPlugin(pluginId) {
|
||||
const pluginsDir = './plugins';
|
||||
const filePath = join(pluginsDir, `${pluginId}.json`);
|
||||
|
||||
try {
|
||||
const content = await readFile(filePath, 'utf-8');
|
||||
return JSON.parse(content);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/server.js
Normal file
86
src/server.js
Normal file
@@ -0,0 +1,86 @@
|
||||
import express from 'express';
|
||||
import { WebhookHandler } from './WebhookHandler.js';
|
||||
import { readFile } from 'fs/promises';
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
const webhookSecret = process.env.WEBHOOK_SECRET || '';
|
||||
|
||||
app.use(express.json());
|
||||
|
||||
const handler = new WebhookHandler({
|
||||
giteaUrl: process.env.GITEA_URL || 'http://synbox.ruv.wtf:8418',
|
||||
giteaToken: process.env.GITEA_TOKEN || '',
|
||||
webhookSecret,
|
||||
repoOwner: 'litruv',
|
||||
repoName: 'Plugin-Directory'
|
||||
});
|
||||
|
||||
/**
|
||||
* Health check endpoint
|
||||
*/
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({ status: 'ok', timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
/**
|
||||
* List all plugins endpoint
|
||||
*/
|
||||
app.get('/plugins', async (req, res) => {
|
||||
try {
|
||||
const plugins = await handler.listPlugins();
|
||||
res.json({ plugins, count: plugins.length });
|
||||
} catch (error) {
|
||||
console.error('Error listing plugins:', error);
|
||||
res.status(500).json({ error: 'Failed to list plugins' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Get specific plugin endpoint
|
||||
*/
|
||||
app.get('/plugins/:pluginId', async (req, res) => {
|
||||
try {
|
||||
const plugin = await handler.getPlugin(req.params.pluginId);
|
||||
if (plugin) {
|
||||
res.json(plugin);
|
||||
} else {
|
||||
res.status(404).json({ error: 'Plugin not found' });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting plugin:', error);
|
||||
res.status(500).json({ error: 'Failed to get plugin' });
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Webhook endpoint for Gitea PR events
|
||||
*/
|
||||
app.post('/webhook', async (req, res) => {
|
||||
try {
|
||||
const signature = req.headers['x-gitea-signature'];
|
||||
const event = req.headers['x-gitea-event'];
|
||||
|
||||
if (!handler.verifySignature(req.body, signature)) {
|
||||
console.warn('Invalid webhook signature');
|
||||
return res.status(401).json({ error: 'Invalid signature' });
|
||||
}
|
||||
|
||||
console.log(`Received webhook event: ${event}`);
|
||||
|
||||
if (event === 'pull_request') {
|
||||
await handler.handlePullRequest(req.body);
|
||||
}
|
||||
|
||||
res.json({ status: 'processed' });
|
||||
} catch (error) {
|
||||
console.error('Webhook error:', error);
|
||||
res.status(500).json({ error: 'Webhook processing failed' });
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Plugin Directory server running on port ${port}`);
|
||||
console.log(`Webhook endpoint: http://localhost:${port}/webhook`);
|
||||
console.log(`Plugin list: http://localhost:${port}/plugins`);
|
||||
});
|
||||
Reference in New Issue
Block a user