Use the options object pattern for frontend API

Signed-off-by: Andrew Shirley <andrew.shirley@sainsburys.co.uk>
Signed-off-by: blam <ben@blam.sh>
This commit is contained in:
Andrew Shirley
2021-07-11 13:19:50 +01:00
committed by blam
parent 37d0143e72
commit 91cc4913ab
4 changed files with 57 additions and 39 deletions
+2 -2
View File
@@ -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<express.Router>;
@@ -55,7 +55,7 @@ export interface RouterOptions {
// (undocumented)
jenkinsInfoProvider: JenkinsInfoProvider;
// (undocumented)
logger: Logger;
logger: Logger_2;
}
+49 -34
View File
@@ -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<Project[]>;
getProjects(options: {
entity: EntityRef;
filter: { branch?: string };
}): Promise<Project[]>;
/**
* 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<Build>;
getBuild(options: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<Build>;
retry(
entity: EntityName,
jobName: string,
buildNumber: string,
): Promise<void>;
retry(options: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<void>;
}
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<Project[]> {
async getProjects({
entity,
filter,
}: {
entity: EntityName;
filter: { branch?: string };
}): Promise<Project[]> {
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<Build> {
async getBuild({
entity,
jobName,
buildNumber,
}: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<Build> {
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<void> {
async retry({
entity,
jobName,
buildNumber,
}: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<void> {
const url = `${await this.discoveryApi.getBaseUrl(
'jenkins',
)}/v1/entity/${encodeURIComponent(entity.namespace)}/${encodeURIComponent(
@@ -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);
+5 -2
View File
@@ -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);