feat(pipelines): add commits query api.

This commit is contained in:
Ivan Li 2021-05-09 16:54:59 +08:00
parent 5a8b699e2f
commit 4041a6fd2a
6 changed files with 55 additions and 7 deletions

View File

@ -1,3 +1,4 @@
import { Query } from '@nestjs/graphql';
import { import {
Args, Args,
Parent, Parent,
@ -6,14 +7,16 @@ import {
Subscription, Subscription,
} from '@nestjs/graphql'; } from '@nestjs/graphql';
import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service'; import { PipelineTasksService } from '../pipeline-tasks/pipeline-tasks.service';
import { LogFields, LogList } from '../repos/dtos/log-list.model'; import { Commit, LogFields, LogList } from '../repos/dtos/log-list.model';
import { ReposService } from '../repos/repos.service';
import { PipelinesService } from './pipelines.service'; import { PipelinesService } from './pipelines.service';
@Resolver(() => LogFields) @Resolver(() => Commit)
export class CommitLogsResolver { export class CommitLogsResolver {
constructor( constructor(
private readonly service: PipelinesService, private readonly service: PipelinesService,
private readonly taskServices: PipelineTasksService, private readonly taskServices: PipelineTasksService,
private readonly reposService: ReposService,
) {} ) {}
@Subscription(() => LogList, { @Subscription(() => LogList, {
resolve: (value) => { resolve: (value) => {
@ -30,4 +33,14 @@ export class CommitLogsResolver {
async tasks(@Parent() commit: LogFields) { async tasks(@Parent() commit: LogFields) {
return await this.taskServices.listTasksByCommitHash(commit.hash); return await this.taskServices.listTasksByCommitHash(commit.hash);
} }
@Query(() => [Commit])
async commits(@Args('pipelineId', { type: () => String }) id: string) {
const pipeline = await this.service.findOneWithProject(id);
const data = await this.reposService.listCommits(
pipeline.project,
pipeline.branch,
);
return data;
}
} }

View File

@ -7,6 +7,7 @@ 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 { CommitLogsResolver } from './commit-logs.resolver';
import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module'; import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
import { ReposModule } from '../repos/repos.module';
@Module({ @Module({
imports: [ imports: [
@ -15,6 +16,7 @@ import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
name: LIST_LOGS_TASK, name: LIST_LOGS_TASK,
}), }),
PipelineTasksModule, PipelineTasksModule,
ReposModule,
], ],
providers: [PipelinesResolver, PipelinesService, CommitLogsResolver], providers: [PipelinesResolver, PipelinesService, CommitLogsResolver],
}) })

View File

@ -26,6 +26,13 @@ export class PipelinesService extends BaseDbService<Pipeline> {
return this.repository.find(dto); return this.repository.find(dto);
} }
async findOneWithProject(id: string) {
return await this.repository.findOne({
where: { id },
relations: ['project'],
});
}
async create(dto: CreatePipelineInput) { async create(dto: CreatePipelineInput) {
await this.isDuplicateEntity(dto); await this.isDuplicateEntity(dto);
return await this.repository.save(this.repository.create(dto)); return await this.repository.save(this.repository.create(dto));

View File

@ -2,6 +2,17 @@ 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'; import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity';
@ObjectType()
export class Commit {
hash: string;
date: Date;
message: string;
refs: string;
body: string;
author_name: string;
author_email: string;
tasks: PipelineTask[];
}
@ObjectType() @ObjectType()
export class LogFields { export class LogFields {
hash: string; hash: string;

View File

@ -6,8 +6,7 @@ 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 { BullModule } from '@nestjs/bull';
import { LIST_LOGS_TASK, LIST_LOGS_PUB_SUB } from './repos.constants'; import { LIST_LOGS_TASK } from './repos.constants';
import { PubSub } from 'graphql-subscriptions';
import { ListLogsConsumer } from './list-logs.consumer'; import { ListLogsConsumer } from './list-logs.consumer';
@Module({ @Module({

View File

@ -10,13 +10,23 @@ import { gitP } from 'simple-git';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
import { Project } from '../projects/project.entity'; import { Project } from '../projects/project.entity';
import { ListBranchesArgs } from './dtos/list-branches.args'; import { ListBranchesArgs } from './dtos/list-branches.args';
import { ListLogsArgs } from './dtos/list-logs.args';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { Commit } from './dtos/log-list.model';
const DEFAULT_REMOTE_NAME = 'origin'; const DEFAULT_REMOTE_NAME = 'origin';
const INFO_PATH = '@info'; const INFO_PATH = '@info';
@Injectable() @Injectable()
export class ReposService { export class ReposService {
async listCommits(project: Project, branch?: string) {
const git = await this.getGit(project, undefined, { fetch: false });
const data = await git.log(
branch ? ['--branches', `remotes/origin/${branch}`, '--'] : ['--all'],
);
return data.all.map((it) => ({
...it,
date: new Date(it.date),
}));
}
constructor( constructor(
@InjectRepository(Project) @InjectRepository(Project)
private readonly projectRepository: Repository<Project>, private readonly projectRepository: Repository<Project>,
@ -31,7 +41,11 @@ export class ReposService {
); );
} }
async getGit(project: Project, workspaceRoot?: string) { async getGit(
project: Project,
workspaceRoot?: string,
{ fetch = true } = {},
) {
if (!workspaceRoot) { if (!workspaceRoot) {
workspaceRoot = this.getWorkspaceRoot(project); workspaceRoot = this.getWorkspaceRoot(project);
} }
@ -44,7 +58,9 @@ export class ReposService {
await git.init(); await git.init();
await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl); await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl);
} }
await git.fetch(); if (fetch) {
await git.fetch();
}
return git; return git;
} }