Skip to content

Commit 9b04c17

Browse files
authored
Add the username to the baseline history who has approved the new benchmark (#151)
Visual-Regression-Tracker/Visual-Regression-Tracker#143
1 parent 149b363 commit 9b04c17

File tree

10 files changed

+289
-13
lines changed

10 files changed

+289
-13
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Migration `20210705154453-baseline-author`
2+
3+
This migration has been generated by Pavlo Strunkin <[email protected]> at 7/5/2021, 6:44:53 PM.
4+
You can check out the [state of the schema](./schema.prisma) after the migration.
5+
6+
## Database Steps
7+
8+
```sql
9+
ALTER TABLE "Baseline" ADD COLUMN "userId" TEXT
10+
11+
ALTER TABLE "Baseline" ADD FOREIGN KEY("userId")REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE
12+
```
13+
14+
## Changes
15+
16+
```diff
17+
diff --git schema.prisma schema.prisma
18+
migration 20210612140950-limit-build-number..20210705154453-baseline-author
19+
--- datamodel.dml
20+
+++ datamodel.dml
21+
@@ -3,9 +3,9 @@
22+
}
23+
datasource db {
24+
provider = "postgresql"
25+
- url = "***"
26+
+ url = "***"
27+
}
28+
model Build {
29+
id String @id @default(uuid())
30+
@@ -102,9 +102,11 @@
31+
testVariationId String
32+
testVariation TestVariation @relation(fields: [testVariationId], references: [id])
33+
testRunId String?
34+
testRun TestRun? @relation(fields: [testRunId], references: [id])
35+
- updatedAt DateTime @updatedAt
36+
+ userId String?
37+
+ user User? @relation(fields: [userId], references: [id])
38+
+ updatedAt DateTime @updatedAt
39+
createdAt DateTime @default(now())
40+
}
41+
model User {
42+
@@ -115,8 +117,9 @@
43+
lastName String?
44+
apiKey String @unique
45+
isActive Boolean @default(true)
46+
builds Build[]
47+
+ baselines Baseline[]
48+
updatedAt DateTime @updatedAt
49+
createdAt DateTime @default(now())
50+
}
51+
```
52+
53+
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
datasource db {
6+
provider = "postgresql"
7+
url = "***"
8+
}
9+
10+
model Build {
11+
id String @id @default(uuid())
12+
ciBuildId String?
13+
number Int?
14+
branchName String?
15+
status String?
16+
testRuns TestRun[]
17+
projectId String
18+
project Project @relation(fields: [projectId], references: [id])
19+
updatedAt DateTime @updatedAt
20+
createdAt DateTime @default(now())
21+
user User? @relation(fields: [userId], references: [id])
22+
userId String?
23+
isRunning Boolean?
24+
25+
@@unique([projectId, ciBuildId])
26+
}
27+
28+
model Project {
29+
id String @id @default(uuid())
30+
name String
31+
mainBranchName String @default("master")
32+
builds Build[]
33+
buildsCounter Int @default(0)
34+
maxBuildAllowed Int @default(100)
35+
testVariations TestVariation[]
36+
updatedAt DateTime @updatedAt
37+
createdAt DateTime @default(now())
38+
// config
39+
autoApproveFeature Boolean @default(false)
40+
imageComparison ImageComparison @default(pixelmatch)
41+
imageComparisonConfig String @default("{ \"threshold\": 0.1, \"ignoreAntialiasing\": true, \"allowDiffDimensions\": false }")
42+
43+
@@unique([name])
44+
}
45+
46+
model TestRun {
47+
id String @id @default(uuid())
48+
imageName String
49+
diffName String?
50+
diffPercent Float?
51+
diffTollerancePercent Float @default(0)
52+
pixelMisMatchCount Int?
53+
status TestStatus
54+
buildId String
55+
build Build @relation(fields: [buildId], references: [id])
56+
testVariationId String?
57+
testVariation TestVariation? @relation(fields: [testVariationId], references: [id])
58+
merge Boolean @default(false)
59+
updatedAt DateTime @updatedAt
60+
createdAt DateTime @default(now())
61+
// Test variation data
62+
name String @default("")
63+
browser String?
64+
device String?
65+
os String?
66+
viewport String?
67+
customTags String? @default("")
68+
baselineName String?
69+
comment String?
70+
baseline Baseline?
71+
branchName String @default("master")
72+
baselineBranchName String?
73+
ignoreAreas String @default("[]")
74+
tempIgnoreAreas String @default("[]")
75+
}
76+
77+
model TestVariation {
78+
id String @id @default(uuid())
79+
name String
80+
branchName String @default("master")
81+
browser String @default("")
82+
device String @default("")
83+
os String @default("")
84+
viewport String @default("")
85+
customTags String @default("")
86+
baselineName String?
87+
ignoreAreas String @default("[]")
88+
projectId String
89+
project Project @relation(fields: [projectId], references: [id])
90+
testRuns TestRun[]
91+
baselines Baseline[]
92+
comment String?
93+
updatedAt DateTime @updatedAt
94+
createdAt DateTime @default(now())
95+
96+
@@unique([projectId, name, browser, device, os, viewport, customTags, branchName])
97+
}
98+
99+
model Baseline {
100+
id String @id @default(uuid())
101+
baselineName String
102+
testVariationId String
103+
testVariation TestVariation @relation(fields: [testVariationId], references: [id])
104+
testRunId String?
105+
testRun TestRun? @relation(fields: [testRunId], references: [id])
106+
userId String?
107+
user User? @relation(fields: [userId], references: [id])
108+
updatedAt DateTime @updatedAt
109+
createdAt DateTime @default(now())
110+
}
111+
112+
model User {
113+
id String @id @default(uuid())
114+
email String @unique
115+
password String
116+
firstName String?
117+
lastName String?
118+
apiKey String @unique
119+
isActive Boolean @default(true)
120+
builds Build[]
121+
baselines Baseline[]
122+
updatedAt DateTime @updatedAt
123+
createdAt DateTime @default(now())
124+
}
125+
126+
enum TestStatus {
127+
failed
128+
new
129+
ok
130+
unresolved
131+
approved
132+
autoApproved
133+
}
134+
135+
enum ImageComparison {
136+
pixelmatch
137+
lookSame
138+
odiff
139+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"version": "0.3.14-fixed",
3+
"steps": [
4+
{
5+
"tag": "CreateField",
6+
"model": "User",
7+
"field": "baselines",
8+
"type": "Baseline",
9+
"arity": "List"
10+
},
11+
{
12+
"tag": "CreateField",
13+
"model": "Baseline",
14+
"field": "userId",
15+
"type": "String",
16+
"arity": "Optional"
17+
},
18+
{
19+
"tag": "CreateField",
20+
"model": "Baseline",
21+
"field": "user",
22+
"type": "User",
23+
"arity": "Optional"
24+
},
25+
{
26+
"tag": "CreateDirective",
27+
"location": {
28+
"path": {
29+
"tag": "Field",
30+
"model": "Baseline",
31+
"field": "user"
32+
},
33+
"directive": "relation"
34+
}
35+
},
36+
{
37+
"tag": "CreateArgument",
38+
"location": {
39+
"tag": "Directive",
40+
"path": {
41+
"tag": "Field",
42+
"model": "Baseline",
43+
"field": "user"
44+
},
45+
"directive": "relation"
46+
},
47+
"argument": "fields",
48+
"value": "[userId]"
49+
},
50+
{
51+
"tag": "CreateArgument",
52+
"location": {
53+
"tag": "Directive",
54+
"path": {
55+
"tag": "Field",
56+
"model": "Baseline",
57+
"field": "user"
58+
},
59+
"directive": "relation"
60+
},
61+
"argument": "references",
62+
"value": "[id]"
63+
}
64+
]
65+
}

prisma/migrations/migrate.lock

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
20210425191116-github_215_project_config
2020
20210517203552-add-custom-tags
2121
20210605124856-image-compare-config-as-json
22-
20210612140950-limit-build-number
22+
20210612140950-limit-build-number
23+
20210705154453-baseline-author

prisma/schema.prisma

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ model Baseline {
103103
testVariation TestVariation @relation(fields: [testVariationId], references: [id])
104104
testRunId String?
105105
testRun TestRun? @relation(fields: [testRunId], references: [id])
106-
updatedAt DateTime @updatedAt
106+
userId String?
107+
user User? @relation(fields: [userId], references: [id])
108+
updatedAt DateTime @updatedAt
107109
createdAt DateTime @default(now())
108110
}
109111

@@ -116,6 +118,7 @@ model User {
116118
apiKey String @unique
117119
isActive Boolean @default(true)
118120
builds Build[]
121+
baselines Baseline[]
119122
updatedAt DateTime @updatedAt
120123
createdAt DateTime @default(now())
121124
}

src/_data_/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const generateBaseline = (baseline?: Partial<Baseline>): Baseline => {
3333
baselineName: 'baselineName',
3434
testVariationId: 'testVariationId',
3535
testRunId: 'testRunId',
36+
userId: 'userId',
3637
updatedAt: new Date(),
3738
createdAt: new Date(),
3839
...baseline,

src/test-runs/test-runs.controller.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
UseGuards,
44
Param,
55
ParseUUIDPipe,
6-
Put,
76
Body,
87
Get,
98
Query,
@@ -26,7 +25,7 @@ import {
2625
ApiBody,
2726
} from '@nestjs/swagger';
2827
import { JwtAuthGuard } from '../auth/guards/auth.guard';
29-
import { TestRun, TestStatus } from '@prisma/client';
28+
import { TestRun, TestStatus, User } from '@prisma/client';
3029
import { TestRunsService } from './test-runs.service';
3130
import { TestRunResultDto } from './dto/testRunResult.dto';
3231
import { ApiGuard } from '../auth/guards/api.guard';
@@ -37,13 +36,15 @@ import { CreateTestRequestMultipartDto } from './dto/create-test-request-multipa
3736
import { FileToBodyInterceptor } from '../shared/fite-to-body.interceptor';
3837
import { UpdateIgnoreAreasDto } from './dto/update-ignore-area.dto';
3938
import { UpdateTestRunDto } from './dto/update-test.dto';
39+
import { Reflector } from '@nestjs/core';
40+
import { CurrentUser } from '../shared/current-user.decorator';
4041

4142
@ApiTags('test-runs')
4243
@Controller('test-runs')
4344
export class TestRunsController {
4445
private readonly logger: Logger = new Logger(TestRunsController.name);
4546

46-
constructor(private testRunsService: TestRunsService) {}
47+
constructor(private testRunsService: TestRunsService, private reflector: Reflector) {}
4748

4849
@Get()
4950
@ApiOkResponse({ type: [TestRunDto] })
@@ -57,10 +58,14 @@ export class TestRunsController {
5758
@ApiQuery({ name: 'merge', required: false })
5859
@ApiBearerAuth()
5960
@UseGuards(JwtAuthGuard)
60-
async approveTestRun(@Body() ids: string[], @Query('merge', new ParseBoolPipe()) merge: boolean): Promise<void> {
61+
async approveTestRun(
62+
@CurrentUser() user: User,
63+
@Body() ids: string[],
64+
@Query('merge', new ParseBoolPipe()) merge: boolean
65+
): Promise<void> {
6166
this.logger.debug(`Going to approve TestRuns: ${ids}`);
6267
for (const id of ids) {
63-
await this.testRunsService.approve(id, merge);
68+
await this.testRunsService.approve(id, merge, false, user.id);
6469
}
6570
}
6671

src/test-runs/test-runs.service.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,8 @@ export class TestRunsService {
9595

9696
/**
9797
* Confirm difference for testRun
98-
*
99-
* @param id
100-
* @param merge replaces main branch baseline with feature one
101-
* @param autoApprove set auto approve status
102-
* @returns
10398
*/
104-
async approve(id: string, merge = false, autoApprove = false): Promise<TestRun> {
99+
async approve(id: string, merge = false, autoApprove = false, userId?: string): Promise<TestRun> {
105100
this.logger.log(`Approving testRun: ${id} merge: ${merge} autoApprove: ${autoApprove}`);
106101
const testRun = await this.findOne(id);
107102
let { testVariation } = testRun;
@@ -147,6 +142,7 @@ export class TestRunsService {
147142
// add baseline
148143
await this.testVariationService.addBaseline({
149144
id: testVariation.id,
145+
userId,
150146
testRunId: testRun.id,
151147
baselineName,
152148
});

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ describe('TestVariationsService', () => {
9797
baselines: {
9898
include: {
9999
testRun: true,
100+
user: true,
100101
},
101102
orderBy: {
102103
createdAt: 'desc',
@@ -555,6 +556,7 @@ describe('TestVariationsService', () => {
555556
baselineName: 'image name 1',
556557
testVariationId: testVariationId,
557558
testRunId: 'test run id 1',
559+
userId: 'userId',
558560
createdAt: new Date(),
559561
updatedAt: new Date(),
560562
},
@@ -588,6 +590,7 @@ describe('TestVariationsService', () => {
588590
baselineName: 'image name 1',
589591
testVariationId: 'test variation id',
590592
testRunId: 'test run id 1',
593+
userId: 'userId',
591594
createdAt: new Date(),
592595
updatedAt: new Date(),
593596
};

0 commit comments

Comments
 (0)