forked from backstage/community-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate announcements-backend from procore-oss (backstage#2399)
* 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
Showing
38 changed files
with
4,754 additions
and
114 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
workspaces/announcements/plugins/announcements-backend/.eslintrc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); |
2 changes: 2 additions & 0 deletions
2
workspaces/announcements/plugins/announcements-backend/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
100
workspaces/announcements/plugins/announcements-backend/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
47 changes: 47 additions & 0 deletions
47
...cements/plugins/announcements-backend/db/migrations/202210311212_announcements_schemas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
48 changes: 48 additions & 0 deletions
48
...ents/plugins/announcements-backend/db/migrations/202304071343_announcements_categories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}; |
31 changes: 31 additions & 0 deletions
31
...ts/plugins/announcements-backend/db/migrations/20240929155025_announcements_add_active.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}; |
25 changes: 25 additions & 0 deletions
25
...ents/plugins/announcements-backend/db/migrations/20240930025128_backfill_active_column.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {}; |
38 changes: 38 additions & 0 deletions
38
workspaces/announcements/plugins/announcements-backend/db/seeds/01_categories.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }, | ||
]); | ||
}; |
Oops, something went wrong.