From cba4c0464c29ac2e51d66753fa75bb00ad6620e2 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Wed, 10 Mar 2021 21:41:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(pipelines):=20list=20commit=20logs=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=9B=B4=E6=8E=A5=E8=AE=A2=E9=98=85=E3=80=82?= =?UTF-8?q?=20=E6=9B=BF=E4=BB=A3=E4=BA=86=E5=85=88=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E5=86=8D=E8=AE=A2=E9=98=85=EF=BC=8C=E7=A1=AE=E4=BF=9D=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=8D=E4=B8=A2=E5=A4=B1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipelines/pipelines.resolver.ts | 13 ++++++++++--- src/pipelines/pipelines.service.spec.ts | 2 +- src/pipelines/pipelines.service.ts | 2 +- src/repos/list-logs.consumer.ts | 16 +++------------- src/repos/repos.module.ts | 10 +--------- src/repos/repos.resolver.ts | 21 ++------------------- 6 files changed, 18 insertions(+), 46 deletions(-) diff --git a/src/pipelines/pipelines.resolver.ts b/src/pipelines/pipelines.resolver.ts index d992976..9accb80 100644 --- a/src/pipelines/pipelines.resolver.ts +++ b/src/pipelines/pipelines.resolver.ts @@ -1,4 +1,4 @@ -import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; +import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql'; import { CreatePipelineInput } from './dtos/create-pipeline.input'; import { UpdatePipelineInput } from './dtos/update-pipeline.input'; import { Pipeline } from './pipeline.entity'; @@ -43,8 +43,15 @@ export class PipelinesResolver { return await this.service.remove(id); } - @Query(() => String) + @Subscription(() => LogList, { + resolve: (value) => { + return value; + }, + }) async listLogsForPipeline(@Args('id', { type: () => String }) id: string) { - return await this.service.listLogsForPipeline(id); + const job = await this.service.listLogsForPipeline(id); + return (async function* () { + yield await job.finished(); + })(); } } diff --git a/src/pipelines/pipelines.service.spec.ts b/src/pipelines/pipelines.service.spec.ts index 7e2c316..dc57557 100644 --- a/src/pipelines/pipelines.service.spec.ts +++ b/src/pipelines/pipelines.service.spec.ts @@ -59,7 +59,7 @@ describe('PipelinesService', () => { const add = jest.spyOn(queue, 'add'); await expect( service.listLogsForPipeline('test-pipeline'), - ).resolves.toEqual(1); + ).resolves.toEqual({ id: 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 20ef905..51eed17 100644 --- a/src/pipelines/pipelines.service.ts +++ b/src/pipelines/pipelines.service.ts @@ -50,6 +50,6 @@ export class PipelinesService extends BaseDbService { project: pipeline.project, branch: pipeline.branch, }); - return job.id; + return job; } } diff --git a/src/repos/list-logs.consumer.ts b/src/repos/list-logs.consumer.ts index 0c457d2..1e45379 100644 --- a/src/repos/list-logs.consumer.ts +++ b/src/repos/list-logs.consumer.ts @@ -1,24 +1,14 @@ -import { PubSub } from 'graphql-subscriptions'; import { ReposService } from './repos.service'; import { Processor, Process } from '@nestjs/bull'; import { Job } from 'bull'; import { ListLogsOption } from './models/list-logs.options'; -import { - LIST_LOGS_DONE, - LIST_LOGS_PUB_SUB, - LIST_LOGS_TASK, -} from './repos.constants'; -import { Inject } from '@nestjs/common'; +import { LIST_LOGS_TASK } from './repos.constants'; @Processor(LIST_LOGS_TASK) export class ListLogsConsumer { - constructor( - private readonly service: ReposService, - @Inject(LIST_LOGS_PUB_SUB) - private readonly pubSub: PubSub, - ) {} + constructor(private readonly service: ReposService) {} @Process() async listLogs(job: Job) { const logs = await this.service.listLogs(job.data); - this.pubSub.publish(LIST_LOGS_DONE, logs); + return logs; } } diff --git a/src/repos/repos.module.ts b/src/repos/repos.module.ts index ac411b6..99cc87b 100644 --- a/src/repos/repos.module.ts +++ b/src/repos/repos.module.ts @@ -19,15 +19,7 @@ import { ListLogsConsumer } from './list-logs.consumer'; name: LIST_LOGS_TASK, }), ], - providers: [ - ReposResolver, - ReposService, - ListLogsConsumer, - { - provide: LIST_LOGS_PUB_SUB, - useValue: new PubSub(), - }, - ], + providers: [ReposResolver, ReposService, ListLogsConsumer], exports: [ReposService], }) export class ReposModule {} diff --git a/src/repos/repos.resolver.ts b/src/repos/repos.resolver.ts index b016d69..ace9f34 100644 --- a/src/repos/repos.resolver.ts +++ b/src/repos/repos.resolver.ts @@ -1,21 +1,4 @@ -import { LIST_LOGS_DONE, LIST_LOGS_PUB_SUB } from './repos.constants'; -import { Resolver, Subscription } from '@nestjs/graphql'; -import { LogList } from './dtos/log-list.model'; -import { Inject } from '@nestjs/common'; -import { PubSub } from 'apollo-server-express'; +import { Resolver } from '@nestjs/graphql'; @Resolver() -export class ReposResolver { - constructor( - @Inject(LIST_LOGS_PUB_SUB) - private readonly pubSub: PubSub, - ) {} - @Subscription(() => LogList, { - resolve: (value) => { - return value; - }, - }) - listLogsDone() { - return this.pubSub.asyncIterator(LIST_LOGS_DONE); - } -} +export class ReposResolver {}