Skip to content

Commit

Permalink
feat: support register to the new backend system
Browse files Browse the repository at this point in the history
Signed-off-by: mingfu <[email protected]>
  • Loading branch information
nanjingfm committed Nov 29, 2023
1 parent 3789218 commit ea86e73
Show file tree
Hide file tree
Showing 6 changed files with 418 additions and 33 deletions.
45 changes: 39 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
- [Features](#features)
- [Screenshots](#screenshots)
- [Setup](#setup)
- [Setup Frontend Plugin](#setup-frontend-plugin)
- [Setup Backend Plugin](#setup-backend-plugin)
- [Register To The New Backend System](#register-to-the-new-backend-system)
- [Annotations](#annotations)
- [Code owners file](#code-owners-file)
- [Old/New GitLab Versions](#oldnew-gitlab-versions)
Expand Down Expand Up @@ -59,7 +62,9 @@ yarn --cwd packages/app add @immobiliarelabs/backstage-plugin-gitlab
yarn --cwd packages/backend add @immobiliarelabs/backstage-plugin-gitlab-backend
```

2. Add a new GitLab tab to the entity page.
### Setup Frontend Plugin

1. Add a new GitLab tab to the entity page.

> NOTE: By default the EntityGitlabContent does not load the README, see the Optional section.
Expand Down Expand Up @@ -88,7 +93,7 @@ const serviceEntityPage = (
);
```

3. (**Optional**) Add the GitLab cards to the Overview tab on the entity page.
2. (**Optional**) Add the GitLab cards to the Overview tab on the entity page.

`packages/app/src/components/catalog/EntityPage.tsx`

Expand Down Expand Up @@ -140,7 +145,7 @@ const overviewContent = (
);
```

4. Add the integration:
3. Add the integration:
`app-config.yaml` add the integration for gitlab:

```yaml
Expand All @@ -152,7 +157,11 @@ integrations:

**Note:** You can have more than one GitLab instance.

5. Add the GitLab Filler Processor, this allows auto-filling of the annotations like the project id and slug:
### Setup Backend Plugin

> NOTE: Currently backstage supports a new way to register backend plugins, see the [Register To The New Backend System](#register-to-the-new-backend-system) section.
1. Add the GitLab Filler Processor, this allows auto-filling of the annotations like the project id and slug:

`packages/backend/src/plugins/catalog.ts`

Expand All @@ -176,7 +185,7 @@ export default async function createPlugin(

This allows auto-filling of the annotations.

6. Add the `gitlab` route by creating the file `packages/backend/src/plugins/gitlab.ts`:
2. Add the `gitlab` route by creating the file `packages/backend/src/plugins/gitlab.ts`:

`packages/backend/src/plugins/gitlab.ts`

Expand Down Expand Up @@ -213,7 +222,7 @@ async function main() {
}
```

7. (**Optional**): You can also add plugin configurations in `app-config.yaml` file:
3. (**Optional**): You can also add plugin configurations in `app-config.yaml` file:

`app-config.yaml`

Expand All @@ -236,6 +245,30 @@ gitlab:
proxySecure: false
```

### Register To The New Backend System

If you're already using the [New Backend System](https://backstage.io/docs/backend-system/), registering backend plugins will become much easier:

`packages/backend/src/index.ts`

```ts
// packages/backend/src/index.ts
import {
gitlabPlugin,
catalogPluginGitlabFillerProcessorModule,
} from '@immobiliarelabs/backstage-plugin-gitlab-backend';

async function start() {
const backend = createBackend();

// ...
backend.add(gitlabPlugin);
backend.add(catalogPluginGitlabFillerProcessorModule);

// ...
}
```

## Annotations

By default, the plugin automatically shows the project info corresponding to the location of the `catalog.yaml` file. But you could need some time to force another project, you can do it with the annotations `gitlab.com/project-id` or `gitlab.com/project-slug`:
Expand Down
2 changes: 2 additions & 0 deletions packages/gitlab-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"@backstage/backend-common": "^0.19.9",
"@backstage/backend-plugin-api": "^0.6.7",
"@backstage/config": "^1.1.1",
"@backstage/integration": "^1.7.0",
"@types/express": "*",
Expand All @@ -48,6 +49,7 @@
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "^0.2.8",
"@backstage/catalog-model": "^1.4.0",
"@backstage/cli": "^0.22.8",
"@backstage/plugin-catalog-common": "^1.0.14",
Expand Down
1 change: 1 addition & 0 deletions packages/gitlab-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './service/router';
export * from './processor';
export * from './plugin';
71 changes: 71 additions & 0 deletions packages/gitlab-backend/src/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { mockServices, startTestBackend } from '@backstage/backend-test-utils';
import request from 'supertest';
import { gitlabPlugin } from './plugin';
import { rest } from 'msw';
import { setupServer } from 'msw/node';

describe('gitlabPlugin', () => {
const mockServer = setupServer(
rest.get(
'https://non-existing-example.com/api/v4/projects/434',
(req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
url: req.url.toString(),
headers: req.headers.all(),
})
);
}
)
);

beforeAll(async () => {
mockServer.listen({
onUnhandledRequest: ({ headers }, print) => {
if (headers.get('User-Agent') === 'supertest') {
return;
}
print.error();
},
});
});

afterAll(() => mockServer.close());

it('can serve values from config', async () => {
const fakeConfig = {
integrations: {
gitlab: [
{
host: 'non-existing-example.com',
apiBaseUrl: 'https://non-existing-example.com/api/v4',
},
],
},
};

const { server } = await startTestBackend({
features: [
gitlabPlugin(),
mockServices.rootConfig.factory({ data: fakeConfig }),
],
});

const agent = request.agent(server);
agent.set('User-Agent', 'supertest');
const response = await agent.get(
'/api/gitlab/rest/non-existing-example.com/projects/434'
);
expect(response.status).toBe(200);
expect(response.body).toEqual({
headers: {
'accept-encoding': 'gzip, deflate',
connection: 'close',
host: 'non-existing-example.com',
'user-agent': 'supertest',
},
url: 'https://non-existing-example.com/api/v4/projects/434',
});
});
});
47 changes: 47 additions & 0 deletions packages/gitlab-backend/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
coreServices,
createBackendModule,
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';
import { loggerToWinstonLogger } from '@backstage/backend-common';
import { GitlabFillerProcessor } from './processor';
import { createRouter } from './service/router';

export const catalogPluginGitlabFillerProcessorModule = createBackendModule({
pluginId: 'catalog',
moduleId: 'gitlabFillerProcessor',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
extensionPoint: catalogProcessingExtensionPoint,
},
async init({ config, extensionPoint }) {
extensionPoint.addProcessor(new GitlabFillerProcessor(config));
},
});
},
});

export const gitlabPlugin = createBackendPlugin({
pluginId: 'gitlab',
register(env) {
env.registerInit({
deps: {
http: coreServices.httpRouter,
logger: coreServices.logger,
config: coreServices.rootConfig,
},
async init({ config, logger, http }) {
const winstonLogger = loggerToWinstonLogger(logger);
http.use(
await createRouter({
logger: winstonLogger,
config,
})
);
},
});
},
});
Loading

0 comments on commit ea86e73

Please sign in to comment.