-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7ee300
commit 0946e52
Showing
10 changed files
with
339 additions
and
15 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
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
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
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
111 changes: 111 additions & 0 deletions
111
packages/gitlab/src/components/widgets/CoverageCard/CoverageCard.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React from 'react'; | ||
import { Progress } from '@backstage/core-components'; | ||
import { Box, makeStyles } from '@material-ui/core'; | ||
import Alert from '@material-ui/lab/Alert'; | ||
import { useAsync } from 'react-use'; | ||
import { | ||
gitlabInstance, | ||
gitlabProjectId, | ||
gitlabProjectSlug, | ||
} from '../../gitlabAppData'; | ||
import { GitlabCIApiRef } from '../../../api'; | ||
import { useApi } from '@backstage/core-plugin-api'; | ||
import { InfoCard, InfoCardVariants } from '@backstage/core-components'; | ||
import { LineChart } from '@mui/x-charts/LineChart'; | ||
import dayjs from 'dayjs'; | ||
|
||
const useStyles = makeStyles(() => ({ | ||
lineChartContainer: { | ||
'& .v5-MuiChartsAxis-directionX .v5-MuiChartsAxis-tickLabel': { | ||
transform: 'rotate(90deg)', | ||
textAnchor: 'start', | ||
}, | ||
'& .v5-MuiLineElement-root, .MuiMarkElement-root': { | ||
strokeWidth: 1, | ||
}, | ||
}, | ||
})); | ||
|
||
type Props = { | ||
variant?: InfoCardVariants; | ||
}; | ||
|
||
const CoverageCard = (props: Props) => { | ||
const project_id = gitlabProjectId(); | ||
const project_slug = gitlabProjectSlug(); | ||
const gitlab_instance = gitlabInstance(); | ||
|
||
const classes = useStyles(); | ||
|
||
const GitlabCIAPI = useApi(GitlabCIApiRef).build( | ||
gitlab_instance || 'gitlab.com' | ||
); | ||
|
||
const { value, loading, error } = useAsync(async () => { | ||
const coverageDetails = await GitlabCIAPI.getProjectCoverage( | ||
project_slug || project_id | ||
); | ||
|
||
return coverageDetails; | ||
}, []); | ||
|
||
if (loading) { | ||
return <Progress />; | ||
} else if (error) { | ||
return <Alert severity="error">{error.message}</Alert>; | ||
} | ||
|
||
const dataset = value | ||
? [ | ||
...new Map( | ||
value.data.project.pipelines.nodes | ||
.filter((node) => !!node.coverage) | ||
.sort((a, b) => | ||
new Date(a.createdAt) > new Date(b.createdAt) ? 1 : -1 | ||
) | ||
.map((node) => ({ | ||
x: dayjs(node.createdAt).format('YYYY-MM-DD'), | ||
y: node.coverage, | ||
})) | ||
.map((node) => [`${node.x}_${node.y}`, node]) | ||
).values(), | ||
] | ||
: []; | ||
|
||
return value ? ( | ||
<InfoCard | ||
title="Coverage statistics" | ||
deepLink={{ | ||
link: `${value.data.project.webUrl}/-/graphs/main/charts`, | ||
title: 'go to Analytics', | ||
onClick: (e) => { | ||
e.preventDefault(); | ||
window.open(`${value.data.project.webUrl}/-/graphs/main/charts`); | ||
}, | ||
}} | ||
variant={props.variant} | ||
> | ||
<Box position="relative"> | ||
<div className={classes.lineChartContainer}> | ||
<LineChart | ||
xAxis={[{ scaleType: 'point', dataKey: 'x' }]} | ||
series={[{ dataKey: 'y' }]} | ||
dataset={dataset} | ||
height={300} | ||
margin={{ bottom: 90, top: 20, left: 40, right: 20 }} | ||
/> | ||
</div> | ||
|
||
<div> | ||
{' '} | ||
<b>Last Coverage: </b> | ||
{`${value.data.project.pipelines.nodes[0].coverage}%`} | ||
</div> | ||
</Box> | ||
</InfoCard> | ||
) : ( | ||
<InfoCard title="Coverage Statistics" /> | ||
); | ||
}; | ||
|
||
export default CoverageCard; |
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 @@ | ||
export { default as CoverageCard } from './CoverageCard'; |
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
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
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.