feat: Add setup pull request flag to CopilotPullRequestAPI.ts

This commit adds the `isSetupPullRequest` flag to the `CopilotPullRequestAPI.ts` file. The flag is set to true when creating a pull request for setting up the repository with OneUptime Copilot. This flag will be used to identify setup pull requests in the application.
This commit is contained in:
Simon Larsen 2024-07-12 19:02:47 +01:00
parent a33b1f20f8
commit 4e0d56de54
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
5 changed files with 150 additions and 91 deletions

View File

@ -68,6 +68,7 @@ export default class CopilotPullRequestAPI extends BaseAPI<
projectId: true,
copilotPullRequestStatus: true,
pullRequestId: true,
isSetupPullRequest: true,
},
skip: 0,
limit: LIMIT_PER_PROJECT,

View File

@ -49,37 +49,13 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
haltProcessWithSuccess();
}
logger.info(
`Cloning the repository ${codeRepositoryResult.codeRepository.name} to a temporary directory.`,
);
// now clone this repository to a temporary directory - /repository
await CodeRepositoryUtil.cloneRepository({
codeRepository: codeRepositoryResult.codeRepository,
await cloneRepository({
codeRepositoryResult,
});
// Check if OneUptime Copilot has setup properly.
await refreshPullRequests();
const onAfterCloneScript: string | null =
await CodeRepositoryUtil.getRepoScript({
scriptType: RepoScriptType.OnAfterClone,
});
if (!onAfterCloneScript) {
logger.debug("No on-after-clone script found for this repository.");
}
if (onAfterCloneScript) {
logger.info("Executing on-after-clone script.");
await CodeRepositoryUtil.executeScript({
script: onAfterCloneScript,
});
logger.info("on-after-clone script executed successfully.");
}
logger.info(
`Repository ${codeRepositoryResult.codeRepository.name} cloned successfully.`,
);
await setUpRepository();
for (const serviceToImrove of codeRepositoryResult.servicesToImprove) {
checkIfCurrentFixCountIsLessThanFixNumberOfCodeEventsInEachRun();
@ -233,6 +209,48 @@ const executeAction: ExecutionActionFunction = async (
}
};
type CloneRepositoryFunction = (data: {
codeRepositoryResult: CodeRepositoryResult;
}) => Promise<void>;
const cloneRepository: CloneRepositoryFunction = async (data: {
codeRepositoryResult: CodeRepositoryResult;
}): Promise<void> => {
const { codeRepositoryResult } = data;
logger.info(
`Cloning the repository ${codeRepositoryResult.codeRepository.name} to a temporary directory.`,
);
// now clone this repository to a temporary directory - /repository
await CodeRepositoryUtil.cloneRepository({
codeRepository: codeRepositoryResult.codeRepository,
});
// Check if OneUptime Copilot has setup properly.
const onAfterCloneScript: string | null =
await CodeRepositoryUtil.getRepoScript({
scriptType: RepoScriptType.OnAfterClone,
});
if (!onAfterCloneScript) {
logger.debug("No on-after-clone script found for this repository.");
}
if (onAfterCloneScript) {
logger.info("Executing on-after-clone script.");
await CodeRepositoryUtil.executeScript({
script: onAfterCloneScript,
});
logger.info("on-after-clone script executed successfully.");
}
logger.info(
`Repository ${codeRepositoryResult.codeRepository.name} cloned successfully.`,
);
};
const checkIfCurrentFixCountIsLessThanFixNumberOfCodeEventsInEachRun: VoidFunction =
(): void => {
if (currentFixCount <= FixNumberOfCodeEventsInEachRun) {
@ -248,4 +266,17 @@ const haltProcessWithSuccess: VoidFunction = (): void => {
process.exit(0);
};
const refreshPullRequests: PromiseVoidFunction = async (): Promise<void> => {};
const setUpRepository: PromiseVoidFunction = async (): Promise<void> => {
const isSetupProperly: boolean =
await CodeRepositoryUtil.isRepoSetupProperly();
if (isSetupProperly) {
return;
}
// if the repo is not set up properly, then check if there's an outstanding setup Pr for this repo.
};
export default init;

View File

@ -24,7 +24,7 @@ import RefactorCode from "./RefactorCode";
import WriteUnitTests from "./WriteUnitTests";
import ImproveReadme from "./ImroveReadme";
import CopilotPullRequest from "Model/Models/CopilotPullRequest";
import CopilotPullRequestStatus from "Common/Types/Copilot/CopilotPullRequestStatus";
import CopilotPullRequestService from "../CopilotPullRequest";
const actionDictionary: Dictionary<typeof CopilotActionBase> = {
[CopilotActionType.IMPROVE_COMMENTS]: ImproveComments,
@ -189,7 +189,7 @@ export default class CopilotActionService {
throw new BadDataException("File commit hash not found");
}
await CopilotActionService.addCopilotAction({
await CopilotActionService.addCopilotActionToDatabase({
serviceCatalogId: data.serviceRepository.serviceCatalog!.id!,
serviceRepositoryId: data.serviceRepository.id!,
filePath: data.input.currentFilePath,
@ -202,61 +202,7 @@ export default class CopilotActionService {
return executionResult;
}
private static async addPullRequestToDatabase(data: {
pullRequest: PullRequest;
serviceCatalogId?: ObjectID | undefined;
serviceRepositoryId?: ObjectID | undefined;
}): Promise<CopilotPullRequest> {
let copilotPullRequest: CopilotPullRequest | null = null;
if (data.pullRequest && data.pullRequest.pullRequestNumber) {
copilotPullRequest = new CopilotPullRequest();
copilotPullRequest.pullRequestId =
data.pullRequest.pullRequestNumber.toString();
copilotPullRequest.copilotPullRequestStatus =
CopilotPullRequestStatus.Created;
if (data.serviceCatalogId) {
copilotPullRequest.serviceCatalogId = data.serviceCatalogId;
}
if (data.serviceRepositoryId) {
copilotPullRequest.serviceRepositoryId = data.serviceRepositoryId;
}
// send this to the API.
const url: URL = URL.fromString(
GetOneUptimeURL().toString() + "/api",
).addRoute(
`${new CopilotPullRequest()
.getCrudApiPath()
?.toString()}/add-pull-request/${GetRepositorySecretKey()}`,
);
const codeRepositoryResult: HTTPErrorResponse | HTTPResponse<JSONObject> =
await API.post(url, {
copilotPullRequest: CopilotPullRequest.toJSON(
copilotPullRequest,
CopilotPullRequest,
),
});
if (codeRepositoryResult instanceof HTTPErrorResponse) {
throw codeRepositoryResult;
}
copilotPullRequest = CopilotPullRequest.fromJSON(
codeRepositoryResult.data,
CopilotPullRequest,
) as CopilotPullRequest;
return copilotPullRequest;
}
throw new BadDataException("Pull Request Number not found");
}
private static async addCopilotAction(data: {
private static async addCopilotActionToDatabase(data: {
serviceCatalogId: ObjectID;
serviceRepositoryId: ObjectID;
filePath: string;
@ -270,11 +216,12 @@ export default class CopilotActionService {
let copilotPullRequest: CopilotPullRequest | null = null;
if (data.pullRequest) {
copilotPullRequest = await CopilotActionService.addPullRequestToDatabase({
pullRequest: data.pullRequest,
serviceCatalogId: data.serviceCatalogId,
serviceRepositoryId: data.serviceRepositoryId,
});
copilotPullRequest =
await CopilotPullRequestService.addPullRequestToDatabase({
pullRequest: data.pullRequest,
serviceCatalogId: data.serviceCatalogId,
serviceRepositoryId: data.serviceRepositoryId,
});
}
const copilotAction: CopilotAction = new CopilotAction();

View File

@ -0,0 +1,73 @@
import BadDataException from "Common/Types/Exception/BadDataException";
import PullRequest from "Common/Types/CodeRepository/PullRequest";
import ObjectID from "Common/Types/ObjectID";
import URL from "Common/Types/API/URL";
import { GetOneUptimeURL, GetRepositorySecretKey } from "../Config";
import HTTPErrorResponse from "Common/Types/API/HTTPErrorResponse";
import HTTPResponse from "Common/Types/API/HTTPResponse";
import { JSONObject } from "Common/Types/JSON";
import API from "Common/Utils/API";
import CopilotPullRequest from "Model/Models/CopilotPullRequest";
import CopilotPullRequestStatus from "Common/Types/Copilot/CopilotPullRequestStatus";
export default class CopilotPullRequestService {
public static async getOpenPullRequests(): Promise<
Array<CopilotPullRequest>
> {
return [];
}
public static async addPullRequestToDatabase(data: {
pullRequest: PullRequest;
serviceCatalogId?: ObjectID | undefined;
serviceRepositoryId?: ObjectID | undefined;
}): Promise<CopilotPullRequest> {
let copilotPullRequest: CopilotPullRequest | null = null;
if (data.pullRequest && data.pullRequest.pullRequestNumber) {
copilotPullRequest = new CopilotPullRequest();
copilotPullRequest.pullRequestId =
data.pullRequest.pullRequestNumber.toString();
copilotPullRequest.copilotPullRequestStatus =
CopilotPullRequestStatus.Created;
if (data.serviceCatalogId) {
copilotPullRequest.serviceCatalogId = data.serviceCatalogId;
}
if (data.serviceRepositoryId) {
copilotPullRequest.serviceRepositoryId = data.serviceRepositoryId;
}
// send this to the API.
const url: URL = URL.fromString(
GetOneUptimeURL().toString() + "/api",
).addRoute(
`${new CopilotPullRequest()
.getCrudApiPath()
?.toString()}/add-pull-request/${GetRepositorySecretKey()}`,
);
const codeRepositoryResult: HTTPErrorResponse | HTTPResponse<JSONObject> =
await API.post(url, {
copilotPullRequest: CopilotPullRequest.toJSON(
copilotPullRequest,
CopilotPullRequest,
),
});
if (codeRepositoryResult instanceof HTTPErrorResponse) {
throw codeRepositoryResult;
}
copilotPullRequest = CopilotPullRequest.fromJSON(
codeRepositoryResult.data,
CopilotPullRequest,
) as CopilotPullRequest;
return copilotPullRequest;
}
throw new BadDataException("Pull Request Number not found");
}
}

View File

@ -22,6 +22,7 @@ import CopilotCodeRepository from "Model/Models/CopilotCodeRepository";
import ServiceCopilotCodeRepository from "Model/Models/ServiceCopilotCodeRepository";
import Text from "Common/Types/Text";
import Execute from "CommonServer/Utils/Execute";
import CopilotPullRequestService from "../Service/CopilotPullRequest";
export interface CodeRepositoryResult {
codeRepository: CopilotCodeRepository;
@ -122,11 +123,17 @@ export default class CodeRepositoryUtil {
// create a pull request.
await this.createPullRequest({
const pullRequest: PullRequest = await this.createPullRequest({
branchName: branchName,
title: "OneUptime Copilot Setup",
body: "This pull request is created by OneUptime Copilot to setup the repository.",
});
// save this to the database.
await CopilotPullRequestService.addPullRequestToDatabase({
pullRequest: pullRequest,
});
}
public static async isRepoSetupProperly(): Promise<boolean> {