-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from chaotic-cx/feature/repo-manager
feat(backend): add initial repo-manager service
- Loading branch information
Showing
16 changed files
with
967 additions
and
14 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,4 @@ core | |
|
||
# Local env files | ||
.env | ||
repos/ |
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
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
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
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,10 @@ | ||
import { registerAs } from "@nestjs/config"; | ||
|
||
export default registerAs("repoMan", () => ({ | ||
gitlabToken: process.env.GITLAB_TOKEN, | ||
gitAuthor: process.env.GIT_AUTHOR ?? "Chaotic Temeraire", | ||
gitEmail: process.env.GIT_EMAIL ?? "[email protected]", | ||
gitUsername: process.env.GIT_USERNAME ?? "git", | ||
schedulerInterval: process.env.REPOMANAGER_SCHEDULE ?? "0 * * * *", | ||
alwaysRebuild: process.env.REPOMANAGER_ALWAYS_REBUILD ?? "{}", | ||
})); |
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
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
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,37 @@ | ||
export interface Repo { | ||
name: string; | ||
url: string; | ||
status: RepoStatus; | ||
} | ||
|
||
export enum RepoStatus { | ||
ACTIVE = 0, | ||
INACTIVE = 1, | ||
RUNNING = 2, | ||
} | ||
|
||
export interface ArchRepoToCheck { | ||
name: string; | ||
path: string; | ||
workDir: string; | ||
} | ||
|
||
export interface ParsedPackage { | ||
base: string; | ||
pkgrel: number; | ||
version: string; | ||
name: string; | ||
} | ||
|
||
export interface RepoSettings { | ||
gitAuthor: string; | ||
gitEmail: string; | ||
gitUsername: string; | ||
gitlabToken: string; | ||
alwaysRebuild: { [key: string]: string }; | ||
} | ||
|
||
export interface PkgnameVersion { | ||
pkgname: string; | ||
archVersion: string; | ||
} |
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 { Controller, Get } from "@nestjs/common"; | ||
import { RepoManagerService } from "./repo-manager.service"; | ||
import { AllowAnonymous } from "../auth/anonymous.decorator"; | ||
|
||
@Controller("repo") | ||
export class RepoManagerController { | ||
constructor(private repoManager: RepoManagerService) {} | ||
|
||
@Get("test") | ||
test(): void { | ||
this.repoManager.test(); | ||
} | ||
|
||
@Get("update") | ||
update(): void { | ||
this.repoManager.testClonePush(); | ||
} | ||
} |
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,76 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn, Repository } from "typeorm"; | ||
import { Mutex } from "async-mutex"; | ||
import { Logger } from "@nestjs/common"; | ||
|
||
@Entity() | ||
export class ArchlinuxPackage { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ type: "varchar" }) | ||
pkgname: string; | ||
|
||
@Column({ type: "varchar", nullable: true }) | ||
version: string; | ||
|
||
@Column({ type: "int", nullable: true }) | ||
pkgrel: number; | ||
|
||
@Column({ type: "varchar", nullable: true }) | ||
arch: string; | ||
|
||
@Column({ type: "timestamp", nullable: true }) | ||
lastUpdated: string; | ||
|
||
@Column({ type: "varchar", nullable: true }) | ||
previousVersion: string; | ||
} | ||
|
||
@Entity() | ||
export class RepoManagerSettings { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ type: "varchar" }) | ||
key: string; | ||
|
||
@Column({ type: "varchar" }) | ||
value: string; | ||
} | ||
|
||
const packageMutex = new Mutex(); | ||
|
||
/** | ||
* Check if a package exists in the database, if not create a new entry | ||
* @param pkg The package object | ||
* @param connection The repository connection | ||
* @returns The package object itself | ||
*/ | ||
export async function archPkgExists( | ||
pkg: { | ||
version: string; | ||
name: string; | ||
}, | ||
connection: Repository<ArchlinuxPackage>, | ||
): Promise<ArchlinuxPackage> { | ||
return packageMutex.runExclusive(async () => { | ||
try { | ||
const packages: ArchlinuxPackage[] = await connection.find({ where: { pkgname: pkg.name } }); | ||
let packageExists: Partial<ArchlinuxPackage> = packages.find((onePkg) => { | ||
return onePkg.pkgname === pkg.name; | ||
}); | ||
|
||
if (packageExists === undefined) { | ||
Logger.log(`Package ${pkg.name} not found in database, creating new entry`, "RepoManagerEntity"); | ||
packageExists = await connection.save({ | ||
pkgname: pkg.name, | ||
version: pkg.version, | ||
}); | ||
} | ||
|
||
return packageExists as ArchlinuxPackage; | ||
} catch (err: unknown) { | ||
Logger.error(err, "RepoManagerEntity"); | ||
} | ||
}); | ||
} |
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,20 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { RepoManagerController } from "./repo-manager.controller"; | ||
import { RepoManagerService } from "./repo-manager.service"; | ||
import { HttpModule } from "@nestjs/axios"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { ArchlinuxPackage, RepoManagerSettings } from "./repo-manager.entity"; | ||
import { ConfigModule } from "@nestjs/config"; | ||
import repoManagerConfig from "../config/repo-manager.config"; | ||
import { Package, Repo } from "../builder/builder.entity"; | ||
|
||
@Module({ | ||
imports: [ | ||
HttpModule, | ||
ConfigModule.forFeature(repoManagerConfig), | ||
TypeOrmModule.forFeature([ArchlinuxPackage, Repo, RepoManagerSettings, Package]), | ||
], | ||
controllers: [RepoManagerController], | ||
providers: [RepoManagerService], | ||
}) | ||
export class RepoManagerModule {} |
Oops, something went wrong.