Add new parameter to set the visibility of the repository created

Signed-off-by: cmoulliard <cmoulliard@redhat.com>
This commit is contained in:
cmoulliard
2024-04-18 19:00:26 +02:00
parent 3a85821e76
commit 9e59823f9b
4 changed files with 114 additions and 2 deletions
@@ -1,6 +1,6 @@
{
"name": "@backstage/plugin-scaffolder-backend-module-gitea",
"version": "0.1.7",
"version": "0.1.8",
"description": "The gitea module for @backstage/plugin-scaffolder-backend",
"backstage": {
"role": "backend-plugin-module"
@@ -50,6 +50,23 @@ export const examples: TemplateExample[] = [
],
}),
},
{
description: 'Initializes a private Gitea repository ',
example: yaml.stringify({
steps: [
{
id: 'publish',
action: 'publish:gitea',
name: 'Publish to Gitea',
input: {
repoUrl: 'gitea.com?repo=repo&owner=owner',
defaultBranch: 'main',
repoVisibility: 'private',
},
},
],
}),
},
{
description:
'Initializes a Gitea repository with a default Branch, if not set defaults to main',
@@ -150,6 +167,7 @@ export const examples: TemplateExample[] = [
gitAuthorName: 'John Doe',
gitAuthorEmail: 'johndoe@email.com',
sourcePath: 'repository/',
repoVisibility: 'public',
},
},
],
@@ -53,6 +53,13 @@ describe('publish:gitea', () => {
description,
},
});
const mockContextWithPublicRepoVisibility = createMockActionContext({
input: {
repoUrl: 'gitea.com?repo=repo&owner=owner',
description,
private: false,
},
});
const server = setupServer();
setupRequestMockHandlers(server);
@@ -111,6 +118,7 @@ describe('publish:gitea', () => {
);
expect(req.body).toEqual({
name: 'repo',
private: false,
description,
});
return res(
@@ -148,6 +156,71 @@ describe('publish:gitea', () => {
);
});
it('should create a Gitea repository where visibility is public', async () => {
server.use(
rest.get('https://gitea.com/api/v1/orgs/org1', (_req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({
id: 1,
name: 'org1',
visibility: 'public',
repo_admin_change_team_access: false,
username: 'org1',
}),
);
}),
rest.get(
'https://gitea.com/org1/repo/src/branch/main',
(_req, res, ctx) => {
return res(
ctx.status(200),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
},
),
rest.post('https://gitea.com/api/v1/orgs/org1/repos', (req, res, ctx) => {
// Basic auth must match the user and password defined part of the config
expect(req.headers.get('Authorization')).toBe(
'basic Z2l0ZWFfdXNlcjpnaXRlYV9wYXNzd29yZA==',
);
expect(req.body).toEqual({
name: 'repo',
private: false,
description,
});
return res(
ctx.status(201),
ctx.set('Content-Type', 'application/json'),
ctx.json({}),
);
}),
);
await action.handler({
...mockContextWithPublicRepoVisibility,
input: {
...mockContextWithPublicRepoVisibility.input,
repoUrl: 'gitea.com?repo=repo&owner=org1',
},
});
expect(initRepoAndPush).toHaveBeenCalledWith({
dir: mockContextWithPublicRepoVisibility.workspacePath,
remoteUrl: 'https://gitea.com/org1/repo.git',
defaultBranch: 'main',
auth: { username: 'gitea_user', password: 'gitea_password' },
logger: mockContextWithPublicRepoVisibility.logger,
commitMessage: expect.stringContaining('initial commit\n\nChange-Id:'),
gitAuthorInfo: {
email: undefined,
name: undefined,
},
});
});
afterEach(() => {
jest.resetAllMocks();
});
@@ -96,10 +96,11 @@ const createGiteaProject = async (
options: {
projectName: string;
owner?: string;
repoVisibility?: string;
description: string;
},
): Promise<void> => {
const { projectName, description, owner } = options;
const { projectName, description, owner, repoVisibility } = options;
/*
Several options exist to create a repository using either the user or organisation
@@ -112,12 +113,23 @@ const createGiteaProject = async (
This is the default scenario that we support currently
*/
let response: Response;
let visibility: boolean;
if (repoVisibility === 'private') {
visibility = true;
} else if (repoVisibility === 'public') {
visibility = false;
} else {
// Provide a default value if repoVisibility is neither "private" nor "public"
visibility = false;
}
const postOptions: RequestInit = {
method: 'POST',
body: JSON.stringify({
name: projectName,
description,
private: visibility,
}),
headers: {
...getGiteaRequestOptions(config).headers,
@@ -202,6 +214,7 @@ export function createPublishGiteaAction(options: {
repoUrl: string;
description: string;
defaultBranch?: string;
repoVisibility?: 'private' | 'public';
gitCommitMessage?: string;
gitAuthorName?: string;
gitAuthorEmail?: string;
@@ -229,6 +242,12 @@ export function createPublishGiteaAction(options: {
type: 'string',
description: `Sets the default branch on the repository. The default value is 'main'`,
},
repoVisibility: {
title: 'Repository Visibility',
description: `Sets the visibility of the repository. The default value is 'public'.`,
type: 'string',
enum: ['private', 'public'],
},
gitCommitMessage: {
title: 'Git Commit Message',
type: 'string',
@@ -274,6 +293,7 @@ export function createPublishGiteaAction(options: {
repoUrl,
description,
defaultBranch = 'main',
repoVisibility = 'public',
gitAuthorName,
gitAuthorEmail,
gitCommitMessage = 'initial commit',
@@ -301,6 +321,7 @@ export function createPublishGiteaAction(options: {
await createGiteaProject(integrationConfig.config, {
description,
repoVisibility,
owner: owner,
projectName: repo,
});