Merge branch 'master' of ssh://git.ivanli.cc:7018/Fennec/fennec-be

This commit is contained in:
Ivan Li 2021-03-28 19:38:18 +08:00
commit 22be1ffb33
7 changed files with 68 additions and 21 deletions

View File

@ -69,6 +69,10 @@ export class PipelineTasksService {
return await this.repository.find({ pipelineId }); return await this.repository.find({ pipelineId });
} }
async listTasksByCommitHash(hash: string) {
return await this.repository.find({ commit: hash });
}
async doNextTask(pipeline: Pipeline) { async doNextTask(pipeline: Pipeline) {
const [lckKey, tasksKey] = this.getRedisTokens(pipeline); const [lckKey, tasksKey] = this.getRedisTokens(pipeline);
const redis = this.redis.getClient(); const redis = this.redis.getClient();

View File

@ -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>(CommitLogsResolver);
});
it('should be defined', () => {
expect(resolver).toBeDefined();
});
});

View File

@ -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);
}
}

View File

@ -5,6 +5,8 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Pipeline } from './pipeline.entity'; import { Pipeline } from './pipeline.entity';
import { BullModule } from '@nestjs/bull'; import { BullModule } from '@nestjs/bull';
import { LIST_LOGS_TASK } from '../repos/repos.constants'; import { LIST_LOGS_TASK } from '../repos/repos.constants';
import { CommitLogsResolver } from './commit-logs.resolver';
import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
@Module({ @Module({
imports: [ imports: [
@ -12,7 +14,8 @@ import { LIST_LOGS_TASK } from '../repos/repos.constants';
BullModule.registerQueue({ BullModule.registerQueue({
name: LIST_LOGS_TASK, name: LIST_LOGS_TASK,
}), }),
PipelineTasksModule,
], ],
providers: [PipelinesResolver, PipelinesService], providers: [PipelinesResolver, PipelinesService, CommitLogsResolver],
}) })
export class PipelinesModule {} export class PipelinesModule {}

View File

@ -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 { CreatePipelineInput } from './dtos/create-pipeline.input';
import { UpdatePipelineInput } from './dtos/update-pipeline.input'; 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 {
@ -42,16 +41,4 @@ 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);
} }
@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();
})();
}
} }

View File

@ -1,5 +1,6 @@
import { ObjectType, Field } from '@nestjs/graphql'; import { ObjectType, Field } from '@nestjs/graphql';
import { LogResult, DefaultLogFields } from 'simple-git'; import { LogResult, DefaultLogFields } from 'simple-git';
import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity';
@ObjectType() @ObjectType()
export class LogFields { export class LogFields {
@ -10,6 +11,7 @@ export class LogFields {
body: string; body: string;
author_name: string; author_name: string;
author_email: string; author_email: string;
tasks: PipelineTask[];
} }
@ObjectType() @ObjectType()

View File

@ -4,7 +4,6 @@ import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
import { GiteaWebhooksController } from './gitea-webhooks.controller'; import { GiteaWebhooksController } from './gitea-webhooks.controller';
import { WebhookLog } from './webhook-log.entity'; import { WebhookLog } from './webhook-log.entity';
import { WebhooksService } from './webhooks.service'; import { WebhooksService } from './webhooks.service';
import { raw } from 'body-parser';
@Module({ @Module({
imports: [TypeOrmModule.forFeature([WebhookLog]), PipelineTasksModule], imports: [TypeOrmModule.forFeature([WebhookLog]), PipelineTasksModule],
@ -12,9 +11,4 @@ import { raw } from 'body-parser';
providers: [WebhooksService], providers: [WebhooksService],
}) })
export class WebhooksModule { export class WebhooksModule {
// configure(consumer: MiddlewareConsumer) {
// consumer
// .apply(raw({ type: 'application/json' }))
// .forRoutes(GiteaWebhooksController);
// }
} }