From 032aa89b050c28db52f81df5a16b17d446879ad2 Mon Sep 17 00:00:00 2001 From: Ivan Date: Sun, 28 Mar 2021 19:37:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(pipelines):=20list=20logs=20=E6=97=B6?= =?UTF-8?q?=E6=8F=90=E4=BE=9B=E5=85=B3=E8=81=94=E7=9A=84=20tasks=20?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pipeline-tasks/pipeline-tasks.service.ts | 4 ++ src/pipelines/commit-logs.resolver.spec.ts | 18 +++++++++ src/pipelines/commit-logs.resolver.ts | 39 ++++++++++++++++++++ src/pipelines/pipelines.module.ts | 5 ++- src/pipelines/pipelines.resolver.ts | 15 +------- src/repos/dtos/log-list.model.ts | 2 + src/webhooks/webhooks.module.ts | 6 --- 7 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/pipelines/commit-logs.resolver.spec.ts create mode 100644 src/pipelines/commit-logs.resolver.ts diff --git a/src/pipeline-tasks/pipeline-tasks.service.ts b/src/pipeline-tasks/pipeline-tasks.service.ts index 2292d3b..3258af6 100644 --- a/src/pipeline-tasks/pipeline-tasks.service.ts +++ b/src/pipeline-tasks/pipeline-tasks.service.ts @@ -69,6 +69,10 @@ export class PipelineTasksService { return await this.repository.find({ pipelineId }); } + async listTasksByCommitHash(hash: string) { + return await this.repository.find({ commit: hash }); + } + async doNextTask(pipeline: Pipeline) { const [lckKey, tasksKey] = this.getRedisTokens(pipeline); const redis = this.redis.getClient(); diff --git a/src/pipelines/commit-logs.resolver.spec.ts b/src/pipelines/commit-logs.resolver.spec.ts new file mode 100644 index 0000000..61593b0 --- /dev/null +++ b/src/pipelines/commit-logs.resolver.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { CommitLogsResolver } from './commit-logs.resolver'; + +describe('CommitLogsResolver', () => { + let resolver: CommitLogsResolver; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [CommitLogsResolver], + }).compile(); + + resolver = module.get(CommitLogsResolver); + }); + + it('should be defined', () => { + expect(resolver).toBeDefined(); + }); +}); diff --git a/src/pipelines/commit-logs.resolver.ts b/src/pipelines/commit-logs.resolver.ts new file mode 100644 index 0000000..575aa16 --- /dev/null +++ b/src/pipelines/commit-logs.resolver.ts @@ -0,0 +1,39 @@ +import { + Args, + Info, + Parent, + ResolveField, + Resolver, + Subscription, +} from '@nestjs/graphql'; +import { GraphQLResolveInfo } from 'graphql'; +import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service'; +import { LogFields, LogList } from '../repos/dtos/log-list.model'; +import { PipelinesService } from './pipelines.service'; + +@Resolver(() => LogFields) +export class CommitLogsResolver { + constructor( + private readonly service: PipelinesService, + private readonly taskServices: PipelineTasksService, + ) {} + @Subscription(() => LogList, { + resolve: (value) => { + return value; + }, + }) + async listLogsForPipeline( + @Args('id', { type: () => String }) id: string, + @Info() info: GraphQLResolveInfo, + ) { + info.returnType.toString(); + const job = await this.service.listLogsForPipeline(id); + return (async function* () { + yield await job.finished(); + })(); + } + @ResolveField() + async tasks(@Parent() commit: LogFields) { + return await this.taskServices.listTasksByCommitHash(commit.hash); + } +} diff --git a/src/pipelines/pipelines.module.ts b/src/pipelines/pipelines.module.ts index f614420..77c76f7 100644 --- a/src/pipelines/pipelines.module.ts +++ b/src/pipelines/pipelines.module.ts @@ -5,6 +5,8 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { Pipeline } from './pipeline.entity'; import { BullModule } from '@nestjs/bull'; import { LIST_LOGS_TASK } from '../repos/repos.constants'; +import { CommitLogsResolver } from './commit-logs.resolver'; +import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module'; @Module({ imports: [ @@ -12,7 +14,8 @@ import { LIST_LOGS_TASK } from '../repos/repos.constants'; BullModule.registerQueue({ name: LIST_LOGS_TASK, }), + PipelineTasksModule, ], - providers: [PipelinesResolver, PipelinesService], + providers: [PipelinesResolver, PipelinesService, CommitLogsResolver], }) export class PipelinesModule {} diff --git a/src/pipelines/pipelines.resolver.ts b/src/pipelines/pipelines.resolver.ts index 9accb80..572e612 100644 --- a/src/pipelines/pipelines.resolver.ts +++ b/src/pipelines/pipelines.resolver.ts @@ -1,10 +1,9 @@ -import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql'; +import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'; import { CreatePipelineInput } from './dtos/create-pipeline.input'; 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 { @@ -42,16 +41,4 @@ export class PipelinesResolver { async deletePipeline(@Args('id', { type: () => String }) id: string) { return await this.service.remove(id); } - - @Subscription(() => LogList, { - resolve: (value) => { - return value; - }, - }) - async listLogsForPipeline(@Args('id', { type: () => String }) id: string) { - const job = await this.service.listLogsForPipeline(id); - return (async function* () { - yield await job.finished(); - })(); - } } diff --git a/src/repos/dtos/log-list.model.ts b/src/repos/dtos/log-list.model.ts index 7e612ac..a144e5e 100644 --- a/src/repos/dtos/log-list.model.ts +++ b/src/repos/dtos/log-list.model.ts @@ -1,5 +1,6 @@ import { ObjectType, Field } from '@nestjs/graphql'; import { LogResult, DefaultLogFields } from 'simple-git'; +import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity'; @ObjectType() export class LogFields { @@ -10,6 +11,7 @@ export class LogFields { body: string; author_name: string; author_email: string; + tasks: PipelineTask[]; } @ObjectType() diff --git a/src/webhooks/webhooks.module.ts b/src/webhooks/webhooks.module.ts index 3b7899c..254bc87 100644 --- a/src/webhooks/webhooks.module.ts +++ b/src/webhooks/webhooks.module.ts @@ -4,7 +4,6 @@ import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module'; import { GiteaWebhooksController } from './gitea-webhooks.controller'; import { WebhookLog } from './webhook-log.entity'; import { WebhooksService } from './webhooks.service'; -import { raw } from 'body-parser'; @Module({ imports: [TypeOrmModule.forFeature([WebhookLog]), PipelineTasksModule], @@ -12,9 +11,4 @@ import { raw } from 'body-parser'; providers: [WebhooksService], }) export class WebhooksModule { - // configure(consumer: MiddlewareConsumer) { - // consumer - // .apply(raw({ type: 'application/json' })) - // .forRoutes(GiteaWebhooksController); - // } }