-
Notifications
You must be signed in to change notification settings - Fork 20
/
processRepository.js
117 lines (100 loc) · 4.65 KB
/
processRepository.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const Repository = require('./repository');
const { cloneAndProcessRepo } = require('./gitHandler');
const { generateSummary } = require('./openaiService');
const { sendEmailNotification } = require('./emailService');
const fs = require('fs-extra');
const path = require('path');
async function processRepository(githubUrl, openaiApiKey) {
processRepoInBackground(githubUrl, openaiApiKey).catch(error => {
console.error('Asynchronous processing error:', error.message, error.stack);
Repository.findOneAndUpdate({ githubUrl }, { isProcessed: true, processingError: error.message }, { new: true }).catch(err => {
console.error('Failed to update repository with error state:', err.message, err.stack);
});
});
console.log(`Processing started for repository: ${githubUrl}`);
}
async function processRepoInBackground(githubUrl, openaiApiKey) {
let tempDirPath;
try {
const { processedFiles, allFiles, tempDirPath: dirPath } = await cloneAndProcessRepo(githubUrl);
tempDirPath = dirPath;
let relevantFiles;
try {
const gitDirPath = dirPath + path.sep + '.git';
console.log(`Checking for git directory using path: ${gitDirPath}`);
relevantFiles = processedFiles.filter(file => !file.includes(gitDirPath));
console.log('Filtered relevantFiles:', relevantFiles);
} catch (error) {
console.error('Error filtering relevant files:', error.message, error.stack);
throw error;
}
if (relevantFiles.length === 0) {
console.log('No text files found in the repository, excluding the .git directory.');
await fs.remove(tempDirPath).catch(fsRemoveError => {
console.error('Error removing temporary directory:', fsRemoveError.message, fsRemoveError.stack);
});
await Repository.findOneAndUpdate({ githubUrl }, { isProcessed: true, processingError: 'No text files found' }, { new: true }).catch(deleteError => {
console.error('Error updating repository entry in database:', deleteError.message, deleteError.stack);
});
// Update to handle notification for 'no text files found' scenario appropriately
const repository = await Repository.findOne({ githubUrl });
if (repository && repository.emails && repository.emails.length > 0) {
console.log(`No text files found. Notifications would be handled through a different mechanism for ${githubUrl}`);
}
return;
}
let fileSummariesObject = {};
for (const file of relevantFiles) {
try {
const content = await fs.readFile(file, 'utf8');
const summary = await generateSummary(content, openaiApiKey);
fileSummariesObject[file] = summary;
} catch (fileReadError) {
console.error('Error reading or summarizing file:', file, fileReadError.message, fileReadError.stack);
}
}
const fileSummariesArray = Object.values(fileSummariesObject);
console.log('File summaries:', fileSummariesArray);
const combinedSummaries = fileSummariesArray.join(' ');
const allFilesString = allFiles.join('\n');
console.log(`All files as string: ${allFilesString}`);
const combinedSummariesWithPaths = `${combinedSummaries}\n\n${allFilesString}`;
const projectSummary = await generateSummary(combinedSummariesWithPaths, openaiApiKey);
console.log(`Project summary with all file paths has been generated.`);
const updatedRepository = await Repository.findOneAndUpdate(
{ githubUrl },
{
summary: projectSummary,
isProcessed: true,
fileSummaries: fileSummariesArray
},
{ new: true }
);
console.log(`Ready to send email notifications. UUID: ${updatedRepository.uuid}`);
try {
const repository = await Repository.findOne({ githubUrl });
if (repository && repository.emails && repository.emails.length > 0) {
await sendEmailNotification(repository.emails, updatedRepository.uuid, githubUrl);
console.log(`Email notifications sent for ${githubUrl}`);
} else {
console.log(`No emails found for repository: ${githubUrl}`);
}
} catch (notificationError) {
console.error(`Error sending email notifications. UUID: ${updatedRepository.uuid}:`, notificationError.message, notificationError.stack);
}
} catch (error) {
console.error('Error during repository background processing:', error.message, error.stack);
} finally {
if (tempDirPath) {
try {
await fs.remove(tempDirPath);
console.log('Temporary directory has been removed.');
} catch (tempDirError) {
console.error('Error removing temporary directory:', tempDirError.message, tempDirError.stack);
}
}
}
}
module.exports = {
processRepository
};