Skip to content

Commit

Permalink
feat: add jacoco
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Zhang (张涛) committed May 22, 2024
1 parent 4c4bba0 commit d59f0e3
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/canyon-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"axios": "^1.6.5",
"better-sqlite3": "^9.4.5",
"body-parser": "^1.20.2",
"camaro": "^6.2.3",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
Expand Down
43 changes: 43 additions & 0 deletions packages/canyon-backend/src/coverage/coverage-test.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
Body,
Controller,
Get,
Post,
Query,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
import { CoverageTestService } from './services/coverage-test.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { jacocoXml2Json } from '../utils/jacoco';
import { compressedData } from '../utils/zstd';

@Controller()
export class CoverageTestController {
constructor(
private readonly coverageTestService: CoverageTestService,
private prisma: PrismaService,
) {}

@Post('coverage/uploadjacoco')
@UseInterceptors(FileInterceptor('file'))
async uploadjacoco(@UploadedFile() file, @Body() body): Promise<any> {
const coverage = await jacocoXml2Json(file.buffer.toString()).then((data) =>
compressedData(JSON.stringify(data)),
);
return this.coverageTestService.create({
projectID: body.projectID,
sha: body.sha,
coverage: coverage,
});
}

@Get('coverage/uploadjacoco')
async getjacoco(@Query() body): Promise<any> {
return this.coverageTestService.find({
projectID: body.projectID,
sha: body.sha,
});
}
}
14 changes: 13 additions & 1 deletion packages/canyon-backend/src/coverage/coverage.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { CoveragediskEntity } from './entity/coveragedisk.entity';
import { CoveragediskService } from './services/core/coveragedisk.service';
import { TestExcludeService } from './services/common/test-exclude.service';
import {
CoverageTest,
CoverageTestSchema,
} from './schemas/coverage-test.schema';
import { CoverageTestController } from './coverage-test.controller';
import { CoverageTestService } from './services/coverage-test.service';

@Module({
imports: [
Expand All @@ -31,10 +37,15 @@ import { TestExcludeService } from './services/common/test-exclude.service';
schema: CoverageLogSchema,
collection: 'canyon_coverage_log',
},
{
name: CoverageTest.name,
schema: CoverageTestSchema,
collection: 'canyon_coverage_test',
},
]),
TypeOrmModule.forFeature([CoveragediskEntity]),
],
controllers: [CoverageController],
controllers: [CoverageController, CoverageTestController],
providers: [
PrismaService,
CoverageClientService,
Expand All @@ -45,6 +56,7 @@ import { TestExcludeService } from './services/common/test-exclude.service';
PullChangeCodeAndInsertDbService,
CoveragediskService,
TestExcludeService,
CoverageTestService,
],
})
export class CoverageModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';

@Schema()
export class CoverageTest {
@Prop()
projectID: string;

@Prop()
sha: string;

@Prop()
coverage: string;

@Prop()
createdAt: Date;
}

export const CoverageTestSchema = SchemaFactory.createForClass(CoverageTest);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CoverageTest } from '../schemas/coverage-test.schema';
import { decompressedData } from '../../utils/zstd';

@Injectable()
export class CoverageTestService {
constructor(
@InjectModel(CoverageTest.name)
private coverageTestModel: Model<CoverageTest>,
) {}
create({ projectID, sha, coverage }) {
return this.coverageTestModel.create({ projectID, sha, coverage });
}

find({ projectID, sha }) {
return this.coverageTestModel
.findOne({ projectID, sha })
.then((r) => {
return decompressedData(r.coverage);
})
.then((r) => JSON.parse(r));
}
}
63 changes: 63 additions & 0 deletions packages/canyon-backend/src/utils/jacoco.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { transform } from 'camaro';
const counterTemplate = [
'counter',
{
type: '@type',
missed: 'number(@missed)',
covered: 'number(@covered)',
},
];

const template = [
'report',
{
report: {
name: '@name',
package: [
'package',
{
name: '@name',
class: [
'class',
{
name: '@name',
sourcefilename: '@sourcefilename',
counter: counterTemplate,
method: [
'method',
{
name: '@name',
desc: '@desc',
line: '@line',
counter: counterTemplate,
},
],
},
],
sourcefile: [
'sourcefile',
{
name: '@name',
line: [
'line',
{
nr: 'number(@nr)',
mi: 'number(@mi)',
ci: 'number(@ci)',
mb: 'number(@mb)',
cb: 'number(@cb)',
},
],
counter: counterTemplate,
},
],
counter: counterTemplate,
},
],
counter: counterTemplate,
},
},
];
export const jacocoXml2Json = (xml) => {
return transform(xml, template).then((r) => r[0]);
};

0 comments on commit d59f0e3

Please sign in to comment.