feat(pipelines): 添加查询 commit logs 接口。

This commit is contained in:
Ivan Li 2021-03-07 22:53:16 +08:00
parent bba7963949
commit f00f75673b
6 changed files with 82 additions and 2 deletions

View File

@ -3,9 +3,16 @@ import { PipelinesResolver } from './pipelines.resolver';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
import { TypeOrmModule } from '@nestjs/typeorm'; import { TypeOrmModule } from '@nestjs/typeorm';
import { Pipeline } from './pipeline.entity'; import { Pipeline } from './pipeline.entity';
import { BullModule } from '@nestjs/bull';
import { LIST_LOGS_TASK } from '../repos/repos.constants';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([Pipeline])], imports: [
TypeOrmModule.forFeature([Pipeline]),
BullModule.registerQueue({
name: LIST_LOGS_TASK,
}),
],
providers: [PipelinesResolver, PipelinesService], providers: [PipelinesResolver, PipelinesService],
}) })
export class PipelinesModule {} export class PipelinesModule {}

View File

@ -4,6 +4,7 @@ import { UpdatePipelineInput } from './dtos/update-pipeline.input';
import { Pipeline } from './pipeline.entity'; import { Pipeline } from './pipeline.entity';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
import { ListPipelineArgs } from './dtos/list-pipelines.args'; import { ListPipelineArgs } from './dtos/list-pipelines.args';
import { LogList } from '../repos/dtos/log-list.model';
@Resolver() @Resolver()
export class PipelinesResolver { export class PipelinesResolver {
@ -41,4 +42,9 @@ export class PipelinesResolver {
async deletePipeline(@Args('id', { type: () => String }) id: string) { async deletePipeline(@Args('id', { type: () => String }) id: string) {
return await this.service.remove(id); return await this.service.remove(id);
} }
@Query(() => String)
async listLogsForPipeline(@Args('id', { type: () => String }) id: string) {
return await this.service.listLogsForPipeline(id);
}
} }

View File

@ -2,25 +2,68 @@ import { Test, TestingModule } from '@nestjs/testing';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
import { Pipeline } from './pipeline.entity'; import { Pipeline } from './pipeline.entity';
import { getRepositoryToken } from '@nestjs/typeorm'; 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', () => { describe('PipelinesService', () => {
let service: PipelinesService; let service: PipelinesService;
let repository: Repository<Pipeline>;
let pipeline: Pipeline;
let queue: Queue<ListLogsOption>;
beforeEach(async () => { 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({ const module: TestingModule = await Test.createTestingModule({
providers: [ providers: [
PipelinesService, PipelinesService,
{ {
provide: getRepositoryToken(Pipeline), 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(); }).compile();
service = module.get<PipelinesService>(PipelinesService); service = module.get<PipelinesService>(PipelinesService);
repository = module.get(getRepositoryToken(Pipeline));
queue = module.get(getQueueToken(LIST_LOGS_TASK));
}); });
it('should be defined', () => { it('should be defined', () => {
expect(service).toBeDefined(); 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,
});
});
});
}); });

View File

@ -6,6 +6,10 @@ import { BaseDbService } from '../commons/services/base-db.service';
import { CreatePipelineInput } from './dtos/create-pipeline.input'; import { CreatePipelineInput } from './dtos/create-pipeline.input';
import { UpdatePipelineInput } from './dtos/update-pipeline.input'; import { UpdatePipelineInput } from './dtos/update-pipeline.input';
import { ListPipelineArgs } from './dtos/list-pipelines.args'; 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() @Injectable()
export class PipelinesService extends BaseDbService<Pipeline> { export class PipelinesService extends BaseDbService<Pipeline> {
@ -15,6 +19,8 @@ export class PipelinesService extends BaseDbService<Pipeline> {
constructor( constructor(
@InjectRepository(Pipeline) @InjectRepository(Pipeline)
readonly repository: Repository<Pipeline>, readonly repository: Repository<Pipeline>,
@InjectQueue(LIST_LOGS_TASK)
private readonly listLogsQueue: Queue<ListLogsOption>,
) { ) {
super(repository); super(repository);
} }
@ -36,4 +42,16 @@ export class PipelinesService extends BaseDbService<Pipeline> {
async remove(id: string) { async remove(id: string) {
return (await this.repository.softDelete({ id })).affected; 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;
}
} }

View File

@ -0,0 +1,5 @@
import { Project } from '../../projects/project.entity';
export interface ListLogsOption {
project: Project;
branch: string;
}

View File

@ -0,0 +1 @@
export const LIST_LOGS_TASK = 'LIST_LOGS_TASK';