From d7ec77862cf0bf46664406d885f6e0a4355c108e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Oro=CC=81s?= Date: Mon, 28 Oct 2024 10:21:18 +0100 Subject: [PATCH] Added unit test for catalog-info.yaml with multiple documents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marc Orós --- .../src/api/CatalogImportClient.test.ts | 71 ++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/plugins/catalog-import/src/api/CatalogImportClient.test.ts b/plugins/catalog-import/src/api/CatalogImportClient.test.ts index fb78066ac0..4c61d4f6a0 100644 --- a/plugins/catalog-import/src/api/CatalogImportClient.test.ts +++ b/plugins/catalog-import/src/api/CatalogImportClient.test.ts @@ -61,12 +61,12 @@ import { MockFetchApi, registerMswTestHooks } from '@backstage/test-utils'; import { Octokit } from '@octokit/rest'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; -import { CatalogImportClient } from './CatalogImportClient'; import { AzurePrOptions, AzurePrResult, createAzurePullRequest, } from './AzureRepoApiClient'; +import { CatalogImportClient } from './CatalogImportClient'; describe('CatalogImportClient', () => { const server = setupServer(); @@ -775,6 +775,75 @@ describe('CatalogImportClient', () => { }), ); }); + it('should create GitHub pull request and validate all documents inside the YAML file', async () => { + catalogApi.validateEntity.mockResolvedValue({ + valid: true, + }); + await expect( + catalogImportClient.submitPullRequest({ + repositoryUrl: 'https://github.com/backstage/backstage', + fileContent: `apiVersion: backstage.io/v1alpha1 +kind: Component +metadata: + name: reg-graphql-shopping + annotations: + github.com/project-slug: tkww/reg-graphql-shopping + backstage.io/techdocs-ref: dir:. +spec: + type: service + lifecycle: production + owner: registry-pandora + system: system +--- + +apiVersion: backstage.io/v1alpha1 +kind: System +metadata: + name: system +spec: + owner: registry-pandora + lifecycle: production +`, + title: 'A title/message', + body: 'A body', + }), + ).resolves.toEqual({ + link: 'http://pull/request/0', + location: + 'https://github.com/backstage/backstage/blob/main/catalog-info.yaml', + }); + expect(catalogApi.validateEntity).toHaveBeenCalledTimes(2); + expect( + (new Octokit().git.createRef as any as jest.Mock).mock.calls[0][0], + ).toEqual({ + owner: 'backstage', + repo: 'backstage', + ref: 'refs/heads/backstage-integration', + sha: 'any', + }); + expect( + (new Octokit().repos.createOrUpdateFileContents as any as jest.Mock) + .mock.calls[0][0], + ).toEqual({ + owner: 'backstage', + repo: 'backstage', + path: 'catalog-info.yaml', + message: 'A title/message', + content: + 'YXBpVmVyc2lvbjogYmFja3N0YWdlLmlvL3YxYWxwaGExCmtpbmQ6IENvbXBvbmVudAptZXRhZGF0YToKICBuYW1lOiByZWctZ3JhcGhxbC1zaG9wcGluZwogIGFubm90YXRpb25zOgogICAgZ2l0aHViLmNvbS9wcm9qZWN0LXNsdWc6IHRrd3cvcmVnLWdyYXBocWwtc2hvcHBpbmcKICAgIGJhY2tzdGFnZS5pby90ZWNoZG9jcy1yZWY6IGRpcjouCnNwZWM6CiAgdHlwZTogc2VydmljZQogIGxpZmVjeWNsZTogcHJvZHVjdGlvbgogIG93bmVyOiByZWdpc3RyeS1wYW5kb3JhCiAgc3lzdGVtOiBzeXN0ZW0KLS0tCgphcGlWZXJzaW9uOiBiYWNrc3RhZ2UuaW8vdjFhbHBoYTEKa2luZDogU3lzdGVtCm1ldGFkYXRhOgogIG5hbWU6IHN5c3RlbQpzcGVjOgogIG93bmVyOiByZWdpc3RyeS1wYW5kb3JhCiAgbGlmZWN5Y2xlOiBwcm9kdWN0aW9uCg==', + branch: 'backstage-integration', + }); + expect( + (new Octokit().pulls.create as any as jest.Mock).mock.calls[0][0], + ).toEqual({ + owner: 'backstage', + repo: 'backstage', + title: 'A title/message', + head: 'backstage-integration', + body: 'A body', + base: 'main', + }); + }); }); describe('preparePullRequest', () => {