Skip to content

Commit

Permalink
feat: add config
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Zhang (张涛) committed Feb 28, 2024
1 parent f5d2da5 commit bed7ab2
Show file tree
Hide file tree
Showing 18 changed files with 447 additions and 88 deletions.
9 changes: 9 additions & 0 deletions examples/config/coverage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"include": [
"src/**/*.js"
],
"exclude": [
"**/*.spec.js"
],
"extensions": [".js"]
}
3 changes: 2 additions & 1 deletion packages/canyon-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"passport-local": "^1.0.0",
"prisma": "5.8.1",
"reflect-metadata": "^0.1.14",
"rxjs": "^7.8.1"
"rxjs": "^7.8.1",
"test-exclude": "^6.0.0"
},
"devDependencies": {
"@nestjs/cli": "^10.3.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/canyon-backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ model Project {
pathWithNamespace String @map("path_with_namespace")
description String
bu String
tag String
coverage String
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp(3)
@@map("project")
Expand Down
5 changes: 5 additions & 0 deletions packages/canyon-backend/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Project {
name: String!
pathWithNamespace: String!
description: String!
tag: String!
coverage: String!
bu: String!
maxCoverage: Float!
reportTimes: Float!
Expand Down Expand Up @@ -216,4 +218,7 @@ type Mutation {

"""删除项目"""
deleteProject(projectID: String!): Project2!

"""更新项目"""
updateProject(projectID: String!, description: String!, tag: String!, coverage: String!): Project2!
}
Empty file.
6 changes: 6 additions & 0 deletions packages/canyon-backend/src/project/project.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ export class Project {
@Field(() => String)
description: string;

@Field(() => String)
tag: string;

@Field(() => String)
coverage: string;

@Field(() => String)
bu: string;

Expand Down
20 changes: 20 additions & 0 deletions packages/canyon-backend/src/project/project.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,24 @@ export class ProjectResolver {
): Promise<Project2> {
return this.projectService.deleteProject(user, projectID);
}

@Mutation(() => Project2, {
description: '更新项目',
})
@UseGuards(GqlAuthGuard)
updateProject(
@GqlUser() user: AuthUser,
@Args('projectID', { type: () => String }) projectID: string,
@Args('description', { type: () => String }) description: string,
@Args('tag', { type: () => String }) tag: string,
@Args('coverage', { type: () => String }) coverage: string,
): Promise<Project2> {
return this.projectService.updateProject(
user,
projectID,
description,
tag,
coverage,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class GetProjectsService {
maxCoverage: maxCoverage,
reportTimes: [...new Set(summarys.map(({ sha }) => sha))].length,
pathWithNamespace: project.pathWithNamespace,
tag: project.tag,
};
}),
);
Expand Down
56 changes: 48 additions & 8 deletions packages/canyon-backend/src/project/services/project.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export class ProjectService {
name: name,
description: description || '',
bu: bu || '默认',
coverage: '',
tag: '',
},
});
}
Expand All @@ -78,26 +80,64 @@ export class ProjectService {
});
}

async updateProject(user, projectID, description, tag, coverage) {
function removeEmptyValues(obj) {
for (const key in obj) {
if (
obj[key] === undefined ||
obj[key] === null ||
obj[key] === '__null__'
) {
delete obj[key];
}
}
return obj;
}
return this.prisma.project.update({
where: {
id: projectID,
},
data: removeEmptyValues({
description: description,
tag: tag,
coverage: coverage,
}),
});
}

async getProjectByID(projectID): Promise<Project> {
return this.prisma.project
.findFirst({
where: {
id: projectID,
},
})
.then(({ id, name, pathWithNamespace, description, bu, createdAt }) => {
return {
.then(
({
id,
name,
pathWithNamespace,
description,
bu,
createdAt,
bu: bu,
reportTimes: 0,
lastReportTime: new Date(),
maxCoverage: 0,
};
});
coverage,
tag,
}) => {
return {
id,
name,
pathWithNamespace,
description,
createdAt,
bu: bu,
reportTimes: 0,
lastReportTime: new Date(),
maxCoverage: 0,
tag,
coverage,
};
},
);
}

async getProjectsBuOptions() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { PullChangeCodeAndInsertDbService } from './pull-change-code-and-insert-db.service';
import { logger } from '../../logger';
const randomInteger = Math.floor(Math.random() * 2000) + 2000;

import * as TestExclude from 'test-exclude';
@Injectable()
export class ConsumerCoverageService {
constructor(
Expand Down Expand Up @@ -101,6 +101,40 @@ export class ConsumerCoverageService {
});
}

async filterCoverage(projectID, coverage) {
const project = await this.prisma.project.findFirst({
where: {
id: projectID,
},
});

let matchRule: any = {}; // Default value

try {
// Attempt to parse project?.coverage
matchRule = JSON.parse(project?.coverage || '{}');
} catch (error) {
// console.error('Error parsing coverage:', error);
// Log the error or handle it as needed
// You can also return an empty object or any default value
}
const exclude = new TestExclude({
cwd: '~',
include: matchRule.include,
exclude: matchRule.exclude,
extension: matchRule.extensions,
});

const filterCoverage = {};

for (const filterCoverageKey of Object.keys(coverage)) {
if (exclude.shouldInstrument(filterCoverageKey)) {
filterCoverage[filterCoverageKey] = coverage[filterCoverageKey];
}
}
return Object.keys(filterCoverage).length > 0 ? filterCoverage : coverage;
}

@Interval(randomInteger) //添加随机数,防止分布式服务同时执行,务必确保原子性,1-2s
async lock() {
const acquired = await this.acquireLock('consumer_coverage', 60000);
Expand Down Expand Up @@ -243,10 +277,17 @@ export class ConsumerCoverageService {
},
});

const mainCovFilterCoverage = await this.filterCoverage(
normalCoverage.projectID,
mainCov,
);

// !!!!!/

// 创建新的覆盖率数据
const { insertedId: newKey } = await createNewCoverageData(mainCov);
const { insertedId: newKey } = await createNewCoverageData(
mainCovFilterCoverage,
);

if (newKey === null) {
logger({
Expand All @@ -268,7 +309,7 @@ export class ConsumerCoverageService {

// 生成覆盖率概览数据
const coverageSummaryMap = genSummaryMapByCoverageMap(
mainCov,
mainCovFilterCoverage,
codechanges,
);
const allSummary = getSummaryByPath('~', coverageSummaryMap);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation UpdateProject($projectID: String!,$description: String!,$tag: String!,$coverage: String!) {
updateProject(projectID: $projectID,description: $description,tag: $tag,coverage: $coverage) {
id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ query GetProjectByID($projectID: ID!) {
description
reportTimes
lastReportTime
coverage
tag
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ query GetProjects($current: Int!, $pageSize: Int!, $keyword: String!,$bu: [Strin
bu
reportTimes
lastReportTime
maxCoverage
maxCoverage,
tag
}
}
}

This file was deleted.

Loading

0 comments on commit bed7ab2

Please sign in to comment.