Skip to content

Commit

Permalink
finishing up
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Sep 21, 2024
1 parent 83a2f83 commit dbdaf8b
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apps/api/src/project/controller/project.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class ProjectController {
async syncFork(
@CurrentUser() user: User,
@Param('projectSlug') projectSlug: Project['slug'],
@Param('hardSync') hardSync: boolean = false
@Query('hardSync') hardSync: boolean = false
) {
return await this.service.syncFork(user, projectSlug, hardSync)
}
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/project/dto/fork.project/fork.project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Workspace } from '@prisma/client'
import { IsOptional, IsString } from 'class-validator'
import { IsBoolean, IsOptional, IsString } from 'class-validator'

export class ForkProject {
@IsString()
Expand All @@ -10,7 +10,7 @@ export class ForkProject {
@IsOptional()
name?: string

@IsString()
@IsBoolean()
@IsOptional()
storePrivateKey?: boolean
}
28 changes: 15 additions & 13 deletions apps/api/src/workspace-role/service/workspace-role.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,22 @@ export class WorkspaceRoleService {
})
)

// Create the project associations
const projectSlugToIdMap = await this.getProjectSlugToIdMap(
dto.projectSlugs
)

if (dto.projectSlugs && dto.projectSlugs.length > 0) {
op.push(
this.prisma.projectWorkspaceRoleAssociation.createMany({
data: dto.projectSlugs.map((projectSlug) => ({
roleId: workspaceRoleId,
projectId: projectSlugToIdMap.get(projectSlug)
}))
})
if (dto.projectSlugs) {
// Create the project associations
const projectSlugToIdMap = await this.getProjectSlugToIdMap(
dto.projectSlugs
)

if (dto.projectSlugs && dto.projectSlugs.length > 0) {
op.push(
this.prisma.projectWorkspaceRoleAssociation.createMany({
data: dto.projectSlugs.map((projectSlug) => ({
roleId: workspaceRoleId,
projectId: projectSlugToIdMap.get(projectSlug)
}))
})
)
}
}

const workspaceRole = (await this.prisma.$transaction(op))[0]
Expand Down
33 changes: 33 additions & 0 deletions apps/cli/src/commands/project/delete.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class DeleteProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +14,31 @@ export default class DeleteProject extends BaseCommand {
getDescription(): string {
return 'Deletes a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description: 'Slug of the project that you want to delete.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [projectSlug] = args

const { error, success } =
await ControllerInstance.getInstance().projectController.deleteProject(
{
projectSlug
},
this.headers
)

if (success) {
Logger.info(`Project ${projectSlug} deleted successfully!`)
} else {
Logger.error(`Failed to delete project: ${error.message}`)
}
}
}
60 changes: 60 additions & 0 deletions apps/cli/src/commands/project/fork.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type {
CommandActionData,
CommandArgument,
CommandOption
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'

export default class ForkProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +15,57 @@ export default class ForkProject extends BaseCommand {
getDescription(): string {
return 'Forks a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description: 'Slug of the project which you want to fork.'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-n',
long: '--name <Workspace Name>',
description: 'Name of the workspace.'
},
{
short: '-k',
long: '--store-private-key <boolean>',
description: 'Store the private key in the project. Defaults to true',
defaultValue: true
},
{
short: '-w',
long: '--workspace <string>',
description: 'Workspace slug to fork the project in'
}
]
}

async action({ options, args }: CommandActionData): Promise<void> {
const [projectSlug] = args

console.log(options)

const { data, error, success } =
await ControllerInstance.getInstance().projectController.forkProject(
{
projectSlug,
...options
},
this.headers
)

if (success) {
Logger.info(`Project ${data.name} (${data.slug}) forked successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
} else {
Logger.error(`Failed to fork project: ${error.message}`)
}
}
}
38 changes: 38 additions & 0 deletions apps/cli/src/commands/project/list-forks.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class ListProjectForks extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +14,36 @@ export default class ListProjectForks extends BaseCommand {
getDescription(): string {
return 'List all forks of a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description: 'Slug of the project whose forks you want to list.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [projectSlug] = args

const { data, error, success } =
await ControllerInstance.getInstance().projectController.getForks(
{ projectSlug },
this.headers
)

if (success) {
const projects = data.items
if (projects.length > 0) {
data.items.forEach((project: any) => {
Logger.info(`- ${project.name} (${project.slug})`)
})
} else {
Logger.info('No forks found')
}
} else {
Logger.error(`Failed fetching forks: ${error.message}`)
}
}
}
12 changes: 9 additions & 3 deletions apps/cli/src/commands/project/list.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ export default class ListProject extends BaseCommand {
)

if (success) {
data.items.forEach((project: any) => {
Logger.info(`- ${project.name} (${project.slug})`)
})
const projects = data.items

if (projects.length > 0) {
data.items.forEach((project: any) => {
Logger.info(`- ${project.name} (${project.slug})`)
})
} else {
Logger.info('No forks found')
}
} else {
Logger.error(`Failed fetching projects: ${error.message}`)
}
Expand Down
47 changes: 47 additions & 0 deletions apps/cli/src/commands/project/sync.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type {
CommandActionData,
CommandArgument,
CommandOption
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class SyncProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +15,44 @@ export default class SyncProject extends BaseCommand {
getDescription(): string {
return 'Sync a forked project with its parent'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description:
'Slug of the forked project that you want to sync with its parent.'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-h',
long: '--hard-sync',
description:
'Upserts a new copy of the parent onto the child. Defaults to soft sync'
}
]
}

async action({ args, options }: CommandActionData): Promise<void> {
const [projectSlug] = args

const { error, success } =
await ControllerInstance.getInstance().projectController.syncFork(
{
projectSlug,
hardSync: options.hardSync !== undefined
},
this.headers
)

if (success) {
Logger.info(`Project ${projectSlug} synced successfully!`)
} else {
Logger.error(`Failed to sync project: ${error.message}`)
}
}
}
34 changes: 34 additions & 0 deletions apps/cli/src/commands/project/unlink.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class UnlinkProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +14,32 @@ export default class UnlinkProject extends BaseCommand {
getDescription(): string {
return 'Unlinks a forked project from its parent project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description:
'Slug of the forked project that you want to unlink from its parent.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [projectSlug] = args

const { error, success } =
await ControllerInstance.getInstance().projectController.unlinkFork(
{
projectSlug
},
this.headers
)

if (success) {
Logger.info(`Project ${projectSlug} unlinked successfully!`)
} else {
Logger.error(`Failed to unlink project: ${error.message}`)
}
}
}
11 changes: 8 additions & 3 deletions apps/cli/src/commands/workspace/list.workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ export default class ListWorkspace extends BaseCommand {
)

if (success) {
data.items.forEach((workspace: any) => {
Logger.info(`- ${workspace.name} (${workspace.slug})`)
})
const workspaces = data.items
if (workspaces.length > 0) {
data.items.forEach((workspace: any) => {
Logger.info(`- ${workspace.name} (${workspace.slug})`)
})
} else {
Logger.info('No workspaces found')
}
} else {
Logger.error(`Failed fetching workspaces: ${error.message}`)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/controllers/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export default class ProjectController {
headers: Record<string, string>
): Promise<ClientResponse<SyncProjectResponse>> {
const response = await this.apiClient.put(
`/project/${request.projectSlug}/fork`,
`/api/project/${request.projectSlug}/fork?hardSync=${request.hardSync}`,
request,
headers
)
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/types/project.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ export interface ForkProjectResponse extends Project {}

export interface SyncProjectRequest {
projectSlug: string
hardSync?: boolean
}

export interface SyncProjectResponse {}

export interface UnlinkProjectRequest {
projectSlug: string
workspaceSlug: string
}

export interface UnlinkProjectResponse {}
Expand Down
2 changes: 1 addition & 1 deletion packages/api-client/src/types/workspace-role.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface WorkspaceRole {
id: string
name: string
slug: string
}[]
}
}[]
}

Expand Down
Loading

0 comments on commit dbdaf8b

Please sign in to comment.