From 1e7f1181a0ac739690dc051a8381c4c14a519058 Mon Sep 17 00:00:00 2001 From: Aleix Date: Wed, 18 Sep 2024 17:48:21 +0200 Subject: [PATCH] feat: Implement catalog_incident api (#2147) --- catalog/api/app.py | 5 ++-- .../src/app/Admin/CatalogItemAdmin.spec.tsx | 2 +- catalog/ui/src/app/Admin/CatalogItemAdmin.tsx | 29 +++++++++++++------ catalog/ui/src/app/api.ts | 6 ++-- 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/catalog/api/app.py b/catalog/api/app.py index cad809ad4..6015ef965 100644 --- a/catalog/api/app.py +++ b/catalog/api/app.py @@ -620,9 +620,8 @@ async def catalog_item_incidents(request): asset_uuid = request.match_info.get('asset_uuid') stage = request.match_info.get('stage') data = await request.json() - headers = { - "Authorization": f"Bearer {reporting_api_authorization_token}" - } + headers = request.headers + headers["Authorization"] = f"Bearer {reporting_api_authorization_token}" return await api_proxy( headers=headers, method="POST", diff --git a/catalog/ui/src/app/Admin/CatalogItemAdmin.spec.tsx b/catalog/ui/src/app/Admin/CatalogItemAdmin.spec.tsx index 58ad179dd..31c2566f5 100644 --- a/catalog/ui/src/app/Admin/CatalogItemAdmin.spec.tsx +++ b/catalog/ui/src/app/Admin/CatalogItemAdmin.spec.tsx @@ -64,7 +64,7 @@ describe('CatalogItemAdmin Component', () => { status: 'Operational', incident_url: '', jira_url: '', - comments: [], + comments: JSON.stringify([]), } await userEvent.click(getByText('Save')); expect(fetcher).toHaveBeenCalledWith(path, { method: 'POST', body: JSON.stringify(patch), headers: {'Content-Type': 'application/json'}}); diff --git a/catalog/ui/src/app/Admin/CatalogItemAdmin.tsx b/catalog/ui/src/app/Admin/CatalogItemAdmin.tsx index a0ae2d500..9612b9565 100644 --- a/catalog/ui/src/app/Admin/CatalogItemAdmin.tsx +++ b/catalog/ui/src/app/Admin/CatalogItemAdmin.tsx @@ -50,18 +50,22 @@ const CatalogItemAdmin: React.FC = () => { const navigate = useNavigate(); const { data: catalogItem } = useSWR(apiPaths.CATALOG_ITEM({ namespace, name }), fetcher); const asset_uuid = catalogItem.metadata.labels['gpte.redhat.com/asset-uuid']; - const { data: catalogItemIncident } = useSWR( + const { data: catalogItemIncident, isLoading: isLoadingIncidents } = useSWR( apiPaths.CATALOG_ITEM_LAST_INCIDENT({ namespace, asset_uuid }), - fetcher + fetcher, + { + suspense: false, + shouldRetryOnError: false, + } ); const { email: userEmail } = useSession().getSession(); const [isReadOnlyValue, setIsReadOnlyValue] = useState(false); const [isOpen, setIsOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); - const [status, setStatus] = useState(catalogItemIncident?.status || 'Operational'); - const [isDisabled, setIsDisabled] = useState(catalogItemIncident?.disabled ?? false); - const [incidentUrl, setIncidentUrl] = useState(catalogItemIncident?.incident_url || ''); - const [jiraIssueId, setJiraIssueId] = useState(catalogItemIncident?.jira_url || ''); + const [status, setStatus] = useState('Operational'); + const [isDisabled, setIsDisabled] = useState(false); + const [incidentUrl, setIncidentUrl] = useState(''); + const [jiraIssueId, setJiraIssueId] = useState(''); const [comment, setComment] = useState(''); const provider = getProvider(catalogItem); @@ -79,6 +83,13 @@ const CatalogItemAdmin: React.FC = () => { } }, [setIsReadOnlyValue, status]); + useEffect(() => { + setStatus(catalogItemIncident?.status || 'Operational'); + setIsDisabled(catalogItemIncident?.disabled ?? false); + setIncidentUrl(catalogItemIncident?.incident_url || ''); + setJiraIssueId(catalogItemIncident?.jira_url || ''); + }, [isLoadingIncidents]) + async function removeComment(comment: comment) { if (!catalogItemIncident?.comments) { throw "Can't find comment to delete"; @@ -93,7 +104,7 @@ const CatalogItemAdmin: React.FC = () => { async function saveForm(comments?: comment[]) { setIsLoading(true); if (comments === null || comments === undefined) { - comments = JSON.parse(catalogItemIncident?.comments) || []; + comments = catalogItemIncident ? JSON.parse(catalogItemIncident.comments) : []; } if (comment) { comments.push({ @@ -111,7 +122,7 @@ const CatalogItemAdmin: React.FC = () => { status, incident_url: incidentUrl, jira_url: jiraIssueId, - comments, + comments: JSON.stringify(comments), }), headers: { 'Content-Type': 'application/json', @@ -123,7 +134,7 @@ const CatalogItemAdmin: React.FC = () => { return ( - {isLoading ? ( + {isLoading || isLoadingIncidents ? ( } /> diff --git a/catalog/ui/src/app/api.ts b/catalog/ui/src/app/api.ts index bafae9dcf..095e097e2 100644 --- a/catalog/ui/src/app/api.ts +++ b/catalog/ui/src/app/api.ts @@ -1612,11 +1612,11 @@ export const apiPaths: { [key in ResourceType]: (args: any) => string } = { continueId ? `&continue=${continueId}` : '' }${labelSelector ? `&labelSelector=${labelSelector}` : ''}`, CATALOG_ITEM_INCIDENTS: ({ namespace, asset_uuid }: { namespace: string; asset_uuid: string }) => - `/api/catalog_incident/incidents/${asset_uuid}/${namespace}`, + `/api/catalog_incident/incidents/${asset_uuid}/${namespace.split('-').slice(-1)[0]}`, CATALOG_ITEM_LAST_INCIDENT: ({ namespace, asset_uuid }: { namespace: string; asset_uuid: string }) => - `/api/catalog_incident/last-incident/${asset_uuid}/${namespace}`, + `/api/catalog_incident/last-incident/${asset_uuid}/${namespace.split('-').slice(-1)[0]}`, CATALOG_ITEMS_ACTIVE_INCIDENTS: ({ namespace }: { namespace?: string }) => - `/api/catalog_incident/active-incidents${namespace ? `?stage=${namespace}` : ''}`, + `/api/catalog_incident/active-incidents${namespace ? `?stage=${namespace.split('-').slice(-1)[0]}` : ''}`, RESOURCE_CLAIMS: ({ namespace, limit,