-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #737 from rottencandy/fix/release-pr-link-473
Use correct workspace for release pipelinerun link
- Loading branch information
Showing
11 changed files
with
165 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from 'react'; | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { WorkspaceProvider } from '../../utils/workspace-context-utils'; | ||
import { useWorkspaceForNamespace } from '../useWorkspaceForNamespace'; | ||
|
||
describe('useWorkspaceForNamespace', () => { | ||
it('should find correct workspace', () => { | ||
const wrapper = ({ children }) => ( | ||
<WorkspaceProvider | ||
value={{ | ||
namespace: 'test-ns', | ||
lastUsedWorkspace: 'test-ws', | ||
workspace: '', | ||
workspaces: [ | ||
{ metadata: { name: 'ws1' }, status: { namespaces: [{ name: 'my-ns' }] } }, | ||
{ metadata: { name: 'ws2' }, status: { namespaces: [{ name: 'my-ns-2' }] } }, | ||
{ metadata: { name: 'ws3' }, status: { namespaces: [{ name: 'test-ns' }] } }, | ||
] as any, | ||
setWorkspace: () => {}, | ||
workspacesLoaded: false, | ||
}} | ||
> | ||
{children} | ||
</WorkspaceProvider> | ||
); | ||
const { result } = renderHook(() => useWorkspaceForNamespace('my-ns'), { wrapper }); | ||
expect(result.current.metadata.name).toBe('ws1'); | ||
}); | ||
|
||
it('should return undefined if workspace is not found', () => { | ||
const { result } = renderHook(() => useWorkspaceForNamespace('my-ns-1')); | ||
expect(result.current).toBe(undefined); | ||
}); | ||
|
||
it('should return undefined if namespace is not provided', () => { | ||
const { result } = renderHook(() => useWorkspaceForNamespace('')); | ||
expect(result.current).toBe(undefined); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useWorkspaceForNamespace } from '../useWorkspaceForNamespace'; | ||
import { useWorkspaceResource } from '../useWorkspaceResource'; | ||
|
||
jest.mock('../useWorkspaceForNamespace', () => ({ | ||
useWorkspaceForNamespace: jest.fn(), | ||
})); | ||
|
||
const useWorkspaceMock = useWorkspaceForNamespace as jest.Mock; | ||
|
||
describe('useWorkspaceResource', () => { | ||
it('should return correct resource name and workspace', () => { | ||
useWorkspaceMock.mockReturnValue({ metadata: { name: 'ws1' } }); | ||
const { result } = renderHook(() => useWorkspaceResource('my-ns/my-resource')); | ||
expect(useWorkspaceMock).toHaveBeenCalledWith('my-ns'); | ||
expect(result.current).toEqual(['my-resource', 'ws1']); | ||
}); | ||
|
||
it('should return undefined if invalid string is provided', () => { | ||
useWorkspaceMock.mockReturnValue(undefined); | ||
const { result } = renderHook(() => useWorkspaceResource('')); | ||
expect(result.current).toEqual([undefined, undefined]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as React from 'react'; | ||
import { WorkspaceContext } from '../utils/workspace-context-utils'; | ||
|
||
export const useWorkspaceForNamespace = (namespace?: string) => { | ||
const { workspaces } = React.useContext(WorkspaceContext); | ||
|
||
return React.useMemo(() => { | ||
if (!namespace) { | ||
return undefined; | ||
} | ||
|
||
return workspaces.find((w) => w.status?.namespaces?.some((ns) => ns.name === namespace)); | ||
}, [namespace, workspaces]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import * as React from 'react'; | ||
import { useWorkspaceForNamespace } from './useWorkspaceForNamespace'; | ||
|
||
/** | ||
* @param namespacedObj string with the format `<namespace>/<resource>` | ||
* @returns resource name and workspace if accessible | ||
*/ | ||
export const useWorkspaceResource = ( | ||
namespacedObj?: string, | ||
): [resource?: string, workspace?: string] => { | ||
const [namespace, resource] = React.useMemo(() => { | ||
if (!namespacedObj) { | ||
return [undefined, undefined]; | ||
} | ||
return namespacedObj.split('/'); | ||
}, [namespacedObj]); | ||
|
||
const workspace = useWorkspaceForNamespace(namespace); | ||
|
||
return [resource, workspace?.metadata.name]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters