diff --git a/plugins/github-release-manager/src/api/PluginApiClient.ts b/plugins/github-release-manager/src/api/PluginApiClient.ts index d348680cd1..38b460b4f9 100644 --- a/plugins/github-release-manager/src/api/PluginApiClient.ts +++ b/plugins/github-release-manager/src/api/PluginApiClient.ts @@ -22,7 +22,6 @@ import { GhCreateCommitResponse, GhCreateReferenceResponse, GhCreateTagObjectResponse, - GhGetBranchResponse, GhGetCommitResponse, GhGetReleaseResponse, GhMergeResponse, @@ -97,10 +96,29 @@ export interface IPluginApiClient { }>; getLatestCommit: ( - args: { defaultBranch: string } & PartialProject, + args: { + defaultBranch: string; + } & PartialProject, ) => Promise; - getBranch: (args: { branchName: string } & PartialProject) => Promise; + getBranch: ( + args: { + branchName: string; + } & PartialProject, + ) => Promise<{ + name: string; + links: { + html: string; + }; + commit: { + sha: string; + commit: { + tree: { + sha: string; + }; + }; + }; + }>; createRc: { createRef: ( @@ -384,7 +402,6 @@ export class PluginApiClient implements IPluginApiClient { defaultBranch, }: { defaultBranch: string } & PartialProject) { const { octokit } = await this.getOctokit(); - const latestCommit: GhGetCommitResponse = ( await octokit.request( `/repos/${this.getRepoPath({ @@ -404,13 +421,26 @@ export class PluginApiClient implements IPluginApiClient { }: { branchName: string } & PartialProject) { const { octokit } = await this.getOctokit(); - const branch: GhGetBranchResponse = ( - await octokit.request( - `/repos/${this.getRepoPath({ owner, repo })}/branches/${branchName}`, - ) - ).data; + const { data: branch } = await octokit.repos.getBranch({ + owner, + repo, + branch: branchName, + }); - return { branch }; + return { + name: branch.name, + links: { + html: branch._links.html, + }, + commit: { + sha: branch.commit.sha, + commit: { + tree: { + sha: branch.commit.commit.tree.sha, + }, + }, + }, + }; } createRc = { diff --git a/plugins/github-release-manager/src/cards/createRc/CreateRc.tsx b/plugins/github-release-manager/src/cards/createRc/CreateRc.tsx index b36478f082..0bc0e91297 100644 --- a/plugins/github-release-manager/src/cards/createRc/CreateRc.tsx +++ b/plugins/github-release-manager/src/cards/createRc/CreateRc.tsx @@ -32,7 +32,6 @@ import { getRcGitHubInfo } from './getRcGitHubInfo'; import { InfoCardPlus } from '../../components/InfoCardPlus'; import { ComponentConfigCreateRc, - GhGetBranchResponse, GhGetRepositoryResponse, SetRefetch, } from '../../types/types'; @@ -49,7 +48,7 @@ interface CreateRcProps { latestRelease: ApiMethodRetval< IPluginApiClient['getLatestRelease'] >['latestRelease']; - releaseBranch: GhGetBranchResponse | null; + releaseBranch: ApiMethodRetval | null; setRefetch: SetRefetch; successCb?: ComponentConfigCreateRc['successCb']; } diff --git a/plugins/github-release-manager/src/cards/info/Info.tsx b/plugins/github-release-manager/src/cards/info/Info.tsx index 782ed30460..05902162d4 100644 --- a/plugins/github-release-manager/src/cards/info/Info.tsx +++ b/plugins/github-release-manager/src/cards/info/Info.tsx @@ -18,7 +18,6 @@ import React from 'react'; import { Link, Typography } from '@material-ui/core'; import { Differ } from '../../components/Differ'; -import { GhGetBranchResponse } from '../../types/types'; import { InfoCardPlus } from '../../components/InfoCardPlus'; import { TEST_IDS } from '../../test-helpers/test-ids'; import { useProjectContext } from '../../contexts/ProjectContext'; @@ -27,7 +26,7 @@ import flowImage from './flow.png'; import { ApiMethodRetval, IPluginApiClient } from '../../api/PluginApiClient'; interface InfoCardProps { - releaseBranch: GhGetBranchResponse | null; + releaseBranch: ApiMethodRetval | null; latestRelease: ApiMethodRetval< IPluginApiClient['getLatestRelease'] >['latestRelease']; diff --git a/plugins/github-release-manager/src/cards/patchRc/Patch.tsx b/plugins/github-release-manager/src/cards/patchRc/Patch.tsx index e19287024c..fc18be1d1d 100644 --- a/plugins/github-release-manager/src/cards/patchRc/Patch.tsx +++ b/plugins/github-release-manager/src/cards/patchRc/Patch.tsx @@ -17,24 +17,20 @@ import React from 'react'; import { Typography } from '@material-ui/core'; +import { ApiMethodRetval, IPluginApiClient } from '../../api/PluginApiClient'; +import { ComponentConfigPatch, SetRefetch } from '../../types/types'; import { getBumpedTag } from '../../helpers/getBumpedTag'; import { InfoCardPlus } from '../../components/InfoCardPlus'; import { NoLatestRelease } from '../../components/NoLatestRelease'; -import { - ComponentConfigPatch, - GhGetBranchResponse, - SetRefetch, -} from '../../types/types'; import { PatchBody } from './PatchBody'; import { useProjectContext } from '../../contexts/ProjectContext'; import { useStyles } from '../../styles/styles'; -import { ApiMethodRetval, IPluginApiClient } from '../../api/PluginApiClient'; interface PatchProps { latestRelease: ApiMethodRetval< IPluginApiClient['getLatestRelease'] >['latestRelease']; - releaseBranch: GhGetBranchResponse | null; + releaseBranch: ApiMethodRetval | null; setRefetch: SetRefetch; successCb?: ComponentConfigPatch['successCb']; } @@ -53,6 +49,10 @@ export const Patch = ({ return ; } + if (releaseBranch === null) { + return ; + } + const { bumpedTag, tagParts } = getBumpedTag({ project, tag: latestRelease.tagName, diff --git a/plugins/github-release-manager/src/cards/patchRc/PatchBody.test.tsx b/plugins/github-release-manager/src/cards/patchRc/PatchBody.test.tsx index 54daf2894f..9b6d971056 100644 --- a/plugins/github-release-manager/src/cards/patchRc/PatchBody.test.tsx +++ b/plugins/github-release-manager/src/cards/patchRc/PatchBody.test.tsx @@ -41,16 +41,16 @@ describe('PatchBody', () => { beforeEach(jest.clearAllMocks); it('should render error', async () => { - (mockApiClient.getBranch as jest.Mock).mockImplementationOnce(() => { + (mockApiClient.getRecentCommits as jest.Mock).mockImplementationOnce(() => { throw new Error('banana'); }); const { getByTestId } = render( , ); diff --git a/plugins/github-release-manager/src/cards/patchRc/PatchBody.tsx b/plugins/github-release-manager/src/cards/patchRc/PatchBody.tsx index 44907d30f1..1548327f45 100644 --- a/plugins/github-release-manager/src/cards/patchRc/PatchBody.tsx +++ b/plugins/github-release-manager/src/cards/patchRc/PatchBody.tsx @@ -36,7 +36,6 @@ import OpenInNewIcon from '@material-ui/icons/OpenInNew'; import { Differ } from '../../components/Differ'; import { ComponentConfigPatch, - GhGetBranchResponse, GhGetCommitResponse, SetRefetch, } from '../../types/types'; @@ -56,7 +55,7 @@ interface PatchBodyProps { latestRelease: NonNullable< ApiMethodRetval['latestRelease'] >; - releaseBranch: GhGetBranchResponse | null; + releaseBranch: ApiMethodRetval; setRefetch: SetRefetch; successCb?: ComponentConfigPatch['successCb']; tagParts: NonNullable; @@ -76,25 +75,17 @@ export const PatchBody = ({ const githubDataResponse = useAsync(async () => { const [ - { branch: releaseBranchResponse }, { recentCommits: recentCommitsOnDefaultBranch }, - ] = await Promise.all([ - pluginApiClient.getBranch({ - ...project, - branchName: latestRelease.targetCommitish, - }), - pluginApiClient.getRecentCommits({ ...project }), - ]); + ] = await Promise.all([pluginApiClient.getRecentCommits({ ...project })]); const { recentCommits: recentCommitsOnReleaseBranch, } = await pluginApiClient.getRecentCommits({ ...project, - releaseBranchName: releaseBranchResponse.name, + releaseBranchName: releaseBranch.name, }); return { - releaseBranch: releaseBranchResponse, recentCommitsOnReleaseBranch, recentCommitsOnDefaultBranch, }; diff --git a/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.test.ts b/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.test.ts index 322fe54255..9d00824b3b 100644 --- a/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.test.ts +++ b/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.test.ts @@ -29,18 +29,18 @@ describe('patch', () => { it('should work', async () => { const result = await patch({ - pluginApiClient: mockApiClient, - latestRelease: mockReleaseVersion, bumpedTag: mockBumpedTag, + latestRelease: mockReleaseVersion, + pluginApiClient: mockApiClient, + project: mockCalverProject, selectedPatchCommit: mockSelectedPatchCommit, tagParts: mockTagParts, - project: mockCalverProject, }); expect(result).toMatchInlineSnapshot(` Array [ Object { - "link": "mock_branch__links_html", + "link": "mock_branch_links_html", "message": "Fetched release branch \\"rc/1.2.3\\"", }, Object { diff --git a/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.ts b/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.ts index b2c226c51c..d254020c62 100644 --- a/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.ts +++ b/plugins/github-release-manager/src/cards/patchRc/sideEffects/patch.ts @@ -63,7 +63,7 @@ export async function patch({ * > branchSha = branch.commit.sha * > branchTree = branch.commit.commit.tree.sha */ - const { branch: releaseBranch } = await pluginApiClient.getBranch({ + const releaseBranch = await pluginApiClient.getBranch({ ...project, branchName: releaseBranchName, }); @@ -71,7 +71,7 @@ export async function patch({ const releaseBranchTree = releaseBranch.commit.commit.tree.sha; responseSteps.push({ message: `Fetched release branch "${releaseBranch.name}"`, - link: releaseBranch._links.html, + link: releaseBranch.links.html, }); /** diff --git a/plugins/github-release-manager/src/components/NoLatestRelease.tsx b/plugins/github-release-manager/src/components/NoLatestRelease.tsx index 232eb1c6b1..b9cf2f6df9 100644 --- a/plugins/github-release-manager/src/components/NoLatestRelease.tsx +++ b/plugins/github-release-manager/src/components/NoLatestRelease.tsx @@ -29,7 +29,7 @@ export const NoLatestRelease = () => { className={classes.paragraph} severity="warning" > - Unable to find any GitHub release + Unable to find any Release ); }; diff --git a/plugins/github-release-manager/src/components/NoReleaseBranch.test.tsx b/plugins/github-release-manager/src/components/NoReleaseBranch.test.tsx new file mode 100644 index 0000000000..6e31be3caa --- /dev/null +++ b/plugins/github-release-manager/src/components/NoReleaseBranch.test.tsx @@ -0,0 +1,31 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { render } from '@testing-library/react'; + +import { TEST_IDS } from '../test-helpers/test-ids'; +import { NoReleaseBranch } from './NoReleaseBranch'; + +describe('NoReleaseBranch', () => { + it('render NoReleaseBranch', () => { + const { getByTestId } = render(); + + expect( + getByTestId(TEST_IDS.components.noReleaseBranch), + ).toBeInTheDocument(); + }); +}); diff --git a/plugins/github-release-manager/src/components/NoReleaseBranch.tsx b/plugins/github-release-manager/src/components/NoReleaseBranch.tsx new file mode 100644 index 0000000000..66c7f61ec0 --- /dev/null +++ b/plugins/github-release-manager/src/components/NoReleaseBranch.tsx @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import React from 'react'; +import { Alert } from '@material-ui/lab'; + +import { useStyles } from '../styles/styles'; +import { TEST_IDS } from '../test-helpers/test-ids'; + +export const NoReleaseBranch = () => { + const classes = useStyles(); + + return ( + + Unable to find any Release Branch + + ); +}; diff --git a/plugins/github-release-manager/src/sideEffects/getGitHubBatchInfo.ts b/plugins/github-release-manager/src/sideEffects/getGitHubBatchInfo.ts index 2b00f0e84b..39ddca4e3b 100644 --- a/plugins/github-release-manager/src/sideEffects/getGitHubBatchInfo.ts +++ b/plugins/github-release-manager/src/sideEffects/getGitHubBatchInfo.ts @@ -39,14 +39,14 @@ export const getGitHubBatchInfo = ({ }; } - const { branch } = await pluginApiClient.getBranch({ + const releaseBranch = await pluginApiClient.getBranch({ ...project, branchName: latestRelease.targetCommitish, }); return { latestRelease, - releaseBranch: branch, + releaseBranch, repository, }; }; diff --git a/plugins/github-release-manager/src/test-helpers/test-helpers.test.ts b/plugins/github-release-manager/src/test-helpers/test-helpers.test.ts index c833efbca8..a690f44811 100644 --- a/plugins/github-release-manager/src/test-helpers/test-helpers.test.ts +++ b/plugins/github-release-manager/src/test-helpers/test-helpers.test.ts @@ -96,9 +96,6 @@ describe('testHelpers', () => { }, ], "mockReleaseBranch": Object { - "_links": Object { - "html": "mock_branch__links_html", - }, "commit": Object { "commit": Object { "tree": Object { @@ -107,6 +104,9 @@ describe('testHelpers', () => { }, "sha": "mock_branch_commit_sha", }, + "links": Object { + "html": "mock_branch_links_html", + }, "name": "rc/1.2.3", }, "mockReleaseVersion": Object { diff --git a/plugins/github-release-manager/src/test-helpers/test-helpers.ts b/plugins/github-release-manager/src/test-helpers/test-helpers.ts index a68926182c..2ae2cf4a32 100644 --- a/plugins/github-release-manager/src/test-helpers/test-helpers.ts +++ b/plugins/github-release-manager/src/test-helpers/test-helpers.ts @@ -92,16 +92,18 @@ export const mockReleaseVersion = createMockRelease({ /** * MOCK BRANCH */ -const createMockBranch = ({ ...rest }: Partial = {}) => +const createMockBranch = ({ + ...rest +}: Partial>> = {}) => ({ name: 'rc/1.2.3', commit: { sha: 'mock_branch_commit_sha', commit: { tree: { sha: 'mock_branch_commit_commit_tree_sha' } }, }, - _links: { html: 'mock_branch__links_html' }, + links: { html: 'mock_branch_links_html' }, ...rest, - } as GhGetBranchResponse); + } as NonNullable>); export const mockReleaseBranch = createMockBranch(); /** @@ -134,21 +136,28 @@ export const mockSelectedPatchCommit = createMockCommit({ */ export const mockApiClient: IPluginApiClient = { getHost: jest.fn(() => 'github.com'), + getRepoPath: jest.fn(() => 'erikengervall/playground'), + getOrganizations: jest.fn(), + getRepositories: jest.fn(), + getUsername: jest.fn(), + getRecentCommits: jest.fn().mockResolvedValue({ recentCommits: mockRecentCommits, }), + getLatestRelease: jest.fn(), // TODO: + getRepository: jest.fn(), + getLatestCommit: jest.fn().mockResolvedValue({ latestCommit: createMockCommit({ node_id: 'mock_latest_commit' }), }), - getBranch: jest.fn().mockResolvedValue({ - branch: mockReleaseBranch, - }), + + getBranch: jest.fn().mockResolvedValue(createMockBranch()), createRc: { createRef: jest.fn().mockResolvedValue({ diff --git a/plugins/github-release-manager/src/test-helpers/test-ids.ts b/plugins/github-release-manager/src/test-helpers/test-ids.ts index 3620b03248..f096af567f 100644 --- a/plugins/github-release-manager/src/test-helpers/test-ids.ts +++ b/plugins/github-release-manager/src/test-helpers/test-ids.ts @@ -39,6 +39,7 @@ export const TEST_IDS = { divider: 'grm--divider', reloadButton: 'grm--reload-button', noLatestRelease: 'grm--no-latest-release', + noReleaseBranch: 'grm--no-release-branch', circularProgress: 'grm--circular-progress', responseStepListDialogContent: 'grm--response-step-list--dialog-content', responseStepListItem: 'grm--response-step-list-item',