Pass a catalogClient to the Github Environment Action

Signed-off-by: Raghunandan Balachandran <raghunandan@spotify.com>
This commit is contained in:
Raghunandan Balachandran
2024-09-16 01:36:41 +02:00
parent 51b4b97bdc
commit 878c8080b7
6 changed files with 65 additions and 20 deletions
@@ -312,7 +312,7 @@ export const examples: TemplateExample[] = [
input: {
repoUrl: 'github.com?repo=repository&owner=owner',
name: 'envname',
wait_timer: 1000,
waitTimer: 1000,
},
},
],
@@ -344,7 +344,7 @@ export const examples: TemplateExample[] = [
input: {
repoUrl: 'github.com?repo=repository&owner=owner',
name: 'envname',
reviewers: ['group:defautl/team-a', 'user:defautl/johndoe'],
reviewers: ['group:default/team-a', 'user:default/johndoe'],
},
},
],
@@ -20,6 +20,7 @@ import { ConfigReader } from '@backstage/config';
import { ScmIntegrations } from '@backstage/integration';
import yaml from 'yaml';
import { examples } from './gitHubEnvironment.examples';
import { CatalogApi } from '@backstage/catalog-client';
const mockOctokit = {
rest: {
@@ -33,8 +34,17 @@ const mockOctokit = {
createOrUpdateEnvironment: jest.fn(),
get: jest.fn(),
},
teams: {
getByName: jest.fn(),
},
users: {
getByUsername: jest.fn(),
},
},
};
const mockCatalogClient: Partial<CatalogApi> = {
getEntitiesByRefs: jest.fn(),
};
jest.mock('octokit', () => ({
Octokit: class {
constructor() {
@@ -42,6 +52,9 @@ jest.mock('octokit', () => ({
}
},
}));
jest.mock('@backstage/catalog-client', () => ({
CatalogClient: mockCatalogClient,
}));
const publicKey = '2Sg8iYjAxxmI2LvUXpJjkYrMxURPc8r+dB7TJyvvcCU=';
@@ -72,9 +85,36 @@ describe('github:environment:create examples', () => {
id: 'repoid',
},
});
mockOctokit.rest.users.getByUsername.mockResolvedValue({
data: {
id: 1,
},
});
mockOctokit.rest.teams.getByName.mockResolvedValue({
data: {
id: 2,
},
});
(mockCatalogClient.getEntitiesByRefs as jest.Mock).mockResolvedValue({
items: [
{
kind: 'User',
metadata: {
name: 'johndoe',
},
},
{
kind: 'Group',
metadata: {
name: 'team-a',
},
},
],
});
action = createGithubEnvironmentAction({
integrations,
catalogClient: mockCatalogClient as CatalogApi,
});
});
@@ -909,12 +949,12 @@ describe('github:environment:create examples', () => {
wait_timer: 0,
reviewers: [
{
Type: 'Team',
ID: 1,
type: 'User',
id: 1,
},
{
Type: 'User',
ID: 2,
type: 'Team',
id: 2,
},
],
prevent_self_review: false,
@@ -19,7 +19,7 @@ import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-
import { TemplateAction } from '@backstage/plugin-scaffolder-node';
import { ConfigReader } from '@backstage/config';
import { ScmIntegrations } from '@backstage/integration';
import { CatalogClient } from '@backstage/catalog-client';
import { CatalogApi } from '@backstage/catalog-client';
const mockOctokit = {
rest: {
@@ -42,7 +42,7 @@ const mockOctokit = {
},
};
const mockCatalogClient: Partial<CatalogClient> = {
const mockCatalogClient: Partial<CatalogApi> = {
getEntitiesByRefs: jest.fn(),
};
@@ -121,7 +121,7 @@ describe('github:environment:create', () => {
action = createGithubEnvironmentAction({
integrations,
catalog: mockCatalogClient as unknown as CatalogClient,
catalogClient: mockCatalogClient as CatalogApi,
});
});
@@ -449,7 +449,7 @@ describe('github:environment:create', () => {
...mockContext,
input: {
...mockContext.input,
reviewers: ['group:defautl/team-a', 'user:defautl/johndoe'],
reviewers: ['group:default/team-a', 'user:default/johndoe'],
},
});
@@ -24,7 +24,7 @@ import { getOctokitOptions } from './helpers';
import { Octokit } from 'octokit';
import Sodium from 'libsodium-wrappers';
import { examples } from './gitHubEnvironment.examples';
import { CatalogClient } from '@backstage/catalog-client';
import { CatalogApi } from '@backstage/catalog-client';
/**
* Creates an `github:environment:create` Scaffolder action that creates a Github Environment.
@@ -33,9 +33,9 @@ import { CatalogClient } from '@backstage/catalog-client';
*/
export function createGithubEnvironmentAction(options: {
integrations: ScmIntegrationRegistry;
catalog: CatalogClient;
catalogClient: CatalogApi;
}) {
const { integrations, catalog } = options;
const { integrations, catalogClient } = options;
// For more information on how to define custom actions, see
// https://backstage.io/docs/features/software-templates/writing-custom-actions
return createTemplateAction<{
@@ -188,9 +188,10 @@ export function createGithubEnvironmentAction(options: {
const githubReviewers: { type: 'User' | 'Team'; id: number }[] = [];
if (reviewers) {
// Fetch reviewers from Catalog
const { items: reviewersEntityRefs } = await catalog.getEntitiesByRefs({
entityRefs: reviewers,
});
const { items: reviewersEntityRefs } =
await catalogClient.getEntitiesByRefs({
entityRefs: reviewers,
});
for (const reviewerEntityRef of reviewersEntityRefs) {
if (reviewerEntityRef?.kind === 'User') {
try {
@@ -35,7 +35,7 @@ import {
DefaultGithubCredentialsProvider,
ScmIntegrations,
} from '@backstage/integration';
import { catalogServiceRef } from '@backstage/plugin-catalog-node';
import { CatalogClient } from '@backstage/catalog-client';
/**
* @public
@@ -49,12 +49,15 @@ export const githubModule = createBackendModule({
deps: {
scaffolder: scaffolderActionsExtensionPoint,
config: coreServices.rootConfig,
catalog: catalogServiceRef,
discovery: coreServices.discovery,
},
async init({ scaffolder, config, catalog }) {
async init({ scaffolder, config, discovery }) {
const integrations = ScmIntegrations.fromConfig(config);
const githubCredentialsProvider =
DefaultGithubCredentialsProvider.fromIntegrations(integrations);
const catalogClient = new CatalogClient({
discoveryApi: discovery,
});
scaffolder.addActions(
createGithubActionsDispatchAction({
@@ -70,7 +73,7 @@ export const githubModule = createBackendModule({
}),
createGithubEnvironmentAction({
integrations,
catalog,
catalogClient,
}),
createGithubIssuesLabelAction({
integrations,
@@ -241,6 +241,7 @@ export const createBuiltinActions = (
}),
createGithubEnvironmentAction({
integrations,
catalogClient,
}),
createGithubDeployKeyAction({
integrations,