Skip to content

Commit b6fb5a1

Browse files
author
Przybylski Krzysztof
committed
fix: old builds delete
1 parent 2bfe11f commit b6fb5a1

File tree

5 files changed

+69
-11
lines changed

5 files changed

+69
-11
lines changed

src/_data_/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const TEST_PROJECT: Project = {
55
id: '1',
66
name: 'Test Project',
77
buildsCounter: 2,
8-
maxBuildAllowed: 1,
8+
maxBuildAllowed: 3,
99
maxBranchLifetime: 30,
1010
mainBranchName: 'master',
1111
createdAt: new Date(),

src/builds/builds.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export class BuildsController {
7373
@Roles(Role.admin, Role.editor)
7474
async create(@Body() createBuildDto: CreateBuildDto): Promise<BuildDto> {
7575
const project = await this.projectService.findOne(createBuildDto.project);
76-
await this.buildsService.deleteOldBuilds(project.id, project.maxBuildAllowed);
76+
await this.buildsService.deleteOldBuilds(project);
7777
const build = await this.buildsService.findOrCreate({
7878
projectId: project.id,
7979
branchName: createBuildDto.branchName ?? project.mainBranchName,

src/builds/builds.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import { TestRunsModule } from '../test-runs/test-runs.module';
77
import { SharedModule } from '../shared/shared.module';
88
import { AuthModule } from '../auth/auth.module';
99
import { ProjectsModule } from '../projects/projects.module';
10+
import { StaticService } from '../static/static.service';
11+
import { StaticFactoryService } from '../static/static.factory';
1012

1113
@Module({
1214
imports: [SharedModule, UsersModule, forwardRef(() => TestRunsModule), AuthModule, forwardRef(() => ProjectsModule)],
13-
providers: [BuildsService, PrismaService],
15+
providers: [BuildsService, PrismaService, StaticFactoryService, StaticService],
1416
controllers: [BuildsController],
1517
exports: [BuildsService],
1618
})

src/builds/builds.service.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
22
import { PrismaService } from '../prisma/prisma.service';
3-
import { Build, Prisma, TestStatus } from '@prisma/client';
3+
import { Build, Prisma, TestStatus, Project } from '@prisma/client';
44
import { TestRunsService } from '../test-runs/test-runs.service';
55
import { EventsGateway } from '../shared/events/events.gateway';
66
import { BuildDto } from './dto/build.dto';
77
import { PaginatedBuildDto } from './dto/build-paginated.dto';
88
import { ModifyBuildDto } from './dto/build-modify.dto';
9+
import { StaticService } from '../static/static.service';
910

1011
@Injectable()
1112
export class BuildsService {
@@ -15,7 +16,8 @@ export class BuildsService {
1516
private prismaService: PrismaService,
1617
private eventsGateway: EventsGateway,
1718
@Inject(forwardRef(() => TestRunsService))
18-
private testRunsService: TestRunsService
19+
private testRunsService: TestRunsService,
20+
private staticService: StaticService
1921
) {}
2022

2123
async findOne(id: string): Promise<BuildDto> {
@@ -93,12 +95,38 @@ export class BuildsService {
9395
return build;
9496
}
9597

96-
async deleteOldBuilds(projectId: string, keepBuilds: number) {
97-
keepBuilds = keepBuilds < 2 ? keepBuilds : keepBuilds - 1;
98-
this.findMany(projectId, undefined, keepBuilds).then((buildList) => {
99-
buildList.data.forEach((eachBuild) => {
100-
this.remove(eachBuild.id);
101-
});
98+
async deleteOldBuilds(project: Project) {
99+
this.logger.log('Going to delete old builds');
100+
101+
const keepBuilds = project.maxBuildAllowed <= 1 ? 1 : project.maxBuildAllowed - 1;
102+
103+
const buildsToDelete = await this.prismaService.build.findMany({
104+
where: { projectId: { equals: project.id } },
105+
orderBy: { createdAt: 'desc' },
106+
skip: keepBuilds,
107+
});
108+
109+
const buildIds = buildsToDelete.map((build) => build.id);
110+
111+
const testRunsToDelete = await this.prismaService.testRun.findMany({ where: { buildId: { in: buildIds } } });
112+
113+
await this.prismaService.testRun.deleteMany({ where: { buildId: { in: buildIds } } });
114+
115+
testRunsToDelete.forEach((testRun) => {
116+
this.staticService.deleteImage(testRun.diffName);
117+
this.staticService.deleteImage(testRun.imageName);
118+
});
119+
120+
testRunsToDelete.forEach((testRun) => {
121+
this.logger.log(`TestRun deleted ${testRun.id}`);
122+
this.eventsGateway.testRunDeleted(testRun);
123+
});
124+
125+
await this.prismaService.build.deleteMany({ where: { id: { in: buildIds } } });
126+
127+
buildsToDelete.forEach((build) => {
128+
this.logger.log(`Build deleted ${build.id}`);
129+
this.eventsGateway.buildDeleted(new BuildDto({ ...build }));
102130
});
103131
}
104132

test/builds.e2e-spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,34 @@ describe('Builds (e2e)', () => {
5757
});
5858

5959
describe('POST /', () => {
60+
it('deletes old builds', async () => {
61+
const createBuild = async (count: number) => {
62+
const image_v1 = './test/image.png';
63+
64+
const createBuildDto: CreateBuildDto = {
65+
branchName: `branchName-${count}`,
66+
project: project.id,
67+
};
68+
await requestWithApiKey(app, 'post', '/builds', user.apiKey).send(createBuildDto).expect(201);
69+
await haveTestRunCreated(buildsService, testRunsService, project.id, `branchName-${count}`, image_v1);
70+
};
71+
72+
for (let i = 0; i < 6; i++) {
73+
await createBuild(i);
74+
}
75+
76+
const builds = await buildsService.findMany(project.id, 10, 0);
77+
78+
expect(builds.total).toBe(project.maxBuildAllowed);
79+
expect(builds.data).toEqual(
80+
expect.arrayContaining([
81+
expect.objectContaining({ branchName: 'branchName-5' }),
82+
expect.objectContaining({ branchName: 'branchName-4' }),
83+
expect.objectContaining({ branchName: 'branchName-3' }),
84+
])
85+
);
86+
});
87+
6088
it('201 by id', () => {
6189
const createBuildDto: CreateBuildDto = {
6290
branchName: 'branchName',

0 commit comments

Comments
 (0)