From 91cc4913ab40277d6502f95b40fa537a93640106 Mon Sep 17 00:00:00 2001 From: Andrew Shirley Date: Sun, 11 Jul 2021 13:19:50 +0100 Subject: [PATCH] Use the options object pattern for frontend API Signed-off-by: Andrew Shirley Signed-off-by: blam --- plugins/jenkins-backend/api-report.md | 4 +- plugins/jenkins/src/api/JenkinsApi.ts | 83 +++++++++++-------- .../src/components/useBuildWithSteps.ts | 2 +- plugins/jenkins/src/components/useBuilds.ts | 7 +- 4 files changed, 57 insertions(+), 39 deletions(-) diff --git a/plugins/jenkins-backend/api-report.md b/plugins/jenkins-backend/api-report.md index 37c1674eff..f81c141f73 100644 --- a/plugins/jenkins-backend/api-report.md +++ b/plugins/jenkins-backend/api-report.md @@ -8,7 +8,7 @@ import { CatalogClient } from '@backstage/catalog-client'; import { Config } from '@backstage/config'; import { EntityName } from '@backstage/catalog-model'; import express from 'express'; -import { Logger } from 'winston'; +import { Logger as Logger_2 } from 'winston'; // @public (undocumented) export function createRouter(options: RouterOptions): Promise; @@ -55,7 +55,7 @@ export interface RouterOptions { // (undocumented) jenkinsInfoProvider: JenkinsInfoProvider; // (undocumented) - logger: Logger; + logger: Logger_2; } diff --git a/plugins/jenkins/src/api/JenkinsApi.ts b/plugins/jenkins/src/api/JenkinsApi.ts index e1e5c2df44..d37fdd2032 100644 --- a/plugins/jenkins/src/api/JenkinsApi.ts +++ b/plugins/jenkins/src/api/JenkinsApi.ts @@ -78,13 +78,13 @@ export interface JenkinsApi { * * Typically, a folder job will be identified and the backend plugin will recursively look for projects (jobs with builds) within that folder. * - * @param entity the entity whose jobs should be retrieved. - * @param filter a filter on jobs. Currently this just takes a branch (and assumes certain structures in jenkins) + * @param options.entity the entity whose jobs should be retrieved. + * @param options.filter a filter on jobs. Currently this just takes a branch (and assumes certain structures in jenkins) */ - getProjects( - entity: EntityRef, - filter: { branch?: string }, - ): Promise; + getProjects(options: { + entity: EntityRef; + filter: { branch?: string }; + }): Promise; /** * Get a single build. @@ -92,21 +92,21 @@ export interface JenkinsApi { * This takes an entity to support selecting between multiple jenkins instances. * * TODO: abstract jobName (so we could support differentiating between the same named job on multiple instances). - * @param entity - * @param jobName - * @param buildNumber + * @param options.entity + * @param options.jobName + * @param options.buildNumber */ - getBuild( - entity: EntityName, - jobName: string, - buildNumber: string, - ): Promise; + getBuild(options: { + entity: EntityName; + jobName: string; + buildNumber: string; + }): Promise; - retry( - entity: EntityName, - jobName: string, - buildNumber: string, - ): Promise; + retry(options: { + entity: EntityName; + jobName: string; + buildNumber: string; + }): Promise; } export class JenkinsClient implements JenkinsApi { @@ -121,10 +121,13 @@ export class JenkinsClient implements JenkinsApi { this.identityApi = options.identityApi; } - async getProjects( - entity: EntityName, - filter: { branch?: string }, - ): Promise { + async getProjects({ + entity, + filter, + }: { + entity: EntityName; + filter: { branch?: string }; + }): Promise { const url = new URL( `${await this.discoveryApi.getBaseUrl( 'jenkins', @@ -149,17 +152,25 @@ export class JenkinsClient implements JenkinsApi { (await response.json()).projects?.map((p: Project) => ({ ...p, onRestartClick: async () => { - await this.retry(entity, p.fullName, String(p.lastBuild.number)); + await this.retry({ + entity, + jobName: p.fullName, + buildNumber: String(p.lastBuild.number), + }); }, })) || [] ); } - async getBuild( - entity: EntityName, - jobName: string, - buildNumber: string, - ): Promise { + async getBuild({ + entity, + jobName, + buildNumber, + }: { + entity: EntityName; + jobName: string; + buildNumber: string; + }): Promise { const url = `${await this.discoveryApi.getBaseUrl( 'jenkins', )}/v1/entity/${encodeURIComponent(entity.namespace)}/${encodeURIComponent( @@ -179,11 +190,15 @@ export class JenkinsClient implements JenkinsApi { return (await response.json()).build; } - async retry( - entity: EntityName, - jobName: string, - buildNumber: string, - ): Promise { + async retry({ + entity, + jobName, + buildNumber, + }: { + entity: EntityName; + jobName: string; + buildNumber: string; + }): Promise { const url = `${await this.discoveryApi.getBaseUrl( 'jenkins', )}/v1/entity/${encodeURIComponent(entity.namespace)}/${encodeURIComponent( diff --git a/plugins/jenkins/src/components/useBuildWithSteps.ts b/plugins/jenkins/src/components/useBuildWithSteps.ts index b109c9f964..e82a4800d5 100644 --- a/plugins/jenkins/src/components/useBuildWithSteps.ts +++ b/plugins/jenkins/src/components/useBuildWithSteps.ts @@ -42,7 +42,7 @@ export function useBuildWithSteps({ const getBuildWithSteps = useCallback(async () => { try { const entityName = await getEntityName(entity); - return api.getBuild(entityName, jobName, buildNumber); + return api.getBuild({ entity: entityName, jobName, buildNumber }); } catch (e) { errorApi.post(e); return Promise.reject(e); diff --git a/plugins/jenkins/src/components/useBuilds.ts b/plugins/jenkins/src/components/useBuilds.ts index 8a68e6f1cf..ad068dd219 100644 --- a/plugins/jenkins/src/components/useBuilds.ts +++ b/plugins/jenkins/src/components/useBuilds.ts @@ -47,7 +47,7 @@ export function useBuilds({ branch }: { branch?: string } = {}) { const restartBuild = async (jobName: string, buildNumber: string) => { try { - await api.retry(entityName, jobName, buildNumber); + await api.retry({ entity: entityName, jobName, buildNumber }); } catch (e) { errorApi.post(e); } @@ -55,7 +55,10 @@ export function useBuilds({ branch }: { branch?: string } = {}) { const { loading, value: projects, retry } = useAsyncRetry(async () => { try { - const build = await api.getProjects(getEntityName(entity), { branch }); + const build = await api.getProjects({ + entity: getEntityName(entity), + filter: { branch }, + }); setTotal(build.length);