CatPtain commited on
Commit
cb4d01c
·
verified ·
1 Parent(s): 9d621ef

Upload githubService.js

Browse files
backend/src/services/githubService.js CHANGED
@@ -9,11 +9,58 @@ class GitHubService {
9
  this.repositories = GITHUB_CONFIG.repositories;
10
  this.useMemoryStorage = !this.token; // 如果没有token,使用内存存储
11
 
 
 
 
 
 
 
12
  if (!this.token) {
13
  console.warn('GitHub token not configured, using memory storage for development');
14
  }
15
  }
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  // 获取仓库信息
18
  parseRepoUrl(repoUrl) {
19
  const match = repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/);
 
9
  this.repositories = GITHUB_CONFIG.repositories;
10
  this.useMemoryStorage = !this.token; // 如果没有token,使用内存存储
11
 
12
+ console.log('=== GitHub Service Configuration ===');
13
+ console.log('Token configured:', !!this.token);
14
+ console.log('Token preview:', this.token ? `${this.token.substring(0, 8)}...` : 'Not set');
15
+ console.log('Repositories:', this.repositories);
16
+ console.log('Using memory storage:', this.useMemoryStorage);
17
+
18
  if (!this.token) {
19
  console.warn('GitHub token not configured, using memory storage for development');
20
  }
21
  }
22
 
23
+ // 验证GitHub连接
24
+ async validateConnection() {
25
+ if (this.useMemoryStorage) {
26
+ return { valid: false, reason: 'No GitHub token configured' };
27
+ }
28
+
29
+ try {
30
+ // 测试GitHub API连接
31
+ const response = await axios.get(`${this.apiUrl}/user`, {
32
+ headers: {
33
+ 'Authorization': `token ${this.token}`,
34
+ 'Accept': 'application/vnd.github.v3+json'
35
+ }
36
+ });
37
+
38
+ console.log('GitHub API connection successful:', response.data.login);
39
+
40
+ // 测试仓库访问
41
+ const repoResults = [];
42
+ for (const repoUrl of this.repositories) {
43
+ try {
44
+ const { owner, repo } = this.parseRepoUrl(repoUrl);
45
+ const repoResponse = await axios.get(`${this.apiUrl}/repos/${owner}/${repo}`, {
46
+ headers: {
47
+ 'Authorization': `token ${this.token}`,
48
+ 'Accept': 'application/vnd.github.v3+json'
49
+ }
50
+ });
51
+ repoResults.push({ url: repoUrl, accessible: true, name: repoResponse.data.full_name });
52
+ } catch (error) {
53
+ repoResults.push({ url: repoUrl, accessible: false, error: error.message });
54
+ }
55
+ }
56
+
57
+ return { valid: true, user: response.data.login, repositories: repoResults };
58
+ } catch (error) {
59
+ console.error('GitHub connection validation failed:', error.message);
60
+ return { valid: false, reason: error.message };
61
+ }
62
+ }
63
+
64
  // 获取仓库信息
65
  parseRepoUrl(repoUrl) {
66
  const match = repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/);