codenuk_backend_mine/services/git-integration/src/services/provider-registry.js
2025-12-01 09:04:09 +05:30

130 lines
4.1 KiB
JavaScript

// services/provider-registry.js
// Simple provider registry/factory to resolve adapters by provider key.
const GithubIntegrationService = require('./github-integration.service');
const GitlabAdapter = require('./providers/gitlab.adapter');
const BitbucketAdapter = require('./providers/bitbucket.adapter');
const GiteaAdapter = require('./providers/gitea.adapter');
class GithubAdapter {
constructor() {
this.impl = new GithubIntegrationService();
}
parseRepoUrl(url) {
return this.impl.parseGitHubUrl(url);
}
async checkRepositoryAccess(owner, repo, userId = null) {
// Use user-specific method if userId is provided
if (userId) {
return await this.impl.checkRepositoryAccessWithUser(owner, repo, userId);
}
return await this.impl.checkRepositoryAccess(owner, repo);
}
async fetchRepositoryMetadata(owner, repo) {
return await this.impl.fetchRepositoryMetadata(owner, repo);
}
async analyzeCodebase(owner, repo, branch) {
return await this.impl.analyzeCodebase(owner, repo, branch);
}
async ensureRepositoryWebhook(owner, repo, callbackUrl) {
return await this.impl.ensureRepositoryWebhook(owner, repo, callbackUrl);
}
async syncRepositoryWithGit(owner, repo, branch, repositoryId) {
return await this.impl.syncRepositoryWithGit(owner, repo, branch, repositoryId);
}
async downloadRepositoryWithStorage(owner, repo, branch, repositoryId) {
return await this.impl.downloadRepositoryWithStorage(owner, repo, branch, repositoryId);
}
async syncRepositoryWithFallback(owner, repo, branch, repositoryId) {
return await this.impl.syncRepositoryWithFallback(owner, repo, branch, repositoryId);
}
async getRepositoryDiff(owner, repo, branch, fromSha, toSha) {
return await this.impl.getRepositoryDiff(owner, repo, branch, fromSha, toSha);
}
async getRepositoryChangesSince(owner, repo, branch, sinceSha) {
return await this.impl.getRepositoryChangesSince(owner, repo, branch, sinceSha);
}
async cleanupRepositoryStorage(repositoryId) {
return await this.impl.cleanupRepositoryStorage(repositoryId);
}
async getUserRepositories(accessToken) {
try {
const url = 'https://api.github.com/user/repos?type=all&per_page=100&sort=updated';
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json',
'User-Agent': 'CodeNuk-GitIntegration/1.0.0'
}
});
if (!response.ok) {
throw new Error(`GitHub API error: ${response.status} ${response.statusText}`);
}
const repos = await response.json();
return repos.map(repo => ({
id: repo.id,
name: repo.name,
full_name: repo.full_name,
description: repo.description,
language: repo.language,
visibility: repo.private ? 'private' : 'public',
html_url: repo.html_url,
clone_url: repo.clone_url,
default_branch: repo.default_branch || 'main',
stargazers_count: repo.stargazers_count,
watchers_count: repo.watchers_count,
forks_count: repo.forks_count,
updated_at: repo.updated_at,
created_at: repo.created_at
}));
} catch (error) {
console.error('Error fetching GitHub repositories:', error);
throw error;
}
}
}
class ProviderRegistry {
constructor() {
this.providers = new Map();
// Register GitHub by default
this.providers.set('github', () => new GithubAdapter());
this.providers.set('gitlab', () => new GitlabAdapter());
this.providers.set('bitbucket', () => new BitbucketAdapter());
this.providers.set('gitea', () => new GiteaAdapter());
}
register(providerKey, factoryFn) {
this.providers.set(providerKey, factoryFn);
}
resolve(providerKey) {
const factory = this.providers.get((providerKey || '').toLowerCase());
if (!factory) {
throw new Error(`Unsupported provider: ${providerKey}`);
}
return factory();
}
}
module.exports = new ProviderRegistry();