Merge branch 'master' into entity-page-responsiveness

This commit is contained in:
Philipp Hugenroth
2021-07-19 11:10:43 +02:00
37 changed files with 525 additions and 252 deletions
@@ -0,0 +1,3 @@
module.exports = {
extends: [require.resolve('@backstage/cli/config/eslint.backend')],
};
@@ -0,0 +1,162 @@
# scaffolder-backend-module-cookiecutter
Welcome to the `fetch:cookiecutter` action for the `scaffolder-backend`.
## Getting started
You need to configure the action in your backend:
## From your Backstage root directory
```
cd packages/backend
yarn add @backstage/plugin-scaffolder-backend-module-cookiecutter
```
Configure the action:
(you can check the [docs](https://backstage.io/docs/features/software-templates/writing-custom-actions#registering-custom-actions) to see all options):
```typescript
// packages/backend/src/plugins/scaffolder.ts
const actions = [
createFetchCookiecutterAction({
integrations,
reader,
containerRunner,
}),
...createBuiltInActions({
...
})
];
return await createRouter({
containerRunner,
logger,
config,
database,
catalogClient,
reader,
actions,
});
```
After that you can use the action in your template:
```yaml
apiVersion: backstage.io/v1beta2
kind: Template
metadata:
name: cookiecutter-demo
title: Cookiecutter Test
description: Cookiecutter example
spec:
owner: backstage/techdocs-core
type: service
parameters:
- title: Fill in some steps
required:
- name
- owner
properties:
name:
title: Name
type: string
description: Unique name of the component
ui:autofocus: true
ui:options:
rows: 5
owner:
title: Owner
type: string
description: Owner of the component
ui:field: OwnerPicker
ui:options:
allowedKinds:
- Group
system:
title: System
type: string
description: System of the component
ui:field: EntityPicker
ui:options:
allowedKinds:
- System
defaultKind: System
- title: Choose a location
required:
- repoUrl
- dryRun
properties:
repoUrl:
title: Repository Location
type: string
ui:field: RepoUrlPicker
ui:options:
allowedHosts:
- github.com
dryRun:
title: Only perform a dry run, don't publish anything
type: boolean
default: false
steps:
- id: fetch-base
name: Fetch Base
action: fetch:cookiecutter
input:
url: ./template
values:
name: '{{ parameters.name }}'
owner: '{{ parameters.owner }}'
system: '{{ parameters.system }}'
destination: '{{ parseRepoUrl parameters.repoUrl }}'
- id: publish
if: '{{ not parameters.dryRun }}'
name: Publish
action: publish:github
input:
allowedHosts: ['github.com']
description: 'This is {{ parameters.name }}'
repoUrl: '{{ parameters.repoUrl }}'
- id: register
if: '{{ not parameters.dryRun }}'
name: Register
action: catalog:register
input:
repoContentsUrl: '{{ steps.publish.output.repoContentsUrl }}'
catalogInfoPath: '/catalog-info.yaml'
- name: Results
if: '{{ parameters.dryRun }}'
action: debug:log
input:
listWorkspace: true
output:
links:
- title: Repository
url: '{{ steps.publish.output.remoteUrl }}'
- title: Open in catalog
icon: 'catalog'
entityRef: '{{ steps.register.output.entityRef }}'
```
You can also visit the `/create/actions` route in your Backstage application to find out more about the parameters this action accepts when it's installed to configure how you like.
### Environment setup
The environment needs to have either `cookiecutter` installed and be available in the `PATH` or access to a `docker` daemon so it can spin up a docker container with `cookiecutter` available.
If you are running Backstage from a Docker container and you want to avoid calling a container inside a container, you can set up `cookiecutter` in your own image, this will use the local installation instead.
You can do so by including the following lines in the last step of your Dockerfile:
```dockerfile
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install cookiecutter
```
@@ -0,0 +1,23 @@
## API Report File for "@backstage/plugin-scaffolder-backend-module-cookiecutter"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
/// <reference types="node" />
import { ContainerRunner } from '@backstage/backend-common';
import { ScmIntegrations } from '@backstage/integration';
import { TemplateAction } from '@backstage/plugin-scaffolder-backend';
import { UrlReader } from '@backstage/backend-common';
// Warning: (ae-missing-release-tag) "createFetchCookiecutterAction" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export function createFetchCookiecutterAction(options: {
reader: UrlReader;
integrations: ScmIntegrations;
containerRunner: ContainerRunner;
}): TemplateAction<any>;
// (No @packageDocumentation comment for this package)
```
@@ -0,0 +1,45 @@
{
"name": "@backstage/plugin-scaffolder-backend-module-cookiecutter",
"version": "0.1.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"scripts": {
"start": "backstage-cli backend:dev",
"build": "backstage-cli backend:build",
"lint": "backstage-cli lint",
"test": "backstage-cli test",
"prepack": "backstage-cli prepack",
"postpack": "backstage-cli postpack",
"clean": "backstage-cli clean"
},
"dependencies": {
"@backstage/backend-common": "^0.8.6",
"@backstage/errors": "^0.1.1",
"@backstage/integration": "^0.5.7",
"@backstage/plugin-scaffolder-backend": "^0.14.0",
"@backstage/config": "^0.1.5",
"command-exists": "^1.2.9",
"fs-extra": "10.0.0",
"winston": "^3.2.1",
"cross-fetch": "^3.0.6",
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/cli": "^0.7.3",
"@types/fs-extra": "^9.0.1",
"@types/mock-fs": "^4.13.0",
"@types/jest": "^26.0.7",
"@types/command-exists": "^1.2.0",
"mock-fs": "^4.13.0",
"msw": "^0.29.0"
},
"files": [
"dist"
]
}
@@ -17,9 +17,12 @@ const runCommand = jest.fn();
const commandExists = jest.fn();
const fetchContents = jest.fn();
jest.mock('./helpers', () => ({ fetchContents }));
jest.mock('@backstage/plugin-scaffolder-backend', () => ({
...jest.requireActual('@backstage/plugin-scaffolder-backend'),
fetchContents,
runCommand,
}));
jest.mock('command-exists', () => commandExists);
jest.mock('../helpers', () => ({ runCommand }));
import {
getVoidLogger,
@@ -33,7 +36,7 @@ import os from 'os';
import { PassThrough } from 'stream';
import { createFetchCookiecutterAction } from './cookiecutter';
import { join } from 'path';
import { ActionContext } from '../../types';
import type { ActionContext } from '@backstage/plugin-scaffolder-backend';
describe('fetch:cookiecutter', () => {
const integrations = ScmIntegrations.fromConfig(
@@ -26,9 +26,11 @@ import commandExists from 'command-exists';
import fs from 'fs-extra';
import path, { resolve as resolvePath } from 'path';
import { Writable } from 'stream';
import { runCommand } from '../helpers';
import { createTemplateAction } from '../../createTemplateAction';
import { fetchContents } from './helpers';
import {
runCommand,
createTemplateAction,
fetchContents,
} from '@backstage/plugin-scaffolder-backend';
export class CookiecutterRunner {
private readonly containerRunner: ContainerRunner;
@@ -136,7 +138,7 @@ export function createFetchCookiecutterAction(options: {
}>({
id: 'fetch:cookiecutter',
description:
"Downloads a template from the given URL into the workspace, and runs cookiecutter on it. This action is deprecated in favor of 'fetch:template'. See https://backstage.io/docs/features/software-templates/builtin-actions#migrating-from-fetch-cookiecutter-to-fetch-template for more details.",
'Downloads a template from the given URL into the workspace, and runs cookiecutter on it.',
schema: {
input: {
type: 'object',
@@ -0,0 +1,16 @@
/*
* Copyright 2021 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 { createFetchCookiecutterAction } from './cookiecutter';
@@ -0,0 +1,16 @@
/*
* Copyright 2021 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 * from './fetch';
@@ -0,0 +1,16 @@
/*
* Copyright 2021 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 * from './actions';
+2 -8
View File
@@ -8,6 +8,7 @@
import { CatalogApi } from '@backstage/catalog-client';
import { Config } from '@backstage/config';
import { ContainerRunner } from '@backstage/backend-common';
import { createFetchCookiecutterAction } from '@backstage/plugin-scaffolder-backend-module-cookiecutter';
import { createPullRequest } from 'octokit-plugin-create-pull-request';
import express from 'express';
import { JsonObject } from '@backstage/config';
@@ -78,14 +79,7 @@ export function createCatalogWriteAction(): TemplateAction<any>;
// @public
export function createDebugLogAction(): TemplateAction<any>;
// Warning: (ae-missing-release-tag) "createFetchCookiecutterAction" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
export function createFetchCookiecutterAction(options: {
reader: UrlReader;
integrations: ScmIntegrations;
containerRunner: ContainerRunner;
}): TemplateAction<any>;
export { createFetchCookiecutterAction };
// Warning: (ae-missing-release-tag) "createFetchPlainAction" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
+1
View File
@@ -35,6 +35,7 @@
"@backstage/config": "^0.1.5",
"@backstage/errors": "^0.1.1",
"@backstage/integration": "^0.5.8",
"@backstage/plugin-scaffolder-backend-module-cookiecutter": "^0.1.0",
"@gitbeaker/core": "^30.2.0",
"@gitbeaker/node": "^30.2.0",
"@octokit/rest": "^18.5.3",
@@ -5,5 +5,10 @@ metadata:
description: A collection of all Backstage example templates
spec:
targets:
- ./local-templates.yaml
- ./remote-templates.yaml
# For local development of a template, you can reference your local templates here.
# Examples:
#
# - ./local-template/template.yaml
# - ../all-templates/local/template.yaml
@@ -1,13 +0,0 @@
apiVersion: backstage.io/v1alpha1
kind: Location
metadata:
name: example-templates-local
description: A collection of locally available Backstage example templates
spec:
targets:
- ./docs-template/template.yaml
- ./react-ssr-template/template.yaml
- ./create-react-app/template.yaml
- ./springboot-grpc-template/template.yaml
- ./v1beta2-demo/template.yaml
- ./pull-request/template.yaml
@@ -7,3 +7,9 @@ spec:
type: url
targets:
- https://github.com/spotify/cookiecutter-golang/blob/master/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/create-react-app/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/docs-template/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/pull-request/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/react-ssr-template/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/springboot-grpc-template/template.yaml
- https://github.com/backstage/software-templates/blob/main/scaffolder-templates/v1beta2-demo/template.yaml
@@ -24,11 +24,8 @@ import {
} from './catalog';
import { createDebugLogAction } from './debug';
import {
createFetchCookiecutterAction,
createFetchPlainAction,
createFetchTemplateAction,
} from './fetch';
import { createFetchPlainAction, createFetchTemplateAction } from './fetch';
import { createFetchCookiecutterAction } from '@backstage/plugin-scaffolder-backend-module-cookiecutter';
import {
createFilesystemDeleteAction,
createFilesystemRenameAction,
@@ -15,6 +15,5 @@
*/
export { createFetchPlainAction } from './plain';
export { createFetchCookiecutterAction } from './cookiecutter';
export { createFetchTemplateAction } from './template';
export { fetchContents } from './helpers';
@@ -20,4 +20,6 @@ export * from './debug';
export * from './fetch';
export * from './filesystem';
export * from './publish';
export { createFetchCookiecutterAction } from '@backstage/plugin-scaffolder-backend-module-cookiecutter';
export { runCommand } from './helpers';
+1 -1
View File
@@ -13,7 +13,7 @@ import { DiscoveryApi } from '@backstage/core-plugin-api';
import { Entity } from '@backstage/catalog-model';
import { EntityName } from '@backstage/catalog-model';
import { IdentityApi } from '@backstage/core-plugin-api';
import { Location as Location_2 } from '@backstage/catalog-model';
import { LocationSpec } from '@backstage/catalog-model';
import { RouteRef } from '@backstage/core-plugin-api';
// Warning: (ae-missing-release-tag) "DocsCardGrid" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
+11 -5
View File
@@ -17,7 +17,7 @@
import { EntityName } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { DiscoveryApi, IdentityApi } from '@backstage/core-plugin-api';
import { NotFoundError } from '@backstage/errors';
import { NotFoundError, ResponseError } from '@backstage/errors';
import EventSource from 'eventsource';
import { SyncResult, TechDocsApi, TechDocsStorageApi } from './api';
import { TechDocsEntityMetadata, TechDocsMetadata } from './types';
@@ -72,9 +72,12 @@ export class TechDocsClient implements TechDocsApi {
const request = await fetch(`${requestUrl}`, {
headers: token ? { Authorization: `Bearer ${token}` } : {},
});
const res = await request.json();
return res;
if (!request.ok) {
throw await ResponseError.fromResponse(request);
}
return await request.json();
}
/**
@@ -97,9 +100,12 @@ export class TechDocsClient implements TechDocsApi {
const request = await fetch(`${requestUrl}`, {
headers: token ? { Authorization: `Bearer ${token}` } : {},
});
const res = await request.json();
return res;
if (!request.ok) {
throw await ResponseError.fromResponse(request);
}
return await request.json();
}
}
@@ -30,7 +30,7 @@ export const TechDocsPage = () => {
const techdocsApi = useApi(techdocsApiRef);
const techdocsMetadataRequest = useAsync(() => {
const { value: techdocsMetadataValue } = useAsync(() => {
if (documentReady) {
return techdocsApi.getTechDocsMetadata({ kind, namespace, name });
}
@@ -38,7 +38,7 @@ export const TechDocsPage = () => {
return Promise.resolve(undefined);
}, [kind, namespace, name, techdocsApi, documentReady]);
const entityMetadataRequest = useAsync(() => {
const { value: entityMetadataValue } = useAsync(() => {
return techdocsApi.getEntityMetadata({ kind, namespace, name });
}, [kind, namespace, name, techdocsApi]);
@@ -49,10 +49,8 @@ export const TechDocsPage = () => {
return (
<Page themeId="documentation">
<TechDocsPageHeader
metadataRequest={{
techdocs: techdocsMetadataRequest,
entity: entityMetadataRequest,
}}
techDocsMetadata={techdocsMetadataValue}
entityMetadata={entityMetadataValue}
entityId={{
kind,
namespace,
@@ -30,26 +30,23 @@ describe('<TechDocsPageHeader />', () => {
name: 'test-name',
namespace: 'test-namespace',
}}
metadataRequest={{
entity: {
loading: false,
value: {
locationMetadata: {
type: 'github',
target: 'https://example.com/',
},
spec: {
owner: 'test',
},
},
entityMetadata={{
locationMetadata: {
type: 'github',
target: 'https://example.com/',
},
techdocs: {
loading: false,
value: {
site_name: 'test-site-name',
site_description: 'test-site-desc',
},
apiVersion: 'v1',
kind: 'Component',
metadata: {
name: 'test',
},
spec: {
owner: 'test',
},
}}
techDocsMetadata={{
site_name: 'test-site-name',
site_description: 'test-site-desc',
}}
/>,
{
@@ -75,14 +72,6 @@ describe('<TechDocsPageHeader />', () => {
name: 'test-name',
namespace: 'test-namespace',
}}
metadataRequest={{
entity: {
loading: false,
},
techdocs: {
loading: false,
},
}}
/>,
{
mountedRoutes: {
@@ -105,17 +94,9 @@ describe('<TechDocsPageHeader />', () => {
name: 'test-name',
namespace: 'test-namespace',
}}
metadataRequest={{
entity: {
loading: false,
},
techdocs: {
loading: false,
value: {
site_name: 'test-site-name',
site_description: 'test-site-desc',
},
},
techDocsMetadata={{
site_name: 'test-site-name',
site_description: 'test-site-desc',
}}
/>,
{
@@ -15,6 +15,8 @@
*/
import { EntityName, RELATION_OWNED_BY } from '@backstage/catalog-model';
import { Header, HeaderLabel } from '@backstage/core-components';
import { useRouteRef } from '@backstage/core-plugin-api';
import {
EntityRefLink,
EntityRefLinks,
@@ -22,45 +24,30 @@ import {
} from '@backstage/plugin-catalog-react';
import CodeIcon from '@material-ui/icons/Code';
import React from 'react';
import { AsyncState } from 'react-use/lib/useAsync';
import { rootRouteRef } from '../../routes';
import { TechDocsMetadata } from '../../types';
import { Header, HeaderLabel } from '@backstage/core-components';
import { useRouteRef } from '@backstage/core-plugin-api';
import { TechDocsEntityMetadata, TechDocsMetadata } from '../../types';
type TechDocsPageHeaderProps = {
entityId: EntityName;
metadataRequest: {
entity: AsyncState<any>;
techdocs: AsyncState<TechDocsMetadata>;
};
entityMetadata?: TechDocsEntityMetadata;
techDocsMetadata?: TechDocsMetadata;
};
export const TechDocsPageHeader = ({
entityId,
metadataRequest,
entityMetadata,
techDocsMetadata,
}: TechDocsPageHeaderProps) => {
const {
techdocs: techdocsMetadata,
entity: entityMetadata,
} = metadataRequest;
const { value: techdocsMetadataValues } = techdocsMetadata;
const { value: entityMetadataValues } = entityMetadata;
const { name } = entityId;
const { site_name: siteName, site_description: siteDescription } =
techdocsMetadataValues || {};
techDocsMetadata || {};
const {
locationMetadata,
spec: { lifecycle },
} = entityMetadataValues || { spec: {} };
const { locationMetadata, spec } = entityMetadata || {};
const lifecycle = spec?.lifecycle;
const ownedByRelations = entityMetadataValues
? getEntityRelations(entityMetadataValues, RELATION_OWNED_BY)
const ownedByRelations = entityMetadata
? getEntityRelations(entityMetadata, RELATION_OWNED_BY)
: [];
const docsRootLink = useRouteRef(rootRouteRef)();
+4 -2
View File
@@ -14,11 +14,13 @@
* limitations under the License.
*/
import { Entity, Location } from '@backstage/catalog-model';
import { Entity, LocationSpec } from '@backstage/catalog-model';
export type TechDocsMetadata = {
site_name: string;
site_description: string;
};
export type TechDocsEntityMetadata = Entity & { locationMetadata?: Location };
export type TechDocsEntityMetadata = Entity & {
locationMetadata?: LocationSpec;
};