77 lines
2.4 KiB
TypeScript
77 lines
2.4 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';
|
|
|
|
@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) {
|
|
await this.isDuplicateEntityForUpdate(dto.id, dto);
|
|
const old = await this.findOne(dto.id);
|
|
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<Commit[]>({
|
|
exchange: EXCHANGE_REPO,
|
|
routingKey: ROUTE_LIST_COMMITS,
|
|
payload: pipeline,
|
|
timeout: 10_000,
|
|
})
|
|
.then((list) =>
|
|
list.map((item) => {
|
|
item.date = new Date(item.date);
|
|
return item;
|
|
}),
|
|
);
|
|
}
|
|
}
|