From 878c1851d49784975592482fcb0e1679498cbd9b Mon Sep 17 00:00:00 2001 From: Crevil Date: Wed, 16 Jun 2021 13:31:00 +0200 Subject: [PATCH 1/3] Add topics input to publish:github action This change adds an array input of topics to attach on a repository upon creation. Signed-off-by: Crevil --- .changeset/proud-jars-look.md | 5 +++ .../__mocks__/@octokit/rest/index.ts | 1 + .../actions/builtin/publish/github.test.ts | 33 +++++++++++++++++++ .../actions/builtin/publish/github.ts | 22 +++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 .changeset/proud-jars-look.md diff --git a/.changeset/proud-jars-look.md b/.changeset/proud-jars-look.md new file mode 100644 index 0000000000..0311452b4f --- /dev/null +++ b/.changeset/proud-jars-look.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +Add a `topics` input to `publish:github` action that can be used to set topics on the repository upon creation. diff --git a/plugins/scaffolder-backend/src/scaffolder/__mocks__/@octokit/rest/index.ts b/plugins/scaffolder-backend/src/scaffolder/__mocks__/@octokit/rest/index.ts index e0e9efa479..1f27745c0c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/__mocks__/@octokit/rest/index.ts +++ b/plugins/scaffolder-backend/src/scaffolder/__mocks__/@octokit/rest/index.ts @@ -19,6 +19,7 @@ export const mockGithubClient = { createInOrg: jest.fn(), createForAuthenticatedUser: jest.fn(), addCollaborator: jest.fn(), + replaceAllTopics: jest.fn(), }, users: { getByUsername: jest.fn(), diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts index dc2f602778..c59ba18bea 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts @@ -341,6 +341,39 @@ describe('publish:github', () => { ]); }); + it('should add topics when provided', async () => { + mockGithubClient.users.getByUsername.mockResolvedValue({ + data: { type: 'User' }, + }); + + mockGithubClient.repos.createForAuthenticatedUser.mockResolvedValue({ + data: { + clone_url: 'https://github.com/clone/url.git', + html_url: 'https://github.com/html/url', + }, + }); + + mockGithubClient.repos.replaceAllTopics.mockResolvedValue({ + data: { + names: ['node.js'], + }, + }); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + topics: ['node.js'], + }, + }); + + expect(mockGithubClient.repos.replaceAllTopics).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + names: ['node.js'], + }); + }); + it('should call output with the remoteUrl and the repoContentsUrl', async () => { mockGithubClient.users.getByUsername.mockResolvedValue({ data: { type: 'User' }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index a3befa0dfe..d7beabe0db 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -48,6 +48,7 @@ export function createPublishGithubAction(options: { sourcePath?: string; repoVisibility: 'private' | 'internal' | 'public'; collaborators: Collaborator[]; + topics?: string[]; }>({ id: 'publish:github', description: @@ -99,6 +100,14 @@ export function createPublishGithubAction(options: { }, }, }, + topics: { + title: 'Topics', + description: 'Uppercase letters no allowed', + type: 'array', + items: { + type: 'string', + }, + }, }, }, output: { @@ -122,6 +131,7 @@ export function createPublishGithubAction(options: { access, repoVisibility = 'private', collaborators, + topics, } = ctx.input; const { owner, repo, host } = parseRepoUrl(repoUrl); @@ -215,6 +225,18 @@ export function createPublishGithubAction(options: { } } + if (topics) { + try { + await client.repos.replaceAllTopics({ + owner, + repo, + names: topics, + }); + } catch (e) { + ctx.logger.warn(`Skipping topics ${topics.join(' ')}, ${e.message}`); + } + } + const remoteUrl = newRepo.clone_url; const repoContentsUrl = `${newRepo.html_url}/blob/master`; From 3b43fc3ab8671327662753d6119aa05a10b178a3 Mon Sep 17 00:00:00 2001 From: Crevil Date: Wed, 16 Jun 2021 16:49:45 +0200 Subject: [PATCH 2/3] Lowercase topics Signed-off-by: Crevil --- .../actions/builtin/publish/github.test.ts | 33 +++++++++++++++++++ .../actions/builtin/publish/github.ts | 3 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts index c59ba18bea..3dae202643 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.test.ts @@ -374,6 +374,39 @@ describe('publish:github', () => { }); }); + it('should lowercase topics when provided', async () => { + mockGithubClient.users.getByUsername.mockResolvedValue({ + data: { type: 'User' }, + }); + + mockGithubClient.repos.createForAuthenticatedUser.mockResolvedValue({ + data: { + clone_url: 'https://github.com/clone/url.git', + html_url: 'https://github.com/html/url', + }, + }); + + mockGithubClient.repos.replaceAllTopics.mockResolvedValue({ + data: { + names: ['backstage'], + }, + }); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + topics: ['BACKSTAGE'], + }, + }); + + expect(mockGithubClient.repos.replaceAllTopics).toHaveBeenCalledWith({ + owner: 'owner', + repo: 'repo', + names: ['backstage'], + }); + }); + it('should call output with the remoteUrl and the repoContentsUrl', async () => { mockGithubClient.users.getByUsername.mockResolvedValue({ data: { type: 'User' }, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index d7beabe0db..613cbe86d7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -102,7 +102,6 @@ export function createPublishGithubAction(options: { }, topics: { title: 'Topics', - description: 'Uppercase letters no allowed', type: 'array', items: { type: 'string', @@ -230,7 +229,7 @@ export function createPublishGithubAction(options: { await client.repos.replaceAllTopics({ owner, repo, - names: topics, + names: topics.map(t => t.toLowerCase()), }); } catch (e) { ctx.logger.warn(`Skipping topics ${topics.join(' ')}, ${e.message}`); From 0b22248d758f78a9e5104081ae39edf91b93d33e Mon Sep 17 00:00:00 2001 From: Crevil Date: Wed, 16 Jun 2021 20:44:42 +0200 Subject: [PATCH 3/3] Change change to patch Signed-off-by: Crevil --- .changeset/proud-jars-look.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/proud-jars-look.md b/.changeset/proud-jars-look.md index 0311452b4f..2bae44db66 100644 --- a/.changeset/proud-jars-look.md +++ b/.changeset/proud-jars-look.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-scaffolder-backend': minor +'@backstage/plugin-scaffolder-backend': patch --- Add a `topics` input to `publish:github` action that can be used to set topics on the repository upon creation.