From f00f75673b8f02efce8fbd0937eecba9a1186ea8 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 7 Mar 2021 22:53:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(pipelines):=20=E6=B7=BB=E5=8A=A0=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=20commit=20logs=20=E6=8E=A5=E5=8F=A3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipelines/pipelines.module.ts | 9 ++++- src/pipelines/pipelines.resolver.ts | 6 ++++ src/pipelines/pipelines.service.spec.ts | 45 ++++++++++++++++++++++++- src/pipelines/pipelines.service.ts | 18 ++++++++++ src/repos/models/list-logs.options.ts | 5 +++ src/repos/repos.constants.ts | 1 + 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/repos/models/list-logs.options.ts create mode 100644 src/repos/repos.constants.ts diff --git a/src/pipelines/pipelines.module.ts b/src/pipelines/pipelines.module.ts index 59f1df1..f614420 100644 --- a/src/pipelines/pipelines.module.ts +++ b/src/pipelines/pipelines.module.ts @@ -3,9 +3,16 @@ import { PipelinesResolver } from './pipelines.resolver'; import { PipelinesService } from './pipelines.service'; import { TypeOrmModule } from '@nestjs/typeorm'; import { Pipeline } from './pipeline.entity'; +import { BullModule } from '@nestjs/bull'; +import { LIST_LOGS_TASK } from '../repos/repos.constants'; @Module({ - imports: [TypeOrmModule.forFeature([Pipeline])], + imports: [ + TypeOrmModule.forFeature([Pipeline]), + BullModule.registerQueue({ + name: LIST_LOGS_TASK, + }), + ], providers: [PipelinesResolver, PipelinesService], }) export class PipelinesModule {} diff --git a/src/pipelines/pipelines.resolver.ts b/src/pipelines/pipelines.resolver.ts index 572e612..d992976 100644 --- a/src/pipelines/pipelines.resolver.ts +++ b/src/pipelines/pipelines.resolver.ts @@ -4,6 +4,7 @@ import { UpdatePipelineInput } from './dtos/update-pipeline.input'; import { Pipeline } from './pipeline.entity'; import { PipelinesService } from './pipelines.service'; import { ListPipelineArgs } from './dtos/list-pipelines.args'; +import { LogList } from '../repos/dtos/log-list.model'; @Resolver() export class PipelinesResolver { @@ -41,4 +42,9 @@ export class PipelinesResolver { async deletePipeline(@Args('id', { type: () => String }) id: string) { return await this.service.remove(id); } + + @Query(() => String) + async listLogsForPipeline(@Args('id', { type: () => String }) id: string) { + return await this.service.listLogsForPipeline(id); + } } diff --git a/src/pipelines/pipelines.service.spec.ts b/src/pipelines/pipelines.service.spec.ts index c0816cc..7e2c316 100644 --- a/src/pipelines/pipelines.service.spec.ts +++ b/src/pipelines/pipelines.service.spec.ts @@ -2,25 +2,68 @@ import { Test, TestingModule } from '@nestjs/testing'; import { PipelinesService } from './pipelines.service'; import { Pipeline } from './pipeline.entity'; import { getRepositoryToken } from '@nestjs/typeorm'; +import { getQueueToken } from '@nestjs/bull'; +import { LIST_LOGS_TASK } from '../repos/repos.constants'; +import { Repository } from 'typeorm'; +import { Project } from '../projects/project.entity'; +import { Job, Queue } from 'bull'; +import { ListLogsOption } from '../repos/models/list-logs.options'; describe('PipelinesService', () => { let service: PipelinesService; + let repository: Repository; + let pipeline: Pipeline; + let queue: Queue; beforeEach(async () => { + pipeline = Object.assign(new Pipeline(), { + id: 'test-pipeline', + name: 'pipeline', + branch: 'master', + projectId: 'test-project', + project: Object.assign(new Project(), { + id: 'test-project', + name: 'project', + } as Project), + } as Pipeline); + const module: TestingModule = await Test.createTestingModule({ providers: [ PipelinesService, { provide: getRepositoryToken(Pipeline), - useValue: {}, + useValue: { + findOneOrFail: jest.fn().mockImplementation(() => pipeline), + }, + }, + { + provide: getQueueToken(LIST_LOGS_TASK), + useValue: { + add: jest.fn().mockImplementation(() => ({ id: 1 } as Job)), + }, }, ], }).compile(); service = module.get(PipelinesService); + repository = module.get(getRepositoryToken(Pipeline)); + queue = module.get(getQueueToken(LIST_LOGS_TASK)); }); it('should be defined', () => { expect(service).toBeDefined(); }); + + describe('listLogsForPipeline', () => { + it('should send task to queue.', async () => { + const add = jest.spyOn(queue, 'add'); + await expect( + service.listLogsForPipeline('test-pipeline'), + ).resolves.toEqual(1); + expect(add).toBeCalledWith({ + project: pipeline.project, + branch: pipeline.branch, + }); + }); + }); }); diff --git a/src/pipelines/pipelines.service.ts b/src/pipelines/pipelines.service.ts index b1f01fe..169b31d 100644 --- a/src/pipelines/pipelines.service.ts +++ b/src/pipelines/pipelines.service.ts @@ -6,6 +6,10 @@ import { BaseDbService } from '../commons/services/base-db.service'; import { CreatePipelineInput } from './dtos/create-pipeline.input'; import { UpdatePipelineInput } from './dtos/update-pipeline.input'; import { ListPipelineArgs } from './dtos/list-pipelines.args'; +import { InjectQueue } from '@nestjs/bull'; +import { LIST_LOGS_TASK } from '../repos/repos.constants'; +import { Queue } from 'bull'; +import { ListLogsOption } from '../repos/models/list-logs.options'; @Injectable() export class PipelinesService extends BaseDbService { @@ -15,6 +19,8 @@ export class PipelinesService extends BaseDbService { constructor( @InjectRepository(Pipeline) readonly repository: Repository, + @InjectQueue(LIST_LOGS_TASK) + private readonly listLogsQueue: Queue, ) { super(repository); } @@ -36,4 +42,16 @@ export class PipelinesService extends BaseDbService { async remove(id: string) { return (await this.repository.softDelete({ id })).affected; } + + async listLogsForPipeline(id: string) { + const pipeline = await this.repository.findOneOrFail({ + where: { id }, + relations: ['project'], + }); + const job = await this.listLogsQueue.add({ + project: pipeline.project, + branch: pipeline.branch, + }); + return job.id; + } } diff --git a/src/repos/models/list-logs.options.ts b/src/repos/models/list-logs.options.ts new file mode 100644 index 0000000..ccc3d02 --- /dev/null +++ b/src/repos/models/list-logs.options.ts @@ -0,0 +1,5 @@ +import { Project } from '../../projects/project.entity'; +export interface ListLogsOption { + project: Project; + branch: string; +} diff --git a/src/repos/repos.constants.ts b/src/repos/repos.constants.ts new file mode 100644 index 0000000..6c179ab --- /dev/null +++ b/src/repos/repos.constants.ts @@ -0,0 +1 @@ +export const LIST_LOGS_TASK = 'LIST_LOGS_TASK';