Skip to content

Commit

Permalink
fix: 🚑 internal gitlab url
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudTA committed Nov 8, 2024
1 parent 48c0a92 commit 16f195e
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 71 deletions.
26 changes: 0 additions & 26 deletions apps/client/src/views/admin/AdminProject.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,6 @@ TimeAgo.addLocale(fr)
// Create relative date/time formatter.
const timeAgo = new TimeAgo('fr-FR')
// const repoRows = computed(() => {
// if (!project.value.repositories?.length) {
// return [[{
// text: 'Aucun dépôt existant',
// cellAttrs: {
// colspan: headerRepos.length,
// },
// }]]
// }
// return sortArrByObjKeyAsc(project.value.repositories, 'internalRepoName')
// .map(({ internalRepoName, isInfra, externalRepoUrl, isPrivate, createdAt }) => (
// [
// internalRepoName,
// isInfra ? 'Infra' : 'Applicatif',
// isPrivate ? 'oui' : 'non',
// externalRepoUrl || '-',
// {
// text: timeAgo.format(new Date(createdAt)),
// title: (new Date(createdAt)).toLocaleString(),
// component: 'span',
// },
// ]
// ),
// )
// })
function unSelectProject() {
router.push({ name: 'ListProjects' })
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/gitlab/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@cpn-console/gitlab-plugin",
"type": "module",
"version": "2.4.0",
"version": "2.4.1",
"private": false,
"description": "",
"main": "dist/index.js",
Expand Down
7 changes: 4 additions & 3 deletions plugins/gitlab/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { VaultProjectApi } from '@cpn-console/vault-plugin/types/class.js'
import { objectEntries } from '@cpn-console/shared'
import type { GitbeakerRequestError } from '@gitbeaker/requester-utils'
import { getOrganizationId } from './group.js'
import { getApi, getConfig, getGroupRootId, infraAppsRepoName, internalMirrorRepoName } from './utils.js'
import { getApi, getGroupRootId, infraAppsRepoName, internalMirrorRepoName } from './utils.js'
import config from './config.js'

type setVariableResult = 'created' | 'updated' | 'already up-to-date'
type AccessLevelAllowed = AccessLevel.NO_ACCESS | AccessLevel.MINIMAL_ACCESS | AccessLevel.GUEST | AccessLevel.REPORTER | AccessLevel.DEVELOPER | AccessLevel.MAINTAINER | AccessLevel.OWNER
Expand Down Expand Up @@ -118,7 +119,7 @@ export class GitlabProjectApi extends PluginApi {
}

public async getProjectId(projectName: string) {
const pathProjectName = `${getConfig().projectsRootDir}/${this.project.organization.name}/${this.project.name}/${projectName}`
const pathProjectName = `${config().projectsRootDir}/${this.project.organization.name}/${this.project.name}/${projectName}`
const project = (await this.api.Projects.search(projectName)).find(p => p.path_with_namespace === pathProjectName)

if (!project) {
Expand Down Expand Up @@ -183,7 +184,7 @@ export class GitlabProjectApi extends PluginApi {

// Repositories
public async getRepoUrl(repoName: string) {
return `${getConfig().url}/${getConfig().projectsRootDir}/${this.project.organization.name}/${this.project.name}/${repoName}.git`
return `${config().publicUrl}/${config().projectsRootDir}/${this.project.organization.name}/${this.project.name}/${repoName}.git`
}

public async listRepositories() {
Expand Down
26 changes: 26 additions & 0 deletions plugins/gitlab/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { removeTrailingSlash, requiredEnv } from '@cpn-console/shared'

class Config {
publicUrl: string
internalUrl: string
token: string
projectsRootDir: string
constructor() {
this.token = requiredEnv('GITLAB_TOKEN')
this.publicUrl = removeTrailingSlash(requiredEnv('GITLAB_URL'))
this.projectsRootDir = requiredEnv('PROJECTS_ROOT_DIR')
this.internalUrl = process.env.GITLAB_INTERNAL_URL
? removeTrailingSlash(process.env.GITLAB_INTERNAL_URL)
: this.publicUrl
}
}

let config: Config | undefined

function getConfig() {
if (!config) {
config = new Config()
}
return config
}
export default getConfig
5 changes: 3 additions & 2 deletions plugins/gitlab/src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { createUsername, getUser } from './user.js'
import { ensureMembers } from './members.js'
import { ensureRepositories } from './repositories.js'
import type { VaultSecrets } from './utils.js'
import { cleanGitlabError, getConfig } from './utils.js'
import { cleanGitlabError } from './utils.js'
import config from './config.js'

// Check
export const checkApi: StepCall<Project> = async (payload) => {
Expand Down Expand Up @@ -53,7 +54,7 @@ export const getDsoProjectSecrets: StepCall<ProjectLite> = async (payload) => {
'-F ref=main',
'-F variables[GIT_BRANCH_DEPLOY]=\${BRANCH_TO_SYNC}',
'-F variables[PROJECT_NAME]=\${REPOSITORY_NAME}',
`"${getConfig().url}/api/v4/projects/${gitlab.GIT_MIRROR_PROJECT_ID}/trigger/pipeline"`,
`"${config().publicUrl}/api/v4/projects/${gitlab.GIT_MIRROR_PROJECT_ID}/trigger/pipeline"`,
]
/* eslint-enable */
const secrets: Record<string, string> = {
Expand Down
5 changes: 4 additions & 1 deletion plugins/gitlab/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import { GitlabProjectApi } from './class.js'
const onlyApi = { api: (project: Project | UniqueRepo) => new GitlabProjectApi(project) }

function start() {
getGroupRootId()
getGroupRootId().catch((error) => {
console.log(error)
throw new Error('Error at gitlab plugin start')
})
}

export const plugin: Plugin = {
Expand Down
4 changes: 2 additions & 2 deletions plugins/gitlab/src/infos.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { ServiceInfos } from '@cpn-console/hooks'
import { ENABLED } from '@cpn-console/shared'
import { getConfig } from './utils.js'
import config from './config.js'

const infos = {
name: 'gitlab',
to: ({ project, organization }) => `${getConfig().url}/${getConfig().projectsRootDir}/${organization}/${project}`,
to: ({ project, organization }) => `${config().publicUrl}/${config().projectsRootDir}/${organization}/${project}`,
title: 'Gitlab',
imgSrc: '/img/gitlab.svg',
description: 'GitLab est un service d\'hébergement de code source et de pipeline CI/CD',
Expand Down
4 changes: 2 additions & 2 deletions plugins/gitlab/src/monitor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Monitor, type MonitorInfos, MonitorStatus } from '@cpn-console/shared'
import axios from 'axios'
import { getConfig } from './utils.js'
import config from './config.js'

enum HealthStatus {
failed = 'failed',
Expand All @@ -12,7 +12,7 @@ const coreComponents = ['gitaly_check', 'master_check', 'db_check', 'sessions_ch
async function monitor(instance: Monitor): Promise<MonitorInfos> {
instance.lastStatus.lastUpdateTimestamp = (new Date()).getTime()
try {
const res = await axios.get(`${getConfig().internalUrl}/-/readiness?all=1`, {
const res = await axios.get(`${config().publicUrl}/-/readiness?all=1`, {
validateStatus: res => res === 200,
})
if (res.status === 200) { // 200 only means api responds
Expand Down
37 changes: 3 additions & 34 deletions plugins/gitlab/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import { Gitlab } from '@gitbeaker/rest'
import type { Gitlab as IGitlab } from '@gitbeaker/core'
import { removeTrailingSlash, requiredEnv } from '@cpn-console/shared'
import { GitbeakerRequestError } from '@gitbeaker/requester-utils'
import config from './config.js'

let api: IGitlab | undefined

let groupRootId: number | void

const config: {
token?: string
url?: string
internaUrl?: string
projectsRootDir?: string
} = {
token: undefined,
url: undefined,
projectsRootDir: undefined,
}

export async function getGroupRootId(): Promise<number> {
const gitlabApi = getApi()
const projectRootDir = getConfig().projectsRootDir
const projectRootDir = config().projectsRootDir
if (groupRootId) return groupRootId
const groupRootSearch = await gitlabApi.Groups.search(projectRootDir)
groupRootId = (groupRootSearch.find(grp => grp.full_path === projectRootDir))?.id
Expand All @@ -32,31 +21,11 @@ export async function getGroupRootId(): Promise<number> {

export function getApi(): IGitlab {
if (!api) {
const gitlabUrl = getConfig().internalUrl
const gitlabToken = requiredEnv('GITLAB_TOKEN')
// @ts-ignore
api = new Gitlab({ token: gitlabToken, host: gitlabUrl })
api = new Gitlab({ token: config().token, host: config().internalUrl })
}
// @ts-ignore
return api
}

export function getConfig(): {
token: string
url: string
internalUrl?: string
projectsRootDir: string
} {
if (!config.projectsRootDir || !config.token || !config.url || !config.internaUrl) {
config.token = requiredEnv('GITLAB_TOKEN')
config.url = removeTrailingSlash(requiredEnv('GITLAB_URL'))
config.internaUrl = process.env.GITLAB_INTERNAL_URL ? removeTrailingSlash(process.env.GITLAB_INTERNAL_URL) : config.url
config.projectsRootDir = requiredEnv('PROJECTS_ROOT_DIR')
}
// @ts-ignore trouver un meilleur softboot
return config
}

export const infraAppsRepoName = 'infra-apps'
export const internalMirrorRepoName = 'mirror'

Expand Down

0 comments on commit 16f195e

Please sign in to comment.