Setup a devapp for SonarQube that shows the different states of the widget

This commit is contained in:
Dominik Henneke
2020-12-08 14:42:02 +01:00
parent faa7be46e5
commit 0bc10faf6f
6 changed files with 296 additions and 117 deletions
+144 -2
View File
@@ -14,7 +14,149 @@
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import {
Content,
createPlugin,
createRouteRef,
Header,
Page,
} from '@backstage/core';
import { createDevApp } from '@backstage/dev-utils';
import { plugin } from '../src/plugin';
import { Grid } from '@material-ui/core';
import React from 'react';
import { SonarQubeCard } from '../src';
import { FindingSummary, SonarQubeApi, sonarQubeApiRef } from '../src/api';
import { SONARQUBE_PROJECT_KEY_ANNOTATION } from '../src/components/useProjectKey';
createDevApp().registerPlugin(plugin).render();
createDevApp()
.registerApi({
api: sonarQubeApiRef,
deps: {},
factory: () =>
({
getFindingSummary: async componentKey => {
switch (componentKey) {
case 'error':
throw new Error('Error!');
case 'never':
return new Promise(() => {});
case 'not-computed':
return {
lastAnalysis: new Date().toISOString(),
metrics: {
bugs: '0',
reliability_rating: '1.0',
vulnerabilities: '0',
security_rating: '1.0',
code_smells: '0',
sqale_rating: '1.0',
coverage: '0.0',
duplicated_lines_density: '0.0',
},
projectUrl: `/#${componentKey}`,
getIssuesUrl: i => `/#${componentKey}/issues/${i}`,
getComponentMeasuresUrl: i => `/#${componentKey}/measures/${i}`,
} as FindingSummary;
case 'failed':
return {
lastAnalysis: new Date().toISOString(),
metrics: {
alert_status: 'FAILED',
bugs: '4',
reliability_rating: '2.0',
vulnerabilities: '18',
security_rating: '3.0',
code_smells: '22',
sqale_rating: '5.0',
coverage: '15.7',
duplicated_lines_density: '15.6',
},
projectUrl: `/#${componentKey}`,
getIssuesUrl: i => `/#${componentKey}/issues/${i}`,
getComponentMeasuresUrl: i => `/#${componentKey}/measures/${i}`,
} as FindingSummary;
case 'passed':
return {
lastAnalysis: new Date().toISOString(),
metrics: {
alert_status: 'OK',
bugs: '0',
reliability_rating: '1.0',
vulnerabilities: '0',
security_rating: '1.0',
code_smells: '0',
sqale_rating: '1.0',
coverage: '100.0',
duplicated_lines_density: '0.0',
},
projectUrl: `/#${componentKey}`,
getIssuesUrl: i => `/#${componentKey}/issues/${i}`,
getComponentMeasuresUrl: i => `/#${componentKey}/measures/${i}`,
} as FindingSummary;
default:
return undefined;
}
},
} as SonarQubeApi),
})
.registerPlugin(
createPlugin({
id: 'defectdojo-demo',
register({ router }) {
const entity = (name?: string) =>
({
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
annotations: {
[SONARQUBE_PROJECT_KEY_ANNOTATION]: name,
},
name: name,
},
} as Entity);
const ExamplePage = () => (
<Page themeId="home">
<Header title="SonarQube" />
<Content>
<Grid container>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('empty')} />
</Grid>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('error')} />
</Grid>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('never')} />
</Grid>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('not-computed')} />
</Grid>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('failed')} />
</Grid>
<Grid item xs={12} sm={6} md={4}>
<SonarQubeCard entity={entity('passed')} />
</Grid>
<Grid item xs={12}>
<SonarQubeCard entity={entity(undefined)} />
</Grid>
</Grid>
</Content>
</Page>
);
router.addRoute(
createRouteRef({ path: '/', title: 'SonarQube' }),
ExamplePage,
);
},
}),
)
.render();