Skip to content

Commit

Permalink
feat(backend): process namcap analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
dr460nf1r3 committed Nov 6, 2024
1 parent 73b929a commit bbd9839
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 5 deletions.
5 changes: 4 additions & 1 deletion backend/src/builder/builder.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type Repository,
} from "typeorm";
import { BuildStatus } from "../types";
import { RepoStatus } from "../interfaces/repo-manager";
import { NamcapAnalysis, RepoStatus } from "../interfaces/repo-manager";

@Entity()
export class Builder {
Expand Down Expand Up @@ -63,6 +63,9 @@ export class Package {

@Column({ type: "int", nullable: true })
pkgrel: number;

@Column({ type: "jsonb", nullable: true })
namcapAnalysis: Partial<NamcapAnalysis>
}

@Entity()
Expand Down
11 changes: 7 additions & 4 deletions backend/src/builder/builder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,13 @@ export class BuilderDatabaseService extends Service {
// Update the chaotic versions as they changed with new successful builds
if (params.status === BuildStatus.SUCCESS) {
try {
await this.repoManagerService.updateChaoticVersions();
await this.repoManagerService.eventuallyBumpAffected(build);
} catch(err: unknown) {
Logger.error(err, "RepoManager")
await Promise.allSettled([
this.repoManagerService.updateChaoticVersions(),
this.repoManagerService.eventuallyBumpAffected(build),
this.repoManagerService.processNamcapAnalysis(build as Build, params.namcapAnalysis),
]);
} catch (err: unknown) {
Logger.error(err, "RepoManager");
}
}

Expand Down
10 changes: 10 additions & 0 deletions backend/src/interfaces/repo-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ export interface BumpLogEntry {
triggerFrom: TriggerType;
timestamp: string;
}

export interface NamcapAnalysis {
"dependency-detected-satisfied": string[];
"dependency-implicitly-satisfied": string[];
"depends-by-namcap-sight": string[];
"libdepends-by-namcap-sight": string[];
"libdepends-detected-not-included": string[];
"libprovides-by-namcap-sight": string[];
"link-level-dependence": string[];
}
82 changes: 82 additions & 0 deletions backend/src/repo-manager/repo-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
BumpLogEntry,
BumpResult,
BumpType,
NamcapAnalysis,
PackageBumpEntry,
PackageConfig,
ParsedPackage,
Expand Down Expand Up @@ -257,6 +258,87 @@ export class RepoManagerService {

return result;
}

/**
* Process namcap analysis, if available, and save it to the database.
* @param build The build object
* @param namcapAnalysis The namcap analysis output
*/
async processNamcapAnalysis(build: Build, namcapAnalysis: string): Promise<void> {
if (!namcapAnalysis) return;

const finalAnalysis: Partial<NamcapAnalysis> = {
"dependency-detected-satisfied": [],
"dependency-implicitly-satisfied": [],
"depends-by-namcap-sight": [],
"libdepends-by-namcap-sight": [],
"libdepends-detected-not-included": [],
"libprovides-by-namcap-sight": [],
"link-level-dependence": [],
};
const relevantRules: string[] = Object.keys(finalAnalysis);

try {
let namcapLines: string[] = namcapAnalysis.split("\n");
for (const line of namcapLines) {
const lineSplit = line.split(": ")[1];
if (!lineSplit) continue;

const [rule, result] = lineSplit.split(" ");
if (!relevantRules.includes(rule)) continue;

switch (rule) {
case "dependency-detected-satisfied": {
const key = result.split(" ")[0];
if (key) finalAnalysis[rule].push(key);
break;
}
case "dependency-implicitly-satisfied": {
const key = result.split(" ")[0];
if (key) finalAnalysis[rule].push(key);
break;
}
case "depends-by-namcap-sight": {
const depends = result.split(" ")[0];
const depsText = depends.match(/(?<=\()[^)]+(?=\))/);
if (!depsText) break;
finalAnalysis[rule] = depsText[0].split(" ");
break;
}
case "libdepends-by-namcap-sight": {
const libDepends = result.split(" ")[0];
const libDepsText = libDepends.match(/(?<=\()[^)]+(?=\))/);
if (!libDepsText) break;
finalAnalysis[rule] = libDepsText[0].split(" ");
break;
}
case "libdepends-detected-not-included": {
const key = result.split(" ")[0];
if (key) finalAnalysis[rule].push(key);
break;
}
case "libprovides-by-namcap-sight": {
const libProvides = result.split(" ")[0];
const libProvidesText = libProvides.match(/(?<=\()[^)]+(?=\))/);
if (!libProvidesText) break;
finalAnalysis[rule] = libProvidesText[0].split(" ");
break;
}
case "link-level-dependence": {
const key = result.split(" ")[0];
if (key) finalAnalysis[rule].push(key);
break;
}
}
}

const pkg: Package = build.pkgbase;
pkg.namcapAnalysis = finalAnalysis;
void this.packageRepository.save(pkg);
} catch (err: unknown) {
Logger.error(err, "RepoManager");
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions backend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface MoleculerBuildObject {
status?: BuildStatus;
target_repo: string;
timestamp: number;
namcapAnalysis?: string;
}

export enum BuildStatus {
Expand Down

0 comments on commit bbd9839

Please sign in to comment.