79 lines
2.5 KiB
TypeScript
79 lines
2.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Pipeline } from './pipeline.entity';
|
|
import { Repository } from 'typeorm';
|
|
import { BaseDbService } from '../commons/services/base-db.service';
|
|
import { CreatePipelineInput } from './dtos/create-pipeline.input';
|
|
import { UpdatePipelineInput } from './dtos/update-pipeline.input';
|
|
import { ListPipelineArgs } from './dtos/list-pipelines.args';
|
|
import {
|
|
EXCHANGE_REPO,
|
|
ROUTE_FETCH,
|
|
ROUTE_LIST_COMMITS,
|
|
} from '../repos/repos.constants';
|
|
import { AmqpConnection } from '@golevelup/nestjs-rabbitmq';
|
|
import { Commit } from '../repos/dtos/log-list.model';
|
|
import { getAppInstanceRouteKey } from '../commons/utils/rabbit-mq';
|
|
import { ApplicationException } from '../commons/exceptions/application.exception';
|
|
import { plainToClass } from 'class-transformer';
|
|
|
|
@Injectable()
|
|
export class PipelinesService extends BaseDbService<Pipeline> {
|
|
readonly uniqueFields: Array<Array<keyof Pipeline>> = [['projectId', 'name']];
|
|
constructor(
|
|
@InjectRepository(Pipeline)
|
|
readonly repository: Repository<Pipeline>,
|
|
private readonly amqpConnection: AmqpConnection,
|
|
) {
|
|
super(repository);
|
|
}
|
|
async list(dto: ListPipelineArgs) {
|
|
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));
|
|
}
|
|
|
|
async update(dto: UpdatePipelineInput) {
|
|
const old = await this.findOne(dto.id);
|
|
await this.isDuplicateEntityForUpdate(old, dto);
|
|
return await this.repository.save(this.repository.merge(old, dto));
|
|
}
|
|
|
|
async remove(id: string) {
|
|
return (await this.repository.softDelete({ id })).affected;
|
|
}
|
|
async syncCommits(pipeline: Pipeline, appInstance?: string) {
|
|
return await this.amqpConnection.request<string | null>({
|
|
exchange: EXCHANGE_REPO,
|
|
routingKey: getAppInstanceRouteKey(ROUTE_FETCH, appInstance),
|
|
payload: pipeline,
|
|
timeout: 30_000,
|
|
});
|
|
}
|
|
async listCommits(pipeline: Pipeline) {
|
|
return await this.amqpConnection
|
|
.request<[Error, Commit[]]>({
|
|
exchange: EXCHANGE_REPO,
|
|
routingKey: ROUTE_LIST_COMMITS,
|
|
payload: pipeline,
|
|
timeout: 30_000,
|
|
})
|
|
.then(([error, list]) => {
|
|
if (error) {
|
|
throw new ApplicationException(error);
|
|
}
|
|
return plainToClass(Commit, list);
|
|
});
|
|
}
|
|
}
|