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:
Simon Larsen
2024-06-11 14:24:19 +01:00
parent c5680f6c26
commit c53b14f88f
3 changed files with 42 additions and 12 deletions

View File

@@ -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
}))
);
}

View File

@@ -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

View File

@@ -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()