Skip to content

Commit

Permalink
refactor: Update CopilotActionAPI to include permission checks for cr…
Browse files Browse the repository at this point in the history
…eate, read, delete, and update actions
  • Loading branch information
simlarsen committed Sep 5, 2024
1 parent 10eb723 commit e11ab9e
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
21 changes: 18 additions & 3 deletions Common/Models/DatabaseModels/CopilotActionTypePriority.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 14 additions & 15 deletions Common/Server/API/CopilotActionAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -274,7 +272,7 @@ export default class CopilotActionAPI extends BaseAPI<
commitHash,
statusMessage,
logs,
actionId
actionId,
}: {
actionStatus: CopilotActionStatus;
pullRequestId?: ObjectID | undefined;
Expand All @@ -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");
Expand Down
36 changes: 36 additions & 0 deletions Common/Server/Services/CopilotCodeRepositoryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
5 changes: 2 additions & 3 deletions Copilot/Service/CopilotActions/ImproveComments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Copilot/Service/CopilotActions/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export default class CopilotActionService {
commitHash: data.commitHash,
statusMessage: data.statusMessage,
logs: data.logs,
actionId: data.copilotActonId
actionId: data.copilotActonId,
});
}
}

0 comments on commit e11ab9e

Please sign in to comment.