feat(stackoverflow-backend): extract search stack overflow module

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2023-10-09 16:19:16 +02:00
parent a8a4911713
commit 27e22c6a21
17 changed files with 377 additions and 77 deletions
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
@@ -0,0 +1,30 @@
# search-backend-module-stack-overflow
> DISCLAIMER: The new backend system is in alpha, and so are the search backend module support for the new backend system. We don't recommend you to migrate your backend installations to the new system yet. But if you want to experiment, you can find getting started guides below.
This package exports a module that extends the search backend to also indexing the questions exposed by the [`Stack Overflow` API](https://api.stackexchange.com/docs/questions).
## Installation
Add the module package as a dependency:
```bash
# From your Backstage root directory
yarn add --cwd packages/backend @backstage/plugin-search-backend-module-stack-overflow
```
Add the collator to your backend instance, along with the search plugin itself:
```tsx
// packages/backend/src/index.ts
import { createBackend } from '@backstage/backend-defaults';
const backend = createBackend();
backend.add(import('@backstage/plugin-search-backend/alpha'));
backend.add(
import('@backstage/plugin-search-backend-module-stack-overflow/alpha'),
);
backend.start();
```
You may also want to add configuration parameters to your app-config, for example for controlling the scheduled indexing interval. These parameters should be placed under the `stackoverflow` key. See [the config definition file](https://github.com/backstage/backstage/blob/master/plugins/search-backend-module-stack-overflow/config.d.ts) for more details.
@@ -1,4 +1,4 @@
## API Report File for "@backstage/plugin-stack-overflow-backend"
## API Report File for "@backstage/plugin-search-backend-module-stack-overflow"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
@@ -0,0 +1,56 @@
## API Report File for "@backstage/plugin-search-backend-module-stack-overflow"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
/// <reference types="node" />
import { Config } from '@backstage/config';
import { DocumentCollatorFactory } from '@backstage/plugin-search-common';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Logger } from 'winston';
import { Readable } from 'stream';
// @public
export interface StackOverflowDocument extends IndexableDocument {
// (undocumented)
answers: number;
// (undocumented)
tags: string[];
}
// @public
export class StackOverflowQuestionsCollatorFactory
implements DocumentCollatorFactory
{
// (undocumented)
execute(): AsyncGenerator<StackOverflowDocument>;
// (undocumented)
static fromConfig(
config: Config,
options: StackOverflowQuestionsCollatorFactoryOptions,
): StackOverflowQuestionsCollatorFactory;
// (undocumented)
getCollator(): Promise<Readable>;
// (undocumented)
protected requestParams: StackOverflowQuestionsRequestParams;
// (undocumented)
readonly type: string;
}
// @public
export type StackOverflowQuestionsCollatorFactoryOptions = {
baseUrl?: string;
maxPage?: number;
apiKey?: string;
apiAccessToken?: string;
teamName?: string;
requestParams?: StackOverflowQuestionsRequestParams;
logger: Logger;
};
// @public
export type StackOverflowQuestionsRequestParams = {
[key: string]: string | string[] | number;
};
```
@@ -0,0 +1,10 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage-plugin-search-backend-module-stack-overflow
title: '@backstage/plugin-search-backend-module-stack-overflow'
description: A module for the search backend that exports stack overflow modules
spec:
lifecycle: experimental
type: backstage-backend-plugin-module
owner: discoverability-maintainers
@@ -0,0 +1,51 @@
/*
* Copyright 2023 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.
*/
export interface Config {
/**
* Configuration options for the stack overflow plugin
*/
stackoverflow?: {
/**
* The base url of the Stack Overflow API used for the plugin
*/
baseUrl?: string;
/**
* The API key to authenticate to Stack Overflow API
* @visibility secret
*/
apiKey?: string;
/**
* The name of the team for a Stack Overflow for Teams account
*/
teamName?: string;
/**
* The API Access Token to authenticate to Stack Overflow API
* @visibility secret
*/
apiAccessToken?: string;
/**
* Type representing the request parameters.
*/
requestParams?: {
[key: string]: string | string[] | number;
};
};
}
@@ -0,0 +1,65 @@
{
"name": "@backstage/plugin-search-backend-module-stack-overflow",
"description": "A module for the search backend that exports stack overflow modules",
"version": "0.0.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
}
},
"backstage": {
"role": "backend-plugin-module"
},
"homepage": "https://backstage.io",
"repository": {
"type": "git",
"url": "https://github.com/backstage/backstage",
"directory": "plugins/search-backend-module-stack-overflow"
},
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack",
"clean": "backstage-cli package clean"
},
"dependencies": {
"@backstage/backend-common": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/backend-tasks": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/plugin-search-backend-node": "workspace:^",
"@backstage/plugin-search-common": "workspace:^",
"node-fetch": "^2.6.7",
"qs": "^6.9.4",
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"msw": "^1.2.1"
},
"files": [
"dist",
"config.d.ts"
],
"configSchema": "config.d.ts"
}
@@ -0,0 +1,55 @@
/*
* Copyright 2023 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 { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import searchModuleStackOverflowCollator from './alpha';
describe('searchModuleStackOverflowCollator', () => {
const schedule = {
frequency: { minutes: 10 },
timeout: { minutes: 15 },
initialDelay: { seconds: 3 },
};
it('should register the stack overflow collator to the search index registry extension point with factory and schedule', async () => {
const extensionPointMock = {
addCollator: jest.fn(),
};
await startTestBackend({
extensionPoints: [
[searchIndexRegistryExtensionPoint, extensionPointMock],
],
features: [
searchModuleStackOverflowCollator(),
mockServices.rootConfig.factory({
data: {
stackoverflow: {
schedule,
},
},
}),
],
});
expect(extensionPointMock.addCollator).toHaveBeenCalledTimes(1);
expect(extensionPointMock.addCollator).toHaveBeenCalledWith({
factory: expect.objectContaining({ type: 'stack-overflow' }),
schedule: expect.objectContaining({ run: expect.any(Function) }),
});
});
});
@@ -21,7 +21,7 @@ import {
createBackendModule,
} from '@backstage/backend-plugin-api';
import { searchIndexRegistryExtensionPoint } from '@backstage/plugin-search-backend-node/alpha';
import { StackOverflowQuestionsCollatorFactory } from './search';
import { StackOverflowQuestionsCollatorFactory } from './collators';
/**
* @packageDocumentation
@@ -52,9 +52,9 @@ export default createBackendModule({
initialDelay: { seconds: 3 },
};
const schedule = config.has('search.collators.stackoverflow.schedule')
const schedule = config.has('stackoverflow.schedule')
? readTaskScheduleDefinitionFromConfig(
config.getConfig('search.collators.stackoverflow.schedule'),
config.getConfig('stackoverflow.schedule'),
)
: defaultSchedule;
@@ -0,0 +1,22 @@
/*
* 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.
*/
export {
type StackOverflowDocument,
type StackOverflowQuestionsRequestParams,
type StackOverflowQuestionsCollatorFactoryOptions,
StackOverflowQuestionsCollatorFactory,
} from './StackOverflowQuestionsCollatorFactory';
@@ -1,5 +1,5 @@
/*
* Copyright 2022 The Backstage Authors
* Copyright 2023 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.
@@ -14,4 +14,9 @@
* limitations under the License.
*/
export * from './StackOverflowQuestionsCollatorFactory';
/**
* @packageDocumentation
* A module for the search backend that exports Stack Overflow modules.
*/
export * from './collators';
+13 -46
View File
@@ -3,54 +3,21 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
/// <reference types="node" />
import { StackOverflowDocument as StackOverflowDocument_2 } from '@backstage/plugin-search-backend-module-stack-overflow';
import { StackOverflowQuestionsCollatorFactory as StackOverflowQuestionsCollatorFactory_2 } from '@backstage/plugin-search-backend-module-stack-overflow';
import { StackOverflowQuestionsRequestParams as StackOverflowQuestionsRequestParams_2 } from '@backstage/plugin-search-backend-module-stack-overflow';
import { Config } from '@backstage/config';
import { DocumentCollatorFactory } from '@backstage/plugin-search-common';
import { IndexableDocument } from '@backstage/plugin-search-common';
import { Logger } from 'winston';
import { Readable } from 'stream';
// @public @deprecated (undocumented)
export type StackOverflowDocument = StackOverflowDocument_2;
// @public
export interface StackOverflowDocument extends IndexableDocument {
// (undocumented)
answers: number;
// (undocumented)
tags: string[];
}
// @public @deprecated (undocumented)
export const StackOverflowQuestionsCollatorFactory: typeof StackOverflowQuestionsCollatorFactory_2;
// @public
export class StackOverflowQuestionsCollatorFactory
implements DocumentCollatorFactory
{
// (undocumented)
execute(): AsyncGenerator<StackOverflowDocument>;
// (undocumented)
static fromConfig(
config: Config,
options: StackOverflowQuestionsCollatorFactoryOptions,
): StackOverflowQuestionsCollatorFactory;
// (undocumented)
getCollator(): Promise<Readable>;
// (undocumented)
protected requestParams: StackOverflowQuestionsRequestParams;
// (undocumented)
readonly type: string;
}
// @public @deprecated (undocumented)
export type StackOverflowQuestionsCollatorFactoryOptions =
StackOverflowQuestionsCollatorFactory_2;
// @public
export type StackOverflowQuestionsCollatorFactoryOptions = {
baseUrl?: string;
maxPage?: number;
apiKey?: string;
apiAccessToken?: string;
teamName?: string;
requestParams?: StackOverflowQuestionsRequestParams;
logger: Logger;
};
// @public
export type StackOverflowQuestionsRequestParams = {
[key: string]: string | string[] | number;
};
// @public @deprecated (undocumented)
export type StackOverflowQuestionsRequestParams =
StackOverflowQuestionsRequestParams_2;
```
+5 -19
View File
@@ -5,22 +5,9 @@
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public"
},
"exports": {
".": "./src/index.ts",
"./alpha": "./src/alpha.ts",
"./package.json": "./package.json"
},
"typesVersions": {
"*": {
"alpha": [
"src/alpha.ts"
],
"package.json": [
"package.json"
]
}
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "backend-plugin"
@@ -46,10 +33,8 @@
},
"dependencies": {
"@backstage/backend-common": "workspace:^",
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/backend-tasks": "workspace:^",
"@backstage/config": "workspace:^",
"@backstage/plugin-search-backend-node": "workspace:^",
"@backstage/plugin-search-backend-module-stack-overflow": "workspace:^",
"@backstage/plugin-search-common": "workspace:^",
"node-fetch": "^2.6.7",
"qs": "^6.9.4",
@@ -58,6 +43,7 @@
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@backstage/plugin-search-backend-node": "workspace:^",
"msw": "^1.0.0"
},
"files": [
+37 -1
View File
@@ -20,4 +20,40 @@
* @packageDocumentation
*/
export * from './search';
import {
StackOverflowDocument as _StackOverflowDocument,
StackOverflowQuestionsRequestParams as _StackOverflowQuestionsRequestParams,
StackOverflowQuestionsCollatorFactory as _StackOverflowQuestionsCollatorFactory,
StackOverflowQuestionsCollatorFactoryOptions as _StackOverflowQuestionsCollatorFactoryOptions,
} from '@backstage/plugin-search-backend-module-stack-overflow';
/**
* @public
* @deprecated
* Import from `@backstage/plugin-search-backend-module-stack-overflow` instead.
*/
export type StackOverflowDocument = _StackOverflowDocument;
/**
* @public
* @deprecated
* Import from `@backstage/plugin-search-backend-module-stack-overflow` instead.
*/
export type StackOverflowQuestionsRequestParams =
_StackOverflowQuestionsRequestParams;
/**
* @public
* @deprecated
* Import from `@backstage/plugin-search-backend-module-stack-overflow` instead.
*/
export type StackOverflowQuestionsCollatorFactoryOptions =
_StackOverflowQuestionsCollatorFactory;
/**
* @public
* @deprecated
* Import from `@backstage/plugin-search-backend-module-stack-overflow` instead.
*/
export const StackOverflowQuestionsCollatorFactory =
_StackOverflowQuestionsCollatorFactory;
+21 -5
View File
@@ -8847,6 +8847,25 @@ __metadata:
languageName: unknown
linkType: soft
"@backstage/plugin-search-backend-module-stack-overflow@workspace:^, @backstage/plugin-search-backend-module-stack-overflow@workspace:plugins/search-backend-module-stack-overflow":
version: 0.0.0-use.local
resolution: "@backstage/plugin-search-backend-module-stack-overflow@workspace:plugins/search-backend-module-stack-overflow"
dependencies:
"@backstage/backend-common": "workspace:^"
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/backend-tasks": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/plugin-search-backend-node": "workspace:^"
"@backstage/plugin-search-common": "workspace:^"
msw: ^1.2.1
node-fetch: ^2.6.7
qs: ^6.9.4
winston: ^3.2.1
languageName: unknown
linkType: soft
"@backstage/plugin-search-backend-module-techdocs@workspace:^, @backstage/plugin-search-backend-module-techdocs@workspace:plugins/search-backend-module-techdocs":
version: 0.0.0-use.local
resolution: "@backstage/plugin-search-backend-module-techdocs@workspace:plugins/search-backend-module-techdocs"
@@ -9182,16 +9201,15 @@ __metadata:
languageName: unknown
linkType: soft
"@backstage/plugin-stack-overflow-backend@workspace:^, @backstage/plugin-stack-overflow-backend@workspace:plugins/stack-overflow-backend":
"@backstage/plugin-stack-overflow-backend@workspace:plugins/stack-overflow-backend":
version: 0.0.0-use.local
resolution: "@backstage/plugin-stack-overflow-backend@workspace:plugins/stack-overflow-backend"
dependencies:
"@backstage/backend-common": "workspace:^"
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/backend-tasks": "workspace:^"
"@backstage/backend-test-utils": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/config": "workspace:^"
"@backstage/plugin-search-backend-module-stack-overflow": "workspace:^"
"@backstage/plugin-search-backend-node": "workspace:^"
"@backstage/plugin-search-common": "workspace:^"
msw: ^1.0.0
@@ -25317,7 +25335,6 @@ __metadata:
"@backstage/plugin-search-react": "workspace:^"
"@backstage/plugin-sentry": "workspace:^"
"@backstage/plugin-shortcuts": "workspace:^"
"@backstage/plugin-stack-overflow": "workspace:^"
"@backstage/plugin-stackstorm": "workspace:^"
"@backstage/plugin-tech-insights": "workspace:^"
"@backstage/plugin-tech-radar": "workspace:^"
@@ -25505,7 +25522,6 @@ __metadata:
"@backstage/plugin-search-backend-module-techdocs": "workspace:^"
"@backstage/plugin-search-backend-node": "workspace:^"
"@backstage/plugin-sonarqube-backend": "workspace:^"
"@backstage/plugin-stack-overflow-backend": "workspace:^"
"@backstage/plugin-techdocs-backend": "workspace:^"
"@backstage/plugin-todo-backend": "workspace:^"
languageName: unknown