-
Notifications
You must be signed in to change notification settings - Fork 0
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 #28 from humanitec/handle400
fix: improve status rendering
- Loading branch information
Showing
6 changed files
with
348 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@humanitec/backstage-plugin-common': patch | ||
'@humanitec/backstage-plugin': patch | ||
--- | ||
|
||
fix: improved status rendering |
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
130 changes: 130 additions & 0 deletions
130
plugins/humanitec-common/src/clients/fetch-app-info.test.ts
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,130 @@ | ||
import { fetchAppInfo, FetchAppInfoClient } from './fetch-app-info'; | ||
|
||
type envs = Awaited<ReturnType<FetchAppInfoClient["getEnvironments"]>> | ||
type resources = Awaited<ReturnType<FetchAppInfoClient["getActiveEnvironmentResources"]>> | ||
type runtime = Awaited<ReturnType<FetchAppInfoClient["getRuntimeInfo"]>> | ||
|
||
describe('fetchAppInfo', () => { | ||
const createClientMock = ({ | ||
envs, | ||
resources, | ||
runtime | ||
}: { | ||
envs: envs, | ||
resources?: resources, | ||
runtime?: runtime | ||
}) => { | ||
return { | ||
getEnvironments: jest.fn().mockResolvedValue(envs), | ||
getActiveEnvironmentResources: jest.fn().mockResolvedValue(resources), | ||
getRuntimeInfo: jest.fn().mockResolvedValue(runtime), | ||
}; | ||
} | ||
|
||
const deployedEnv = () => ({ | ||
type: 'test', | ||
id: 'test', | ||
name: 'test', | ||
last_deploy: { | ||
id: 'test', | ||
created_at: 'test', | ||
status: 'succeeded' as 'succeeded', | ||
created_by: 'test', | ||
comment: 'test', | ||
env_id: 'test', | ||
export_file: 'test', | ||
from_id: 'test', | ||
export_status: 'test', | ||
set_id: 'test', | ||
status_changed_at: 'test', | ||
} | ||
}) | ||
const k8sClusterResource = (clusterType: string) => ({ | ||
app_id: 'test', | ||
def_id: 'test', | ||
env_id: 'test', | ||
env_type: 'test', | ||
org_id: 'test', | ||
res_id: 'k8s-cluster', | ||
resource: { | ||
cluster_type: clusterType, | ||
}, | ||
status: 'active' as 'active', | ||
type: 'test', | ||
updated_at: 'test', | ||
}) | ||
|
||
it('without environments', async () => { | ||
const client = createClientMock({envs: []}) | ||
const res = await fetchAppInfo({ client }, 'appId') | ||
|
||
expect(res).toEqual([]) | ||
|
||
expect(client.getEnvironments).toHaveBeenCalledWith('appId') | ||
expect(client.getActiveEnvironmentResources).not.toHaveBeenCalled() | ||
expect(client.getRuntimeInfo).not.toHaveBeenCalled() | ||
}) | ||
|
||
|
||
it('without a deployment', async () => { | ||
const env = { | ||
type: 'test', | ||
id: 'test', | ||
name: 'test', | ||
} | ||
const client = createClientMock({envs: [env]}) | ||
|
||
const res = await fetchAppInfo({ client }, 'appId') | ||
|
||
expect(res).toEqual([{ | ||
...env, | ||
usesGitCluster: false, | ||
runtime: null, | ||
resources: [] | ||
}]) | ||
|
||
expect(client.getEnvironments).toHaveBeenCalledWith('appId') | ||
expect(client.getActiveEnvironmentResources).not.toHaveBeenCalled() | ||
expect(client.getRuntimeInfo).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('with a git resource', async () => { | ||
const env = deployedEnv() | ||
const gitClusterResource = k8sClusterResource('git') | ||
const client = createClientMock({envs: [env], resources: [gitClusterResource]}) | ||
|
||
const res = await fetchAppInfo({ client }, 'appId') | ||
|
||
expect(res).toEqual([{ | ||
...env, | ||
usesGitCluster: true, | ||
runtime: null, | ||
resources: [gitClusterResource] | ||
}]) | ||
expect(client.getEnvironments).toHaveBeenCalledWith('appId') | ||
expect(client.getActiveEnvironmentResources).toHaveBeenCalledWith('appId', 'test') | ||
expect(client.getRuntimeInfo).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('without a k8s-cluster git resource', async () => { | ||
const env = deployedEnv() | ||
const gkeClusterResource = k8sClusterResource('gke') | ||
const runtime = { | ||
namespace: 'test', | ||
modules: {} | ||
} | ||
const client = createClientMock({envs: [env], resources: [gkeClusterResource], runtime }) | ||
|
||
const res = await fetchAppInfo({ client }, 'appId') | ||
|
||
expect(res).toEqual([{ | ||
...env, | ||
usesGitCluster: false, | ||
runtime, | ||
resources: [gkeClusterResource] | ||
}]) | ||
expect(client.getEnvironments).toHaveBeenCalledWith('appId') | ||
expect(client.getActiveEnvironmentResources).toHaveBeenCalledWith('appId', 'test') | ||
expect(client.getRuntimeInfo).toHaveBeenCalledWith('appId', 'test') | ||
}) | ||
}); |
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 |
---|---|---|
@@ -1,21 +1,50 @@ | ||
import { HumanitecClient } from './humanitec'; | ||
|
||
export async function fetchAppInfo({ client }: { client: HumanitecClient; }, appId: string) { | ||
const k8sResID = 'k8s-cluster'; | ||
const gitClusterType = 'git'; | ||
|
||
export type FetchAppInfoClient = Pick<HumanitecClient, 'getEnvironments' | 'getActiveEnvironmentResources' | 'getRuntimeInfo'>; | ||
|
||
export async function fetchAppInfo({ client }: { client: FetchAppInfoClient; }, appId: string) { | ||
const environments = await client.getEnvironments(appId); | ||
|
||
return await Promise.all(environments.map(async (env) => { | ||
const [runtime, resources] = await Promise.all([ | ||
client.getRuntimeInfo(appId, env.id), | ||
client.getActiveEnvironmentResources(appId, env.id), | ||
]); | ||
let usesGitCluster = false | ||
if (!env.last_deploy) { | ||
return { | ||
...env, | ||
usesGitCluster, | ||
runtime: null, | ||
resources: [] | ||
}; | ||
} | ||
|
||
const resources = await client.getActiveEnvironmentResources(appId, env.id); | ||
|
||
// k8s-cluster of cluster_type git have no runtime information | ||
for (const resource of resources) { | ||
if (resource.res_id === k8sResID && resource.resource?.cluster_type === gitClusterType) { | ||
usesGitCluster = true; | ||
|
||
return { | ||
...env, | ||
usesGitCluster, | ||
runtime: null, | ||
resources | ||
}; | ||
} | ||
} | ||
|
||
const runtime = await client.getRuntimeInfo(appId, env.id) | ||
|
||
return { | ||
...env, | ||
usesGitCluster, | ||
runtime, | ||
resources | ||
}; | ||
})); | ||
} | ||
|
||
export type FetchAppInfoResponse = Awaited<ReturnType<typeof fetchAppInfo>> | ||
export type FetchAppInfoEnvironment = FetchAppInfoResponse[0] | ||
export type FetchAppInfoEnvironment = FetchAppInfoResponse[0] |
120 changes: 119 additions & 1 deletion
120
plugins/humanitec/src/components/HumanitecCardComponent.test.tsx
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
Oops, something went wrong.