feat(repos): 添加订阅commit log 接口。

This commit is contained in:
Ivan 2021-03-08 11:26:12 +08:00
parent ba0ba46a35
commit 22d3dc299c
6 changed files with 60 additions and 29 deletions

View File

@ -37,6 +37,7 @@ import { RedisModule } from 'nestjs-redis';
debug: configService.get<string>('env') !== 'prod', debug: configService.get<string>('env') !== 'prod',
playground: true, playground: true,
autoSchemaFile: true, autoSchemaFile: true,
installSubscriptionHandlers: true,
}), }),
inject: [ConfigService], inject: [ConfigService],
}), }),

View File

@ -0,0 +1,24 @@
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';
@Processor(LIST_LOGS_TASK)
export class ListLogsConsumer {
constructor(
private readonly service: ReposService,
@Inject(LIST_LOGS_PUB_SUB)
private readonly pubSub: PubSub,
) {}
@Process()
async listLogs(job: Job<ListLogsOption>) {
const logs = await this.service.listLogs(job.data);
this.pubSub.publish(LIST_LOGS_DONE, logs);
}
}

View File

@ -1 +1,3 @@
export const LIST_LOGS_TASK = 'LIST_LOGS_TASK'; export const LIST_LOGS_TASK = 'LIST_LOGS_TASK';
export const LIST_LOGS_PUB_SUB = 'LIST_LOGS_PUB_SUB';
export const LIST_LOGS_DONE = 'LIST_LOGS_DONE';

View File

@ -5,10 +5,27 @@ import { ReposResolver } from './repos.resolver';
import { ReposService } from './repos.service'; import { ReposService } from './repos.service';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { ProjectsModule } from '../projects/projects.module'; import { ProjectsModule } from '../projects/projects.module';
import { BullModule } from '@nestjs/bull';
import { LIST_LOGS_TASK, LIST_LOGS_PUB_SUB } from './repos.constants';
import { PubSub } from 'graphql-subscriptions';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Project]), ConfigModule, ProjectsModule], imports: [
providers: [ReposResolver, ReposService], TypeOrmModule.forFeature([Project]),
ConfigModule,
ProjectsModule,
BullModule.registerQueue({
name: LIST_LOGS_TASK,
}),
],
providers: [
ReposResolver,
ReposService,
{
provide: LIST_LOGS_PUB_SUB,
useValue: new PubSub(),
},
],
exports: [ReposService], exports: [ReposService],
}) })
export class ReposModule {} export class ReposModule {}

View File

@ -1,26 +1,17 @@
import { Args, Query, Resolver } from '@nestjs/graphql'; import { LIST_LOGS_DONE, LIST_LOGS_PUB_SUB } from './repos.constants';
import { ListLogsArgs } from './dtos/list-logs.args'; import { Resolver, Subscription } from '@nestjs/graphql';
import { ReposService } from './repos.service';
import { LogList } from './dtos/log-list.model'; import { LogList } from './dtos/log-list.model';
import { ListBranchesArgs } from './dtos/list-branches.args'; import { Inject } from '@nestjs/common';
import { BranchList } from './dtos/branch-list.model'; import { PubSub } from 'apollo-server-express';
@Resolver() @Resolver()
export class ReposResolver { export class ReposResolver {
constructor(private readonly service: ReposService) {} constructor(
@Query(() => LogList) @Inject(LIST_LOGS_PUB_SUB)
async listLogs(@Args('listLogsArgs') dto: ListLogsArgs) { private readonly pubSub: PubSub,
return await this.service.listLogs(dto); ) {}
} @Subscription(() => LogList)
@Query(() => BranchList) listLogsDone() {
async listBranches( return this.pubSub.asyncIterator(LIST_LOGS_DONE);
@Args('listBranchesArgs') dto: ListBranchesArgs,
): Promise<BranchList> {
return await this.service.listBranches(dto).then((data) => {
return {
...data,
branches: Object.values(data.branches),
};
});
} }
} }

View File

@ -1,3 +1,4 @@
import { ListLogsOption } from './models/list-logs.options';
import { Pipeline } from './../pipelines/pipeline.entity'; import { Pipeline } from './../pipelines/pipeline.entity';
import { PipelineTask } from './../pipeline-tasks/pipeline-task.entity'; import { PipelineTask } from './../pipeline-tasks/pipeline-task.entity';
import { Injectable, NotFoundException } from '@nestjs/common'; import { Injectable, NotFoundException } from '@nestjs/common';
@ -47,14 +48,9 @@ export class ReposService {
return git; return git;
} }
async listLogs(dto: ListLogsArgs) { async listLogs({ project, branch }: ListLogsOption) {
const project = await this.projectRepository.findOneOrFail({
id: dto.projectId,
});
const git = await this.getGit(project); const git = await this.getGit(project);
return await git.log( return await git.log(branch ? ['--branches', branch, '--'] : ['--all']);
dto.branch ? ['--branches', dto.branch, '--'] : ['--all'],
);
} }
async listBranches(dto: ListBranchesArgs) { async listBranches(dto: ListBranchesArgs) {