Merge pull request #25281 from stephenglass/fix/dry-run-user

Fix user entity not being fetched for scaffolder dry runner
This commit is contained in:
Fredrik Adelöw
2024-06-25 11:11:06 +02:00
committed by GitHub
4 changed files with 50 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Fix user entity not being fetched for scaffolder dry runner
@@ -38,12 +38,17 @@ import {
BackstageCredentials,
resolveSafeChildPath,
} from '@backstage/backend-plugin-api';
import type { UserEntity } from '@backstage/catalog-model';
interface DryRunInput {
spec: TaskSpec;
secrets?: TaskSecrets;
directoryContents: SerializedFile[];
credentials: BackstageCredentials;
user?: {
entity?: UserEntity;
ref?: string;
};
}
interface DryRunResult {
@@ -693,6 +693,34 @@ data: {"id":1,"taskId":"a-random-id","type":"completion","createdAt":"","body":{
expect(subscriber!.closed).toBe(true);
});
});
describe('POST /v2/dry-run', () => {
it('should get user entity', async () => {
const mockToken = mockCredentials.user.token();
const mockTemplate = getMockTemplate();
const catalogSpy = jest.spyOn(catalogClient, 'getEntityByRef');
await request(app)
.post('/v2/dry-run')
.set('Authorization', `Bearer ${mockToken}`)
.send({
template: mockTemplate,
values: {
requiredParameter1: 'required-value-1',
requiredParameter2: 'required-value-2',
},
directoryContents: [],
});
expect(catalogSpy).toHaveBeenCalledTimes(1);
expect(catalogSpy).toHaveBeenCalledWith(
'user:default/mock',
expect.anything(),
);
});
});
});
describe('providing an identity api', () => {
@@ -734,6 +734,14 @@ export async function createRouter(
targetPluginId: 'catalog',
});
const userEntityRef = auth.isPrincipal(credentials, 'user')
? credentials.principal.userEntityRef
: undefined;
const userEntity = userEntityRef
? await catalogClient.getEntityByRef(userEntityRef, { token })
: undefined;
for (const parameters of [template.spec.parameters ?? []].flat()) {
const result = validate(body.values, parameters);
if (!result.valid) {
@@ -754,6 +762,10 @@ export async function createRouter(
steps,
output: template.spec.output ?? {},
parameters: body.values as JsonObject,
user: {
entity: userEntity as UserEntity,
ref: userEntityRef,
},
},
directoryContents: (body.directoryContents ?? []).map(file => ({
path: file.path,