Merge branch 'backstage:master' into master
This commit is contained in:
@@ -40,6 +40,7 @@ import React from 'react';
|
||||
import { registerComponentRouteRef } from '../../routes';
|
||||
|
||||
const defaultColumns: TableColumn<CatalogTableRow>[] = [
|
||||
CatalogTable.columns.createTitleColumn({ hidden: true }),
|
||||
CatalogTable.columns.createNameColumn({ defaultKind: 'API' }),
|
||||
CatalogTable.columns.createSystemColumn(),
|
||||
CatalogTable.columns.createOwnerColumn(),
|
||||
|
||||
@@ -136,6 +136,13 @@ export const CatalogTable: {
|
||||
createSpecLifecycleColumn(): TableColumn<CatalogTableRow>;
|
||||
createMetadataDescriptionColumn(): TableColumn<CatalogTableRow>;
|
||||
createTagsColumn(): TableColumn<CatalogTableRow>;
|
||||
createTitleColumn(
|
||||
options?:
|
||||
| {
|
||||
hidden?: boolean | undefined;
|
||||
}
|
||||
| undefined,
|
||||
): TableColumn<CatalogTableRow>;
|
||||
}>;
|
||||
};
|
||||
|
||||
|
||||
@@ -68,6 +68,7 @@ export const CatalogTable = (props: CatalogTableProps) => {
|
||||
|
||||
const defaultColumns: TableColumn<CatalogTableRow>[] = useMemo(() => {
|
||||
return [
|
||||
columnFactories.createTitleColumn({ hidden: true }),
|
||||
columnFactories.createNameColumn({ defaultKind: filters.kind?.value }),
|
||||
...createEntitySpecificColumns(),
|
||||
columnFactories.createMetadataDescriptionColumn(),
|
||||
|
||||
@@ -131,4 +131,14 @@ export const columnFactories = Object.freeze({
|
||||
),
|
||||
};
|
||||
},
|
||||
createTitleColumn(options?: {
|
||||
hidden?: boolean;
|
||||
}): TableColumn<CatalogTableRow> {
|
||||
return {
|
||||
title: 'Title',
|
||||
field: 'entity.metadata.title',
|
||||
hidden: options?.hidden,
|
||||
searchable: true,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
"@material-ui/core": "^4.9.10",
|
||||
"@material-ui/icons": "^4.9.1",
|
||||
"@material-ui/lab": "^4.0.0-alpha.57",
|
||||
"rc-progress": "3.3.3",
|
||||
"rc-progress": "3.4.0",
|
||||
"react-router-dom": "6.0.0-beta.0",
|
||||
"react-use": "^17.2.4"
|
||||
},
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
"express": "^4.17.1",
|
||||
"express-promise-router": "^4.1.0",
|
||||
"jenkins": "^0.28.1",
|
||||
"promise-any-polyfill": "^1.0.1",
|
||||
"winston": "^3.2.1",
|
||||
"yn": "^4.0.0"
|
||||
},
|
||||
|
||||
@@ -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,
|
||||
@@ -134,6 +133,35 @@ 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 = {
|
||||
|
||||
@@ -30,6 +30,12 @@ import {
|
||||
import { jenkinsExecutePermission } from '@backstage/plugin-jenkins-common';
|
||||
import { NotAllowedError } from '@backstage/errors';
|
||||
|
||||
if (!Promise.any) {
|
||||
(async () => {
|
||||
await import('promise-any-polyfill');
|
||||
})();
|
||||
}
|
||||
|
||||
export class JenkinsApiImpl {
|
||||
private static readonly lastBuildTreeSpec = `lastBuild[
|
||||
number,
|
||||
@@ -70,18 +76,21 @@ 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) {
|
||||
// we have been asked to filter to a single 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 client.job.get({
|
||||
name: `${jenkinsInfo.jobFullName}/${branch}`,
|
||||
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
|
||||
});
|
||||
const job = await Promise.any(
|
||||
branches.map(branch =>
|
||||
client.job.get({
|
||||
name: `${jenkinsInfo.jobFullName}/${branch}`,
|
||||
tree: JenkinsApiImpl.jobTreeSpec.replace(/\s/g, ''),
|
||||
}),
|
||||
),
|
||||
);
|
||||
projects.push(this.augmentProject(job));
|
||||
} else {
|
||||
// We aren't filtering
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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>
|
||||
{/* ... */}
|
||||
|
||||
@@ -46,7 +46,7 @@
|
||||
"@material-ui/lab": "4.0.0-alpha.57",
|
||||
"@material-ui/styles": "^4.10.0",
|
||||
"cross-fetch": "^3.1.5",
|
||||
"rc-progress": "3.3.3",
|
||||
"rc-progress": "3.4.0",
|
||||
"react-use": "^17.2.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
|
||||
@@ -11,6 +11,7 @@ import { FactCheckerFactory } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactLifecycle } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactRetriever } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactRetrieverRegistration } from '@backstage/plugin-tech-insights-node';
|
||||
import { FactSchema } from '@backstage/plugin-tech-insights-node';
|
||||
import { Logger } from 'winston';
|
||||
import { PluginDatabaseManager } from '@backstage/backend-common';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
@@ -51,6 +52,22 @@ export type FactRetrieverRegistrationOptions = {
|
||||
lifecycle?: FactLifecycle;
|
||||
};
|
||||
|
||||
// @public (undocumented)
|
||||
export interface FactRetrieverRegistry {
|
||||
// (undocumented)
|
||||
get(retrieverReference: string): FactRetrieverRegistration;
|
||||
// (undocumented)
|
||||
getSchemas(): FactSchema[];
|
||||
// (undocumented)
|
||||
listRegistrations(): FactRetrieverRegistration[];
|
||||
// (undocumented)
|
||||
listRetrievers(): FactRetriever[];
|
||||
// (undocumented)
|
||||
register(registration: FactRetrieverRegistration): void;
|
||||
// (undocumented)
|
||||
readonly retrievers: Map<string, FactRetrieverRegistration>;
|
||||
}
|
||||
|
||||
// @public
|
||||
export type PersistenceContext = {
|
||||
techInsightsStore: TechInsightsStore;
|
||||
@@ -91,7 +108,8 @@ export interface TechInsightsOptions<
|
||||
// (undocumented)
|
||||
discovery: PluginEndpointDiscovery;
|
||||
factCheckerFactory?: FactCheckerFactory<CheckType, CheckResultType>;
|
||||
factRetrievers: FactRetrieverRegistration[];
|
||||
factRetrieverRegistry?: FactRetrieverRegistry;
|
||||
factRetrievers?: FactRetrieverRegistration[];
|
||||
// (undocumented)
|
||||
logger: Logger;
|
||||
// (undocumented)
|
||||
|
||||
@@ -25,5 +25,6 @@ export type {
|
||||
|
||||
export type { PersistenceContext } from './service/persistence/persistenceContext';
|
||||
export { createFactRetrieverRegistration } from './service/fact/createFactRetriever';
|
||||
export type { FactRetrieverRegistry } from './service/fact/FactRetrieverRegistry';
|
||||
export type { FactRetrieverRegistrationOptions } from './service/fact/createFactRetriever';
|
||||
export * from './service/fact/factRetrievers';
|
||||
|
||||
@@ -21,8 +21,21 @@ import {
|
||||
} from '@backstage/plugin-tech-insights-node';
|
||||
import { ConflictError, NotFoundError } from '@backstage/errors';
|
||||
|
||||
export class FactRetrieverRegistry {
|
||||
private readonly retrievers = new Map<string, FactRetrieverRegistration>();
|
||||
/**
|
||||
* @public
|
||||
*
|
||||
*/
|
||||
export interface FactRetrieverRegistry {
|
||||
readonly retrievers: Map<string, FactRetrieverRegistration>;
|
||||
register(registration: FactRetrieverRegistration): void;
|
||||
get(retrieverReference: string): FactRetrieverRegistration;
|
||||
listRetrievers(): FactRetriever[];
|
||||
listRegistrations(): FactRetrieverRegistration[];
|
||||
getSchemas(): FactSchema[];
|
||||
}
|
||||
|
||||
export class DefaultFactRetrieverRegistry implements FactRetrieverRegistry {
|
||||
readonly retrievers = new Map<string, FactRetrieverRegistration>();
|
||||
|
||||
constructor(retrievers: FactRetrieverRegistration[]) {
|
||||
retrievers.forEach(it => {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright 2022 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { buildTechInsightsContext } from './techInsightsContextBuilder';
|
||||
import {
|
||||
DatabaseManager,
|
||||
getVoidLogger,
|
||||
PluginDatabaseManager,
|
||||
ServerTokenManager,
|
||||
} from '@backstage/backend-common';
|
||||
import { ConfigReader } from '@backstage/config';
|
||||
import { TaskScheduler } from '@backstage/backend-tasks';
|
||||
import { DefaultFactRetrieverRegistry } from './fact/FactRetrieverRegistry';
|
||||
import { Knex } from 'knex';
|
||||
|
||||
jest.mock('./fact/FactRetrieverRegistry');
|
||||
jest.mock('./fact/FactRetrieverEngine', () => ({
|
||||
FactRetrieverEngine: {
|
||||
create: jest.fn().mockResolvedValue({
|
||||
schedule: jest.fn(),
|
||||
}),
|
||||
},
|
||||
}));
|
||||
|
||||
describe('buildTechInsightsContext', () => {
|
||||
const pluginDatabase: PluginDatabaseManager = {
|
||||
getClient: () => {
|
||||
return Promise.resolve({
|
||||
migrate: {
|
||||
latest: () => {},
|
||||
},
|
||||
}) as unknown as Promise<Knex>;
|
||||
},
|
||||
};
|
||||
const databaseManager: Partial<DatabaseManager> = {
|
||||
forPlugin: () => pluginDatabase,
|
||||
};
|
||||
const manager = databaseManager as DatabaseManager;
|
||||
const discoveryMock = {
|
||||
getBaseUrl: (_: string) => Promise.resolve('http://mock.url'),
|
||||
getExternalBaseUrl: (_: string) => Promise.resolve('http://mock.url'),
|
||||
};
|
||||
const scheduler = new TaskScheduler(manager, getVoidLogger()).forPlugin(
|
||||
'tech-insights',
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('constructs the default FactRetrieverRegistry if factRetrievers but no factRetrieverRegistry are passed', () => {
|
||||
buildTechInsightsContext({
|
||||
database: pluginDatabase,
|
||||
logger: getVoidLogger(),
|
||||
factRetrievers: [],
|
||||
scheduler: scheduler,
|
||||
config: ConfigReader.fromConfigs([]),
|
||||
discovery: discoveryMock,
|
||||
tokenManager: ServerTokenManager.noop(),
|
||||
});
|
||||
expect(DefaultFactRetrieverRegistry).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('uses factRetrieverRegistry implementation instead of the default FactRetrieverRegistry if it is passed in', () => {
|
||||
const factRetrieverRegistryMock = {} as DefaultFactRetrieverRegistry;
|
||||
|
||||
buildTechInsightsContext({
|
||||
database: pluginDatabase,
|
||||
logger: getVoidLogger(),
|
||||
factRetrievers: [],
|
||||
factRetrieverRegistry: factRetrieverRegistryMock,
|
||||
scheduler: scheduler,
|
||||
config: ConfigReader.fromConfigs([]),
|
||||
discovery: discoveryMock,
|
||||
tokenManager: ServerTokenManager.noop(),
|
||||
});
|
||||
expect(DefaultFactRetrieverRegistry).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
import { FactRetrieverEngine } from './fact/FactRetrieverEngine';
|
||||
import { Logger } from 'winston';
|
||||
import { FactRetrieverRegistry } from './fact/FactRetrieverRegistry';
|
||||
import {
|
||||
DefaultFactRetrieverRegistry,
|
||||
FactRetrieverRegistry,
|
||||
} from './fact/FactRetrieverRegistry';
|
||||
import { Config } from '@backstage/config';
|
||||
import {
|
||||
PluginDatabaseManager,
|
||||
@@ -49,16 +52,25 @@ export interface TechInsightsOptions<
|
||||
CheckResultType extends CheckResult,
|
||||
> {
|
||||
/**
|
||||
* A collection of FactRetrieverRegistrations.
|
||||
* Optional collection of FactRetrieverRegistrations (required if no factRetrieverRegistry passed in).
|
||||
* Used to register FactRetrievers and their schemas and schedule an execution loop for them.
|
||||
*
|
||||
* Not needed if passing in your own FactRetrieverRegistry implementation. Required otherwise.
|
||||
*/
|
||||
factRetrievers: FactRetrieverRegistration[];
|
||||
factRetrievers?: FactRetrieverRegistration[];
|
||||
|
||||
/**
|
||||
* Optional factory exposing a `construct` method to initialize a FactChecker implementation
|
||||
*/
|
||||
factCheckerFactory?: FactCheckerFactory<CheckType, CheckResultType>;
|
||||
|
||||
/**
|
||||
* Optional FactRetrieverRegistry implementation that replaces the default one.
|
||||
*
|
||||
* If passing this in you don't need to pass in factRetrievers also.
|
||||
*/
|
||||
factRetrieverRegistry?: FactRetrieverRegistry;
|
||||
|
||||
logger: Logger;
|
||||
config: Config;
|
||||
discovery: PluginEndpointDiscovery;
|
||||
@@ -109,7 +121,19 @@ export const buildTechInsightsContext = async <
|
||||
tokenManager,
|
||||
} = options;
|
||||
|
||||
const factRetrieverRegistry = new FactRetrieverRegistry(factRetrievers);
|
||||
const buildFactRetrieverRegistry = (): FactRetrieverRegistry => {
|
||||
if (!options.factRetrieverRegistry) {
|
||||
if (!factRetrievers) {
|
||||
throw new Error(
|
||||
'Failed to build FactRetrieverRegistry because no factRetrievers found',
|
||||
);
|
||||
}
|
||||
return new DefaultFactRetrieverRegistry(factRetrievers);
|
||||
}
|
||||
return options.factRetrieverRegistry;
|
||||
};
|
||||
|
||||
const factRetrieverRegistry = buildFactRetrieverRegistry();
|
||||
|
||||
const persistenceContext = await initializePersistenceContext(
|
||||
await database.getClient(),
|
||||
|
||||
Reference in New Issue
Block a user