Skip to content

Commit

Permalink
feat: ✨ add project infra repos url in the gitlab commit
Browse files Browse the repository at this point in the history
  • Loading branch information
this-is-tobi committed Jan 22, 2025
1 parent 1208b7a commit 658be74
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
17 changes: 9 additions & 8 deletions src/function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Environment, Project, StepCall, UserObject } from '@cpn-console/hooks'
import type { Gitlab as IGitlab } from '@gitbeaker/core'
import type { Gitlab as GitlabInterface } from '@gitbeaker/core'
import type { BaseParams, Stage } from './utils.js'
import { parseError } from '@cpn-console/hooks'
import { removeTrailingSlash, requiredEnv } from '@cpn-console/shared'
Expand Down Expand Up @@ -50,7 +50,7 @@ function getListPrems(environments: Environment[]): ListPerms {
return listPerms
}

function getApi(): IGitlab {
function getGitlabApi(): GitlabInterface {
const gitlabUrl = removeTrailingSlash(requiredEnv('GITLAB_URL'))
const gitlabToken = requiredEnv('GITLAB_TOKEN')
return new Gitlab({ token: gitlabToken, host: gitlabUrl })
Expand All @@ -61,8 +61,9 @@ export const upsertProject: StepCall<Project> = async (payload) => {
// init args
const project = payload.args
const keycloakApi = payload.apis.keycloak
const vaultApi = payload.apis.vault
// init gitlab api
const gitlabApi = getApi()
const gitlabApi = getGitlabApi()
const keycloakRootGroupPath = await keycloakApi.getProjectGroupPath()

const hasProd = project.environments.find(env => env.stage === 'prod')
Expand All @@ -75,10 +76,10 @@ export const upsertProject: StepCall<Project> = async (payload) => {
ensureKeycloakGroups(listPerms, keycloakApi),
// Upsert or delete Gitlab config based on prod/non-prod environment
...(hasProd
? [await upsertGitlabConfig(prodParams, keycloakRootGroupPath, project, gitlabApi)]
? [await upsertGitlabConfig(prodParams, keycloakRootGroupPath, project, gitlabApi, vaultApi)]
: [await deleteGitlabYamlConfig(prodParams, project, gitlabApi)]),
...(hasNonProd
? [await upsertGitlabConfig(hProdParams, keycloakRootGroupPath, project, gitlabApi)]
? [await upsertGitlabConfig(hProdParams, keycloakRootGroupPath, project, gitlabApi, vaultApi)]
: [await deleteGitlabYamlConfig(hProdParams, project, gitlabApi)]),
])

Expand All @@ -102,15 +103,15 @@ export const upsertProject: StepCall<Project> = async (payload) => {
export const deleteProject: StepCall<Project> = async (payload) => {
try {
const project = payload.args
const api = getApi()
const gitlabApi = getGitlabApi()
const keycloakApi = payload.apis.keycloak
const hProdParams = getBaseParams(project, 'hprod')
const prodParams = getBaseParams(project, 'prod')

await Promise.all([
deleteKeycloakGroup(keycloakApi),
deleteGitlabYamlConfig(prodParams, project, api),
deleteGitlabYamlConfig(hProdParams, project, api),
deleteGitlabYamlConfig(prodParams, project, gitlabApi),
deleteGitlabYamlConfig(hProdParams, project, gitlabApi),
])

return {
Expand Down
53 changes: 33 additions & 20 deletions src/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Project } from '@cpn-console/hooks'
import type { Gitlab as IGitlab } from '@gitbeaker/core'
import type { VaultProjectApi } from '@cpn-console/vault-plugin/types/class.js'
import type { Gitlab as GitlabInterface } from '@gitbeaker/core'
import type {
Project as GitlabProject,
Group,
Expand All @@ -23,6 +24,7 @@ interface ProjectLoki {
name: string
groups: string[]
uuid: string
// urls: string[]
}

interface YamlLokiData {
Expand Down Expand Up @@ -50,16 +52,16 @@ export function writeYamlFile(data: object): string {
}
}

async function findOrCreateGroup(api: IGitlab, groupName: string) {
const group = await findGroup(api, groupName)
return group ?? await createGroup(api, groupName)
async function findOrCreateGroup(gitlabApi: GitlabInterface, groupName: string) {
const group = await findGroup(gitlabApi, groupName)
return group ?? await createGroup(gitlabApi, groupName)
}

async function findOrCreateRepo(api: IGitlab, group: Group, repoName: string): Promise<GitlabProject> {
async function findOrCreateRepo(gitlabApi: GitlabInterface, group: Group, repoName: string): Promise<GitlabProject> {
try {
const repo = await findProject(api, group, repoName)
const repo = await findProject(gitlabApi, group, repoName)
if (!repo) {
return await createProject(api, group, repoName, 'Repo for obervatorium values, managed by DSO console')
return await createProject(gitlabApi, group, repoName, 'Repo for obervatorium values, managed by DSO console')
}
return repo
} catch (error) {
Expand All @@ -68,7 +70,7 @@ async function findOrCreateRepo(api: IGitlab, group: Group, repoName: string): P
}

// Fonction pour trouver ou créer un fichier values.yaml
async function findOrCreateValuesFile(api: IGitlab, project: GitlabProject): Promise<string> {
async function findOrCreateValuesFile(gitlabApi: GitlabInterface, project: GitlabProject): Promise<string> {
const yamlData = `
global:
tenants: []
Expand All @@ -77,15 +79,15 @@ async function findOrCreateValuesFile(api: IGitlab, project: GitlabProject): Pro
try {
// Essayer de récupérer le fichier
const file = await getGitlabYamlFileContent(
api,
gitlabApi,
project,
valuesPath,
valuesBranch,
)
return Buffer.from(file.content, 'base64').toString('utf-8')
} catch (_error) {
await commitAndPushYamlFile(
api,
gitlabApi,
project,
valuesPath,
valuesBranch,
Expand All @@ -96,24 +98,35 @@ async function findOrCreateValuesFile(api: IGitlab, project: GitlabProject): Pro
}
}

export async function upsertGitlabConfig(params: BaseParams, keycloakRootGroupPath: string, project: Project, api: IGitlab) {
export async function upsertGitlabConfig(params: BaseParams, keycloakRootGroupPath: string, project: Project, gitlabApi: GitlabInterface, _vaultApi: VaultProjectApi) {
// Déplacer toute la logique de création ou de récupération de groupe et de repo ici
const lokiGroupName = 'observability'
const lokiRepoName = 'observability'
const gitlabLokiGroup = await findOrCreateGroup(api, lokiGroupName)
const gitlabLokiRepo = await findOrCreateRepo(api, gitlabLokiGroup, lokiRepoName)
const gitlabLokiGroup = await findOrCreateGroup(gitlabApi, lokiGroupName)
const gitlabLokiRepo = await findOrCreateRepo(gitlabApi, gitlabLokiGroup, lokiRepoName)

// Récupérer ou créer le fichier values.yaml
const file = await findOrCreateValuesFile(api, gitlabLokiRepo)
const file = await findOrCreateValuesFile(gitlabApi, gitlabLokiRepo)
let yamlFile = await readYamlFile<YamlLokiData>(Buffer.from(file, 'utf-8').toString('utf-8'))

const tenantName = `${params.stage}-${params.organizationName}-${params.projectName}`
const tenantRbac = [`${keycloakRootGroupPath}/grafana/${params.stage}-RW`, `${keycloakRootGroupPath}/grafana/${params.stage}-RO`]

// const infraReposUrls: string[] = []
// for (const repo of project.repositories) {
// if (repo.isInfra) {
// const repoInternalUrl = (await vaultApi.read(`${params.organizationName}/${params.projectName}/${repo.internalRepoName}-mirror`)).data.GIT_OUTPUT_URL as string
// if (repoInternalUrl) {
// infraReposUrls.push(`https://${repoInternalUrl}`)
// }
// }
// }

const projectData: ProjectLoki = {
name: tenantName,
groups: tenantRbac,
uuid: project.id,
// urls: infraReposUrls,
}

if (findTenantByName(yamlFile, tenantName)) {
Expand All @@ -125,7 +138,7 @@ export async function upsertGitlabConfig(params: BaseParams, keycloakRootGroupPa
const yamlString = writeYamlFile(yamlFile)

return commitAndPushYamlFile(
api,
gitlabApi,
gitlabLokiRepo,
valuesPath,
valuesBranch,
Expand All @@ -134,15 +147,15 @@ export async function upsertGitlabConfig(params: BaseParams, keycloakRootGroupPa
)
}

export async function deleteGitlabYamlConfig(params: BaseParams, project: Project, api: IGitlab) {
export async function deleteGitlabYamlConfig(params: BaseParams, project: Project, gitlabApi: GitlabInterface) {
// Même logique de groupe et de repo que pour l'upsert
const lokiGroupName = 'observability'
const lokiRepoName = 'observability'
const gitlabLokiGroup = await findOrCreateGroup(api, lokiGroupName)
const gitlabLokiRepo = await findOrCreateRepo(api, gitlabLokiGroup, lokiRepoName)
const gitlabLokiGroup = await findOrCreateGroup(gitlabApi, lokiGroupName)
const gitlabLokiRepo = await findOrCreateRepo(gitlabApi, gitlabLokiGroup, lokiRepoName)

// Récupérer le fichier values.yaml
const file = await findOrCreateValuesFile(api, gitlabLokiRepo)
const file = await findOrCreateValuesFile(gitlabApi, gitlabLokiRepo)
let yamlFile = await readYamlFile<YamlLokiData>(Buffer.from(file, 'utf-8').toString('utf-8'))

const tenantName = `${params.stage}-${params.organizationName}-${params.projectName}`
Expand All @@ -158,7 +171,7 @@ export async function deleteGitlabYamlConfig(params: BaseParams, project: Projec
const yamlString = writeYamlFile(yamlFile)

return commitAndPushYamlFile(
api,
gitlabApi,
gitlabLokiRepo,
valuesPath,
valuesBranch,
Expand Down

0 comments on commit 658be74

Please sign in to comment.