refactor: Add API endpoint for updating CopilotAction

This commit is contained in:
Simon Larsen 2024-09-04 21:01:58 +01:00
parent c4fbc7fadd
commit 87234c95ce
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
3 changed files with 101 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import CopilotActionStatus from "../../Types/Copilot/CopilotActionStatus";
import CopilotActionTypePriority from "../../Models/DatabaseModels/CopilotActionTypePriority";
import CopilotActionTypePriorityService from "../Services/CopilotActionTypePriorityService";
import SortOrder from "../../Types/BaseDatabase/SortOrder";
import JSONFunctions from "../../Types/JSONFunctions";
export default class CopilotActionAPI extends BaseAPI<
CopilotAction,
@ -171,7 +172,7 @@ export default class CopilotActionAPI extends BaseAPI<
this.router.post(
`${new this.entityType()
.getCrudApiPath()
?.toString()}/queue-copilot-action/:secretkey`,
?.toString()}/create-copilot-action/:secretkey`,
CodeRepositoryAuthorization.isAuthorizedRepository,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
try {
@ -229,5 +230,99 @@ export default class CopilotActionAPI extends BaseAPI<
}
},
);
this.router.post(
`${new this.entityType()
.getCrudApiPath()
?.toString()}/update-copilot-action/:secretkey`,
CodeRepositoryAuthorization.isAuthorizedRepository,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
try {
const secretkey: string = req.params["secretkey"]!;
if (!secretkey) {
throw new BadDataException("Secret key is required");
}
const codeRepository: CopilotCodeRepository | null =
await CopilotCodeRepositoryService.findOneBy({
query: {
secretToken: new ObjectID(secretkey),
},
select: {
_id: true,
projectId: true,
},
props: {
isRoot: true,
},
});
if (!codeRepository) {
throw new BadDataException(
"Code repository not found. Secret key is invalid.",
);
}
req.body = JSONFunctions.deserialize(req.body);
const {
actionStatus,
pullRequestId,
commitHash,
statusMessage,
logs,
actionId
}: {
actionStatus: CopilotActionStatus;
pullRequestId?: ObjectID | undefined;
commitHash?: string | undefined;
statusMessage?: string | undefined;
logs?: Array<string> | undefined;
actionId: ObjectID;
} = req.body;
const exisingAction = await CopilotActionService.findOneBy({
query: {
_id: actionId,
codeRepositoryId: codeRepository.id!,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
if (!exisingAction) {
throw new BadDataException("Action not found");
}
await CopilotActionService.updateOneBy({
query: {
_id: actionId,
codeRepositoryId: codeRepository.id!,
},
data: {
copilotActionStatus: actionStatus!,
copilotPullRequestId: pullRequestId!,
commitHash: commitHash!,
statusMessage: statusMessage!,
logs: logs?.join("\n") || "",
},
props: {
isRoot: true,
},
});
return Response.sendEmptySuccessResponse(req, res);
} catch (err) {
next(err);
}
},
);
}
}

View File

@ -213,6 +213,7 @@ export default class CopilotActionService {
commitHash: data.commitHash,
statusMessage: data.statusMessage,
logs: data.logs,
actionId: data.copilotActonId
});
}
}

View File

@ -130,7 +130,7 @@ export default class CopilotActionUtil {
for (const action of actions) {
try {
const savedAction: CopilotAction =
await CopilotActionUtil.addCopilotAction({
await CopilotActionUtil.createCopilotAction({
copilotAction: action,
});
@ -144,6 +144,7 @@ export default class CopilotActionUtil {
}
public static async updateCopilotAction(data: {
actionId: ObjectID;
actionStatus: CopilotActionStatus;
pullRequestId?: ObjectID | undefined;
commitHash?: string | undefined;
@ -169,7 +170,7 @@ export default class CopilotActionUtil {
}
}
public static async addCopilotAction(data: {
public static async createCopilotAction(data: {
copilotAction: CopilotAction;
}): Promise<CopilotAction> {
// send this to the API.
@ -178,7 +179,7 @@ export default class CopilotActionUtil {
).addRoute(
`${new CopilotAction()
.getCrudApiPath()
?.toString()}/add-copilot-action/${GetRepositorySecretKey()}`,
?.toString()}/create-copilot-action/${GetRepositorySecretKey()}`,
);
const codeRepositoryResult: HTTPErrorResponse | HTTPResponse<JSONObject> =