Normalize getBranch

Signed-off-by: Erik Engervall <erik.engervall@gmail.com>
This commit is contained in:
Erik Engervall
2021-04-15 21:42:19 +02:00
parent 6c68b63d32
commit 6482c52f9f
15 changed files with 150 additions and 55 deletions
@@ -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<Todo>;
getBranch: (args: { branchName: string } & PartialProject) => Promise<Todo>;
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 = {
@@ -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<IPluginApiClient['getBranch']> | null;
setRefetch: SetRefetch;
successCb?: ComponentConfigCreateRc['successCb'];
}
@@ -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<IPluginApiClient['getBranch']> | null;
latestRelease: ApiMethodRetval<
IPluginApiClient['getLatestRelease']
>['latestRelease'];
@@ -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<IPluginApiClient['getBranch']> | null;
setRefetch: SetRefetch;
successCb?: ComponentConfigPatch['successCb'];
}
@@ -53,6 +49,10 @@ export const Patch = ({
return <NoLatestRelease />;
}
if (releaseBranch === null) {
return <NoLatestRelease />;
}
const { bumpedTag, tagParts } = getBumpedTag({
project,
tag: latestRelease.tagName,
@@ -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(
<PatchBody
latestRelease={mockRcRelease}
setRefetch={jest.fn()}
releaseBranch={mockReleaseBranch}
bumpedTag={mockBumpedTag}
latestRelease={mockRcRelease}
releaseBranch={mockReleaseBranch}
setRefetch={jest.fn()}
tagParts={mockTagParts}
/>,
);
@@ -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<IPluginApiClient['getLatestRelease']>['latestRelease']
>;
releaseBranch: GhGetBranchResponse | null;
releaseBranch: ApiMethodRetval<IPluginApiClient['getBranch']>;
setRefetch: SetRefetch;
successCb?: ComponentConfigPatch['successCb'];
tagParts: NonNullable<CalverTagParts | SemverTagParts>;
@@ -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,
};
@@ -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 {
@@ -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,
});
/**
@@ -29,7 +29,7 @@ export const NoLatestRelease = () => {
className={classes.paragraph}
severity="warning"
>
Unable to find any GitHub release
Unable to find any Release
</Alert>
);
};
@@ -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(<NoReleaseBranch />);
expect(
getByTestId(TEST_IDS.components.noReleaseBranch),
).toBeInTheDocument();
});
});
@@ -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 (
<Alert
data-testid={TEST_IDS.components.noReleaseBranch}
className={classes.paragraph}
severity="warning"
>
Unable to find any Release Branch
</Alert>
);
};
@@ -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,
};
};
@@ -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 {
@@ -92,16 +92,18 @@ export const mockReleaseVersion = createMockRelease({
/**
* MOCK BRANCH
*/
const createMockBranch = ({ ...rest }: Partial<GhGetBranchResponse> = {}) =>
const createMockBranch = ({
...rest
}: Partial<NonNullable<ApiMethodRetval<IPluginApiClient['getBranch']>>> = {}) =>
({
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<ApiMethodRetval<IPluginApiClient['getBranch']>>);
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({
@@ -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',