Merge pull request #20270 from cursedcoder/fix-gitlab-namespace

Handle gitlab namespace request properly
This commit is contained in:
Patrik Oldsberg
2023-10-03 11:26:58 +02:00
committed by GitHub
3 changed files with 43 additions and 6 deletions
@@ -393,4 +393,23 @@ 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({
response: {
statusCode: 404,
},
});
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,33 @@ 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) {
if (e.response && e.response.statusCode === 404) {
throw new InputError(
`The namespace ${owner} is not found or the user doesn't have permissions to access it`,
);
}
throw e;
}
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 } : {}),