Add repoVisibility for GitLab repos
This commit is contained in:
@@ -54,11 +54,14 @@ describe('GitLab Publisher', () => {
|
||||
|
||||
describe('publish: createRemoteInGitLab', () => {
|
||||
it('should use gitbeaker to create a repo in a namespace if the namespace property is set', async () => {
|
||||
const publisher = await GitlabPublisher.fromConfig({
|
||||
host: 'gitlab.com',
|
||||
token: 'fake-token',
|
||||
baseUrl: 'https://gitlab.hosted.com',
|
||||
});
|
||||
const publisher = await GitlabPublisher.fromConfig(
|
||||
{
|
||||
host: 'gitlab.com',
|
||||
token: 'fake-token',
|
||||
baseUrl: 'https://gitlab.hosted.com',
|
||||
},
|
||||
{ repoVisibility: 'public' },
|
||||
);
|
||||
|
||||
mockGitlabClient.Namespaces.show.mockResolvedValue({
|
||||
id: 42,
|
||||
@@ -88,6 +91,7 @@ describe('GitLab Publisher', () => {
|
||||
expect(mockGitlabClient.Projects.create).toHaveBeenCalledWith({
|
||||
namespace_id: 42,
|
||||
name: 'test',
|
||||
visibility: 'public',
|
||||
});
|
||||
expect(initRepoAndPush).toHaveBeenCalledWith({
|
||||
dir: resultPath,
|
||||
@@ -98,10 +102,13 @@ describe('GitLab Publisher', () => {
|
||||
});
|
||||
|
||||
it('should use gitbeaker to create a repo in the authed user if the namespace property is not set', async () => {
|
||||
const publisher = await GitlabPublisher.fromConfig({
|
||||
host: 'gitlab.com',
|
||||
token: 'fake-token',
|
||||
});
|
||||
const publisher = await GitlabPublisher.fromConfig(
|
||||
{
|
||||
host: 'gitlab.com',
|
||||
token: 'fake-token',
|
||||
},
|
||||
{ repoVisibility: 'public' },
|
||||
);
|
||||
|
||||
mockGitlabClient.Namespaces.show.mockResolvedValue({});
|
||||
mockGitlabClient.Users.current.mockResolvedValue({
|
||||
@@ -128,6 +135,7 @@ describe('GitLab Publisher', () => {
|
||||
expect(mockGitlabClient.Projects.create).toHaveBeenCalledWith({
|
||||
namespace_id: 21,
|
||||
name: 'test',
|
||||
visibility: 'public',
|
||||
});
|
||||
expect(initRepoAndPush).toHaveBeenCalledWith({
|
||||
dir: resultPath,
|
||||
|
||||
@@ -22,18 +22,31 @@ import parseGitUrl from 'git-url-parse';
|
||||
import path from 'path';
|
||||
import { GitLabIntegrationConfig } from '@backstage/integration';
|
||||
|
||||
export type RepoVisibilityOptions = 'private' | 'internal' | 'public';
|
||||
|
||||
export class GitlabPublisher implements PublisherBase {
|
||||
static async fromConfig(config: GitLabIntegrationConfig) {
|
||||
static async fromConfig(
|
||||
config: GitLabIntegrationConfig,
|
||||
{ repoVisibility }: { repoVisibility: RepoVisibilityOptions },
|
||||
) {
|
||||
if (!config.token) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const client = new Gitlab({ host: config.baseUrl, token: config.token });
|
||||
return new GitlabPublisher({ token: config.token, client });
|
||||
return new GitlabPublisher({
|
||||
token: config.token,
|
||||
client,
|
||||
repoVisibility,
|
||||
});
|
||||
}
|
||||
|
||||
constructor(
|
||||
private readonly config: { token: string; client: GitlabClient },
|
||||
private readonly config: {
|
||||
token: string;
|
||||
client: GitlabClient;
|
||||
repoVisibility: RepoVisibilityOptions;
|
||||
},
|
||||
) {}
|
||||
|
||||
async publish({
|
||||
@@ -85,6 +98,7 @@ export class GitlabPublisher implements PublisherBase {
|
||||
const project = (await this.config.client.Projects.create({
|
||||
namespace_id: targetNamespace,
|
||||
name: name,
|
||||
visibility: this.config.repoVisibility,
|
||||
})) as { http_url_to_repo: string };
|
||||
|
||||
return project?.http_url_to_repo;
|
||||
|
||||
@@ -98,7 +98,13 @@ export class Publishers implements PublisherBuilder {
|
||||
}
|
||||
|
||||
for (const integration of scm.gitlab.list()) {
|
||||
const publisher = await GitlabPublisher.fromConfig(integration.config);
|
||||
const repoVisibility = (config.getOptionalString(
|
||||
'scaffolder.gitlab.visibility',
|
||||
) ?? 'public') as RepoVisibilityOptions;
|
||||
|
||||
const publisher = await GitlabPublisher.fromConfig(integration.config, {
|
||||
repoVisibility,
|
||||
});
|
||||
|
||||
if (publisher) {
|
||||
publishers.register(integration.config.host, publisher);
|
||||
@@ -107,10 +113,13 @@ export class Publishers implements PublisherBuilder {
|
||||
|
||||
publishers.register(
|
||||
integration.config.host,
|
||||
await GitlabPublisher.fromConfig({
|
||||
token: config.getOptionalString('scaffolder.gitlab.token') ?? '',
|
||||
host: integration.config.host,
|
||||
}),
|
||||
await GitlabPublisher.fromConfig(
|
||||
{
|
||||
token: config.getOptionalString('scaffolder.gitlab.token') ?? '',
|
||||
host: integration.config.host,
|
||||
},
|
||||
{ repoVisibility },
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user