Merge pull request #21235 from awanlin/fix/refactor-error-handling
[Azure DevOps] Refactor error handling to be more consistent
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/plugin-azure-devops': patch
|
||||
---
|
||||
|
||||
Refactored the error handling for some of the hooks
|
||||
+2
-10
@@ -16,21 +16,13 @@
|
||||
|
||||
import { BuildTable } from '../BuildTable/BuildTable';
|
||||
import React from 'react';
|
||||
import { getAnnotationValuesFromEntity } from '../../utils/getAnnotationValuesFromEntity';
|
||||
import { useBuildRuns } from '../../hooks/useBuildRuns';
|
||||
import { useBuildRuns } from '../../hooks';
|
||||
import { useEntity } from '@backstage/plugin-catalog-react';
|
||||
|
||||
export const EntityPageAzurePipelines = (props: { defaultLimit?: number }) => {
|
||||
const { entity } = useEntity();
|
||||
|
||||
const { project, repo, definition } = getAnnotationValuesFromEntity(entity);
|
||||
|
||||
const { items, loading, error } = useBuildRuns(
|
||||
project,
|
||||
props.defaultLimit,
|
||||
repo,
|
||||
definition,
|
||||
);
|
||||
const { items, loading, error } = useBuildRuns(entity, props.defaultLimit);
|
||||
|
||||
return <BuildTable items={items} loading={loading} error={error} />;
|
||||
};
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
export * from './useAllTeams';
|
||||
export * from './useDashboardPullRequests';
|
||||
export * from './usePullRequests';
|
||||
export * from './useRepoBuilds';
|
||||
export * from './useUserEmail';
|
||||
export * from './useUserTeamIds';
|
||||
export * from './useReadme';
|
||||
export * from './useBuildRuns';
|
||||
|
||||
+12
-12
@@ -17,14 +17,14 @@
|
||||
import React from 'react';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import { RepoBuild } from '@backstage/plugin-azure-devops-common';
|
||||
import { BuildRun } from '@backstage/plugin-azure-devops-common';
|
||||
import { TestApiProvider } from '@backstage/test-utils';
|
||||
import { AzureDevOpsApi, azureDevOpsApiRef } from '../api';
|
||||
import { useRepoBuilds } from './useRepoBuilds';
|
||||
import { useBuildRuns } from './useBuildRuns';
|
||||
|
||||
describe('useRepoBuilds', () => {
|
||||
describe('useBuildRuns', () => {
|
||||
const azureDevOpsApiMock = {
|
||||
getRepoBuilds: jest.fn(),
|
||||
getBuildRuns: jest.fn(),
|
||||
};
|
||||
const azureDevOpsApi =
|
||||
azureDevOpsApiMock as Partial<AzureDevOpsApi> as AzureDevOpsApi;
|
||||
@@ -35,7 +35,7 @@ describe('useRepoBuilds', () => {
|
||||
</TestApiProvider>
|
||||
);
|
||||
|
||||
it('should provide an array of RepoBuild', async () => {
|
||||
it('should provide an array of BuildRun', async () => {
|
||||
const entity: Entity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
kind: 'Component',
|
||||
@@ -47,7 +47,7 @@ describe('useRepoBuilds', () => {
|
||||
},
|
||||
},
|
||||
};
|
||||
const repoBuilds: RepoBuild[] = [
|
||||
const buildRuns: BuildRun[] = [
|
||||
{
|
||||
id: 1,
|
||||
title: 'title-1',
|
||||
@@ -67,10 +67,10 @@ describe('useRepoBuilds', () => {
|
||||
link: 'https://dev.azure.com/org/project/repo',
|
||||
},
|
||||
];
|
||||
azureDevOpsApiMock.getRepoBuilds.mockResolvedValue({
|
||||
items: repoBuilds,
|
||||
azureDevOpsApiMock.getBuildRuns.mockResolvedValue({
|
||||
items: buildRuns,
|
||||
});
|
||||
const { result } = renderHook(() => useRepoBuilds(entity), {
|
||||
const { result } = renderHook(() => useBuildRuns(entity), {
|
||||
wrapper: Wrapper,
|
||||
});
|
||||
|
||||
@@ -79,7 +79,7 @@ describe('useRepoBuilds', () => {
|
||||
await waitFor(() => {
|
||||
expect(result.current).toEqual({
|
||||
error: undefined,
|
||||
items: repoBuilds,
|
||||
items: buildRuns,
|
||||
loading: false,
|
||||
});
|
||||
});
|
||||
@@ -96,7 +96,7 @@ describe('useRepoBuilds', () => {
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
renderHook(() => useRepoBuilds(entity), {
|
||||
renderHook(() => useBuildRuns(entity), {
|
||||
wrapper: Wrapper,
|
||||
}),
|
||||
).toThrow('Value for annotation "dev.azure.com/project" was not found');
|
||||
@@ -116,7 +116,7 @@ describe('useRepoBuilds', () => {
|
||||
};
|
||||
|
||||
expect(() =>
|
||||
renderHook(() => useRepoBuilds(entity), {
|
||||
renderHook(() => useBuildRuns(entity), {
|
||||
wrapper: Wrapper,
|
||||
}),
|
||||
).toThrow(
|
||||
@@ -23,12 +23,12 @@ import { AZURE_DEVOPS_DEFAULT_TOP } from '../constants';
|
||||
import { azureDevOpsApiRef } from '../api';
|
||||
import { useApi } from '@backstage/core-plugin-api';
|
||||
import useAsync from 'react-use/lib/useAsync';
|
||||
import { getAnnotationValuesFromEntity } from '../utils';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
|
||||
export function useBuildRuns(
|
||||
projectName: string,
|
||||
entity: Entity,
|
||||
defaultLimit?: number,
|
||||
repoName?: string,
|
||||
definitionName?: string,
|
||||
): {
|
||||
items?: BuildRun[];
|
||||
loading: boolean;
|
||||
@@ -42,8 +42,9 @@ export function useBuildRuns(
|
||||
const api = useApi(azureDevOpsApiRef);
|
||||
|
||||
const { value, loading, error } = useAsync(() => {
|
||||
return api.getBuildRuns(projectName, repoName, definitionName, options);
|
||||
}, [api, projectName, repoName, definitionName]);
|
||||
const { project, repo, definition } = getAnnotationValuesFromEntity(entity);
|
||||
return api.getBuildRuns(project, repo, definition, options);
|
||||
}, [api]);
|
||||
|
||||
return {
|
||||
items: value?.items,
|
||||
|
||||
@@ -28,11 +28,11 @@ export function useGitTags(entity: Entity): {
|
||||
error?: Error;
|
||||
} {
|
||||
const api = useApi(azureDevOpsApiRef);
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
|
||||
const { value, loading, error } = useAsync(async () => {
|
||||
return await api.getGitTags(project, repo as string);
|
||||
}, [api, project, repo]);
|
||||
const { value, loading, error } = useAsync(() => {
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
return api.getGitTags(project, repo as string);
|
||||
}, [api]);
|
||||
|
||||
return {
|
||||
items: value?.items,
|
||||
|
||||
@@ -44,11 +44,11 @@ export function usePullRequests(
|
||||
};
|
||||
|
||||
const api = useApi(azureDevOpsApiRef);
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
|
||||
const { value, loading, error } = useAsync(async () => {
|
||||
return await api.getPullRequests(project, repo as string, options);
|
||||
}, [api, project, repo, top, status]);
|
||||
const { value, loading, error } = useAsync(() => {
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
return api.getPullRequests(project, repo as string, options);
|
||||
}, [api, top, status]);
|
||||
|
||||
return {
|
||||
items: value?.items,
|
||||
|
||||
@@ -28,11 +28,11 @@ export function useReadme(entity: Entity): {
|
||||
error?: Error;
|
||||
} {
|
||||
const api = useApi(azureDevOpsApiRef);
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
|
||||
const { value, loading, error } = useAsync(async () => {
|
||||
return await api.getReadme({ project, repo: repo as string });
|
||||
}, [api, project, repo]);
|
||||
const { value, loading, error } = useAsync(() => {
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
return api.getReadme({ project, repo: repo as string });
|
||||
}, [api]);
|
||||
|
||||
return {
|
||||
item: value,
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
/*
|
||||
* Copyright 2021 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
RepoBuild,
|
||||
RepoBuildOptions,
|
||||
} from '@backstage/plugin-azure-devops-common';
|
||||
|
||||
import { AZURE_DEVOPS_DEFAULT_TOP } from '../constants';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import { azureDevOpsApiRef } from '../api';
|
||||
import { useApi } from '@backstage/core-plugin-api';
|
||||
import useAsync from 'react-use/lib/useAsync';
|
||||
import { getAnnotationValuesFromEntity } from '../utils';
|
||||
|
||||
export function useRepoBuilds(
|
||||
entity: Entity,
|
||||
defaultLimit?: number,
|
||||
): {
|
||||
items?: RepoBuild[];
|
||||
loading: boolean;
|
||||
error?: Error;
|
||||
} {
|
||||
const top = defaultLimit ?? AZURE_DEVOPS_DEFAULT_TOP;
|
||||
const options: RepoBuildOptions = {
|
||||
top: top,
|
||||
};
|
||||
|
||||
const api = useApi(azureDevOpsApiRef);
|
||||
const { project, repo } = getAnnotationValuesFromEntity(entity);
|
||||
|
||||
const { value, loading, error } = useAsync(async () => {
|
||||
return await api.getRepoBuilds(project, repo as string, options);
|
||||
}, [api, project, repo, entity]);
|
||||
|
||||
return {
|
||||
items: value?.items,
|
||||
loading,
|
||||
error,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user