Skip to content

Commit

Permalink
Use the options object pattern for frontend API
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Shirley <andrew.shirley@sainsburys.co.uk>
Signed-off-by: blam <ben@blam.sh>
  • Loading branch information
ashirley authored and benjdlambert committed Jul 14, 2021
1 parent 37d0143 commit 91cc491
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
4 changes: 2 additions & 2 deletions plugins/jenkins-backend/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -55,7 +55,7 @@ export interface RouterOptions {
// (undocumented)
jenkinsInfoProvider: JenkinsInfoProvider;
// (undocumented)
logger: Logger;
logger: Logger_2;
}


Expand Down
85 changes: 50 additions & 35 deletions plugins/jenkins/src/api/JenkinsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,35 @@ 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.
*
* 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>;

retry(
entity: EntityName,
jobName: string,
buildNumber: string,
): Promise<void>;
getBuild(options: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<Build>;

retry(options: {
entity: EntityName;
jobName: string;
buildNumber: string;
}): Promise<void>;
}

export class JenkinsClient implements JenkinsApi {
Expand All @@ -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',
Expand All @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion plugins/jenkins/src/components/useBuildWithSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions plugins/jenkins/src/components/useBuilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,18 @@ 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);
}
};

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);

Expand Down

0 comments on commit 91cc491

Please sign in to comment.