Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catalog-backend-module-github-org: fix default namespace for groups #24700

Merged
merged 10 commits into from
May 13, 2024
5 changes: 5 additions & 0 deletions .changeset/red-mangos-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-github-org': patch
---

Fixed an issue where the `catalog-backend-module-github-org` would not correctly create groups using `default` as namespace in case a single organization was configured.
7 changes: 7 additions & 0 deletions .changeset/wild-cats-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@backstage/plugin-catalog-backend-module-github': patch
---

Added `alwaysUseDefaultNamespace` option to `GithubMultiOrgEntityProvider`.

If set to true, the provider will use `default` as the namespace for all group entities. Groups with the same name across different orgs will be considered the same group.
75 changes: 67 additions & 8 deletions docs/backend-system/building-backends/08-migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ catalog:
/* highlight-add-end */
```

To migrate `GithubMultiOrgEntityProvider` and `GithubOrgEntityProvider` to the new backend system, add a reference to `@backstage/plugin-catalog-backend-module-github-org`.
To migrate `GithubMultiOrgEntityProvider` or `GithubOrgEntityProvider` to the new backend system, add a reference to `@backstage/plugin-catalog-backend-module-github-org`.

```ts title="packages/backend/src/index.ts"
backend.add(import('@backstage/plugin-catalog-backend/alpha'));
Expand All @@ -461,20 +461,79 @@ backend.add(import('@backstage/plugin-catalog-backend-module-github-org'));
/* highlight-add-end */
```

If you were providing a `schedule` in code, this now needs to be set via configuration.
All other Github configuration in `app-config.yaml` remains the same.
##### GithubOrgEntityProvider

If you were using `GithubOrgEntityProvider` you might have been configured in code like this:

```ts title="packages/backend/src/plugins/catalog.ts"
// The org URL below needs to match a configured integrations.github entry
// specified in your app-config.
builder.addEntityProvider(
GithubOrgEntityProvider.fromConfig(env.config, {
id: 'production',
orgUrl: 'https://github.com/backstage',
logger: env.logger,
schedule: env.scheduler.createScheduledTaskRunner({
frequency: { minutes: 60 },
timeout: { minutes: 15 },
}),
}),
);
```

This now needs to be set via configuration. The options defined above are now set in `app-config.yaml` instead as shown below:

```yaml title="app-config.yaml"
catalog:
/* highlight-add-start */
providers:
githubOrg:
yourProviderId:
# ...
/* highlight-add-start */
- id: production
githubUrl: 'https://github.com',
orgs: ['backstage']
schedule:
frequency: PT30M
timeout: PT3M
/* highlight-add-end */
timeout: PT15M
/* highlight-add-end */
```

##### GithubMultiOrgEntityProvider

If you were using `GithubMultiOrgEntityProvider` you might have been configured in code like this:

```ts title="packages/backend/src/plugins/catalog.ts"
// The GitHub URL below needs to match a configured integrations.github entry
// specified in your app-config.
builder.addEntityProvider(
GithubMultiOrgEntityProvider.fromConfig(env.config, {
id: 'production',
githubUrl: 'https://github.com',
// Set the following to list the GitHub orgs you wish to ingest from. You can
// also omit this option to ingest all orgs accessible by your GitHub integration
orgs: ['org-a', 'org-b'],
logger: env.logger,
schedule: env.scheduler.createScheduledTaskRunner({
frequency: { minutes: 60 },
timeout: { minutes: 15 },
}),
}),
);
```

This now needs to be set via configuration. The options defined above are now set in `app-config.yaml` instead as shown below:

```yaml title="app-config.yaml"
catalog:
/* highlight-add-start */
providers:
githubOrg:
- id: production
githubUrl: 'https://github.com',
orgs: ['org-a', 'org-b'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah so it wasn't possible to configure this by org after all, that explains the code. Doesn't need to be done now but prolly makes sense to add support for Array<string | { name: string, groupNamespace?: string, userNamespace?: string}> in the future

schedule:
frequency: PT30M
timeout: PT15M
/* highlight-add-end */
```

If you were providing transformers, these can be configured by extending `githubOrgEntityProviderTransformsExtensionPoint`
Expand Down
2 changes: 1 addition & 1 deletion docs/integrations/github/org.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Next add the basic configuration to `app-config.yaml`
catalog:
providers:
githubOrg:
id: github
id: production
githubUrl: https://github.com
orgs: ['organization-1', 'organization-2', 'organization-3']
schedule:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 { LoggerService } from '@backstage/backend-plugin-api';
import {
EntityProvider,
EntityProviderConnection,
} from '@backstage/plugin-catalog-node';

export class GithubOrgEntityCleanerProvider implements EntityProvider {
logger: LoggerService;
constructor(private readonly options: { id: string; logger: LoggerService }) {
this.logger = options.logger.child({ target: this.getProviderName() });
}

getProviderName() {
return `GithubOrgEntityProvider:${this.options.id}`;
}
async connect(connection: EntityProviderConnection) {
// Clean up any existing entities
connection
.applyMutation({
type: 'full',
entities: [],
})
.catch(error => {
this.logger.error('Failed to clean up entities', error);
});
}
}
11 changes: 10 additions & 1 deletion plugins/catalog-backend-module-github-org/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
} from '@backstage/plugin-catalog-backend-module-github';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';
import { eventsServiceRef } from '@backstage/plugin-events-node';
import { GithubOrgEntityCleanerProvider } from './GithubOrgEntityCleanerProvider';

/**
* Interface for {@link githubOrgEntityProviderTransformsExtensionPoint}.
Expand Down Expand Up @@ -99,8 +100,14 @@ export const catalogModuleGithubOrgEntityProvider = createBackendModule({
logger: coreServices.logger,
scheduler: coreServices.scheduler,
},

async init({ catalog, config, events, logger, scheduler }) {
for (const definition of readDefinitionsFromConfig(config)) {
const definitions = readDefinitionsFromConfig(config);

for (const definition of definitions) {
catalog.addEntityProvider(
new GithubOrgEntityCleanerProvider({ id: definition.id, logger }),
);
catalog.addEntityProvider(
GithubMultiOrgEntityProvider.fromConfig(config, {
id: definition.id,
Expand All @@ -113,6 +120,8 @@ export const catalogModuleGithubOrgEntityProvider = createBackendModule({
logger,
userTransformer,
teamTransformer,
alwaysUseDefaultNamespace:
definitions.length === 1 && definition.orgs?.length === 1,
}),
);
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/catalog-backend-module-github/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ export class GithubMultiOrgEntityProvider implements EntityProvider {
orgs?: string[];
userTransformer?: UserTransformer;
teamTransformer?: TeamTransformer;
alwaysUseDefaultNamespace?: boolean;
});
// (undocumented)
connect(connection: EntityProviderConnection): Promise<void>;
Expand All @@ -165,6 +166,7 @@ export class GithubMultiOrgEntityProvider implements EntityProvider {

// @public
export interface GithubMultiOrgEntityProviderOptions {
alwaysUseDefaultNamespace?: boolean;
events?: EventsService;
githubCredentialsProvider?: GithubCredentialsProvider;
githubUrl: string;
Expand Down
Loading
Loading