Display meaningful error to the output if Gitlab namespace not found inside publish:gitlab

Signed-off-by: Yevhenii Huselietov <yevhenii.huselietov@gmail.com>
This commit is contained in:
Yevhenii Huselietov
2023-09-29 16:33:34 +03:00
parent 436170b07b
commit f41099bb31
3 changed files with 38 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': minor
---
Display meaningful error to the output if Gitlab namespace not found inside `publish:gitlab`.
@@ -393,4 +393,21 @@ describe('publish:gitlab', () => {
visibility: 'private',
});
});
it('should show proper error message when token has insufficient permissions or namespace not found', async () => {
mockGitlabClient.Namespaces.show.mockRejectedValue({
message: '404 Namespace Not Found',
});
const owner = 'infrastructure/devex';
const repoName = 'backstage';
await expect(
action.handler({
...mockContext,
input: { repoUrl: `gitlab.com?owner=${owner}&repo=${repoName}` },
}),
).rejects.toThrow(
`The namespace ${owner} is not found or the user doesn't have permissions to access it`,
);
});
});
@@ -174,20 +174,30 @@ export function createPublishGitlabAction(options: {
[tokenType]: token,
});
let { id: targetNamespace } = (await client.Namespaces.show(owner)) as {
id: number;
};
let targetNamespaceId;
try {
const namespaceResponse = (await client.Namespaces.show(owner)) as {
id: number;
};
targetNamespaceId = namespaceResponse.id;
} catch (e) {
throw new InputError(
`The namespace ${owner} is not found or the user doesn't have permissions to access it`,
);
}
const { id: userId } = (await client.Users.current()) as {
id: number;
};
if (!targetNamespace) {
targetNamespace = userId;
if (!targetNamespaceId) {
targetNamespaceId = userId;
}
const { id: projectId, http_url_to_repo } = await client.Projects.create({
namespace_id: targetNamespace,
namespace_id: targetNamespaceId,
name: repo,
visibility: repoVisibility,
...(topics.length ? { topics } : {}),