Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
403 changes: 398 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"@nestjs/jwt": "^7.0.0",
"@nestjs/passport": "^7.0.0",
"@nestjs/platform-express": "^7.0.0",
"@nestjs/platform-socket.io": "^7.1.3",
"@nestjs/swagger": "^4.5.1",
"@nestjs/websockets": "^7.1.3",
"@prisma/client": "^2.0.0-beta.6",
"bcryptjs": "^2.4.3",
"class-transformer": "^0.2.3",
Expand Down Expand Up @@ -59,6 +61,7 @@
"@types/passport-local": "^1.0.33",
"@types/pixelmatch": "^5.1.0",
"@types/pngjs": "^3.4.2",
"@types/socket.io": "^2.1.8",
"@types/supertest": "^2.0.8",
"@types/uuid-apikey": "^1.4.0",
"@typescript-eslint/eslint-plugin": "^2.23.0",
Expand Down
23 changes: 8 additions & 15 deletions src/builds/builds.controller.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
import {
Controller,
Get,
UseGuards,
Post,
Body,
Param,
ParseUUIDPipe,
Delete,
Query,
} from '@nestjs/common';
import { Controller, Get, UseGuards, Post, Body, Param, ParseUUIDPipe, Delete, Query } from '@nestjs/common';
import { BuildsService } from './builds.service';
import { JwtAuthGuard } from '../auth/guards/auth.guard';
import { ApiBearerAuth, ApiTags, ApiParam, ApiSecurity, ApiQuery } from '@nestjs/swagger';
import { ApiBearerAuth, ApiTags, ApiParam, ApiSecurity, ApiQuery, ApiResponse } from '@nestjs/swagger';
import { CreateBuildDto } from './dto/build-create.dto';
import { ApiGuard } from '../auth/guards/api.guard';
import { Build } from '@prisma/client';
import { BuildDto } from './dto/build.dto';

@Controller('builds')
@ApiTags('builds')
export class BuildsController {
constructor(private buildsService: BuildsService) { }
constructor(private buildsService: BuildsService) {}

@Get()
@ApiQuery({ name: 'projectId', required: true })
@ApiResponse({ type: [BuildDto] })
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
get(@Query('projectId', new ParseUUIDPipe()) projectId: string): Promise<Build[]> {
get(@Query('projectId', new ParseUUIDPipe()) projectId: string): Promise<BuildDto[]> {
return this.buildsService.findMany(projectId);
}

Expand All @@ -38,9 +30,10 @@ export class BuildsController {
}

@Post()
@ApiResponse({ type: BuildDto })
@ApiSecurity('api_key')
@UseGuards(ApiGuard)
create(@Body() createBuildDto: CreateBuildDto): Promise<Build> {
create(@Body() createBuildDto: CreateBuildDto): Promise<BuildDto> {
return this.buildsService.create(createBuildDto);
}
}
3 changes: 2 additions & 1 deletion src/builds/builds.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { BuildsController } from './builds.controller';
import { UsersModule } from '../users/users.module';
import { PrismaService } from '../prisma/prisma.service';
import { TestRunsModule } from '../test-runs/test-runs.module';
import { EventsGateway } from '../events/events.gateway';

@Module({
imports: [UsersModule, TestRunsModule],
providers: [BuildsService, PrismaService],
providers: [BuildsService, PrismaService, EventsGateway],
controllers: [BuildsController],
exports: [BuildsService],
})
Expand Down
5 changes: 4 additions & 1 deletion src/builds/builds.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { Test, TestingModule } from '@nestjs/testing';
import { BuildsService } from './builds.service';
import { PrismaService } from '../prisma/prisma.service';
import { TestRunsService } from '../test-runs/test-runs.service';
import { EventsGateway } from '../events/events.gateway';

describe('BuildsService', () => {
let service: BuildsService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [BuildsService,
providers: [
BuildsService,
{ provide: PrismaService, useValue: {} },
{ provide: TestRunsService, useValue: {} },
{ provide: EventsGateway, useValue: {} },
],
}).compile();

Expand Down
28 changes: 21 additions & 7 deletions src/builds/builds.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,46 @@ import { CreateBuildDto } from './dto/build-create.dto';
import { PrismaService } from '../prisma/prisma.service';
import { Build } from '@prisma/client';
import { TestRunsService } from '../test-runs/test-runs.service';
import { EventsGateway } from '../events/events.gateway';
import { BuildDto } from './dto/build.dto';

@Injectable()
export class BuildsService {
constructor(private prismaService: PrismaService, private testRunsService: TestRunsService) {}
constructor(
private prismaService: PrismaService,
private testRunsService: TestRunsService,
private eventsGateway: EventsGateway
) {}

async findMany(projectId: string): Promise<Build[]> {
return this.prismaService.build.findMany({
async findMany(projectId: string): Promise<BuildDto[]> {
const buildList = await this.prismaService.build.findMany({
where: { projectId },
include: {
testRuns: true,
},
orderBy: { createdAt: 'desc' },
});

return buildList.map(build => new BuildDto(build));
}

async create(buildDto: CreateBuildDto): Promise<Build> {
return this.prismaService.build.create({
async create(createBuildDto: CreateBuildDto): Promise<BuildDto> {
const build = await this.prismaService.build.create({
data: {
branchName: buildDto.branchName,
branchName: createBuildDto.branchName,
project: {
connect: {
id: buildDto.projectId,
id: createBuildDto.projectId,
},
},
},
include: {
testRuns: true,
},
});
const buildDto = new BuildDto(build);
this.eventsGateway.buildCreated(buildDto);
return buildDto;
}

async remove(id: string): Promise<Build> {
Expand Down
80 changes: 80 additions & 0 deletions src/builds/dto/build.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { ApiProperty } from '@nestjs/swagger';
import { Build, TestRun, TestStatus } from '@prisma/client';

export class BuildDto {
@ApiProperty()
id: string;

@ApiProperty()
number: number | null;

@ApiProperty()
branchName: string | null;

@ApiProperty()
status: string | null;

@ApiProperty()
projectId: string;

@ApiProperty()
updatedAt: Date;

@ApiProperty()
createdAt: Date;

@ApiProperty()
userId: string | null;

@ApiProperty()
passedCount: number;
@ApiProperty()
unresolvedCount: number;
@ApiProperty()
failedCount: number;

constructor(build: Build & { testRuns: TestRun[] }) {
this.id = build.id;
this.number = build.number;
this.branchName = build.branchName;
this.status = build.status;
this.projectId = build.projectId;
this.updatedAt = build.updatedAt;
this.createdAt = build.createdAt;

this.passedCount = 0;
this.unresolvedCount = 0;
this.failedCount = 0;

build.testRuns.forEach(testRun => {
switch (testRun.status) {
case TestStatus.approved:
case TestStatus.ok: {
this.passedCount += 1;
break;
}
case TestStatus.unresolved:
case TestStatus.new: {
this.unresolvedCount += 1;
break;
}
case TestStatus.failed: {
this.failedCount += 1;
break;
}
}
});

if (build.testRuns.length === 0) {
this.status = 'new';
} else {
this.status = 'passed';
}
if (this.failedCount > 0) {
this.status = 'failed';
}
if (this.unresolvedCount > 0) {
this.status = 'unresolved';
}
}
}
18 changes: 18 additions & 0 deletions src/events/events.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
import { TestRun } from '@prisma/client';
import { BuildDto } from '../builds/dto/build.dto';

@WebSocketGateway()
export class EventsGateway {
@WebSocketServer()
server: Server;

buildCreated(build: BuildDto) {
this.server.emit('build_created', build);
}

newTestRun(testRun: TestRun) {
this.server.emit('testRun_created', testRun);
}
}
3 changes: 2 additions & 1 deletion src/test-runs/test-runs.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { TestRunsService } from './test-runs.service';
import { SharedModule } from '../shared/shared.module';
import { PrismaService } from '../prisma/prisma.service';
import { TestRunsController } from './test-runs.controller';
import { EventsGateway } from '../events/events.gateway';

@Module({
imports: [SharedModule],
providers: [TestRunsService, PrismaService],
providers: [TestRunsService, PrismaService, EventsGateway],
exports: [TestRunsService],
controllers: [TestRunsController]
})
Expand Down
Loading