From df8b22cec3f49736f43611f34215d677511e4b9d Mon Sep 17 00:00:00 2001 From: Vladyslav Palyvoda Date: Fri, 20 Dec 2024 14:57:05 +0200 Subject: [PATCH] feat: Update build git-source-url (#532) --- .../index.test.ts | 459 +++++++++++++----- .../createBuildPipelineRunInstance/index.ts | 23 +- .../createCleanPipelineRunInstance/index.ts | 3 +- .../createDeployPipelineRunInstance/index.ts | 3 +- .../components/Summary/index.tsx | 44 +- .../components/fields/Name/index.tsx | 4 + 6 files changed, 396 insertions(+), 140 deletions(-) diff --git a/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.test.ts b/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.test.ts index 091beb4d..49575a6d 100644 --- a/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.test.ts +++ b/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.test.ts @@ -2,6 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { CodebaseKubeObjectInterface } from '../../../../EDP/Codebase/types'; import { CodebaseBranchKubeObjectInterface } from '../../../../EDP/CodebaseBranch/types'; import { GitServerKubeObjectInterface } from '../../../../EDP/GitServer/types'; +import { PipelineRunKubeObjectInterface } from '../../types'; import { createBuildPipelineRunInstance } from './index'; jest.mock('uuid', () => ({ @@ -11,136 +12,360 @@ jest.mock('uuid', () => ({ const MOCKED_UUID = '1234'; (uuidv4 as jest.Mock).mockReturnValue(MOCKED_UUID); +const mockCodebase = { + metadata: { name: 'test-codebase-name' }, + spec: { + buildTool: 'test-build-tool', + type: 'application', + framework: 'test-framework', + gitUrlPath: '/test-git-url-path', + versioning: { + type: 'test-versioning-type', + }, + gitServer: 'gerrit', + }, +} as CodebaseKubeObjectInterface; + +const mockCodebaseBranch = { + metadata: { + name: 'test-codebase-name-test-codebase-branch-name', + }, + spec: { + branchName: 'test-codebase-branch-name', + pipelines: { + build: 'test-build-pipeline', + review: 'test-review-pipeline', + }, + }, +} as CodebaseBranchKubeObjectInterface; + +const mockPipelineRunTemplate = { + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + // @ts-ignore + metadata: { + annotations: { + 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous', + }, + generateName: '$(tt.params.codebasebranch)-build-', + labels: { + 'app.edp.epam.com/codebase': '$(tt.params.codebase)', + 'app.edp.epam.com/codebasebranch': '$(tt.params.codebasebranch)', + 'app.edp.epam.com/pipelinetype': 'build', + }, + }, + spec: { + params: [ + { + name: 'git-source-url', + value: 'ssh://git-user@gitProvider:777/$(tt.params.gerritproject)', + }, + { + name: 'git-source-revision', + value: '$(tt.params.gitrevision)', + }, + { + name: 'CODEBASE_NAME', + value: '$(tt.params.codebase)', + }, + { + name: 'CODEBASEBRANCH_NAME', + value: '$(tt.params.codebasebranch)', + }, + { + name: 'changeNumber', + value: '$(tt.params.changeNumber)', + }, + { + name: 'patchsetNumber', + value: '$(tt.params.patchsetNumber)', + }, + { + name: 'TICKET_NAME_PATTERN', + value: '$(tt.params.ticketNamePattern)', + }, + { + name: 'COMMIT_MESSAGE_PATTERN', + value: '$(tt.params.commitMessagePattern)', + }, + { + name: 'COMMIT_MESSAGE', + value: '$(tt.params.commitMessage)', + }, + { + name: 'JIRA_ISSUE_METADATA_PAYLOAD', + value: '$(tt.params.jiraIssueMetadataPayload)', + }, + { + name: 'JIRA_SERVER', + value: '$(tt.params.jiraServer)', + }, + { + name: 'gitfullrepositoryname', + value: '$(tt.params.gitfullrepositoryname)', + }, + ], + pipelineRef: { + name: '$(tt.params.pipelineName)', + }, + taskRunTemplate: { + serviceAccountName: 'tekton', + }, + workspaces: [ + { + name: 'shared-workspace', + subPath: 'codebase', + volumeClaimTemplate: { + spec: { + accessModes: ['ReadWriteOnce'], + resources: { + requests: { + storage: '5Gi', + }, + }, + }, + }, + }, + { + name: 'ssh-creds', + secret: { + secretName: 'secretName', + }, + }, + ], + }, +} as unknown as PipelineRunKubeObjectInterface; + describe('testing createBuildPipelineRunInstance', () => { - it('should return valid kube object', () => { + it('should return valid build pipelinerun for Gerrit GitServer', () => { const object = createBuildPipelineRunInstance({ - codebase: { - metadata: { name: 'test-codebase-name' }, + codebase: mockCodebase, + codebaseBranch: mockCodebaseBranch, + pipelineRunTemplate: mockPipelineRunTemplate, + gitServer: { spec: { - buildTool: 'test-build-tool', - type: 'application', - framework: 'test-framework', - gitUrlPath: '/test-git-url-path', - versioning: { - type: 'test-versioning-type', - }, + gitHost: 'test-git-host', + gitUser: 'test-git-user', + sshPort: 123, }, - } as CodebaseKubeObjectInterface, - codebaseBranch: { - metadata: { - name: 'test-codebase-name-test-codebase-branch-name', + } as GitServerKubeObjectInterface, + }); + + expect(object).toEqual({ + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + metadata: { + annotations: { 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous' }, + name: `test-codebase-name-test-codebase-branch-name-build-${MOCKED_UUID}`, + labels: { + 'app.edp.epam.com/codebase': 'test-codebase-name', + 'app.edp.epam.com/codebasebranch': 'test-codebase-name-test-codebase-branch-name', + 'app.edp.epam.com/pipelinetype': 'build', }, - spec: { - branchName: 'test-codebase-branch-name', - pipelines: { - build: 'test-build-pipeline', - review: 'test-review-pipeline', + }, + spec: { + params: [ + { + name: 'git-source-url', + value: 'ssh://test-git-user@test-git-host:123/test-git-url-path', }, - }, - } as CodebaseBranchKubeObjectInterface, - pipelineRunTemplate: { - apiVersion: 'tekton.dev/v1', - kind: 'PipelineRun', - // @ts-ignore - metadata: { - annotations: { - 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous', - }, - generateName: '$(tt.params.codebasebranch)-build-', - labels: { - 'app.edp.epam.com/codebase': '$(tt.params.codebase)', - 'app.edp.epam.com/codebasebranch': '$(tt.params.codebasebranch)', - 'app.edp.epam.com/pipelinetype': 'build', + { + name: 'git-source-revision', + value: 'test-codebase-branch-name', }, - }, - spec: { - params: [ - { - name: 'git-source-url', - value: 'ssh://git-user@gitProvider:777/$(tt.params.gerritproject)', - }, - { - name: 'git-source-revision', - value: '$(tt.params.gitrevision)', - }, - { - name: 'CODEBASE_NAME', - value: '$(tt.params.codebase)', - }, - { - name: 'CODEBASEBRANCH_NAME', - value: '$(tt.params.codebasebranch)', - }, - { - name: 'changeNumber', - value: '$(tt.params.changeNumber)', - }, - { - name: 'patchsetNumber', - value: '$(tt.params.patchsetNumber)', - }, - { - name: 'TICKET_NAME_PATTERN', - value: '$(tt.params.ticketNamePattern)', - }, - { - name: 'COMMIT_MESSAGE_PATTERN', - value: '$(tt.params.commitMessagePattern)', - }, - { - name: 'COMMIT_MESSAGE', - value: '$(tt.params.commitMessage)', - }, - { - name: 'JIRA_ISSUE_METADATA_PAYLOAD', - value: '$(tt.params.jiraIssueMetadataPayload)', - }, - { - name: 'JIRA_SERVER', - value: '$(tt.params.jiraServer)', - }, - { - name: 'gitfullrepositoryname', - value: '$(tt.params.gitfullrepositoryname)', - }, - ], - pipelineRef: { - name: '$(tt.params.pipelineName)', - }, - taskRunTemplate: { - serviceAccountName: 'tekton', - }, - workspaces: [ - { - name: 'shared-workspace', - subPath: 'codebase', - volumeClaimTemplate: { - spec: { - accessModes: ['ReadWriteOnce'], - resources: { - requests: { - storage: '5Gi', - }, - }, - }, + { name: 'CODEBASE_NAME', value: 'test-codebase-name' }, + { + name: 'CODEBASEBRANCH_NAME', + value: 'test-codebase-name-test-codebase-branch-name', + }, + { name: 'changeNumber', value: '1' }, + { name: 'patchsetNumber', value: '1' }, + { name: 'TICKET_NAME_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE', value: '' }, + { name: 'JIRA_ISSUE_METADATA_PAYLOAD', value: '' }, + { name: 'JIRA_SERVER', value: '' }, + { name: 'gitfullrepositoryname', value: 'test-git-url-path' }, + ], + pipelineRef: { name: 'test-build-pipeline' }, + taskRunTemplate: { serviceAccountName: 'tekton' }, + workspaces: [ + { + name: 'shared-workspace', + subPath: 'codebase', + volumeClaimTemplate: { + spec: { + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: '5Gi' } }, }, }, - { - name: 'ssh-creds', - secret: { - secretName: 'secretName', + }, + { name: 'ssh-creds', secret: { secretName: 'secretName' } }, + { + name: 'settings', + configMap: { name: 'custom-test-build-tool-settings' }, + }, + ], + }, + }); + }); + + it('should return valid build pipelinerun for Github GitServer', () => { + const object = createBuildPipelineRunInstance({ + codebase: mockCodebase, + codebaseBranch: mockCodebaseBranch, + pipelineRunTemplate: mockPipelineRunTemplate, + gitServer: { + spec: { + gitHost: 'github.com', + gitUser: 'git', + sshPort: 22, + }, + } as GitServerKubeObjectInterface, + }); + + expect(object).toEqual({ + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + metadata: { + annotations: { 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous' }, + labels: { + 'app.edp.epam.com/codebase': 'test-codebase-name', + 'app.edp.epam.com/codebasebranch': 'test-codebase-name-test-codebase-branch-name', + 'app.edp.epam.com/pipelinetype': 'build', + }, + name: 'test-codebase-name-test-codebase-branch-name-build-1234', + }, + spec: { + params: [ + { + name: 'git-source-url', + value: 'ssh://git@github.com:22/test-git-url-path', + }, + { + name: 'git-source-revision', + value: 'test-codebase-branch-name', + }, + { name: 'CODEBASE_NAME', value: 'test-codebase-name' }, + { + name: 'CODEBASEBRANCH_NAME', + value: 'test-codebase-name-test-codebase-branch-name', + }, + { name: 'changeNumber', value: '1' }, + { name: 'patchsetNumber', value: '1' }, + { name: 'TICKET_NAME_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE', value: '' }, + { name: 'JIRA_ISSUE_METADATA_PAYLOAD', value: '' }, + { name: 'JIRA_SERVER', value: '' }, + { name: 'gitfullrepositoryname', value: 'test-git-url-path' }, + ], + pipelineRef: { name: 'test-build-pipeline' }, + taskRunTemplate: { serviceAccountName: 'tekton' }, + workspaces: [ + { + name: 'shared-workspace', + subPath: 'codebase', + volumeClaimTemplate: { + spec: { + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: '5Gi' } }, }, }, - ], + }, + { name: 'ssh-creds', secret: { secretName: 'secretName' } }, + { + name: 'settings', + configMap: { name: 'custom-test-build-tool-settings' }, + }, + ], + }, + }); + }); + + it('should return valid build pipelinerun for Gitlab GitServer', () => { + const object = createBuildPipelineRunInstance({ + codebase: mockCodebase, + codebaseBranch: mockCodebaseBranch, + pipelineRunTemplate: mockPipelineRunTemplate, + gitServer: { + spec: { + gitHost: 'git.epam.com', + gitUser: 'git', + sshPort: 22, }, + } as GitServerKubeObjectInterface, + }); + + expect(object).toEqual({ + apiVersion: 'tekton.dev/v1', + kind: 'PipelineRun', + metadata: { + annotations: { 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous' }, + labels: { + 'app.edp.epam.com/codebase': 'test-codebase-name', + 'app.edp.epam.com/codebasebranch': 'test-codebase-name-test-codebase-branch-name', + 'app.edp.epam.com/pipelinetype': 'build', + }, + name: 'test-codebase-name-test-codebase-branch-name-build-1234', }, + spec: { + params: [ + { + name: 'git-source-url', + value: 'ssh://git@git.epam.com:22/test-git-url-path', + }, + { + name: 'git-source-revision', + value: 'test-codebase-branch-name', + }, + { name: 'CODEBASE_NAME', value: 'test-codebase-name' }, + { + name: 'CODEBASEBRANCH_NAME', + value: 'test-codebase-name-test-codebase-branch-name', + }, + { name: 'changeNumber', value: '1' }, + { name: 'patchsetNumber', value: '1' }, + { name: 'TICKET_NAME_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE_PATTERN', value: '' }, + { name: 'COMMIT_MESSAGE', value: '' }, + { name: 'JIRA_ISSUE_METADATA_PAYLOAD', value: '' }, + { name: 'JIRA_SERVER', value: '' }, + { name: 'gitfullrepositoryname', value: 'test-git-url-path' }, + ], + pipelineRef: { name: 'test-build-pipeline' }, + taskRunTemplate: { serviceAccountName: 'tekton' }, + workspaces: [ + { + name: 'shared-workspace', + subPath: 'codebase', + volumeClaimTemplate: { + spec: { + accessModes: ['ReadWriteOnce'], + resources: { requests: { storage: '5Gi' } }, + }, + }, + }, + { name: 'ssh-creds', secret: { secretName: 'secretName' } }, + { + name: 'settings', + configMap: { name: 'custom-test-build-tool-settings' }, + }, + ], + }, + }); + }); + + it('should return valid build pipelinerun for Bitbucket GitServer', () => { + const object = createBuildPipelineRunInstance({ + codebase: mockCodebase, + codebaseBranch: mockCodebaseBranch, + pipelineRunTemplate: mockPipelineRunTemplate, gitServer: { spec: { - gitHost: 'test-git-host', - gitUser: 'test-git-user', - sshPort: 123, - nameSshKeySecret: 'test-ssh-key-secret', - gitProvider: 'test-git-provider', + gitHost: 'bitbucket.org', + gitUser: 'git', + sshPort: 22, }, } as GitServerKubeObjectInterface, }); @@ -150,18 +375,18 @@ describe('testing createBuildPipelineRunInstance', () => { kind: 'PipelineRun', metadata: { annotations: { 'argocd.argoproj.io/compare-options': 'IgnoreExtraneous' }, - name: `test-codebase-name-test-codebase-branch-name-build-${MOCKED_UUID}`, labels: { 'app.edp.epam.com/codebase': 'test-codebase-name', 'app.edp.epam.com/codebasebranch': 'test-codebase-name-test-codebase-branch-name', 'app.edp.epam.com/pipelinetype': 'build', }, + name: 'test-codebase-name-test-codebase-branch-name-build-1234', }, spec: { params: [ { name: 'git-source-url', - value: 'ssh://test-git-user@test-git-host:123/test-git-url-path', + value: 'ssh://git@bitbucket.org:22/test-git-url-path', }, { name: 'git-source-revision', diff --git a/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.ts b/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.ts index 655ceab6..60e41dcc 100644 --- a/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.ts +++ b/src/k8s/groups/Tekton/PipelineRun/utils/createBuildPipelineRunInstance/index.ts @@ -1,3 +1,5 @@ +import { cloneDeep } from 'lodash'; +import { GIT_PROVIDERS } from '../../../../../../constants/gitProviders'; import { PIPELINE_TYPES } from '../../../../../../constants/pipelineTypes'; import { createRandomString } from '../../../../../../utils/createRandomString'; import { truncateName } from '../../../../../../utils/truncateName'; @@ -24,7 +26,11 @@ export const createBuildPipelineRunInstance = ({ }): PipelineRunKubeObjectInterface => { const { metadata: { name: codebaseName }, - spec: { gitUrlPath: codebaseGitUrlPath, buildTool: codebaseBuildTool }, + spec: { + gitUrlPath: codebaseGitUrlPath, + buildTool: codebaseBuildTool, + gitServer: codebaseGitServer, + }, } = codebase; const { @@ -36,7 +42,7 @@ export const createBuildPipelineRunInstance = ({ spec: { gitUser, gitHost, sshPort }, } = gitServer; - const base = { ...pipelineRunTemplate }; + const base = cloneDeep(pipelineRunTemplate); const pipelineRunPostfix = `-build-${createRandomString(4)}`; @@ -65,10 +71,17 @@ export const createBuildPipelineRunInstance = ({ }, ]; + const gitUrlPathWithoutSlashAtStart = codebaseGitUrlPath.startsWith('/') + ? codebaseGitUrlPath.slice(1) + : codebaseGitUrlPath; + for (const param of base.spec.params) { switch (param.name) { case 'git-source-url': - param.value = `ssh://${gitUser}@${gitHost}:${sshPort}${codebaseGitUrlPath}`; + param.value = + codebaseGitServer === GIT_PROVIDERS.GERRIT + ? `ssh://${gitUser}@${gitHost}:${sshPort}/${gitUrlPathWithoutSlashAtStart}` + : `${gitUser}@${gitHost}:${gitUrlPathWithoutSlashAtStart}`; break; case 'git-source-revision': param.value = codebaseBranchName; @@ -101,9 +114,7 @@ export const createBuildPipelineRunInstance = ({ param.value = codebase.spec.jiraServer ?? ''; break; case 'gitfullrepositoryname': - param.value = codebaseGitUrlPath.startsWith('/') - ? codebaseGitUrlPath.slice(1) - : codebaseGitUrlPath ?? ''; + param.value = gitUrlPathWithoutSlashAtStart; break; default: break; diff --git a/src/k8s/groups/Tekton/PipelineRun/utils/createCleanPipelineRunInstance/index.ts b/src/k8s/groups/Tekton/PipelineRun/utils/createCleanPipelineRunInstance/index.ts index cdb08528..f2156606 100644 --- a/src/k8s/groups/Tekton/PipelineRun/utils/createCleanPipelineRunInstance/index.ts +++ b/src/k8s/groups/Tekton/PipelineRun/utils/createCleanPipelineRunInstance/index.ts @@ -1,3 +1,4 @@ +import { cloneDeep } from 'lodash'; import { PIPELINE_TYPES } from '../../../../../../constants/pipelineTypes'; import { createRandomString } from '../../../../../../utils/createRandomString'; import { truncateName } from '../../../../../../utils/truncateName'; @@ -19,7 +20,7 @@ export const createCleanPipelineRunInstance = ({ stage: StageKubeObjectInterface; pipelineRunTemplate: PipelineRunKubeObjectInterface; }): PipelineRunKubeObjectInterface => { - const base = { ...pipelineRunTemplate }; + const base = cloneDeep(pipelineRunTemplate); const generateName = `clean-${CDPipeline.metadata.name}-${stage.spec.name}`; const namePostfix = `-${createRandomString(4)}`; diff --git a/src/k8s/groups/Tekton/PipelineRun/utils/createDeployPipelineRunInstance/index.ts b/src/k8s/groups/Tekton/PipelineRun/utils/createDeployPipelineRunInstance/index.ts index 306a49f4..7bd8b4e7 100644 --- a/src/k8s/groups/Tekton/PipelineRun/utils/createDeployPipelineRunInstance/index.ts +++ b/src/k8s/groups/Tekton/PipelineRun/utils/createDeployPipelineRunInstance/index.ts @@ -1,3 +1,4 @@ +import { cloneDeep } from 'lodash'; import { PIPELINE_TYPES } from '../../../../../../constants/pipelineTypes'; import { createRandomString } from '../../../../../../utils/createRandomString'; import { truncateName } from '../../../../../../utils/truncateName'; @@ -27,7 +28,7 @@ export const createDeployPipelineRunInstance = ({ } >; }): PipelineRunKubeObjectInterface => { - const base = { ...pipelineRunTemplate }; + const base = cloneDeep(pipelineRunTemplate); const generateName = `deploy-${CDPipeline.metadata.name}-${stage.spec.name}`; const namePostfix = `-${createRandomString(4)}`; diff --git a/src/pages/component-details/components/CodebaseBranchesList/components/CodebaseBranch/components/Summary/index.tsx b/src/pages/component-details/components/CodebaseBranchesList/components/CodebaseBranch/components/Summary/index.tsx index 2f78c80d..5e773973 100644 --- a/src/pages/component-details/components/CodebaseBranchesList/components/CodebaseBranch/components/Summary/index.tsx +++ b/src/pages/component-details/components/CodebaseBranchesList/components/CodebaseBranch/components/Summary/index.tsx @@ -1,4 +1,4 @@ -import { Box, Chip, Grid, Stack, Typography, useTheme } from '@mui/material'; +import { Box, Chip, Grid, Stack, Tooltip, Typography, useTheme } from '@mui/material'; import clsx from 'clsx'; import React from 'react'; import { QuickLink } from '../../../../../../../../components/QuickLink'; @@ -47,6 +47,7 @@ export const Summary = ({ codebaseBranchData, pipelineRuns }: SummaryProps) => { direction="row" width={'100%'} justifyContent="space-between" + flexWrap="nowrap" > { } /> - - {codebaseBranchData.spec.branchName} - + + + {codebaseBranchData.spec.branchName} + + {isDefaultBranch(codebaseData, codebaseBranchData) && ( { Version: - + + + ) : null} - + { }, setValueAs: (value: string) => normalizeNameValue(value), onChange: onChange, + maxLength: { + value: 30, + message: `Component name must be less than 30 characters long`, + }, })} label={`Component name`} title={'Provide a clear and concise name for your component.'}