chore: updated getProjects to accept arrays of branches

Signed-off-by: planeiii <planeiii@thousandeyes.com>
This commit is contained in:
planeiii
2022-06-20 11:54:09 -05:00
parent 8747824221
commit 4764e30cc8
4 changed files with 15 additions and 17 deletions
@@ -118,10 +118,9 @@ describe('JenkinsApi', () => {
it('standard github layout', async () => {
mockedJenkinsClient.job.get.mockResolvedValueOnce(project);
const result = await jenkinsApi.getProjects(
jenkinsInfo,
const result = await jenkinsApi.getProjects(jenkinsInfo, [
'testBranchName',
);
]);
expect(mockedJenkins).toHaveBeenCalledWith({
baseUrl: jenkinsInfo.baseUrl,
@@ -138,10 +137,11 @@ describe('JenkinsApi', () => {
it('supports multiple branches', async () => {
mockedJenkinsClient.job.get.mockResolvedValue(project);
const result = await jenkinsApi.getProjects(
jenkinsInfo,
'foo,bar,catpants',
);
const result = await jenkinsApi.getProjects(jenkinsInfo, [
'foo',
'bar',
'catpants',
]);
expect(mockedJenkins).toHaveBeenCalledWith({
baseUrl: jenkinsInfo.baseUrl,
@@ -70,17 +70,17 @@ export class JenkinsApiImpl {
* Get a list of projects for the given JenkinsInfo.
* @see ../../../jenkins/src/api/JenkinsApi.ts#getProjects
*/
async getProjects(jenkinsInfo: JenkinsInfo, branch?: string) {
async getProjects(jenkinsInfo: JenkinsInfo, branches?: string[]) {
const client = await JenkinsApiImpl.getClient(jenkinsInfo);
const projects: BackstageProject[] = [];
if (branch) {
if (branches) {
// Assume jenkinsInfo.jobFullName is a folder which contains one job per branch.
// TODO: extract a strategy interface for this
const job = await Promise.any(
branch.split(/,/g).map(name =>
branches.map(branch =>
client.job.get({
name: `${jenkinsInfo.jobFullName}/${name}`,
name: `${jenkinsInfo.jobFullName}/${branch}`,
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
}),
),
@@ -64,12 +64,12 @@ export async function createRouter(
request.header('authorization'),
);
const branch = request.query.branch;
let branchStr: string | undefined;
let branches: string[] | undefined;
if (branch === undefined) {
branchStr = undefined;
branches = undefined;
} else if (typeof branch === 'string') {
branchStr = branch;
branches = branch.split(/,/g);
} else {
// this was passed in as something weird -> 400
// https://evanhahn.com/gotchas-with-express-query-parsing-and-how-to-avoid-them/
@@ -88,7 +88,7 @@ export async function createRouter(
},
backstageToken: token,
});
const projects = await jenkinsApi.getProjects(jenkinsInfo, branchStr);
const projects = await jenkinsApi.getProjects(jenkinsInfo, branches);
response.json({
projects: projects,