From 9e59823f9bd977b8ecf894f29d94513bfe2690fc Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Thu, 18 Apr 2024 19:00:26 +0200 Subject: [PATCH] Add new parameter to set the visibility of the repository created Signed-off-by: cmoulliard --- .../package.json | 2 +- .../src/actions/gitea.examples.ts | 18 +++++ .../src/actions/gitea.test.ts | 73 +++++++++++++++++++ .../src/actions/gitea.ts | 23 +++++- 4 files changed, 114 insertions(+), 2 deletions(-) diff --git a/plugins/scaffolder-backend-module-gitea/package.json b/plugins/scaffolder-backend-module-gitea/package.json index 1751a1a6ce..1d8ad190bc 100644 --- a/plugins/scaffolder-backend-module-gitea/package.json +++ b/plugins/scaffolder-backend-module-gitea/package.json @@ -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" diff --git a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.examples.ts b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.examples.ts index ccf0f4ddf6..dd4d038ead 100644 --- a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.examples.ts +++ b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.examples.ts @@ -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', }, }, ], diff --git a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.test.ts b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.test.ts index 8675b1ff97..8a72265df0 100644 --- a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.test.ts +++ b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.test.ts @@ -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(); }); diff --git a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts index 089f9b2669..03ac364538 100644 --- a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts +++ b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts @@ -96,10 +96,11 @@ const createGiteaProject = async ( options: { projectName: string; owner?: string; + repoVisibility?: string; description: string; }, ): Promise => { - 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, });