Skip to content

feat(#455): handle baselineBranchName #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
47 changes: 47 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.28.1",
"jest": "^29.6.4",
"jest-mock-extended": "^4.0.0-beta1",
"prettier": "^3.0.3",
"prisma": "^5.3.1",
"supertest": "^6.3.3",
Expand Down
5 changes: 5 additions & 0 deletions src/test-runs/dto/create-test-request.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ export class CreateTestRequestDto extends BaselineDataDto {
@IsOptional()
@IsString()
comment?: string;

@ApiPropertyOptional()
@IsOptional()
@IsString()
baselineBranchName?: string;
}
20 changes: 17 additions & 3 deletions src/test-runs/test-runs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,16 @@ export class TestRunsService {

// try auto approve
if (project.autoApproveFeature) {
testRunWithResult = await this.tryAutoApproveByPastBaselines({ testVariation, testRun: testRunWithResult });
testRunWithResult = await this.tryAutoApproveByNewBaselines({ testVariation, testRun: testRunWithResult });
testRunWithResult = await this.tryAutoApproveByPastBaselines({
testVariation,
testRun: testRunWithResult,
baselineBranchName: createTestRequestDto.baselineBranchName,
});
testRunWithResult = await this.tryAutoApproveByNewBaselines({
testVariation,
testRun: testRunWithResult,
baselineBranchName: createTestRequestDto.baselineBranchName,
});
}
return new TestRunResultDto(testRunWithResult, testVariation);
}
Expand Down Expand Up @@ -348,7 +356,11 @@ export class TestRunsService {
* @param testVariation
* @param testRun
*/
private async tryAutoApproveByNewBaselines({ testVariation, testRun }: AutoApproveProps): Promise<TestRun> {
private async tryAutoApproveByNewBaselines({
testVariation,
testRun,
baselineBranchName,
}: AutoApproveProps): Promise<TestRun> {
if (testRun.status === TestStatus.ok) {
return testRun;
}
Expand All @@ -358,6 +370,7 @@ export class TestRunsService {
where: {
...getTestVariationUniqueData(testVariation),
baselineName: testVariation.baselineName,
baselineBranchName,
status: TestStatus.approved,
testVariation: {
projectId: testVariation.projectId,
Expand Down Expand Up @@ -407,4 +420,5 @@ export class TestRunsService {
interface AutoApproveProps {
testVariation: TestVariation;
testRun: TestRun;
baselineBranchName?: string;
}
120 changes: 120 additions & 0 deletions src/test-variations/test-variations.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,126 @@ describe('TestVariationsService', () => {
});
expect(result).toBe(variationMainMock);
});

it('can find by baselineBranchName', async () => {
const createRequest: CreateTestRequestDto = {
buildId: 'buildId',
projectId: projectMock.id,
name: 'Test name',
os: 'OS',
browser: 'browser',
viewport: 'viewport',
device: 'device',
customTags: '',
branchName: 'develop',
baselineBranchName: 'main',
};

const variationMock: TestVariation = {
id: '123',
projectId: projectMock.id,
name: 'Test name',
baselineName: 'main',
os: 'OS',
browser: 'browser',
viewport: 'viewport',
device: 'device',
customTags: '',
ignoreAreas: '[]',
comment: 'some comment',
branchName: 'develop',
createdAt: new Date(),
updatedAt: new Date(),
};
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
service = await initModule({ projectFindUniqueMock });
service.findUnique = jest.fn().mockResolvedValueOnce(variationMock).mockResolvedValueOnce(undefined);

const result = await service.find(createRequest);

expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
name: createRequest.name,
projectId: createRequest.projectId,
os: createRequest.os,
browser: createRequest.browser,
viewport: createRequest.viewport,
device: createRequest.device,
customTags: createRequest.customTags,
branchName: createRequest.baselineBranchName,
});
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
name: createRequest.name,
projectId: createRequest.projectId,
os: createRequest.os,
browser: createRequest.browser,
viewport: createRequest.viewport,
device: createRequest.device,
customTags: createRequest.customTags,
branchName: createRequest.branchName,
});
expect(result).toBe(variationMock);
});

it("can find by current branch if baselineBranchName doesn't exist", async () => {
const createRequest: CreateTestRequestDto = {
buildId: 'buildId',
projectId: projectMock.id,
name: 'Test name',
os: 'OS',
browser: 'browser',
viewport: 'viewport',
device: 'device',
customTags: '',
branchName: 'main',
baselineBranchName: 'release-1',
};

const variationMock: TestVariation = {
id: '123',
projectId: projectMock.id,
name: 'Test name',
baselineName: 'baselineName',
os: 'OS',
browser: 'browser',
viewport: 'viewport',
device: 'device',
customTags: '',
ignoreAreas: '[]',
comment: 'some comment',
branchName: 'develop',
createdAt: new Date(),
updatedAt: new Date(),
};
const projectFindUniqueMock = jest.fn().mockReturnValueOnce(projectMock);
service = await initModule({ projectFindUniqueMock });
service.findUnique = jest.fn().mockResolvedValueOnce(undefined).mockResolvedValueOnce(variationMock);

const result = await service.find(createRequest);

expect(projectFindUniqueMock).toHaveBeenCalledWith({ where: { id: createRequest.projectId } });
expect(service.findUnique).toHaveBeenNthCalledWith(1, {
name: createRequest.name,
projectId: createRequest.projectId,
os: createRequest.os,
browser: createRequest.browser,
viewport: createRequest.viewport,
device: createRequest.device,
customTags: createRequest.customTags,
branchName: createRequest.baselineBranchName,
});
expect(service.findUnique).toHaveBeenNthCalledWith(2, {
name: createRequest.name,
projectId: createRequest.projectId,
os: createRequest.os,
browser: createRequest.browser,
viewport: createRequest.viewport,
device: createRequest.device,
customTags: createRequest.customTags,
branchName: createRequest.branchName,
});
expect(result).toBe(variationMock);
});
});

it('create', async () => {
Expand Down
16 changes: 7 additions & 9 deletions src/test-variations/test-variations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,36 +79,34 @@ export class TestVariationsService {
* @param baselineData
* @returns
*/
async find(
createTestRequestDto: BaselineDataDto & { projectId: string; sourceBranch?: string }
): Promise<TestVariation | null> {
async find(createTestRequestDto: Omit<CreateTestRequestDto, 'buildId'>): Promise<TestVariation | null> {
const project = await this.prismaService.project.findUnique({ where: { id: createTestRequestDto.projectId } });
const mainBranch = createTestRequestDto.sourceBranch ?? project.mainBranchName;
const baselineBranch = createTestRequestDto.baselineBranchName ?? project.mainBranchName;
Comment on lines -86 to +84
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that sourceBranch was never used, so I replaced it with baselineBranchName to match existing parameter since that can already be set on the config and is send by the sdk-js if present.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it was used before to make manual test runs with comparison within branches not base on main branch
please, update docs on this method


const [mainBranchTestVariation, currentBranchTestVariation] = await Promise.all([
// search main branch variation
// search baseline branch variation
this.findUnique({
projectId: createTestRequestDto.projectId,
branchName: mainBranch,
branchName: baselineBranch,
...getTestVariationUniqueData(createTestRequestDto),
}),
// search current branch variation
createTestRequestDto.branchName !== mainBranch &&
createTestRequestDto.branchName !== baselineBranch &&
this.findUnique({
projectId: createTestRequestDto.projectId,
branchName: createTestRequestDto.branchName,
...getTestVariationUniqueData(createTestRequestDto),
}),
]);

if (!!currentBranchTestVariation) {
if (currentBranchTestVariation) {
if (mainBranchTestVariation && mainBranchTestVariation.updatedAt > currentBranchTestVariation.updatedAt) {
return mainBranchTestVariation;
}
return currentBranchTestVariation;
}

if (!!mainBranchTestVariation) {
if (mainBranchTestVariation) {
return mainBranchTestVariation;
}
}
Expand Down
4 changes: 3 additions & 1 deletion test/preconditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export const haveTestRunCreated = async (
projectId: string,
branchName: string,
imagePath: string,
merge?: boolean
merge?: boolean,
baselineBranchName?: string
): Promise<{ testRun: TestRunResultDto; build: Build }> => {
const build = await buildsService.findOrCreate({ projectId: projectId, branchName });
const testRun = await testRunsService.postTestRun({
Expand All @@ -61,6 +62,7 @@ export const haveTestRunCreated = async (
buildId: build.id,
name: 'Image name',
merge,
baselineBranchName,
},
imageBuffer: readFileSync(imagePath),
});
Expand Down
Loading
Loading