UNPKG

@immobiliarelabs/backstage-plugin-gitlab

Version:
124 lines (121 loc) 4.46 kB
import React, { useState } from 'react'; import { Progress, InfoCard, StructuredMetadataTable } from '@backstage/core-components'; import { makeStyles, Box, FormControl, Select, MenuItem, FormHelperText } from '@material-ui/core'; import Alert from '@material-ui/lab/Alert'; import { useAsync } from 'react-use'; import { gitlabProjectId, gitlabProjectSlug, gitlabInstance } from '../../gitlabAppData.esm.js'; import { GitlabCIApiRef } from '../../../api/GitlabCIApi.esm.js'; import '../../utils.esm.js'; import '@gitbeaker/rest'; import dayjs from 'dayjs'; import { useApi } from '@backstage/core-plugin-api'; import { useTranslationRef } from '@backstage/core-plugin-api/alpha'; import { gitlabTranslationRef } from '../../../translation.esm.js'; const useStyles = makeStyles((theme) => ({ infoCard: { marginBottom: theme.spacing(3), "& + .MuiAlert-root": { marginTop: theme.spacing(3) }, "& .MuiCardContent-root": { padding: theme.spacing(2, 1, 2, 2) }, "& td": { whiteSpace: "normal" } } })); function evalStats(mergeRequestStatusData) { let sumOfDiff = 0; let closedCount = 0; let mergedCount = 0; mergeRequestStatusData?.forEach((element) => { sumOfDiff += element.merged_at ? new Date(element.merged_at).getTime() - new Date(element.created_at).getTime() : 0; mergedCount += element.merged_at ? 1 : 0; closedCount += element.closed_at ? 1 : 0; }); const avgTimeUntilMergeDiff = dayjs.duration(sumOfDiff / mergedCount); return { avgTimeUntilMerge: avgTimeUntilMergeDiff, mergedCount, closedCount }; } const MergeRequestStats = (props) => { const [count, setCount] = useState(20); const classes = useStyles(); const project_id = gitlabProjectId(); const project_slug = gitlabProjectSlug(); const gitlab_instance = gitlabInstance(); const { t } = useTranslationRef(gitlabTranslationRef); const GitlabCIAPI = useApi(GitlabCIApiRef).build( gitlab_instance || "gitlab.com" ); const { value, loading, error } = useAsync(async () => { const projectDetails = await GitlabCIAPI.getProjectDetails( project_slug || project_id ); if (!projectDetails) throw new Error("wrong project_slug or project_id"); const mergeRequestStatusData = await GitlabCIAPI.getMergeRequestsStatusSummary( projectDetails.id, count ); if (!mergeRequestStatusData) throw new Error("getMergeRequestsStatusSummary error"); const stats = evalStats(mergeRequestStatusData); if (stats.mergedCount === 0) return { avgTimeUntilMerge: "Never", mergedToTotalRatio: "0%" }; return { avgTimeUntilMerge: stats.avgTimeUntilMerge.humanize(), mergedToTotalRatio: `${Math.round( stats.mergedCount / count * 100 )}%` }; }, [count]); if (loading) { return /* @__PURE__ */ React.createElement(Progress, null); } else if (error) { return /* @__PURE__ */ React.createElement(Alert, { severity: "error" }, error.message); } return value ? /* @__PURE__ */ React.createElement( InfoCard, { title: t("mergeRequestStats.title"), className: classes.infoCard, variant: props.variant }, /* @__PURE__ */ React.createElement(Box, { position: "relative" }, /* @__PURE__ */ React.createElement( StructuredMetadataTable, { metadata: value, options: { titleFormat: (key) => t( `mergeRequestStats.${key}` ) } } ), /* @__PURE__ */ React.createElement(Box, { display: "flex", justifyContent: "flex-end" }, /* @__PURE__ */ React.createElement(FormControl, null, /* @__PURE__ */ React.createElement( Select, { value: count, onChange: (event) => setCount(Number(event.target.value)) }, /* @__PURE__ */ React.createElement(MenuItem, { value: 10 }, "10"), /* @__PURE__ */ React.createElement(MenuItem, { value: 20 }, "20"), /* @__PURE__ */ React.createElement(MenuItem, { value: 50 }, "50"), /* @__PURE__ */ React.createElement(MenuItem, { value: 100 }, "100") ), /* @__PURE__ */ React.createElement(FormHelperText, null, t("mergeRequestStats.helperText"))))) ) : /* @__PURE__ */ React.createElement( InfoCard, { title: t("mergeRequestStats.title"), variant: props.variant } ); }; export { MergeRequestStats as default }; //# sourceMappingURL=MergeRequestStats.esm.js.map