Skip to content

Commit faa48af

Browse files
authored
Issue#197 : Add possibility to update build with ci build id. (#97)
1 parent 58ea1a3 commit faa48af

File tree

7 files changed

+30
-30
lines changed

7 files changed

+30
-30
lines changed

src/builds/builds.controller.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import { Build } from '@prisma/client';
2121
import { BuildDto } from './dto/build.dto';
2222
import { MixedGuard } from '../auth/guards/mixed.guard';
2323
import { PaginatedBuildDto } from './dto/build-paginated.dto';
24+
import { ModifyBuildDto } from './dto/build-modify.dto';
2425

2526
@Controller('builds')
2627
@ApiTags('builds')
2728
export class BuildsController {
28-
constructor(private buildsService: BuildsService) {}
29+
constructor(private buildsService: BuildsService) { }
2930

3031
@Get()
3132
@ApiOkResponse({ type: PaginatedBuildDto })
@@ -67,8 +68,12 @@ export class BuildsController {
6768
@ApiSecurity('api_key')
6869
@ApiBearerAuth()
6970
@UseGuards(MixedGuard)
70-
stop(@Param('id', new ParseUUIDPipe()) id: string): Promise<BuildDto> {
71-
return this.buildsService.stop(id);
71+
update(@Param('id', new ParseUUIDPipe()) id: string, @Body() modifyBuildDto?: ModifyBuildDto): Promise<BuildDto> {
72+
//In future, no or empty body will do nothing as this check will be removed. It will expect a proper body to perform any patch.
73+
if (modifyBuildDto === null || Object.keys(modifyBuildDto).length === 0) {
74+
modifyBuildDto.isRunning = false;
75+
}
76+
return this.buildsService.update(id, modifyBuildDto);
7277
}
7378

7479
@Patch(':id/approve')

src/builds/builds.service.spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ const initService = async ({
2323
testRunApproveMock = jest.fn(),
2424
eventsBuildUpdatedMock = jest.fn(),
2525
eventsBuildCreatedMock = jest.fn(),
26-
eventsBuildFinishedMock = jest.fn(),
2726
projectFindOneMock = jest.fn(),
2827
projectUpdateMock = jest.fn(),
2928
}) => {
@@ -58,8 +57,7 @@ const initService = async ({
5857
provide: EventsGateway,
5958
useValue: {
6059
buildUpdated: eventsBuildUpdatedMock,
61-
buildCreated: eventsBuildCreatedMock,
62-
buildFinished: eventsBuildFinishedMock,
60+
buildCreated: eventsBuildCreatedMock
6361
},
6462
},
6563
{
@@ -284,22 +282,20 @@ describe('BuildsService', () => {
284282
it('should stop', async () => {
285283
const id = 'some id';
286284
const buildUpdateMock = jest.fn();
287-
const eventsBuildFinishedMock = jest.fn();
285+
const eventsBuildUpdatedMock = jest.fn();
288286
mocked(BuildDto).mockReturnValueOnce(buildDto);
289-
service = await initService({ buildUpdateMock, eventsBuildFinishedMock });
287+
service = await initService({ buildUpdateMock, eventsBuildUpdatedMock });
290288

291-
const result = await service.stop(id);
289+
const result = await service.update(id, { "isRunning": false });
292290

293291
expect(buildUpdateMock).toHaveBeenCalledWith({
294292
where: { id },
295293
include: {
296294
testRuns: true,
297295
},
298-
data: {
299-
isRunning: false,
300-
},
296+
data: { "isRunning": false }
301297
});
302-
expect(eventsBuildFinishedMock).toHaveBeenCalledWith(buildDto);
298+
expect(eventsBuildUpdatedMock).toHaveBeenCalledWith(buildDto);
303299
expect(result).toBe(buildDto);
304300
});
305301

src/builds/builds.service.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { EventsGateway } from '../shared/events/events.gateway';
77
import { BuildDto } from './dto/build.dto';
88
import { ProjectsService } from '../projects/projects.service';
99
import { PaginatedBuildDto } from './dto/build-paginated.dto';
10+
import { ModifyBuildDto } from './dto/build-modify.dto';
1011

1112
@Injectable()
1213
export class BuildsService {
@@ -17,7 +18,7 @@ export class BuildsService {
1718
private testRunsService: TestRunsService,
1819
@Inject(forwardRef(() => ProjectsService))
1920
private projectService: ProjectsService
20-
) {}
21+
) { }
2122

2223
async findOne(id: string): Promise<BuildDto> {
2324
return this.prismaService.build
@@ -99,18 +100,16 @@ export class BuildsService {
99100
return new BuildDto(build);
100101
}
101102

102-
async stop(id: string): Promise<BuildDto> {
103+
async update(id: string, modifyBuildDto: ModifyBuildDto): Promise<BuildDto> {
103104
const build = await this.prismaService.build.update({
104105
where: { id },
105106
include: {
106107
testRuns: true,
107108
},
108-
data: {
109-
isRunning: false,
110-
},
109+
data: modifyBuildDto
111110
});
112111
const buildDto = new BuildDto(build);
113-
this.eventsGateway.buildFinished(buildDto);
112+
this.eventsGateway.buildUpdated(buildDto);
114113
return buildDto;
115114
}
116115

src/builds/dto/build-modify.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface ModifyBuildDto {
2+
ciBuildId?: string;
3+
isRunning?: boolean;
4+
}

src/shared/events/events.gateway.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ export class EventsGateway {
1212
this.server.emit('build_created', build);
1313
}
1414

15-
buildFinished(build: BuildDto): void {
16-
this.server.emit('build_finished', build);
17-
}
18-
1915
buildUpdated(build: BuildDto): void {
2016
this.server.emit('build_updated', build);
2117
}

src/test-variations/test-variations.service.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const initModule = async ({
2222
baselineDeleteMock = jest.fn(),
2323
projectFindUniqueMock = jest.fn(),
2424
buildCreateMock = jest.fn(),
25-
buildStopMock = jest.fn(),
25+
buildUpdateMock = jest.fn(),
2626
testRunCreateMock = jest.fn(),
2727
testRunFindMany = jest.fn(),
2828
testRunDeleteMock = jest.fn(),
@@ -41,7 +41,7 @@ const initModule = async ({
4141
provide: BuildsService,
4242
useValue: {
4343
create: buildCreateMock,
44-
stop: buildStopMock,
44+
update: buildUpdateMock,
4545
},
4646
},
4747
{
@@ -417,11 +417,11 @@ describe('TestVariationsService', () => {
417417
.mockResolvedValueOnce(testVariationMainBranch)
418418
.mockResolvedValueOnce(testVariationMainBranch);
419419
const testRunCreateMock = jest.fn();
420-
const buildStopMock = jest.fn();
420+
const buildUpdateMock = jest.fn();
421421
const service = await initModule({
422422
projectFindUniqueMock,
423423
buildCreateMock,
424-
buildStopMock,
424+
buildUpdateMock,
425425
testRunCreateMock,
426426
variationFindManyMock,
427427
getImageMock,
@@ -466,7 +466,7 @@ describe('TestVariationsService', () => {
466466
ignoreAreas: JSON.parse(testVariationSecond.ignoreAreas),
467467
});
468468
expect(testRunCreateMock).toHaveBeenCalledTimes(2);
469-
expect(buildStopMock).toHaveBeenCalledWith(build.id);
469+
expect(buildUpdateMock).toHaveBeenCalledWith(build.id, { "isRunning": false });
470470
});
471471

472472
it('delete', async () => {

src/test-variations/test-variations.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class TestVariationsService {
2121
private testRunsService: TestRunsService,
2222
@Inject(forwardRef(() => BuildsService))
2323
private buildsService: BuildsService
24-
) {}
24+
) { }
2525

2626
async getDetails(id: string): Promise<TestVariation & { baselines: Baseline[] }> {
2727
return this.prismaService.testVariation.findUnique({
@@ -138,7 +138,7 @@ export class TestVariationsService {
138138
});
139139

140140
// stop build
141-
return this.buildsService.stop(build.id);
141+
return this.buildsService.update(build.id, { "isRunning": false });
142142
}
143143

144144
async delete(id: string): Promise<TestVariation> {

0 commit comments

Comments
 (0)