Skip to content

Commit

Permalink
plugin(code-coverage): remove support for legacy backend (#2587)
Browse files Browse the repository at this point in the history
* plugin(code-coverage): remove support for legacy backend

Signed-off-by: Kashish Mittal <[email protected]>

* yarn dedupe

Signed-off-by: Kashish Mittal <[email protected]>

* README fixes

Signed-off-by: Kashish Mittal <[email protected]>

* regen yarn

Signed-off-by: Kashish Mittal <[email protected]>

---------

Signed-off-by: Kashish Mittal <[email protected]>
  • Loading branch information
04kash authored Feb 4, 2025
1 parent b206871 commit 02cdb81
Show file tree
Hide file tree
Showing 14 changed files with 5,790 additions and 5,987 deletions.
7 changes: 7 additions & 0 deletions workspaces/code-coverage/.changeset/shaggy-dogs-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@backstage-community/plugin-code-coverage-backend': minor
---

**BREAKING** Removed support for the legacy backend system. Please refer to the [README](https://github.com/backstage/community-plugins/blob/main/workspaces/code-coverage/plugins/code-coverage-backend/README.md) for instructions on how to use the new backend system.

Removed usages and references of `@backstage/backend-common`
62 changes: 0 additions & 62 deletions workspaces/code-coverage/plugins/code-coverage-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,68 +9,6 @@ This is the backend part of the `code-coverage` plugin. It takes care of process
yarn --cwd packages/backend add @backstage-community/plugin-code-coverage-backend
```

First create a `codecoverage.ts` file here: `packages/backend/src/plugins`. Now add the following as its content:

```diff
diff --git a/packages/backend/src/plugins/codecoverage.ts b/packages/backend/src/plugins/codecoverage.ts
--- /dev/null
+++ b/packages/backend/src/plugins/codecoverage.ts
@@ -0,0 +1,15 @@
+import { createRouter } from '@backstage-community/plugin-code-coverage-backend';
+import { Router } from 'express';
+import { PluginEnvironment } from '../types';
+
+export default async function createPlugin(
+ env: PluginEnvironment,
+): Promise<Router> {
+ return await createRouter({
+ config: env.config,
+ discovery: env.discovery,
+ database: env.database,
+ urlReader: env.reader,
+ logger: env.logger,
+ });
+}

```

Finally we need to load the plugin in `packages/backend/src/index.ts`, make the following edits:

```diff
diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts
--- a/packages/backend/src/index.ts
+++ b/packages/backend/src/index.ts
@@ -28,6 +28,7 @@ import scaffolder from './plugins/scaffolder';
import proxy from './plugins/proxy';
import techdocs from './plugins/techdocs';
import search from './plugins/search';
+import codeCoverage from './plugins/codecoverage';
import { PluginEnvironment } from './types';
import { ServerPermissionClient } from '@backstage/plugin-permission-node';
import { DefaultIdentityClient } from '@backstage/plugin-auth-node';
@@ -85,6 +86,9 @@ async function main() {
const techdocsEnv = useHotMemoize(module, () => createEnv('techdocs'));
const searchEnv = useHotMemoize(module, () => createEnv('search'));
const appEnv = useHotMemoize(module, () => createEnv('app'));
+ const codeCoverageEnv = useHotMemoize(module, () =>
+ createEnv('code-coverage'),
+ );

const apiRouter = Router();
apiRouter.use('/catalog', await catalog(catalogEnv));
@@ -93,6 +97,7 @@ async function main() {
apiRouter.use('/techdocs', await techdocs(techdocsEnv));
apiRouter.use('/proxy', await proxy(proxyEnv));
apiRouter.use('/search', await search(searchEnv));
+ apiRouter.use('/code-coverage', await codeCoverage(codeCoverageEnv));

apiRouter.use(notFoundHandler());
```

## New Backend System

The code coverage backend plugin has support for the [new backend system](https://backstage.io/docs/backend-system/), here's how you can set that up:

In your `packages/backend/src/index.ts` make the following changes:

```diff
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"test": "backstage-cli package test"
},
"dependencies": {
"@backstage/backend-common": "^0.25.0",
"@backstage/backend-defaults": "^0.7.0",
"@backstage/backend-plugin-api": "^1.1.1",
"@backstage/catalog-client": "^1.9.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,9 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { AuthService } from '@backstage/backend-plugin-api';
import { BackendFeature } from '@backstage/backend-plugin-api';
import { CatalogApi } from '@backstage/catalog-client';
import { Config } from '@backstage/config';
import { DatabaseService } from '@backstage/backend-plugin-api';
import { DiscoveryService } from '@backstage/backend-plugin-api';
import express from 'express';
import { HttpAuthService } from '@backstage/backend-plugin-api';
import { LoggerService } from '@backstage/backend-plugin-api';
import { UrlReaderService } from '@backstage/backend-plugin-api';

// @public
const codeCoveragePlugin: BackendFeature;
export default codeCoveragePlugin;

// @public @deprecated (undocumented)
export function createRouter(options: RouterOptions): Promise<express.Router>;

// @public @deprecated (undocumented)
export interface RouterOptions {
// (undocumented)
auth?: AuthService;
// (undocumented)
catalogApi?: CatalogApi;
// (undocumented)
config: Config;
// (undocumented)
database: DatabaseService;
// (undocumented)
discovery: DiscoveryService;
// (undocumented)
httpAuth?: HttpAuthService;
// (undocumented)
logger: LoggerService;
// (undocumented)
urlReader: UrlReaderService;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@
* @packageDocumentation
*/

export { createRouter } from './service/router';
export type { RouterOptions } from './service/router';
export { codeCoveragePlugin as default } from './plugin';
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export const codeCoveragePlugin = createBackendPlugin({
httpRouter: coreServices.httpRouter,
discovery: coreServices.discovery,
database: coreServices.database,
auth: coreServices.auth,
httpAuth: coreServices.httpAuth,
},
async init({
config,
Expand All @@ -43,6 +45,8 @@ export const codeCoveragePlugin = createBackendPlugin({
httpRouter,
discovery,
database,
auth,
httpAuth,
}) {
httpRouter.use(
await createRouter({
Expand All @@ -51,6 +55,8 @@ export const codeCoveragePlugin = createBackendPlugin({
urlReader,
discovery,
database,
auth,
httpAuth,
}),
);
httpRouter.addAuthPolicy({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Knex as KnexType } from 'knex';
import { DatabaseManager } from '@backstage/backend-common';
import { stringifyEntityRef } from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
import { mockServices, TestDatabases } from '@backstage/backend-test-utils';
import {
CodeCoverageDatabase,
CodeCoverageStore,
} from './CodeCoverageDatabase';
import { JsonCodeCoverage } from './types';

const db = DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'better-sqlite3',
connection: ':memory:',
},
},
}),
).forPlugin('code-coverage');

const coverage: Array<JsonCodeCoverage> = [
{
metadata: {
Expand Down Expand Up @@ -96,23 +83,17 @@ const coverage: Array<JsonCodeCoverage> = [
],
},
];
function createDatabaseManager(
client: KnexType,
skipMigrations: boolean = false,
) {
return {
getClient: async () => client,
migrations: {
skip: skipMigrations,
},
};
}

const databases = TestDatabases.create();

let database: CodeCoverageStore;
describe('CodeCoverageDatabase', () => {
beforeAll(async () => {
const client = await db.getClient();
const databaseManager = createDatabaseManager(client);
database = await CodeCoverageDatabase.create(databaseManager);
const knex = await databases.init('SQLITE_3');
const getClient = jest.fn(async () => knex);
database = await CodeCoverageDatabase.create(
mockServices.database.mock({ getClient }),
);

await database.insertCodeCoverage(coverage[0]);
await database.insertCodeCoverage(coverage[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/
import {
PluginDatabaseManager,
DatabaseService,
resolvePackagePath,
} from '@backstage/backend-common';
} from '@backstage/backend-plugin-api';
import { NotFoundError } from '@backstage/errors';
import { parseEntityRef, stringifyEntityRef } from '@backstage/catalog-model';
import { Knex } from 'knex';
Expand Down Expand Up @@ -44,9 +44,7 @@ const migrationsDir = resolvePackagePath(
);

export class CodeCoverageDatabase implements CodeCoverageStore {
static async create(
database: PluginDatabaseManager,
): Promise<CodeCoverageStore> {
static async create(database: DatabaseService): Promise<CodeCoverageStore> {
const knex = await database.getClient();

if (!database.migrations?.skip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import fs from 'fs';
import path from 'path';
import { Cobertura } from './cobertura';
import { CoberturaXML } from './types';
import { getVoidLogger } from '@backstage/backend-common';
import { mockServices } from '@backstage/backend-test-utils';

/* eslint-disable no-restricted-syntax */

describe('convert cobertura', () => {
const converter = new Cobertura(getVoidLogger());
const converter = new Cobertura(mockServices.rootLogger());
[1, 2, 3, 4, 5, 6].forEach(idx => {
let fixture: CoberturaXML;
parseString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import fs from 'fs';
import path from 'path';
import { Jacoco } from './jacoco';
import { JacocoXML } from './types';
import { getVoidLogger } from '@backstage/backend-common';
import { mockServices } from '@backstage/backend-test-utils';

/* eslint-disable no-restricted-syntax */

describe('convert jacoco', () => {
const converter = new Jacoco(getVoidLogger());
const converter = new Jacoco(mockServices.rootLogger());
let fixture: JacocoXML;
parseString(
fs.readFileSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
import fs from 'fs';
import path from 'path';
import { Lcov } from './lcov';
import { getVoidLogger } from '@backstage/backend-common';
import { mockServices } from '@backstage/backend-test-utils';

describe('convert lcov', () => {
const converter = new Lcov(getVoidLogger());
const converter = new Lcov(mockServices.rootLogger());
[1, 2].forEach(idx => {
const lcov = fs
.readFileSync(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import express from 'express';
import request from 'supertest';
import { DatabaseManager } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import { createRouter } from './router';
import { CatalogRequestOptions } from '@backstage/catalog-client';
Expand All @@ -26,7 +25,6 @@ jest.mock('./CodeCoverageDatabase');

import { CodeCoverageDatabase } from './CodeCoverageDatabase';
import {
DatabaseService,
DiscoveryService,
UrlReaderService,
} from '@backstage/backend-plugin-api';
Expand Down Expand Up @@ -62,19 +60,6 @@ jest.mock('@backstage/catalog-client', () => ({
})),
}));

function createDatabase(): DatabaseService {
return DatabaseManager.fromConfig(
new ConfigReader({
backend: {
database: {
client: 'better-sqlite3',
connection: ':memory:',
},
},
}),
).forPlugin('code-coverage');
}

const testDiscovery: jest.Mocked<DiscoveryService> = {
getBaseUrl: jest
.fn()
Expand All @@ -98,7 +83,7 @@ describe('createRouter', () => {
beforeAll(async () => {
const router = await createRouter({
config: new ConfigReader({}),
database: createDatabase(),
database: mockServices.database.mock(),
discovery: testDiscovery,
urlReader: mockUrlReader,
logger: mockServices.logger.mock(),
Expand Down Expand Up @@ -164,10 +149,12 @@ describe('createRouter', () => {
bodySizeLimit: '1b',
},
}),
database: createDatabase(),
database: mockServices.database.mock(),
discovery: testDiscovery,
urlReader: mockUrlReader,
logger: mockServices.logger.mock(),
auth: mockServices.auth.mock(),
httpAuth: mockServices.httpAuth.mock(),
});
app = express().use(router);

Expand Down
Loading

0 comments on commit 02cdb81

Please sign in to comment.