From d9a4b672c2f81aed85cd0ed6e08107acfc2fc757 Mon Sep 17 00:00:00 2001 From: djamaile Date: Wed, 30 Mar 2022 17:10:16 +0200 Subject: [PATCH] test: add test cases to test if the util functions are working correctly Signed-off-by: djamaile --- .../cicd-statistics-module-gitlab/README.md | 35 +++++++++++- .../src/api/gitlab.ts | 56 +++---------------- 2 files changed, 41 insertions(+), 50 deletions(-) diff --git a/plugins/cicd-statistics-module-gitlab/README.md b/plugins/cicd-statistics-module-gitlab/README.md index 3cce8b0cde..58c053167f 100644 --- a/plugins/cicd-statistics-module-gitlab/README.md +++ b/plugins/cicd-statistics-module-gitlab/README.md @@ -1,5 +1,38 @@ # cicd-statistics-module-gitlab +This is an extension module to the `cicd-statistics` plugin, providing a `CicdStatisticsApiGitlab` that you can use to extract the cicd statistics from your Gitlab repository. + ## Getting started -Will update in another commit +1. Install the `cicd-statistics` and `cicd-statistics-module-gitlab` plugins in the `app` package. + +2. Configure your ApiFactory: + +```tsx +// packages/app/src/apis.ts +import { 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({ + api: cicdStatisticsApiRef, + deps: { gitlabAuthApi: gitlabAuthApiRef }, + factory({ gitlabAuthApi }) { + return new CicdStatisticsApiGitlab(gitlabAuthApi); + }, + }), +]; +``` + +3. Add the component to your EntityPage: + +```tsx +// packages/app/src/components/catalog/EntityPage.tsx +import { EntityCicdStatisticsContent } from '@backstage/plugin-cicd-statistics'; + + + +; +``` diff --git a/plugins/cicd-statistics-module-gitlab/src/api/gitlab.ts b/plugins/cicd-statistics-module-gitlab/src/api/gitlab.ts index 1c4866557c..7439d1800c 100644 --- a/plugins/cicd-statistics-module-gitlab/src/api/gitlab.ts +++ b/plugins/cicd-statistics-module-gitlab/src/api/gitlab.ts @@ -19,54 +19,14 @@ import { 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 = { - 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): 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): 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, - }; - }); -} +import { pipelinesToBuilds, jobsToStages } from './utils'; /** * This type represents a initialized gitlab client with gitbeaker @@ -127,10 +87,10 @@ export class CicdStatisticsApiGitlab implements CicdStatisticsApi { owner: string, build: Build, ): Promise { - const pipeline = (await gitbeaker.Pipelines.show( + const pipeline = await gitbeaker.Pipelines.show( owner, parseInt(build.id, 10), - )) as Types.PipelineExtendedSchema; + ); return parseInt(pipeline.duration as string, 10) * 1000; } @@ -139,7 +99,7 @@ export class CicdStatisticsApiGitlab implements CicdStatisticsApi { owner: string, ): Promise { const branches = await gitbeaker.Branches.all(owner); - return branches.find(b => b.default)?.name; + return branches.find(branch => branch.default)?.name; } public async fetchBuilds(options: FetchBuildsOptions): Promise { @@ -151,9 +111,7 @@ export class CicdStatisticsApiGitlab implements CicdStatisticsApi { filterStatus = ['all'], filterType = 'all', } = options; - const { api, owner } = await this.createGitlabApi(entity as Entity, [ - 'read_api', - ]); + const { api, owner } = await this.createGitlabApi(entity, ['read_api']); updateProgress(0, 0, 0); const branch = @@ -168,7 +126,7 @@ export class CicdStatisticsApiGitlab implements CicdStatisticsApi { }); const limiter = limiterFactory(10); - const builds = jobtToBuild(pipelines).map(async build => ({ + const builds = pipelinesToBuilds(pipelines).map(async build => ({ ...build, duration: await limiter(() => CicdStatisticsApiGitlab.getDurationOfBuild(api, owner, build), @@ -179,7 +137,7 @@ export class CicdStatisticsApiGitlab implements CicdStatisticsApi { })); const promisedBuilds = (await Promise.all(builds)).filter(b => filterStatus.includes(b.status), - ) as unknown as Build[]; + ); return { builds: promisedBuilds }; }