Skip to content

Commit

Permalink
Migrate announcements-backend from procore-oss (backstage#2399)
Browse files Browse the repository at this point in the history
* migrate announcements-backend

Signed-off-by: Kurt King <[email protected]>

* update workspace deps

Signed-off-by: Kurt King <[email protected]>

* fix: backstage pluginPackages

Signed-off-by: Kurt King <[email protected]>

* remove local.sqlite from git tracking

Signed-off-by: Kurt King <[email protected]>

---------

Signed-off-by: Kurt King <[email protected]>
  • Loading branch information
kurtaking authored Dec 25, 2024
1 parent 9a85445 commit b708db5
Show file tree
Hide file tree
Showing 38 changed files with 4,754 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# local sqlite database
db/local.sqlite
100 changes: 100 additions & 0 deletions workspaces/announcements/plugins/announcements-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# announcements

This is the backend for the Announcements plugin. This plugin provides:

- REST APIs for managing announcements and categories
- Integration with the [`@backstage/plugin-search`](https://github.com/backstage/backstage/tree/master/plugins/search) plugin
- Integration with the [`@backstage/plugin-permission-backend`](https://github.com/backstage/backstage/tree/master/plugins/permission-backend) plugin

## Installation

This plugin is installed via the `@backstage-community/plugin-announcements-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-announcements-backend
```

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

```ts
const backend = createBackend();
// ...
backend.add(import('@backstage-community/plugin-announcements-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.

### Setup

```sh
# install dependencies
yarn install

# set .env
cp env.sample .env
source .env

# start the backend
yarn start
```

### Database

The plugin includes support for postgres and better-sqlite3 databases. By default, the plugin uses a postgres database via docker-compose. Update the `app-config.yaml` to use the `better-sqlite3` database.

#### Postgres

The postgres database can be started with docker-compose. Don't forget to copy the `env.sample`.

```sh
# start the postgres database
docker-compose up -d

# stop the postgres database
docker-compose down -v
```

#### better-sqlite3

The better-sqlite3 database can be seeded with categories and announcements.

With the backend running,

```sh
# runs migrations and seeds the database
yarn db:setup

# or run them separately
yarn db:migrations
yarn db:seed
```

This will create a `local.sqlite` file under the `db/` directory.

Visit [knexjs](https://knexjs.org/guide/migrations.html) to learn more about the database migrations and seeding.

### API Examples

```sh
# get all announcements
curl http://localhost:7007/api/announcements/announcements

# get all categories
curl http://localhost:7007/api/categories
```

```ts
// get all announcements
const response = await fetch(
'http://localhost:7007/api/announcements/announcements',
);
const data = await response.json();
return data;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.
*/

/**
* @param {import('knex').Knex} knex
*/
exports.up = async function up(knex) {
await knex.schema.createTable('announcements', table => {
table.comment('The table for announcements.');
table.text('id').notNullable().primary().comment('Announcement ID');
table
.string('publisher')
.notNullable()
.comment('A catalog reference to the team publishing the announcement.');
table.text('title').notNullable().comment('Title of the announcement.');
table.text('excerpt').notNullable().comment('Short summary (one-liner).');
table.text('body').notNullable();
table
.timestamp('created_at')
.notNullable()
.index('announcements_created_at_idx');
});
};

/**
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
await knex.schema.alterTable('announcements', table => {
table.dropIndex([], 'announcements_created_at_idx');
});

await knex.schema.dropTable('announcements');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.
*/

/**
* @param {import('knex').Knex} knex
*/
exports.up = async function up(knex) {
await knex.schema.createTable('categories', table => {
table.comment('The table for announcement categories.');
table.string('slug').notNullable().primary().comment('Category slug');
table.string('title').notNullable().comment('Title of the category.');
});

await knex.schema.alterTable('announcements', table => {
table.string('category').comment('Announcement category');

table
.foreign('category', 'category_fk')
.references('slug')
.inTable('categories')
.onDelete('SET NULL');
});
};

/**
* @param {import('knex').Knex} knex
*/
exports.down = async function down(knex) {
await knex.schema.alterTable('announcements', table => {
table.dropForeign('category', 'category_fk');
table.dropColumn('category');
});

await knex.schema.dropTable('categories');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*/

exports.up = async function up(knex) {
await knex.schema.alterTable('announcements', table => {
table.boolean('active');
});
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function down(knex) {
await knex.schema.alterTable('announcements', table => {
table.dropColumn('active');
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

exports.up = async function up(knex) {
await knex.table('announcements').update('active', true).whereNull('active');
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function down(_knex) {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
// Deletes ALL existing entries
await knex('categories').del();
await knex('categories').insert([
{ slug: 'infrastructure', title: 'Infrastructure' },
{ slug: 'internal-developer-portal', title: 'IDP' },
{ slug: 'cost-savings', title: 'Cost Savings' },
{ slug: 'javascript', title: 'Javascript' },
{ slug: 'ruby-on-rails', title: 'Ruby on Rails' },
{ slug: 'monolith', title: 'Monolith' },
{ slug: 'micro-service', title: 'Micro Service' },
{ slug: 'engineering-community', title: 'Engineering Community' },
{ slug: 'product-updates', title: 'Product Updates' },
{ slug: 'security', title: 'Security' },
{ slug: 'documentation', title: 'Documenation' },
{ slug: 'events', title: 'Events' },
]);
};
Loading

0 comments on commit b708db5

Please sign in to comment.