-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Allen Zhang (张涛)
committed
May 22, 2024
1 parent
4c4bba0
commit d59f0e3
Showing
6 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/canyon-backend/src/coverage/coverage-test.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/canyon-backend/src/coverage/schemas/coverage-test.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
25 changes: 25 additions & 0 deletions
25
packages/canyon-backend/src/coverage/services/coverage-test.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |