feat_the_progress_of_tasks #5

Merged
Ivan merged 20 commits from feat_the_progress_of_tasks into master 2021-06-27 19:57:14 +08:00
6 changed files with 55 additions and 7 deletions
Showing only changes of commit 4041a6fd2a - Show all commits

View File

@ -1,3 +1,4 @@
import { Query } from '@nestjs/graphql';
import {
Args,
Parent,
@ -6,14 +7,16 @@ import {
Subscription,
} from '@nestjs/graphql';
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';
@Resolver(() => LogFields)
@Resolver(() => Commit)
export class CommitLogsResolver {
constructor(
private readonly service: PipelinesService,
private readonly taskServices: PipelineTasksService,
private readonly reposService: ReposService,
) {}
@Subscription(() => LogList, {
resolve: (value) => {
@ -30,4 +33,14 @@ export class CommitLogsResolver {
async tasks(@Parent() commit: LogFields) {
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 { CommitLogsResolver } from './commit-logs.resolver';
import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
import { ReposModule } from '../repos/repos.module';
@Module({
imports: [
@ -15,6 +16,7 @@ import { PipelineTasksModule } from '../pipeline-tasks/pipeline-tasks.module';
name: LIST_LOGS_TASK,
}),
PipelineTasksModule,
ReposModule,
],
providers: [PipelinesResolver, PipelinesService, CommitLogsResolver],
})

View File

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

View File

@ -6,8 +6,7 @@ import { ReposService } from './repos.service';
import { ConfigModule } from '@nestjs/config';
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';
import { LIST_LOGS_TASK } from './repos.constants';
import { ListLogsConsumer } from './list-logs.consumer';
@Module({

View File

@ -10,13 +10,23 @@ import { gitP } from 'simple-git';
import { Repository } from 'typeorm';
import { Project } from '../projects/project.entity';
import { ListBranchesArgs } from './dtos/list-branches.args';
import { ListLogsArgs } from './dtos/list-logs.args';
import { ConfigService } from '@nestjs/config';
import { Commit } from './dtos/log-list.model';
const DEFAULT_REMOTE_NAME = 'origin';
const INFO_PATH = '@info';
@Injectable()
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(
@InjectRepository(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) {
workspaceRoot = this.getWorkspaceRoot(project);
}
@ -44,7 +58,9 @@ export class ReposService {
await git.init();
await git.addRemote(DEFAULT_REMOTE_NAME, project.sshUrl);
}
await git.fetch();
if (fetch) {
await git.fetch();
}
return git;
}