Merge branch 'master' into timbonicus/catalog-entity-context

This commit is contained in:
Tim Hansen
2021-05-26 17:21:23 -06:00
84 changed files with 2103 additions and 870 deletions
@@ -277,6 +277,9 @@ function createEmitter(logger: Logger, parentEntity: Entity) {
return;
}
if (i.type === 'entity') {
// TODO(freben): Perform the most basic validation here
// (apiVersion, kind, metadata, metadata.name, metadata.namespace, spec)
const originLocation = getEntityOriginLocationRef(parentEntity);
deferredEntities.push({
@@ -23,6 +23,7 @@ import {
import {
HeaderIconLinkRow,
IconLinkVerticalProps,
InfoCardVariants,
useApi,
} from '@backstage/core';
import { scmIntegrationsApiRef } from '@backstage/integration-react';
@@ -50,15 +51,23 @@ const useStyles = makeStyles({
height: 'calc(100% - 10px)', // for pages without content header
marginBottom: '10px',
},
fullHeightCard: {
display: 'flex',
flexDirection: 'column',
height: '100%',
},
gridItemCardContent: {
flex: 1,
},
fullHeightCardContent: {
flex: 1,
},
});
type AboutCardProps = {
/** @deprecated The entity is now grabbed from context instead */
entity?: Entity;
variant?: 'gridItem';
variant?: InfoCardVariants;
};
export function AboutCard({ variant }: AboutCardProps) {
@@ -103,8 +112,22 @@ export function AboutCard({ variant }: AboutCardProps) {
href: 'api',
};
let cardClass = '';
if (variant === 'gridItem') {
cardClass = classes.gridItemCard;
} else if (variant === 'fullHeight') {
cardClass = classes.fullHeightCard;
}
let cardContentClass = '';
if (variant === 'gridItem') {
cardContentClass = classes.gridItemCardContent;
} else if (variant === 'fullHeight') {
cardContentClass = classes.fullHeightCardContent;
}
return (
<Card className={variant === 'gridItem' ? classes.gridItemCard : ''}>
<Card className={cardClass}>
<CardHeader
title="About"
action={
@@ -124,9 +147,7 @@ export function AboutCard({ variant }: AboutCardProps) {
}
/>
<Divider />
<CardContent
className={variant === 'gridItem' ? classes.gridItemCardContent : ''}
>
<CardContent className={cardContentClass}>
<AboutContent entity={entity} />
</CardContent>
</Card>
@@ -61,7 +61,9 @@ describe.each`
params | expected
${''} | ${{}}
${'?foo=bar'} | ${{}}
${'?group'} | ${{ group: null }}
${'?project'} | ${{ project: null }}
${'?project&group'} | ${{ group: null, project: null }}
${'?group=some-group'} | ${{ group: 'some-group' }}
${'?group=some-group&project'} | ${{ group: 'some-group', project: null }}
${'?group=some-group&project=some-project'} | ${{ group: 'some-group', project: 'some-project' }}
@@ -72,9 +74,3 @@ describe.each`
expect(pageFilters).toMatchObject(expected);
});
});
describe('invalidate', () => {
it("should throw an error if param values don't match schema", async () => {
await expect(validate('?group')).rejects.toThrowError();
});
});
+1 -1
View File
@@ -23,7 +23,7 @@ import { ConfigContextProps } from '../hooks/useConfig';
const schema = yup
.object()
.shape({
group: yup.string(),
group: yup.string().nullable(),
project: yup.string().nullable(),
})
.required();
+1 -1
View File
@@ -60,7 +60,7 @@ export type GithubDeployment = {
commit: {
abbreviatedOid: string;
commitUrl: string;
};
} | null;
creator: {
login: string;
};
@@ -66,11 +66,12 @@ export function createStatusColumn(): TableColumn<GithubDeployment> {
export function createCommitColumn(): TableColumn<GithubDeployment> {
return {
title: 'Commit',
render: (row: GithubDeployment): JSX.Element => (
<Link to={row.commit.commitUrl} target="_blank" rel="noopener">
{row.commit.abbreviatedOid}
</Link>
),
render: (row: GithubDeployment) =>
row.commit && (
<Link to={row.commit.commitUrl} target="_blank" rel="noopener">
{row.commit.abbreviatedOid}
</Link>
),
};
}
@@ -0,0 +1,94 @@
/*
* Copyright 2020 Spotify AB
*
* 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 React from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { LatestRunCard } from './Cards';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { ApiProvider, ApiRegistry } from '@backstage/core';
import { JenkinsApi, jenkinsApiRef } from '../../api';
describe('<LatestRunCard />', () => {
const entity = {
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'software',
description: 'This is the description',
annotations: { JENKINS_ANNOTATION: 'jenkins' },
},
};
const jenkinsApi: Partial<JenkinsApi> = {
getLastBuild: () => Promise.resolve({ timestamp: 0, result: 'success' }),
};
it('should show success status of latest build', async () => {
const apis = ApiRegistry.from([[jenkinsApiRef, jenkinsApi]]);
const { getByText } = await renderInTestApp(
<ApiProvider apis={apis}>
<EntityProvider entity={entity}>
<LatestRunCard branch="master" />
</EntityProvider>
</ApiProvider>,
);
expect(getByText('Completed')).toBeInTheDocument();
});
it('should show the appropriate error in case of a connection error', async () => {
const jenkinsApiWithError: Partial<JenkinsApi> = {
getLastBuild: () => Promise.reject(new Error('Unauthorized')),
};
const apis = ApiRegistry.from([[jenkinsApiRef, jenkinsApiWithError]]);
const { getByText } = await renderInTestApp(
<ApiProvider apis={apis}>
<EntityProvider entity={entity}>
<LatestRunCard branch="master" />
</EntityProvider>
</ApiProvider>,
);
expect(getByText("Error: Can't connect to Jenkins")).toBeInTheDocument();
expect(getByText('Unauthorized')).toBeInTheDocument();
});
it('should show the appropriate error in case Jenkins project is not found', async () => {
const jenkinsApiWithError: Partial<JenkinsApi> = {
getLastBuild: () =>
Promise.reject({
notFound: true,
message: 'jenkins-project not found',
}),
};
const apis = ApiRegistry.from([[jenkinsApiRef, jenkinsApiWithError]]);
const { getByText } = await renderInTestApp(
<ApiProvider apis={apis}>
<EntityProvider entity={entity}>
<LatestRunCard branch="master" />
</EntityProvider>
</ApiProvider>,
);
expect(getByText("Error: Can't find Jenkins project")).toBeInTheDocument();
expect(getByText('jenkins-project not found')).toBeInTheDocument();
});
});
+32 -3
View File
@@ -17,13 +17,14 @@ import {
InfoCard,
InfoCardVariants,
StructuredMetadataTable,
WarningPanel,
} from '@backstage/core';
import { LinearProgress, Link, makeStyles, Theme } from '@material-ui/core';
import ExternalLinkIcon from '@material-ui/icons/Launch';
import { DateTime, Duration } from 'luxon';
import React from 'react';
import { JenkinsRunStatus } from '../BuildsPage/lib/Status';
import { useBuilds } from '../useBuilds';
import { ErrorType, useBuilds } from '../useBuilds';
import { useProjectSlugFromEntity } from '../useProjectSlugFromEntity';
const useStyles = makeStyles<Theme>({
@@ -75,6 +76,23 @@ const WidgetContent = ({
);
};
const JenkinsApiErrorPanel = ({
message,
errorType,
}: {
message: string;
errorType: ErrorType;
}) => {
let title = undefined;
if (errorType === ErrorType.CONNECTION_ERROR) {
title = "Can't connect to Jenkins";
} else if (errorType === ErrorType.NOT_FOUND) {
title = "Can't find Jenkins project";
}
return <WarningPanel severity="error" title={title} message={message} />;
};
export const LatestRunCard = ({
branch = 'master',
variant,
@@ -83,11 +101,22 @@ export const LatestRunCard = ({
variant?: InfoCardVariants;
}) => {
const projectName = useProjectSlugFromEntity();
const [{ builds, loading }] = useBuilds(projectName, branch);
const [{ builds, loading, error }] = useBuilds(projectName, branch);
const latestRun = builds ?? {};
return (
<InfoCard title={`Latest ${branch} build`} variant={variant}>
<WidgetContent loading={loading} branch={branch} latestRun={latestRun} />
{!error ? (
<WidgetContent
loading={loading}
branch={branch}
latestRun={latestRun}
/>
) : (
<JenkinsApiErrorPanel
message={error.message}
errorType={error.errorType}
/>
)}
</InfoCard>
);
};
+14 -1
View File
@@ -18,6 +18,11 @@ import { useState } from 'react';
import { useAsyncRetry } from 'react-use';
import { jenkinsApiRef } from '../api';
export enum ErrorType {
CONNECTION_ERROR,
NOT_FOUND,
}
export function useBuilds(projectName: string, branch?: string) {
const api = useApi(jenkinsApiRef);
const errorApi = useApi(errorApiRef);
@@ -25,6 +30,10 @@ export function useBuilds(projectName: string, branch?: string) {
const [total, setTotal] = useState(0);
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(5);
const [error, setError] = useState<{
message: string;
errorType: ErrorType;
}>();
const restartBuild = async (buildName: string) => {
try {
@@ -48,7 +57,10 @@ export function useBuilds(projectName: string, branch?: string) {
return build || [];
} catch (e) {
errorApi.post(e);
const errorType = e.notFound
? ErrorType.NOT_FOUND
: ErrorType.CONNECTION_ERROR;
setError({ message: e.message, errorType });
throw e;
}
}, [api, errorApi, projectName, branch]);
@@ -61,6 +73,7 @@ export function useBuilds(projectName: string, branch?: string) {
builds,
projectName,
total,
error,
},
{
builds,
+2 -2
View File
@@ -35,8 +35,8 @@
"@backstage/config": "^0.1.5",
"@backstage/errors": "^0.1.1",
"@backstage/integration": "^0.5.3",
"@gitbeaker/core": "^28.0.2",
"@gitbeaker/node": "^28.0.2",
"@gitbeaker/core": "^29.2.0",
"@gitbeaker/node": "^29.2.0",
"@octokit/rest": "^18.5.3",
"@types/express": "^4.17.6",
"@types/git-url-parse": "^9.0.0",