feat(jenkins-backend): add multiple branch support

Signed-off-by: planeiii <planeiii@thousandeyes.com>
This commit is contained in:
planeiii
2022-06-16 14:49:36 -05:00
parent fe391c8bb4
commit 8747824221
4 changed files with 49 additions and 7 deletions
+8
View File
@@ -0,0 +1,8 @@
---
'@backstage/plugin-jenkins': patch
'@backstage/plugin-jenkins-backend': patch
---
---
feature: added support for multiple branches to the `JenkinsApi`
@@ -134,6 +134,34 @@ describe('JenkinsApi', () => {
});
expect(result).toHaveLength(1);
});
it('supports multiple branches', async () => {
mockedJenkinsClient.job.get.mockResolvedValue(project);
const result = await jenkinsApi.getProjects(
jenkinsInfo,
'foo,bar,catpants',
);
expect(mockedJenkins).toHaveBeenCalledWith({
baseUrl: jenkinsInfo.baseUrl,
headers: jenkinsInfo.headers,
promisify: true,
});
expect(mockedJenkinsClient.job.get).toBeCalledWith({
name: `${jenkinsInfo.jobFullName}/foo`,
tree: expect.anything(),
});
expect(mockedJenkinsClient.job.get).toBeCalledWith({
name: `${jenkinsInfo.jobFullName}/bar`,
tree: expect.anything(),
});
expect(mockedJenkinsClient.job.get).toBeCalledWith({
name: `${jenkinsInfo.jobFullName}/catpants`,
tree: expect.anything(),
});
expect(result).toHaveLength(1);
});
});
describe('augmented', () => {
const projectWithScmActions: JenkinsProject = {
@@ -75,13 +75,16 @@ export class JenkinsApiImpl {
const projects: BackstageProject[] = [];
if (branch) {
// we have been asked to filter to a single branch.
// Assume jenkinsInfo.jobFullName is a folder which contains one job per branch.
// TODO: extract a strategy interface for this
const job = await client.job.get({
name: `${jenkinsInfo.jobFullName}/${branch}`,
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
});
const job = await Promise.any(
branch.split(/,/g).map(name =>
client.job.get({
name: `${jenkinsInfo.jobFullName}/${name}`,
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
}),
),
);
projects.push(this.augmentProject(job));
} else {
// We aren't filtering
+5 -2
View File
@@ -19,7 +19,7 @@ yarn add --cwd packages/app @backstage/plugin-jenkins
3. Add the `EntityJenkinsContent` extension to the `CI/CD` page and `EntityLatestJenkinsRunCard` to the `overview` page in the app (or wherever you'd prefer):
Note that if you configured a custom JenkinsInfoProvider in step 2, you may need a custom isJenkinsAvailable.
Note that if you configured a custom JenkinsInfoProvider in step 2, you may need a custom isJenkinsAvailable. Also if you're transitioning to a new default branch name, you can pass multiple branch names as a comma-separated list and it will check for each branch name.
```tsx
// In packages/app/src/components/catalog/EntityPage.tsx
@@ -38,7 +38,10 @@ const serviceEntityPage = (
<EntitySwitch>
<EntitySwitch.Case if={isJenkinsAvailable}>
<Grid item sm={6}>
<EntityLatestJenkinsRunCard branch="master" variant="gridItem" />
<EntityLatestJenkinsRunCard
branch="main,master"
variant="gridItem"
/>
</Grid>
</EntitySwitch.Case>
{/* ... */}