Skip to content

Commit a13ee2b

Browse files
committed
refactor: VariableDataTable - move common code & global variables service to common-lib
1 parent c5e56c8 commit a13ee2b

File tree

10 files changed

+168
-4
lines changed

10 files changed

+168
-4
lines changed

src/Assets/Icon/ic-var-initial.svg

Lines changed: 25 additions & 0 deletions
Loading

src/Common/Common.service.ts

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { MutableRefObject } from 'react'
1718
import moment from 'moment'
1819
import { RuntimeParamsAPIResponseType, RuntimeParamsListItemType } from '@Shared/types'
1920
import { getIsManualApprovalSpecific, sanitizeUserApprovalConfig, stringComparatorBySortOrder } from '@Shared/Helpers'
20-
import { get, post } from './Api'
21+
import { get, getIsRequestAborted, post } from './Api'
2122
import { GitProviderType, ROUTES } from './Constants'
22-
import { getUrlWithSearchParams, sortCallback } from './Helper'
23+
import { getUrlWithSearchParams, showError, sortCallback } from './Helper'
2324
import {
2425
TeamList,
2526
ResponseType,
@@ -41,10 +42,15 @@ import {
4142
UserApprovalMetadataType,
4243
UserApprovalConfigType,
4344
CDMaterialListModalServiceUtilProps,
45+
GlobalVariableDTO,
46+
GlobalVariableOptionType,
4447
} from './Types'
4548
import { ApiResourceType } from '../Pages'
4649
import { API_TOKEN_PREFIX } from '@Shared/constants'
4750
import { DefaultUserKey } from '@Shared/types'
51+
import { RefVariableType } from './CIPipeline.Types'
52+
import { ServerErrors } from './ServerError'
53+
import { ToastManager, ToastVariantType } from '@Shared/Services'
4854

4955
export const getTeamListMin = (): Promise<TeamList> => {
5056
// ignore active field
@@ -530,3 +536,46 @@ export const getGitBranchUrl = (gitUrl: string, branchName: string): string | nu
530536
else if (trimmedGitUrl.includes(GitProviderType.AZURE)) return `${trimmedGitUrl}/src/branch/${branchName}`
531537
return null
532538
}
539+
540+
export const getGlobalVariables = async ({
541+
appId,
542+
isCD = false,
543+
abortControllerRef,
544+
}: {
545+
appId: number
546+
isCD?: boolean
547+
abortControllerRef?: MutableRefObject<AbortController>
548+
}): Promise<GlobalVariableOptionType[]> => {
549+
try {
550+
const { result } = await get<GlobalVariableDTO[]>(`${ROUTES.PLUGIN_GLOBAL_VARIABLES}?appId=${appId}`, {
551+
abortControllerRef,
552+
})
553+
const variableList = (result ?? [])
554+
.filter((item) => (isCD ? item.stageType !== 'ci' : item.stageType === 'ci'))
555+
.map((variable) => {
556+
const { name, ...updatedVariable } = { ...variable }
557+
558+
return {
559+
...updatedVariable,
560+
label: name,
561+
value: name,
562+
description: updatedVariable.description || '',
563+
variableType: RefVariableType.GLOBAL,
564+
}
565+
})
566+
567+
return variableList
568+
} catch (err) {
569+
if (!getIsRequestAborted(err)) {
570+
if (err instanceof ServerErrors && err.code === 403) {
571+
ToastManager.showToast({
572+
variant: ToastVariantType.notAuthorized,
573+
description: 'You are not authorized to access global variables',
574+
})
575+
} else {
576+
showError(err)
577+
}
578+
}
579+
throw err
580+
}
581+
}

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export const ROUTES = {
117117
CONFIG_DATA: 'config/data',
118118
K8S_RESOURCE_LIST: 'k8s/resource/list',
119119
FILE_UPLOAD: 'file/upload',
120+
PLUGIN_GLOBAL_VARIABLES: 'plugin/global/list/global-variable',
120121
}
121122

122123
export enum KEY_VALUE {

src/Common/Helper.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,10 @@ export const getIframeWithDefaultAttributes = (iframeString: string, defaultName
10311031

10321032
return iframeString
10331033
}
1034+
1035+
export const getGoLangFormattedDateWithTimezone = (dateFormat: string) => {
1036+
const now = moment()
1037+
const formattedDate = now.format(dateFormat)
1038+
const timezone = now.format('Z').replace(/([+/-])(\d{2})[:.](\d{2})/, '$1$2$3')
1039+
return formattedDate.replace('Z', timezone)
1040+
}

src/Common/Types.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,19 @@ import { TippyProps } from '@tippyjs/react'
1919
import { Placement } from 'tippy.js'
2020
import { UserGroupDTO } from '@Pages/GlobalConfigurations'
2121
import { ImageComment, ReleaseTag } from './ImageTags.Types'
22-
import { MandatoryPluginBaseStateType, RegistryType, RuntimePluginVariables, Severity } from '../Shared'
22+
import {
23+
MandatoryPluginBaseStateType,
24+
RegistryType,
25+
RuntimePluginVariables,
26+
Severity,
27+
} from '../Shared'
2328
import {
2429
ACTION_STATE,
25-
ConsequenceType,
2630
DEPLOYMENT_WINDOW_TYPE,
2731
DockerConfigOverrideType,
2832
SortingOrder,
2933
TaskErrorObj,
34+
VariableTypeFormat,
3035
} from '.'
3136

3237
/**
@@ -1014,3 +1019,15 @@ export interface WidgetEventDetails {
10141019
age: string
10151020
lastSeen: string
10161021
}
1022+
1023+
export interface GlobalVariableDTO {
1024+
name: string
1025+
format: VariableTypeFormat
1026+
description: string
1027+
stageType: 'cd' | 'post-cd' | 'ci'
1028+
}
1029+
1030+
export type GlobalVariableOptionType = Omit<GlobalVariableDTO, 'name'> & {
1031+
label: string
1032+
value: string
1033+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ReactComponent as Var } from '@Icons/ic-var-initial.svg'
2+
import { Tooltip } from '@Common/Tooltip'
3+
4+
const TIPPY_VAR_MSG = 'This is a variable. It will be replaced with the value during execution.'
5+
6+
export const SystemVariableIcon = () => (
7+
<Tooltip content={TIPPY_VAR_MSG} placement="left" animation="shift-away" alwaysShowTippyOnHover>
8+
<div className="flex">
9+
<Var className="icon-dim-18 icon-n4" />
10+
</div>
11+
</Tooltip>
12+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './SystemVariableIcon'
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { SelectPickerOptionType } from '@Shared/Components'
2+
3+
export const IO_VARIABLES_VALUE_COLUMN_BOOL_OPTIONS: SelectPickerOptionType<string>[] = [
4+
{ label: 'TRUE', value: 'TRUE' },
5+
{ label: 'FALSE', value: 'FALSE' },
6+
]
7+
8+
export const IO_VARIABLES_VALUE_COLUMN_DATE_OPTIONS: SelectPickerOptionType<string>[] = [
9+
{
10+
label: 'YYYY-MM-DD',
11+
value: 'YYYY-MM-DD',
12+
description: 'RFC 3339',
13+
},
14+
{
15+
label: 'YYYY-MM-DD HH:mm',
16+
value: 'YYYY-MM-DD HH:mm',
17+
description: 'RFC 3339 with minutes',
18+
},
19+
{
20+
label: 'YYYY-MM-DD HH:mm:ss',
21+
value: 'YYYY-MM-DD HH:mm:ss',
22+
description: 'RFC 3339 with seconds',
23+
},
24+
{
25+
label: 'YYYY-MM-DD HH:mm:ss-TZ',
26+
value: 'YYYY-MM-DD HH:mm:ssZ',
27+
description: 'RFC 3339 with seconds and timezone',
28+
},
29+
{
30+
label: "YYYY-MM-DDTHH'Z'ZZZZ",
31+
value: 'YYYY-MM-DDTHH[Z]',
32+
description: 'ISO8601 with hour',
33+
},
34+
{
35+
label: "YYYY-MM-DDTHH:mm'Z'ZZZZ",
36+
value: 'YYYY-MM-DDTHH:mm[Z]',
37+
description: 'ISO8601 with minutes',
38+
},
39+
{
40+
label: "YYYY-MM-DDTHH:mm:ss'Z'ZZZZ",
41+
value: 'YYYY-MM-DDTHH:mm:ss[Z]',
42+
description: 'ISO8601 with seconds',
43+
},
44+
{
45+
label: "YYYY-MM-DDTHH:mm:ss.SSSSSSSSS'Z'ZZZZ",
46+
value: 'YYYY-MM-DDTHH:mm:ss.SSSSSSSSS[Z]',
47+
description: 'ISO8601 with nanoseconds',
48+
},
49+
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export * from './services'
2+
export * from './constants'
3+
export * from './components'

src/Shared/Components/DynamicDataTable/DynamicDataTableRow.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export const DynamicDataTableRow = <K extends string, CustomStateType = Record<s
150150
refVar={cellRef?.current?.[row.id]?.[key]}
151151
dependentRefs={cellRef?.current?.[row.id]}
152152
selectPickerProps={{
153+
...row.data[key].props?.selectPickerProps,
153154
classNamePrefix: 'dynamic-data-table__cell__select-picker',
154155
}}
155156
textAreaProps={{

0 commit comments

Comments
 (0)