From ab6c56e55a8c942df3faab9249c9523c6cf6d560 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Tue, 23 Feb 2021 20:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BF=9C=E7=A8=8B=E4=BB=93?= =?UTF-8?q?=E5=BA=93=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yml | 4 ++- package.json | 2 +- ...hes-list.model.ts => branch-list.model.ts} | 2 +- .../{logs-list.model.ts => log-list.model.ts} | 2 +- src/repos/repos.module.ts | 3 +- src/repos/repos.resolver.ts | 12 +++---- src/repos/repos.service.spec.ts | 18 +++++++++- src/repos/repos.service.ts | 36 ++++++++++++++++--- workspaces/.gitkeep | 0 9 files changed, 62 insertions(+), 17 deletions(-) rename src/repos/dtos/{branches-list.model.ts => branch-list.model.ts} (93%) rename src/repos/dtos/{logs-list.model.ts => log-list.model.ts} (85%) delete mode 100644 workspaces/.gitkeep diff --git a/config.yml b/config.yml index ff3fc55..3aa6f0a 100644 --- a/config.yml +++ b/config.yml @@ -8,4 +8,6 @@ db: port: 5432 database: fennec username: fennec - password: \ No newline at end of file + password: +workspaces: + root: '/Users/ivanli/Projects/fennec/workspaces' \ No newline at end of file diff --git a/package.json b/package.json index da2f17e..9aa40c0 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", - "start:debug": "nest start --debug --watch", + "start:debug": "DEBUG=simple-git,simple-git:* nest start --debug --watch", "start:prod": "node dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", diff --git a/src/repos/dtos/branches-list.model.ts b/src/repos/dtos/branch-list.model.ts similarity index 93% rename from src/repos/dtos/branches-list.model.ts rename to src/repos/dtos/branch-list.model.ts index 10f698e..4ec71d7 100644 --- a/src/repos/dtos/branches-list.model.ts +++ b/src/repos/dtos/branch-list.model.ts @@ -15,7 +15,7 @@ export class Branch implements BranchSummaryBranch { } @ObjectType() -export class BranchesList { +export class BranchList { detached: boolean; current: string; all: string[]; diff --git a/src/repos/dtos/logs-list.model.ts b/src/repos/dtos/log-list.model.ts similarity index 85% rename from src/repos/dtos/logs-list.model.ts rename to src/repos/dtos/log-list.model.ts index 63a7f6a..7e612ac 100644 --- a/src/repos/dtos/logs-list.model.ts +++ b/src/repos/dtos/log-list.model.ts @@ -13,7 +13,7 @@ export class LogFields { } @ObjectType() -export class LogsList implements LogResult { +export class LogList implements LogResult { @Field(() => [LogFields]) all: LogFields[]; total: number; diff --git a/src/repos/repos.module.ts b/src/repos/repos.module.ts index 6922ecf..ae00feb 100644 --- a/src/repos/repos.module.ts +++ b/src/repos/repos.module.ts @@ -3,9 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Project } from '../projects/project.entity'; import { ReposResolver } from './repos.resolver'; import { ReposService } from './repos.service'; +import { ConfigModule } from '@nestjs/config'; @Module({ - imports: [TypeOrmModule.forFeature([Project])], + imports: [TypeOrmModule.forFeature([Project]), ConfigModule], providers: [ReposResolver, ReposService], }) export class ReposModule {} diff --git a/src/repos/repos.resolver.ts b/src/repos/repos.resolver.ts index d6fe32e..e5b83d5 100644 --- a/src/repos/repos.resolver.ts +++ b/src/repos/repos.resolver.ts @@ -1,21 +1,21 @@ import { Args, Query, Resolver } from '@nestjs/graphql'; import { ListLogsArgs } from './dtos/list-logs.args'; import { ReposService } from './repos.service'; -import { LogsList } from './dtos/logs-list.model'; +import { LogList } from './dtos/log-list.model'; import { ListBranchesArgs } from './dtos/list-branches.args'; -import { BranchesList } from './dtos/branches-list.model'; +import { BranchList } from './dtos/branch-list.model'; @Resolver() export class ReposResolver { - con - @Query(() => LogsList) + constructor(private readonly service: ReposService) {} + @Query(() => LogList) async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) { return await this.service.listLogs(dto); } - @Query(() => BranchesList) + @Query(() => BranchList) async ListBranchesArgs( @Args('listBranchesArgs') dto: ListBranchesArgs, - ): Promise { + ): Promise { return await this.service.listBranches(dto).then((data) => { return { ...data, diff --git a/src/repos/repos.service.spec.ts b/src/repos/repos.service.spec.ts index 790f3af..1170ef2 100644 --- a/src/repos/repos.service.spec.ts +++ b/src/repos/repos.service.spec.ts @@ -2,6 +2,11 @@ import { Test, TestingModule } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; import { Project } from '../projects/project.entity'; import { ReposService } from './repos.service'; +import { ConfigService } from '@nestjs/config'; +import { rm, unlink } from 'fs/promises'; +import { join } from 'path'; + +const workspacesRoot = '/Users/ivanli/Projects/fennec/workspaces'; describe('ReposService', () => { let service: ReposService; @@ -24,6 +29,14 @@ describe('ReposService', () => { provide: getRepositoryToken(Project), useFactory: repositoryMockFactory, }, + { + provide: ConfigService, + useValue: { + get() { + return workspacesRoot; + }, + }, + }, ], }).compile(); @@ -34,10 +47,13 @@ describe('ReposService', () => { expect(service).toBeDefined(); }); describe('listLogs', () => { + beforeEach(async () => { + await rm(join(workspacesRoot, 'test1'), { recursive: true }); + }); it('should be return logs', async () => { const result = await service.listLogs({ projectId: '1' }); expect(result).toBeDefined(); - }, 10_000); + }, 20_000); }); describe('listBranch', () => { it('should be return branches', async () => { diff --git a/src/repos/repos.service.ts b/src/repos/repos.service.ts index 642d224..b0062dc 100644 --- a/src/repos/repos.service.ts +++ b/src/repos/repos.service.ts @@ -8,18 +8,35 @@ import { Repository } from 'typeorm'; import { Project } from '../projects/project.entity'; import { ListBranchesArgs } from './dtos/list-branches.args'; import { ListLogsArgs } from './dtos/list-logs.args'; +import { ConfigService } from '@nestjs/config'; +import { log } from 'console'; @Injectable() export class ReposService { constructor( @InjectRepository(Project) private readonly projectRepository: Repository, + private readonly configService: ConfigService, ) {} async getGit(project: Project) { - const workspacePath = join(__dirname, '../../workspaces', project.name); - await access(workspacePath, F_OK).catch(() => mkdir(workspacePath)); - return gitP(workspacePath); + const workspacePath = join( + this.configService.get('workspaces.root'), + project.name, + ); + const firstInit = await access(workspacePath, F_OK) + .then(() => false) + .catch(async () => { + await mkdir(workspacePath); + return true; + }); + const git = gitP(workspacePath); + if (firstInit) { + await git.init(); + await git.addRemote('origin', project.sshUrl); + // await git.clone(project.sshUrl, workspacePath); + } + return git; } async listLogs(dto: ListLogsArgs) { @@ -27,8 +44,17 @@ export class ReposService { id: dto.projectId, }); const git = await this.getGit(project); - await git.fetch(); - return git.log(); + await git + .outputHandler((command, stdout, stderr) => { + stdout.pipe(process.stdout); + stderr.pipe(process.stderr); + }) + .fetch(); + // await git.checkoutBranch('master', 'origin/master'); + return await git.log({ + '--branches': dto.branch ?? '', + '--remotes': 'origin', + }); } async listBranches(dto: ListBranchesArgs) { diff --git a/workspaces/.gitkeep b/workspaces/.gitkeep deleted file mode 100644 index e69de29..0000000