Skip to content

Commit

Permalink
feat(plugins): jenkins plugin implemented (#217)
Browse files Browse the repository at this point in the history
jenkins plugin implemented

ARC-101

## Description

Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] Intermediate change (work in progress)

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration

- [ ] Test A
- [ ] Test B

## Checklist:

- [ ] Performed a self-review of my own code
- [ ] npm test passes on your machine
- [ ] New tests added or existing tests modified to cover all changes
- [ ] Code conforms with the style guide
- [ ] API Documentation in code was updated
- [ ] Any dependent changes have been merged and published in downstream
modules
  • Loading branch information
sadarunnisa-sf authored Dec 24, 2024
1 parent 515ad30 commit 60004f9
Showing 1 changed file with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,56 +84,55 @@ export class JenkinsApiImpl {
*/
async getProjects(jenkinsInfo: JenkinsInfo, branches?: string[]) {
const client = await JenkinsApiImpl.getClient(jenkinsInfo);
const projects: BackstageProject[] = [];
const projects = [];
const treeSpec = JenkinsApiImpl.jobsTreeSpec;

if (branches) {
// Assume jenkinsInfo.jobFullName is a MultiBranch Pipeline project which contains one job per branch.

const job = await Promise.any(
branches.map(branch =>
client.job.get({
name: `${jenkinsInfo.jobFullName}/${encodeURIComponent(branch)}`,
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
}),
),
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ""),
})
)
);
projects.push(this.augmentProject(job));
// @ts-ignore
projects.push(this.augmentProject(job)); //NOSONAR
} else {
// We aren't filtering
// Assume jenkinsInfo.jobFullName is either
// a MultiBranch Pipeline (folder with one job per branch) project
// a Pipeline (standalone) project

// Add count limit to projects
// If limit is set in the config, use it, otherwise use the default limit of 50
const limitedJobsTreeSpec: string = `${JenkinsApiImpl.jobsTreeSpec}{0,${jenkinsInfo.projectCountLimit}}`; //NOSONAR

const project = await client.job.get({
name: jenkinsInfo.jobFullName,
// Filter only be the information we need, instead of loading all fields.
// Whitespaces are only included for readability here and stripped out
// before sending to Jenkins
tree: limitedJobsTreeSpec.replace(/\s/g, ''),
});

const isStandaloneProject = !project.jobs;
if (isStandaloneProject) {
const limitedStandaloneJobTreeSpec = `${JenkinsApiImpl.jobTreeSpec}{0,${jenkinsInfo.projectCountLimit}}`;
const standaloneProject = await client.job.get({
name: jenkinsInfo.jobFullName,
tree: limitedStandaloneJobTreeSpec.replace(/\s/g, ''),
});
projects.push(this.augmentProject(standaloneProject));
return projects;
}
for (const jobDetails of project.jobs) {
// for each branch (we assume)
projects.push(this.augmentProject(jobDetails));
const rootProjects = await this.getNestedJobs(client, jenkinsInfo.jobFullName, treeSpec);
// @ts-ignore
projects.push(...rootProjects); //NOSONAR
}

return projects;
}

async getNestedJobs(client:any, jobName : string, treeSpec: string):Promise<any> { //NOSONAR
const project = await client.job.get({
name: jobName,
tree: treeSpec.replace(/\s/g, ""),
});

const projects = [];
if (project.jobs) {
for (const subJob of project.jobs) {
if (subJob._class === 'com.cloudbees.hudson.plugins.folder.Folder') {
const nestedProjects = await this.getNestedJobs(client, subJob.fullName, treeSpec);
// @ts-ignore
projects.push(...nestedProjects); //NOSONAR
} else {
// @ts-ignore
projects.push(this.augmentProject(subJob)); //NOSONAR
}
}
} else {
// @ts-ignore
projects.push(this.augmentProject(project)); //NOSONAR
}
return projects;
}


/**
* Get a single build.
* @see ../../../jenkins/src/api/JenkinsApi.ts#getBuild
Expand Down

0 comments on commit 60004f9

Please sign in to comment.