Skip to content

Commit

Permalink
feat(graphql): add queries for deleting and archiving projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed Jul 4, 2024
1 parent 4deded8 commit beb5927
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 1 deletion.
83 changes: 83 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/server/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export type KV = {
count: () => Promise<number>;
updateStats: (id: string) => Promise<void>;
byName: (name: string) => Promise<Project | null>;
remove: (id: string) => Promise<void>;
};
runs: {
save: (project: string, data: Run) => Promise<void>;
Expand Down
9 changes: 9 additions & 0 deletions src/server/graphql/objects/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export class Project {
reliability?: number;
buildsPerWeek?: number;
recentRuns?: Run[];
isPrivate?: boolean;
owner?: string;
archived?: boolean;

constructor({
id,
Expand All @@ -34,6 +37,9 @@ export class Project {
reliability,
buildsPerWeek,
recentRuns,
isPrivate,
owner,
archived,
}: Project) {
this.id = id;
this.path = path;
Expand All @@ -50,5 +56,8 @@ export class Project {
this.reliability = reliability;
this.buildsPerWeek = buildsPerWeek;
this.recentRuns = recentRuns;
this.isPrivate = isPrivate;
this.owner = owner;
this.archived = archived;
}
}
54 changes: 54 additions & 0 deletions src/server/graphql/resolvers/project/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,57 @@ export async function updateProject(

return ctx.kv.projects.get(args.id);
}

export async function deleteProject(
root: any,
args: any,
ctx: Context
): Promise<boolean> {
const project = await ctx.kv.projects.get(args.id);

if (!project) {
return false;
}

await ctx.kv.projects.remove(args.id);

return true;
}

export async function archiveProject(
root: any,
args: any,
ctx: Context
): Promise<Project | null> {
const project = await ctx.kv.projects.get(args.id);

if (!project) {
return null;
}

await ctx.kv.projects.save({
...project,
archived: true,
});

return ctx.kv.projects.get(args.id);
}

export async function unarchiveProject(
root: any,
args: any,
ctx: Context
): Promise<Project | null> {
const project = await ctx.kv.projects.get(args.id);

if (!project) {
return null;
}

await ctx.kv.projects.save({
...project,
archived: false,
});

return ctx.kv.projects.get(args.id);
}
44 changes: 43 additions & 1 deletion src/server/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
getProjects,
} from "./resolvers/project/queries.ts";
import { runJob } from "./resolvers/job/mutations.ts";
import { createProject, updateProject } from "./resolvers/project/mutations.ts";
import {
archiveProject,
createProject,
deleteProject,
unarchiveProject,
updateProject,
} from "./resolvers/project/mutations.ts";
import { runPipeline } from "./resolvers/run/mutations.ts";
import { countRuns, getRun, getRuns } from "./resolvers/run/queries.ts";
import { Run } from "./objects/run.ts";
Expand Down Expand Up @@ -76,6 +82,9 @@ builder.objectType(Project, {
nullable: true,
resolve: (root) => root.recentRuns,
}),
isPrivate: t.exposeBoolean("isPrivate", { nullable: true }),
owner: t.exposeString("owner", { nullable: true }),
archived: t.exposeBoolean("archived", { nullable: true }),
}),
});

Expand Down Expand Up @@ -130,6 +139,15 @@ export const ActionInput = builder.inputType("ActionInput", {
}),
});

const ProjectFilters = builder.inputType("ProjectFilters", {
fields: (t) => ({
org: t.string({ required: false }),
user: t.string({ required: false }),
archived: t.boolean({ required: false }),
tags: t.stringList({ required: false }),
}),
});

builder.queryType({
fields: (t) => ({
projects: t.field({
Expand All @@ -139,6 +157,7 @@ builder.queryType({
limit: t.arg.int(),
cursor: t.arg.string(),
reverse: t.arg.boolean(),
filters: t.arg({ type: ProjectFilters, required: false }),
},
resolve: getProjects,
}),
Expand Down Expand Up @@ -270,6 +289,29 @@ builder.mutationType({
},
resolve: saveActions,
}),
archiveProject: t.field({
type: Project,
nullable: true,
args: {
id: t.arg.id({ required: true }),
},
resolve: archiveProject,
}),
unarchiveProject: t.field({
type: Project,
nullable: true,
args: {
id: t.arg.id({ required: true }),
},
resolve: unarchiveProject,
}),
deleteProject: t.field({
type: "Boolean",
args: {
id: t.arg.id({ required: true }),
},
resolve: deleteProject,
}),
}),
});

Expand Down
16 changes: 16 additions & 0 deletions src/server/kv/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,19 @@ export async function count() {
for await (const _ of iter) n++;
return n;
}

export async function remove(id: string) {
const project = await get(id);
if (!project) return;
await kv
.atomic()
.delete([FLUENTCI_KV_PREFIX, "projects", id])
.delete([FLUENTCI_KV_PREFIX, "path", project.path || "empty"])
.delete([FLUENTCI_KV_PREFIX, "projects_by_name", project.name])
.delete([
FLUENTCI_KV_PREFIX,
"projects_by_date",
dayjs(project.createdAt).unix(),
])
.commit();
}

0 comments on commit beb5927

Please sign in to comment.