1+ import type { GitlabProjectApi } from '@cpn-console/gitlab-plugin/types/class.js'
12import type { Project } from '@cpn-console/hooks'
3+ import type { Gitlab as IGitlab , ProjectSchema } from '@gitbeaker/core'
24import { removeTrailingSlash , requiredEnv } from '@cpn-console/shared'
3- import { Gitlab , type ProjectSchema } from '@gitbeaker/core '
5+ import { Gitlab } from '@gitbeaker/rest '
46import yaml from 'js-yaml'
57
68const valuesPath = 'helm/values.yaml'
79const valuesBranch = 'main'
810const groupName = 'observability'
911const repoName = 'observability'
12+ export const observabilityRepository = 'infra-observability'
13+ const observabilityChartVersion = requiredEnv ( 'DSO_OBSERVABILITY_CHART_VERSION' )
14+ const observabilityChartContent = `
15+ apiVersion: v2
16+ name: dso-observability
17+ type: application
18+ version: 0.1.0
19+ appVersion: "0.0.1"
20+ dependencies:
21+ - name: charts/dso-observability
22+ version: ${ observabilityChartVersion }
23+ repository: https://cloud-pi-native.github.io/helm-charts/
24+ `
25+ const observabilityTemplateContent = `
26+ {{- include "grafana-dashboards.dashboards" . -}}
27+ {{- include "grafana-dashboards.rules" . -}}
28+ `
1029
1130export type EnvType = 'prod' | 'hprod'
1231interface Tenant { }
@@ -19,6 +38,10 @@ interface Env {
1938}
2039export interface ObservabilityProject {
2140 projectName : string // slug
41+ projectRepository : {
42+ url : string
43+ path : string
44+ }
2245 envs : {
2346 prod : Env
2447 hprod : Env
@@ -39,20 +62,22 @@ const yamlInitData = `
3962 `
4063
4164export class ObservabilityRepoManager {
42- private gitlabApi : Gitlab
65+ private gitlabApi : IGitlab
66+ private gitlabProjectApi : GitlabProjectApi
4367
44- constructor ( ) {
68+ constructor ( gitlabProjectApi : GitlabProjectApi ) {
4569 const gitlabUrl = removeTrailingSlash ( requiredEnv ( 'GITLAB_URL' ) )
4670 const gitlabToken = requiredEnv ( 'GITLAB_TOKEN' )
4771 this . gitlabApi = new Gitlab ( { token : gitlabToken , host : gitlabUrl } )
72+ this . gitlabProjectApi = gitlabProjectApi
4873 }
4974
5075 private async findOrCreateRepo ( ) : Promise < ProjectSchema > {
5176 try {
5277 // Find or create parent Gitlab group
5378 const groups = await this . gitlabApi . Groups . search ( groupName )
5479 let group = groups . find ( g => g . full_path === groupName || g . name === groupName )
55- if ( ! group ) {
80+ if ( ! group ) {
5681 group = await this . gitlabApi . Groups . create ( groupName , groupName )
5782 }
5883 // Find or create parent Gitlab repository
@@ -119,16 +144,33 @@ export class ObservabilityRepoManager {
119144 }
120145
121146 public async updateProjectConfig ( project : Project , projectValue : ObservabilityProject ) : Promise < string > {
122- // Déplacer toute la logique de création ou de récupération de groupe et de repo ici
147+ // Repository created during 'pre' step if needed
148+ const projectId = await this . gitlabProjectApi . getProjectId ( observabilityRepository )
149+ const observabilityProjectRepository = await this . gitlabProjectApi . getProjectById ( projectId )
150+
151+ // Add or update chart files
152+ const chartUpdated = await this . gitlabProjectApi . commitCreateOrUpdate (
153+ observabilityProjectRepository . id ,
154+ observabilityChartContent ,
155+ 'Chart.yaml' ,
156+ )
157+ const templateUpdated = await this . gitlabProjectApi . commitCreateOrUpdate (
158+ observabilityProjectRepository . id ,
159+ observabilityTemplateContent ,
160+ 'templates/includes.yaml' ,
161+ )
162+
163+ // Dépôt d'infra scruté par ArgoCD (charts dso-grafana et dso-observatorium)
123164 const gitlabRepo = await this . findOrCreateRepo ( )
124165
125166 // Récupérer le fichier values.yaml
126- const yamlFile = await this . getValuesFile ( gitlabRepo )
167+ const yamlFile = await this . getValuesFile ( gitlabRepo )
127168 || yaml . load ( Buffer . from ( yamlInitData , 'base64' ) . toString ( 'utf-8' ) ) as ObservabilityData
128169
129170 const projects = yamlFile . global ?. projects || { }
130171
131- if ( JSON . stringify ( projects [ project . id ] ) === JSON . stringify ( projectValue ) ) {
172+ if ( ! chartUpdated && ! templateUpdated
173+ && JSON . stringify ( projects [ project . id ] ) === JSON . stringify ( projectValue ) ) {
132174 return 'Already up-to-date'
133175 }
134176
0 commit comments