Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/docker_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
name: Docker Build

on:
schedule:
- cron: '0 0 * * *' # Midnight
# schedule:
# - cron: '0 0 * * *' # Midnight
Comment on lines +13 to +14
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bibhuti230185 !
I think we need this schedule cron job

workflow_dispatch:
pull_request:
paths:
Expand Down
51 changes: 49 additions & 2 deletions src/components/ExternalIds/ExternalIds.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,60 @@ interface Props {
}
}

/**
* Expands external IDs for display, with special handling for 'package-url'.
* If package-url value is a JSON array string, it expands into multiple entries.
*/
const expandExternalIds = (externalIds: {
[k: string]: string
}): Array<
[
string,
string,
]
> => {
const result: Array<
[
string,
string,
]
> = []

Object.entries(externalIds).forEach(([key, value]) => {
if (key === 'package-url' && value.trimStart().startsWith('[')) {
try {
const urls = JSON.parse(value) as string[]
if (Array.isArray(urls)) {
urls.forEach((url) => {
result.push([
'package-url',
url,
])
})
return
}
} catch {
// Not valid JSON, fall through to default handling
}
}
result.push([
key,
value,
])
})

return result
}

const ExternalIds = ({ externalIds }: Props): JSX.Element => {
const expandedIds = expandExternalIds(externalIds)

return (
<>
{' '}
{Object.entries(externalIds).map(([key, value]) => {
{expandedIds.map(([key, value], index) => {
return (
<li key={key}>
<li key={`${key}-${index}`}>
<span className='fw-bold'>{key}: </span>
<span> {value}</span>
</li>
Expand Down
41 changes: 30 additions & 11 deletions src/components/sw360/AddKeyValue/AddKeyValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use client'

import { useTranslations } from 'next-intl'
import React, { type JSX, useEffect, useState } from 'react'
import React, { type JSX, useCallback, useEffect, useState } from 'react'
import { OverlayTrigger, Tooltip } from 'react-bootstrap'
import { BsFillTrashFill } from 'react-icons/bs'
import DeleteItemWarning from '@/components/sw360/DeleteItemWarning/DeleteItemWarning'
Expand Down Expand Up @@ -44,6 +44,33 @@ function AddKeyValue(props: Props): JSX.Element {
props.data,
])

/**
* Converts an input list to a map, with special handling for 'package-url' key.
* Multiple 'package-url' entries are combined into a single JSON array string.
*/
const convertListToMap = useCallback((list: Input[]): Map<string, string> => {
const map = new Map<string, string>()
const packageUrls: string[] = []

list.forEach((item) => {
if (item.key === 'package-url') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @bibhuti230185 !
Comparing a key with user defined string, can lead to buggy code.

// Collect all package-url values
if (item.value.trim() !== '') {
packageUrls.push(item.value)
}
} else {
map.set(item.key, item.value)
}
})

// Combine package-url values into a JSON array string
if (packageUrls.length > 0) {
map.set('package-url', JSON.stringify(packageUrls))
}

return map
}, [])

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>, index: number) => {
const { name, value } = e.target
const list: Input[] = [
Expand All @@ -52,11 +79,7 @@ function AddKeyValue(props: Props): JSX.Element {
list[index][name as keyof Input] = value
props.setData(list)
if (props.setObject) {
const map = new Map<string, string>()
list.forEach((item) => {
map.set(item.key, item.value)
})
props.setObject(map)
props.setObject(convertListToMap(list))
}
}

Expand All @@ -67,11 +90,7 @@ function AddKeyValue(props: Props): JSX.Element {
list[index].key = value
props.setData(list)
if (props.setObject) {
const map = new Map<string, string>()
list.forEach((item) => {
map.set(item.key, item.value)
})
props.setObject(map)
props.setObject(convertListToMap(list))
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/common.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,32 @@ const getEmailsModerators = (users: User[]): string[] => {

/**
* Converts an object to a map of key-value pairs.
* Special handling for 'package-url' key: if the value is a JSON array string,
* it will be expanded into multiple entries with the same key.
* @param data - The object to convert.
* @returns An array of key-value pairs.
*/
const convertObjectToMap = (data: { [k: string]: string }): InputKeyValue[] => {
const map = new Map(Object.entries(data))
const inputs: InputKeyValue[] = []
map.forEach((value, key) => {
// Special handling for package-url: expand JSON array into multiple entries
if (key === 'package-url' && value.trimStart().startsWith('[')) {
try {
const urls = JSON.parse(value) as string[]
if (Array.isArray(urls)) {
urls.forEach((url) => {
inputs.push({
key: key,
value: url,
})
})
return
}
} catch {
// If parsing fails, treat as regular value
}
}
const input: InputKeyValue = {
key: key,
value: value,
Expand Down
Loading