feat: introduce cicd-statistics-module-gitlab plugin

Signed-off-by: djamaile <rdjamaile@gmail.com>
This commit is contained in:
djamaile
2022-03-11 19:33:32 +01:00
committed by Fredrik Adelöw
parent e9b48b20c9
commit 08d0a6abf1
13 changed files with 326 additions and 3 deletions
+12 -1
View File
@@ -32,8 +32,10 @@ import {
configApiRef,
createApiFactory,
errorApiRef,
githubAuthApiRef,
githubAuthApiRef, gitlabAuthApiRef,
} from '@backstage/core-plugin-api';
import { cicdStatisticsApiRef } from '@backstage/plugin-cicd-statistics';
import {CicdStatisticsApiGitlab} from "@backstage/plugin-cicd-statistics-module-gitlab";
export const apis: AnyApiFactory[] = [
createApiFactory({
@@ -42,6 +44,7 @@ export const apis: AnyApiFactory[] = [
factory: ({ configApi }) => ScmIntegrationsApi.fromConfig(configApi),
}),
ScmAuth.createDefaultApiFactory(),
createApiFactory({
@@ -64,4 +67,12 @@ export const apis: AnyApiFactory[] = [
}),
createApiFactory(costInsightsApiRef, new ExampleCostInsightsClient()),
createApiFactory({
api: cicdStatisticsApiRef,
deps: {gitlabAuthApi: gitlabAuthApiRef},
factory({ gitlabAuthApi }) {
return new CicdStatisticsApiGitlab(gitlabAuthApi);
},
}),
// createApiFactory(cicdStatisticsApiRef, new CicdStatisticsApiGitlab()),
];
@@ -136,6 +136,7 @@ import {
EntityNewRelicDashboardCard,
} from '@backstage/plugin-newrelic-dashboard';
import { EntityGoCdContent, isGoCdAvailable } from '@backstage/plugin-gocd';
import {EntityCicdStatisticsContent} from "@backstage/plugin-cicd-statistics";
import React, { ReactNode, useMemo, useState } from 'react';
@@ -444,6 +445,10 @@ const websiteEntityPage = (
{cicdContent}
</EntityLayout.Route>
<EntityLayout.Route path="/ci-cd-statistics" title="CI/CD Statistics">
<EntityCicdStatisticsContent />
</EntityLayout.Route>
<EntityLayout.Route path="/lighthouse" title="Lighthouse">
<EntityLighthouseContent />
</EntityLayout.Route>
@@ -0,0 +1,3 @@
module.exports = {
extends: [require.resolve('@backstage/cli/config/eslint')],
};
@@ -0,0 +1,9 @@
# cicd-statistics-module-gitlab
## Getting started
Your plugin has been added to the example app in this repository, meaning you'll be able to access it by running `yarn start` in the root directory, and then navigating to [/cicd-statistics-module-gitlab](http://localhost:3000/cicd-statistics-module-gitlab).
You can also serve the plugin in isolation by running `yarn start` in the plugin directory.
This method of serving the plugin provides quicker iteration speed and a faster startup and hot reloads.
It is only meant for local development, and the setup for it can be found inside the [/dev](./dev) directory.
@@ -0,0 +1,48 @@
{
"name": "@backstage/plugin-cicd-statistics-module-gitlab",
"description": "CI/CD Statistics plugin module; Gitlab CICD",
"version": "0.0.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"private": false,
"publishConfig": {
"access": "public",
"main": "dist/index.esm.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "frontend-plugin"
},
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "plugins/cicd-statistics-module-gitlab"
},
"keywords": [
"backstage",
"cicd statestics",
"gitlab"
],
"scripts": {
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test"
},
"dependencies": {
"@backstage/catalog-model": "^0.9.10",
"@backstage/core-plugin-api": "^0.8.0",
"@backstage/plugin-cicd-statistics": "^0.1.0",
"@gitbeaker/browser": "34.2.0",
"@gitbeaker/core": "^35.4.0",
"lodash": "^4.17.21",
"luxon": "^2.0.2",
"p-limit": "^4.0.0"
},
"peerDependencies": {
"react": "^16.13.1 || ^17.0.0"
},
"files": [
"dist"
]
}
@@ -0,0 +1,189 @@
/*
* Copyright 2022 The Backstage Authors
*
* 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 {
CicdStatisticsApi,
CicdState,
CicdConfiguration,
Build,
FilterStatusType,
FetchBuildsOptions,
Stage,
} from '@backstage/plugin-cicd-statistics';
import { Gitlab } from '@gitbeaker/browser';
import { OAuthApi } from '@backstage/core-plugin-api';
import limiterFactory from 'p-limit';
import { Types } from '@gitbeaker/core';
import { Entity, getEntitySourceLocation } from '@backstage/catalog-model';
const statusMap: Record<string, FilterStatusType> = {
manual: 'unknown',
created: 'enqueued',
waiting_for_resource: 'stalled',
preparing: 'unknown',
pending: 'scheduled',
running: 'running',
success: 'succeeded',
failed: 'failed',
canceled: 'aborted',
skipped: 'aborted',
scheduled: 'scheduled',
};
function jobtToBuild(jobs: Array<Types.PipelineSchema>): Build[] {
return jobs.map(j => {
return {
id: j.id.toString(),
status: statusMap[j.status],
branchType: 'master',
duration: 0, // will get filled in later in a seperate API call
requestedAt: new Date(j.created_at),
stages: [],
};
});
}
function jobsToStages(jobs: Array<Types.JobSchema>): Stage[] {
return jobs.map(j => {
const status = statusMap[j.status] ? statusMap[j.status] : 'unknown';
return {
name: j.name,
status: status,
duration: j.duration ? ((j.duration * 1000) as number) : 0,
};
});
}
type GitlabClient = {
api: InstanceType<typeof Gitlab>;
owner: string;
};
export class CicdStatisticsApiGitlab implements CicdStatisticsApi {
readonly #gitLabAuthApi: OAuthApi;
constructor(gitLabAuthApi: OAuthApi) {
this.#gitLabAuthApi = gitLabAuthApi;
}
public async createGitlabApi(
entity: Entity,
scopes: string[],
): Promise<GitlabClient> {
const entityInfo = getEntitySourceLocation(entity);
const url = new URL(entityInfo.target);
const owner = url.pathname.split('/-/blob/')[0];
const oauthToken = await this.#gitLabAuthApi.getAccessToken(scopes);
return {
api: new Gitlab({
host: `https://${url.host}`,
oauthToken,
}),
owner: owner.substring(1),
};
}
private static async updateBuildWithStages(
gitbeaker: InstanceType<typeof Gitlab>,
owner: string,
build: Build,
): Promise<Stage[]> {
const jobs = await gitbeaker.Jobs.showPipelineJobs(
owner,
parseInt(build.id, 10),
);
const stages = jobsToStages(jobs);
return stages;
}
private static async getDurationOfBuild(
gitbeaker: InstanceType<typeof Gitlab>,
owner: string,
build: Build,
): Promise<number> {
const pipeline = (await gitbeaker.Pipelines.show(
owner,
parseInt(build.id, 10),
)) as Types.PipelineExtendedSchema;
return parseInt(pipeline.duration as string, 10) * 1000;
}
private static async getDefaultBranch(
gitbeaker: InstanceType<typeof Gitlab>,
owner: string,
): Promise<string | undefined> {
const branches = await gitbeaker.Branches.all(owner);
return branches.find(b => b.default)?.name;
}
public async fetchBuilds(options: FetchBuildsOptions): Promise<CicdState> {
const {
entity,
updateProgress,
timeFrom,
timeTo,
filterStatus = ['all'],
filterType = 'all',
} = options;
const { api, owner } = await this.createGitlabApi(entity as Entity, [
'read_api',
]);
updateProgress(0, 0, 0);
const branch =
filterType === 'master'
? await CicdStatisticsApiGitlab.getDefaultBranch(api, owner)
: undefined;
const pipelines = await api.Pipelines.all(owner, {
perPage: 25,
updated_after: timeFrom.toISOString(),
updated_before: timeTo.toISOString(),
ref: branch,
});
const limiter = limiterFactory(10);
const builds = jobtToBuild(pipelines).map(async build => ({
...build,
duration: await limiter(() =>
CicdStatisticsApiGitlab.getDurationOfBuild(api, owner, build),
),
stages: await limiter(() =>
CicdStatisticsApiGitlab.updateBuildWithStages(api, owner, build),
),
}));
const promisedBuilds = (await Promise.all(builds)).filter(b =>
filterStatus.includes(b.status),
) as unknown as Build[];
return { builds: promisedBuilds };
}
public async getConfiguration(): Promise<Partial<CicdConfiguration>> {
return {
availableStatuses: [
'succeeded',
'failed',
'enqueued',
'running',
'aborted',
'stalled',
'expired',
'unknown',
] as const,
};
}
}
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* 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.
*/
export * from "./gitlab";
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* 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.
*/
export { CicdStatisticsApiGitlab } from './api';
@@ -0,0 +1,17 @@
/*
* Copyright 2022 The Backstage Authors
*
* 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 '@testing-library/jest-dom';
import 'cross-fetch/polyfill';
+1 -1
View File
@@ -117,7 +117,7 @@ export interface Build {
duration: number;
/** Top-level build stages */
stages: Array<Stage>;
stages?: Array<Stage>;
}
/**
@@ -78,6 +78,9 @@ export function ButtonSwitch<T extends string>(props: ButtonSwitchProps<T>) {
);
const value = switchValue(values[index]);
if(!value){
console.log(values[index]);
}
if (props.multi) {
props.onChange(
props.selection.includes(value as T)
@@ -166,6 +166,8 @@ function CicdCharts(props: CicdChartsProps) {
errorApi.post(chartableStagesState.error);
}, [errorApi, chartableStagesState.error]);
console.log(chartableStagesState);
return (
<Grid container>
<Grid item lg={2} className={classes.pane}>
@@ -68,6 +68,8 @@ export async function cleanupBuildTree(
return map(builds, { chunk: 'idle' }, build => ({
...build,
stages: build.stages.map(stage => recurseStage(stage, [])),
stages: build.stages
? build.stages.map(stage => recurseStage(stage, []))
: [],
}));
}