mirror of
https://github.com/OneUptime/oneuptime.git
synced 2026-04-06 00:32:12 +02:00
refactor: Update CodeRepositoryUtil methods to accept repoPath parameter
The CodeRepositoryUtil methods getGitCommitHashForFile and getFilesInDirectory have been updated to accept a repoPath parameter. This change allows for specifying the repository path when retrieving the git commit hash for a file or getting the files in a directory. It improves the flexibility and reusability of the CodeRepositoryUtil class.
This commit is contained in:
@@ -2,18 +2,29 @@ import Execute from '../Execute';
|
||||
import CodeRepositoryFile from './CodeRepositoryFile';
|
||||
|
||||
export default class CodeRepositoryUtil {
|
||||
public static async getGitCommitHashForFile(
|
||||
public static async getGitCommitHashForFile(data: {
|
||||
repoPath: string,
|
||||
filePath: string
|
||||
}
|
||||
): Promise<string> {
|
||||
|
||||
const { repoPath, filePath } = data;
|
||||
|
||||
return await Execute.executeCommand(
|
||||
`git log -1 --pretty=format:"%H" ${filePath}`
|
||||
`cd ${repoPath} && git log -1 --pretty=format:"%H" "${filePath}"`
|
||||
);
|
||||
}
|
||||
|
||||
public static async getFilesInDirectory(directoryPath: string): Promise<{
|
||||
public static async getFilesInDirectory(data: {
|
||||
directoryPath: string,
|
||||
repoPath: string
|
||||
}): Promise<{
|
||||
files: Array<CodeRepositoryFile>;
|
||||
subDirectories: Array<string>;
|
||||
}> {
|
||||
|
||||
const { directoryPath, repoPath } = data;
|
||||
|
||||
const files: Array<CodeRepositoryFile> = [];
|
||||
const output: string = await Execute.executeCommand(
|
||||
`ls ${directoryPath}`
|
||||
@@ -40,9 +51,10 @@ export default class CodeRepositoryUtil {
|
||||
}
|
||||
|
||||
const filePath: string = `${directoryPath}/${fileName}`;
|
||||
const gitCommitHash: string = await this.getGitCommitHashForFile(
|
||||
filePath
|
||||
);
|
||||
const gitCommitHash: string = await this.getGitCommitHashForFile({
|
||||
filePath,
|
||||
repoPath
|
||||
});
|
||||
const fileExtension: string = fileName.split('.').pop() || '';
|
||||
files.push({
|
||||
filePath,
|
||||
@@ -58,18 +70,25 @@ export default class CodeRepositoryUtil {
|
||||
};
|
||||
}
|
||||
|
||||
public static async getFilesInDirectoryRecursive(
|
||||
public static async getFilesInDirectoryRecursive(data: {
|
||||
repoPath: string,
|
||||
directoryPath: string
|
||||
): Promise<Array<CodeRepositoryFile>> {
|
||||
}): Promise<Array<CodeRepositoryFile>> {
|
||||
const files: Array<CodeRepositoryFile> = [];
|
||||
|
||||
const { files: filesInDirectory, subDirectories } =
|
||||
await this.getFilesInDirectory(directoryPath);
|
||||
await this.getFilesInDirectory({
|
||||
directoryPath: data.directoryPath,
|
||||
repoPath: data.repoPath
|
||||
});
|
||||
files.push(...filesInDirectory);
|
||||
|
||||
for (const subDirectory of subDirectories) {
|
||||
files.push(
|
||||
...(await this.getFilesInDirectoryRecursive(subDirectory))
|
||||
...(await this.getFilesInDirectoryRecursive({
|
||||
repoPath: data.repoPath,
|
||||
directoryPath: subDirectory
|
||||
}))
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
ONEUPTIME_URL=https://oneuptime.com
|
||||
ONEUPTIME_REPOSITORY_SECRET_KEY=your-repository-secret-key
|
||||
LOCAL_REPOSITORY_PATH=/repository
|
||||
ONEUPTIME_LOCAL_REPOSITORY_PATH=/repository
|
||||
|
||||
@@ -3,15 +3,26 @@ import { PromiseVoidFunction } from 'Common/Types/FunctionTypes';
|
||||
import logger from 'CommonServer/Utils/Logger';
|
||||
import CodeRepository from 'Model/Models/CodeRepository';
|
||||
import dotenv from 'dotenv';
|
||||
import CodeRepositoryFile from 'CommonServer/Utils/CodeRepository/CodeRepositoryFile'
|
||||
import CodeRepositoryCommonServerUtil from 'CommonServer/Utils/CodeRepository/CodeRepository';
|
||||
import { GetLocalRepositoryPath } from './Config';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
logger.info('OneUptime Copilot is starting...');
|
||||
logger.info('OneUptime Copilot is started...');
|
||||
|
||||
const init: PromiseVoidFunction = async (): Promise<void> => {
|
||||
const codeRepository: CodeRepository =
|
||||
await CodeRepositoryUtil.getCodeRepository();
|
||||
|
||||
logger.info(`Code Repository found: ${codeRepository.name}`);
|
||||
|
||||
const allFiles: Array<CodeRepositoryFile> = await CodeRepositoryCommonServerUtil.getFilesInDirectoryRecursive({
|
||||
repoPath: GetLocalRepositoryPath(),
|
||||
directoryPath: GetLocalRepositoryPath()
|
||||
});
|
||||
|
||||
logger.info(`All files found: ${allFiles.length}`);
|
||||
};
|
||||
|
||||
init()
|
||||
|
||||
Reference in New Issue
Block a user