From 7e17de0f15c0deb70b7b9bf5bcbe2eeb8cc35a44 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 27 Jun 2021 10:33:32 +0800 Subject: [PATCH] =?UTF-8?q?feat(repos):=20=E8=8E=B7=E5=8F=96=20commit=20lo?= =?UTF-8?q?gs=20=E6=97=B6=EF=BC=8C=E5=85=81=E8=AE=B8=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E4=BF=A1=E6=81=AF=E7=BB=99=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E6=96=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exceptions/application.exception.ts | 10 ++++++--- src/repos/dtos/log-list.model.ts | 2 ++ src/repos/repos.service.ts | 22 +++++++++++-------- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/commons/exceptions/application.exception.ts b/src/commons/exceptions/application.exception.ts index d7f425f..1a01717 100644 --- a/src/commons/exceptions/application.exception.ts +++ b/src/commons/exceptions/application.exception.ts @@ -1,11 +1,11 @@ +import { pick } from 'ramda'; + export class ApplicationException extends Error { code: number; error: Error; constructor( - message: - | string - | { error?: Error; message?: string | object; code?: number }, + message: string | { error?: Error; message?: string | any; code?: number }, ) { if (message instanceof Object) { super(); @@ -18,4 +18,8 @@ export class ApplicationException extends Error { super((message as unknown) as any); } } + + toJSON() { + return pick(['code', 'message'], this); + } } diff --git a/src/repos/dtos/log-list.model.ts b/src/repos/dtos/log-list.model.ts index 58bca5a..614a297 100644 --- a/src/repos/dtos/log-list.model.ts +++ b/src/repos/dtos/log-list.model.ts @@ -1,10 +1,12 @@ import { ObjectType, Field } from '@nestjs/graphql'; +import { Type } from 'class-transformer'; import { LogResult, DefaultLogFields } from 'simple-git'; import { PipelineTask } from '../../pipeline-tasks/pipeline-task.entity'; @ObjectType() export class Commit { hash: string; + @Type(() => Date) date: Date; message: string; refs: string; diff --git a/src/repos/repos.service.ts b/src/repos/repos.service.ts index 66376dd..83a6919 100644 --- a/src/repos/repos.service.ts +++ b/src/repos/repos.service.ts @@ -26,6 +26,7 @@ import { getInstanceName, getSelfInstanceRouteKey, } from '../commons/utils/rabbit-mq'; +import { ApplicationException } from '../commons/exceptions/application.exception'; const DEFAULT_REMOTE_NAME = 'origin'; const INFO_PATH = '@info'; @@ -129,7 +130,7 @@ export class ReposService { autoDelete: true, }, }) - async listCommits(pipeline: Pipeline): Promise { + async listCommits(pipeline: Pipeline): Promise<[Error, Commit[]?]> { const git = await this.getGit(pipeline.project, undefined, { fetch: false, }); @@ -140,20 +141,23 @@ export class ReposService { `remotes/origin/${pipeline.branch}`, '--', ]); - return data.all.map( - (it) => - ({ - ...it, - date: new Date(it.date), - } as Commit), - ); + return [ + null, + data.all.map( + (it) => + ({ + ...it, + date: new Date(it.date), + } as Commit), + ), + ]; } catch (error) { this.logger.error( { error, pipeline }, '[listCommits] %s', error?.message, ); - return new Nack(); + return [new ApplicationException(error)]; } }