refactor: Update CopilotActionAPI to include permission checks for create, read, delete, and update actions

This commit is contained in:
Simon Larsen 2024-09-05 11:26:01 +01:00
parent 10eb7239dc
commit e11ab9ee5c
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
5 changed files with 71 additions and 22 deletions

View File

@ -25,15 +25,30 @@ import ColumnLength from "../../Types/Database/ColumnLength";
@EnableDocumentation()
@TenantColumn("projectId")
@TableAccessControl({
create: [],
create: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.CreateCopilotAction,
],
read: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.ReadCopilotAction,
],
delete: [],
update: [],
delete: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.DeleteCopilotAction,
],
update: [
Permission.ProjectOwner,
Permission.ProjectAdmin,
Permission.ProjectMember,
Permission.EditCopilotAction,
],
})
@EnableWorkflow({
create: true,

View File

@ -237,9 +237,7 @@ export default class CopilotActionAPI extends BaseAPI<
?.toString()}/update-copilot-action/:secretkey`,
CodeRepositoryAuthorization.isAuthorizedRepository,
async (req: ExpressRequest, res: ExpressResponse, next: NextFunction) => {
try {
const secretkey: string = req.params["secretkey"]!;
if (!secretkey) {
@ -274,7 +272,7 @@ export default class CopilotActionAPI extends BaseAPI<
commitHash,
statusMessage,
logs,
actionId
actionId,
}: {
actionStatus: CopilotActionStatus;
pullRequestId?: ObjectID | undefined;
@ -284,18 +282,19 @@ export default class CopilotActionAPI extends BaseAPI<
actionId: ObjectID;
} = req.body;
const exisingAction = await CopilotActionService.findOneBy({
query: {
_id: actionId,
codeRepositoryId: codeRepository.id!,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
const exisingAction: CopilotAction | null =
await CopilotActionService.findOneBy({
query: {
_id: actionId,
codeRepositoryId: codeRepository.id!,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
if (!exisingAction) {
throw new BadDataException("Action not found");

View File

@ -3,6 +3,12 @@ import { OnCreate } from "../Types/Database/Hooks";
import DatabaseService from "./DatabaseService";
import ObjectID from "../../Types/ObjectID";
import Model from "Common/Models/DatabaseModels/CopilotCodeRepository";
import {
CopilotActionTypeData,
CopilotActionTypeUtil,
} from "../../Types/Copilot/CopilotActionType";
import CopilotActionTypePriority from "../../Models/DatabaseModels/CopilotActionTypePriority";
import CopilotActionTypePriorityService from "./CopilotActionTypePriorityService";
export class Service extends DatabaseService<Model> {
public constructor() {
@ -19,6 +25,36 @@ export class Service extends DatabaseService<Model> {
createBy: createBy,
};
}
protected override async onCreateSuccess(
_onCreate: OnCreate<Model>,
createdItem: Model,
): Promise<Model> {
// add all the actions.
const repo: Model = createdItem;
const defaultCopilotActionTypes: Array<CopilotActionTypeData> =
CopilotActionTypeUtil.getAllCopilotActionTypes();
for (const defaultAction of defaultCopilotActionTypes) {
const copilotActionTypePriority: CopilotActionTypePriority =
new CopilotActionTypePriority();
copilotActionTypePriority.projectId = repo.projectId!;
copilotActionTypePriority.actionType = defaultAction.type;
copilotActionTypePriority.priority = defaultAction.defaultPriority;
copilotActionTypePriority.codeRepositoryId = repo.id!;
await CopilotActionTypePriorityService.create({
data: copilotActionTypePriority,
props: {
isRoot: true,
},
});
}
return createdItem;
}
}
export default new Service();

View File

@ -19,13 +19,12 @@ export default class ImproveComments extends CopilotActionBase {
this.acceptFileExtentions = CodeRepositoryUtil.getCodeFileExtentions();
}
protected override async isActionRequired(data: {
protected override async isActionRequired(_data: {
copilotActionProp: FileActionProp;
}): Promise<boolean> {
return true;
}
public override isActionComplete(_data: CopilotProcess): Promise<boolean> {
return Promise.resolve(this.isRequirementsMet);
}

View File

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