2022-03-29 17:13:08 -08:00
|
|
|
/*
|
|
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
|
|
|
|
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 * as React from 'react';
|
|
|
|
import './colors.css';
|
|
|
|
import './common.css';
|
|
|
|
import * as icons from './icons';
|
|
|
|
import { AutoChip } from './chip';
|
|
|
|
import './reportView.css';
|
|
|
|
import './theme.css';
|
|
|
|
|
2022-05-02 16:28:14 -07:00
|
|
|
export type Metainfo = {
|
|
|
|
'revision.id'?: string;
|
|
|
|
'revision.author'?: string;
|
|
|
|
'revision.email'?: string;
|
|
|
|
'revision.subject'?: string;
|
|
|
|
'revision.timestamp'?: number | Date;
|
|
|
|
'revision.link'?: string;
|
|
|
|
'ci.link'?: string;
|
|
|
|
'timestamp'?: number
|
2022-06-06 21:05:47 -07:00
|
|
|
};
|
2022-05-02 16:28:14 -07:00
|
|
|
|
2022-06-06 21:05:47 -07:00
|
|
|
class ErrorBoundary extends React.Component<React.PropsWithChildren<{}>, { error: Error | null, errorInfo: React.ErrorInfo | null }> {
|
2022-05-02 16:28:14 -07:00
|
|
|
state: { error: Error | null, errorInfo: React.ErrorInfo | null } = {
|
|
|
|
error: null,
|
|
|
|
errorInfo: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidCatch(error: Error, errorInfo: React.ErrorInfo) {
|
|
|
|
this.setState({ error, errorInfo });
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.error || this.state.errorInfo) {
|
|
|
|
return (
|
|
|
|
<AutoChip header={'Commit Metainfo Error'} dataTestId='metadata-error'>
|
|
|
|
<p>An error was encountered when trying to render Commit Metainfo. Please file a GitHub issue to report this error.</p>
|
|
|
|
<p>
|
|
|
|
<pre style={{ overflow: 'scroll' }}>{this.state.error?.message}<br/>{this.state.error?.stack}<br/>{this.state.errorInfo?.componentStack}</pre>
|
|
|
|
</p>
|
|
|
|
</AutoChip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.props.children;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const MetadataView: React.FC<Metainfo> = metadata => <ErrorBoundary><InnerMetadataView {...metadata} /></ErrorBoundary>;
|
|
|
|
|
|
|
|
const InnerMetadataView: React.FC<Metainfo> = metadata => {
|
|
|
|
if (!Object.keys(metadata).find(k => k.startsWith('revision.') || k.startsWith('ci.')))
|
|
|
|
return null;
|
|
|
|
|
2022-03-29 17:13:08 -08:00
|
|
|
return (
|
|
|
|
<AutoChip header={
|
|
|
|
<span>
|
|
|
|
{metadata['revision.id'] && <span style={{ float: 'right', fontFamily: 'var(--monospace-font)' }}>
|
|
|
|
{metadata['revision.id'].slice(0, 7)}
|
|
|
|
</span>}
|
2022-05-02 16:28:14 -07:00
|
|
|
{metadata['revision.subject'] || 'Commit Metainfo'}
|
|
|
|
</span>} initialExpanded={false} dataTestId='metadata-chip'>
|
2022-03-29 17:13:08 -08:00
|
|
|
{metadata['revision.subject'] &&
|
|
|
|
<MetadatViewItem
|
|
|
|
testId='revision.subject'
|
|
|
|
content={<span>{metadata['revision.subject']}</span>}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{metadata['revision.id'] &&
|
|
|
|
<MetadatViewItem
|
|
|
|
testId='revision.id'
|
|
|
|
content={<span style={{ fontFamily: 'var(--monospace-font)' }}>{metadata['revision.id']}</span>}
|
|
|
|
href={metadata['revision.link']}
|
|
|
|
icon='commit'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{(metadata['revision.author'] || metadata['revision.email']) &&
|
|
|
|
<MetadatViewItem
|
2022-03-29 21:08:10 -07:00
|
|
|
content={`${metadata['revision.author']} ${metadata['revision.email']}`}
|
2022-03-29 17:13:08 -08:00
|
|
|
icon='person'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{metadata['revision.timestamp'] &&
|
|
|
|
<MetadatViewItem
|
|
|
|
testId='revision.timestamp'
|
|
|
|
content={
|
|
|
|
<>
|
|
|
|
{Intl.DateTimeFormat(undefined, { dateStyle: 'full' }).format(metadata['revision.timestamp'])}
|
2022-04-20 18:10:55 +02:00
|
|
|
{' '}
|
2022-03-29 17:13:08 -08:00
|
|
|
{Intl.DateTimeFormat(undefined, { timeStyle: 'long' }).format(metadata['revision.timestamp'])}
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
icon='calendar'
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
{metadata['ci.link'] &&
|
|
|
|
<MetadatViewItem
|
|
|
|
content='CI/CD Logs'
|
|
|
|
href={metadata['ci.link']}
|
|
|
|
icon='externalLink'
|
|
|
|
/>
|
|
|
|
}
|
2022-05-02 16:28:14 -07:00
|
|
|
{metadata['timestamp'] &&
|
2022-03-29 17:13:08 -08:00
|
|
|
<MetadatViewItem
|
|
|
|
content={<span style={{ color: 'var(--color-fg-subtle)' }}>
|
2022-05-02 16:28:14 -07:00
|
|
|
Report generated on {Intl.DateTimeFormat(undefined, { dateStyle: 'full', timeStyle: 'long' }).format(metadata['timestamp'])}
|
2022-03-29 17:13:08 -08:00
|
|
|
</span>}></MetadatViewItem>
|
|
|
|
}
|
|
|
|
</AutoChip>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const MetadatViewItem: React.FC<{ content: JSX.Element | string; icon?: keyof typeof icons, href?: string, testId?: string }> = ({ content, icon, href, testId }) => {
|
|
|
|
return (
|
|
|
|
<div className='my-1 hbox' data-test-id={testId} >
|
|
|
|
<div className='mr-2'>
|
|
|
|
{icons[icon || 'blank']()}
|
|
|
|
</div>
|
|
|
|
<div style={{ flex: 1 }}>
|
|
|
|
{href ? <a href={href} target='_blank' rel='noopener noreferrer'>{content}</a> : content}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|