Skip to content

Commit

Permalink
feat(npm): add backend to fetch data from private registries
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Jerolimov <jerolimov+git@redhat.com>
  • Loading branch information
christoph-jerolimov committed Nov 5, 2024
1 parent 0b1d398 commit 86495bf
Show file tree
Hide file tree
Showing 47 changed files with 1,424 additions and 210 deletions.
20 changes: 8 additions & 12 deletions workspaces/npm/app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,17 @@ catalog:
rules:
- allow: [Component, System, API, Resource, Location]
locations:
# Local example data, file locations are relative to the backend process, typically `packages/backend`
# Local example data
- type: file
target: ../../examples/entities.yaml

# Local example template
- type: file
target: ../../examples/template/template.yaml
rules:
- allow: [Template]

# Local example organizational data
- type: file
target: ../../examples/org.yaml
target: ../../examples/example-org.yaml
rules:
- allow: [User, Group]
- type: file
target: ../../examples/example-system.yaml
- type: file
target: ../../examples/npmjs-components.yaml
- type: file
target: ../../examples/github-components.yaml

## Uncomment these lines to add more example data
# - type: url
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions workspaces/npm/examples/example-system.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-system
apiVersion: backstage.io/v1alpha1
kind: System
metadata:
name: backstage
spec:
owner: guests
14 changes: 14 additions & 0 deletions workspaces/npm/examples/github-components.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-component
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: github-packages-examples
annotations:
npm/registry: github
npm/package: '@github-packages-examples/npm-publish'
spec:
type: library
lifecycle: production
owner: guests
system: backstage
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
---
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-system
apiVersion: backstage.io/v1alpha1
kind: System
metadata:
name: backstage
spec:
owner: guests
---
# https://backstage.io/docs/features/software-catalog/descriptor-format#kind-component
apiVersion: backstage.io/v1alpha1
kind: Component
Expand Down
8 changes: 0 additions & 8 deletions workspaces/npm/examples/template/content/catalog-info.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions workspaces/npm/examples/template/content/package.json

This file was deleted.

74 changes: 0 additions & 74 deletions workspaces/npm/examples/template/template.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion workspaces/npm/packages/app/e2e-tests/app.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020 The Backstage Authors
* Copyright 2024 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.
Expand Down
1 change: 1 addition & 0 deletions workspaces/npm/packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"build-image": "docker build ../.. -f Dockerfile --tag backstage"
},
"dependencies": {
"@backstage-community/plugin-npm-backend": "workspace:^",
"@backstage/backend-defaults": "^0.5.2",
"@backstage/config": "^1.2.0",
"@backstage/plugin-app-backend": "^0.3.76",
Expand Down
1 change: 1 addition & 0 deletions workspaces/npm/packages/backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ backend.add(import('@backstage/plugin-search-backend-module-pg/alpha'));
backend.add(import('@backstage/plugin-search-backend-module-catalog/alpha'));
backend.add(import('@backstage/plugin-search-backend-module-techdocs/alpha'));

backend.add(import('@backstage-community/plugin-npm-backend'));
backend.start();
1 change: 1 addition & 0 deletions workspaces/npm/plugins/npm-backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
28 changes: 28 additions & 0 deletions workspaces/npm/plugins/npm-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# npm

This plugin backend was templated using the Backstage CLI. You should replace this text with a description of your plugin backend.

## Installation

This plugin is installed via the `@backstage-community/plugin-npm-backend` package. To install it to your backend package, run the following command:

```bash
# From your root directory
yarn --cwd packages/backend add @backstage-community/plugin-npm-backend
```

Then add the plugin to your backend in `packages/backend/src/index.ts`:

```ts
const backend = createBackend();
// ...
backend.add(import('@backstage-community/plugin-npm-backend'));
```

## Development

This plugin backend can be started in a standalone mode from directly in this
package with `yarn start`. It is a limited setup that is most convenient when
developing the plugin backend itself.

If you want to run the entire project, including the frontend, run `yarn dev` from the root directory.
10 changes: 10 additions & 0 deletions workspaces/npm/plugins/npm-backend/catalog-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: backstage-plugin-npm
title: '@backstage-community/plugin-npm-backend'
description: A Backstage plugin that shows meta info and latest versions from a npm registry
spec:
lifecycle: experimental
type: backstage-backend-plugin
owner: maintainers
34 changes: 34 additions & 0 deletions workspaces/npm/plugins/npm-backend/config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2024 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 {
npm?: {
registries?: {
/**
* Registry name
*/
name?: string;
/**
* Registry base url
*/
url?: string;
/**
* Registry auth token
* @visibility secret
*/
token?: string;
}[];
};
}
75 changes: 75 additions & 0 deletions workspaces/npm/plugins/npm-backend/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2024 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 { createBackend } from '@backstage/backend-defaults';
import { mockServices } from '@backstage/backend-test-utils';
import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils';

// TEMPLATE NOTE:
// This is the development setup for your plugin that wires up a
// minimal backend that can use both real and mocked plugins and services.
//
// Start up the backend by running `yarn start` in the package directory.
// Once it's up and running, try out the following requests:
//
// Create a new todo item, standalone or for the sample component:
//
// curl http://localhost:7007/api/npm/todos -H 'Content-Type: application/json' -d '{"title": "My Todo"}'
// curl http://localhost:7007/api/npm/todos -H 'Content-Type: application/json' -d '{"title": "My Todo", "entityRef": "component:default/sample"}'
//
// List TODOs:
//
// curl http://localhost:7007/api/npm/todos
//
// Explicitly make an unauthenticated request, or with service auth:
//
// curl http://localhost:7007/api/npm/todos -H 'Authorization: Bearer mock-none-token'
// curl http://localhost:7007/api/npm/todos -H 'Authorization: Bearer mock-service-token'

const backend = createBackend();

// TEMPLATE NOTE:
// Mocking the auth and httpAuth service allows you to call your plugin API without
// having to authenticate.
//
// If you want to use real auth, you can install the following instead:
// backend.add(import('@backstage/plugin-auth-backend'));
// backend.add(import('@backstage/plugin-auth-backend-module-guest-provider'));
backend.add(mockServices.auth.factory());
backend.add(mockServices.httpAuth.factory());

// TEMPLATE NOTE:
// Rather than using a real catalog you can use a mock with a fixed set of entities.
backend.add(
catalogServiceMock.factory({
entities: [
{
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'sample',
title: 'Sample Component',
},
spec: {
type: 'service',
},
},
],
}),
);

backend.add(import('../src'));

backend.start();
Loading

0 comments on commit 86495bf

Please sign in to comment.