merged for orderbranches and routing

This commit is contained in:
Florian Scholdei
2019-04-03 16:43:36 +02:00
9 changed files with 178 additions and 106 deletions

View File

@@ -0,0 +1,32 @@
// @flow
import React from "react";
import BranchDetail from "./BranchDetail";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
import type { Repository, Branch } from "@scm-manager/ui-types";
type Props = {
repository: Repository,
branch: Branch
};
class BranchView extends React.Component<Props> {
render() {
const { repository, branch } = this.props;
return (
<div>
<BranchDetail repository={repository} branch={branch} />
<hr />
<div className="content">
<ExtensionPoint
name="repos.branch-details.information"
renderAll={true}
props={{ repository, branch }}
/>
</div>
</div>
);
}
}
export default BranchView;

View File

@@ -1,11 +1,10 @@
// @flow //@flow
import React from "react"; import React from "react";
import BranchDetail from "../components/BranchDetail"; import BranchView from "../components/BranchView";
import { ExtensionPoint } from "@scm-manager/ui-extensions";
import type { Repository, Branch } from "@scm-manager/ui-types";
import { connect } from "react-redux"; import { connect } from "react-redux";
import { Redirect, Route, Switch, withRouter } from "react-router-dom";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import { withRouter } from "react-router-dom"; import type { Repository, Branch } from "@scm-manager/ui-types";
import { import {
fetchBranch, fetchBranch,
getBranch, getBranch,
@@ -13,32 +12,61 @@ import {
isFetchBranchPending isFetchBranchPending
} from "../modules/branches"; } from "../modules/branches";
import { ErrorPage, Loading } from "@scm-manager/ui-components"; import { ErrorPage, Loading } from "@scm-manager/ui-components";
import type { History } from "history";
type Props = { type Props = {
repository: Repository, repository: Repository,
branchName: string, branchName: string,
branch: Branch,
loading: boolean, loading: boolean,
error?: Error, error?: Error,
branch?: Branch,
// dispatch functions
fetchBranch: (repository: Repository, branchName: string) => void,
// context props // context props
t: string => string t: string => string,
history: History,
match: any,
location: any,
// dispatch functions
fetchBranch: (repository: Repository, branchName: string) => void
}; };
class BranchView extends React.Component<Props> { class BranchRoot extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const { fetchBranch, repository, branchName } = this.props; const { fetchBranch, repository, branchName } = this.props;
fetchBranch(repository, branchName); fetchBranch(repository, branchName);
} }
stripEndingSlash = (url: string) => {
if (url.endsWith("/")) {
return url.substring(0, url.length - 1);
}
return url;
};
matchedUrl = () => {
return this.stripEndingSlash(this.props.match.url);
};
render() { render() {
const { loading, error, t, repository, branch } = this.props; const {
repository,
branch,
loading,
error,
t,
match,
location
} = this.props;
const url = this.matchedUrl();
if (error) { if (error) {
if(location.search.indexOf("?create=true") > -1) {
return <Redirect to={`/repo/${repository.namespace}/${repository.name}/branches/create?name=${match.params.branch}`} />;
}
return ( return (
<ErrorPage <ErrorPage
title={t("branches.errorTitle")} title={t("branches.errorTitle")}
@@ -48,22 +76,20 @@ class BranchView extends React.Component<Props> {
); );
} }
if (!branch || loading) { if (loading || !branch) {
return <Loading />; return <Loading />;
} }
return ( return (
<div> <Switch>
<BranchDetail repository={repository} branch={branch} /> <Redirect exact from={url} to={`${url}/info`} />
<hr /> <Route
<div className="content"> path={`${url}/info`}
<ExtensionPoint component={() => (
name="repos.branch-details.information" <BranchView repository={repository} branch={branch} />
renderAll={true} )}
props={{ repository, branch }}
/> />
</div> </Switch>
</div>
); );
} }
} }
@@ -95,5 +121,5 @@ export default withRouter(
connect( connect(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
)(translate("repos")(BranchView)) )(translate("repos")(BranchRoot))
); );

View File

@@ -4,9 +4,9 @@ import {
fetchBranches, fetchBranches,
getBranches, getBranches,
getFetchBranchesFailure, getFetchBranchesFailure,
isFetchBranchesPending, isFetchBranchesPending
orderBranches
} from "../modules/branches"; } from "../modules/branches";
import { orderBranches } from "../util/orderBranches";
import { connect } from "react-redux"; import { connect } from "react-redux";
import type { Branch, Repository } from "@scm-manager/ui-types"; import type { Branch, Repository } from "@scm-manager/ui-types";
import { compose } from "redux"; import { compose } from "redux";

View File

@@ -310,32 +310,3 @@ function createKey(repository: Repository): string {
const { namespace, name } = repository; const { namespace, name } = repository;
return `${namespace}/${name}`; return `${namespace}/${name}`;
} }
// master, default should always be the first one,
// followed by develop the rest should be ordered by its name
export function orderBranches(branches: Branch[]) {
branches.sort((a, b) => {
if (a.defaultBranch && !b.defaultBranch) {
return -20;
} else if (!a.defaultBranch && b.defaultBranch) {
return 20;
} else if (a.name === "master" && b.name !== "master") {
return -10;
} else if (a.name !== "master" && b.name === "master") {
return 10;
} else if (a.name === "default" && b.name !== "default") {
return -10;
} else if (a.name !== "default" && b.name === "default") {
return 10;
} else if (a.name === "develop" && b.name !== "develop") {
return -5;
} else if (a.name !== "develop" && b.name === "develop") {
return 5;
} else if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
return 0;
});
}

View File

@@ -6,7 +6,6 @@ import reducer, {
FETCH_BRANCHES_FAILURE, FETCH_BRANCHES_FAILURE,
FETCH_BRANCHES_PENDING, FETCH_BRANCHES_PENDING,
FETCH_BRANCHES_SUCCESS, FETCH_BRANCHES_SUCCESS,
FETCH_BRANCH,
FETCH_BRANCH_PENDING, FETCH_BRANCH_PENDING,
FETCH_BRANCH_SUCCESS, FETCH_BRANCH_SUCCESS,
FETCH_BRANCH_FAILURE, FETCH_BRANCH_FAILURE,
@@ -16,8 +15,7 @@ import reducer, {
getBranch, getBranch,
getBranches, getBranches,
getFetchBranchesFailure, getFetchBranchesFailure,
isFetchBranchesPending, isFetchBranchesPending
orderBranches
} from "./branches"; } from "./branches";
const namespace = "foo"; const namespace = "foo";
@@ -35,18 +33,7 @@ const repository = {
const branch1 = { name: "branch1", revision: "revision1" }; const branch1 = { name: "branch1", revision: "revision1" };
const branch2 = { name: "branch2", revision: "revision2" }; const branch2 = { name: "branch2", revision: "revision2" };
const branch3 = { name: "branch3", revision: "revision3", defaultBranch: true }; const branch3 = { name: "branch3", revision: "revision3" };
const defaultBranch = {
name: "default",
revision: "revision4",
defaultBranch: false
};
const developBranch = {
name: "develop",
revision: "revision5",
defaultBranch: false
};
const masterBranch = { name: "master", revision: "revision6", defaultBranch: false };
describe("branches", () => { describe("branches", () => {
describe("fetch branches", () => { describe("fetch branches", () => {
@@ -293,30 +280,4 @@ describe("branches", () => {
expect(getFetchBranchesFailure({}, repository)).toBeUndefined(); expect(getFetchBranchesFailure({}, repository)).toBeUndefined();
}); });
}); });
describe("sort branches", () => {
it("should return branches", () => {
let branches = [branch1, branch2];
orderBranches(branches);
expect(branches).toEqual([branch1, branch2]);
});
it("should return defaultBranch first", () => {
let branches = [branch1, branch2, branch3];
orderBranches(branches);
expect(branches).toEqual([branch3, branch1, branch2]);
});
it("should order special branches as follows: master > default > develop", () => {
let branches = [defaultBranch, developBranch, masterBranch];
orderBranches(branches);
expect(branches).toEqual([masterBranch, defaultBranch, developBranch]);
});
it("should order special branches but starting with defaultBranch", () => {
let branches = [masterBranch, developBranch, defaultBranch, branch3];
orderBranches(branches);
expect(branches).toEqual([branch3, masterBranch, defaultBranch, developBranch]);
});
});
}); });

View File

@@ -0,0 +1,32 @@
// @flow
// master, default should always be the first one,
// followed by develop the rest should be ordered by its name
import type {Branch} from "@scm-manager/ui-types";
export function orderBranches(branches: Branch[]) {
branches.sort((a, b) => {
if (a.defaultBranch && !b.defaultBranch) {
return -20;
} else if (!a.defaultBranch && b.defaultBranch) {
return 20;
} else if (a.name === "master" && b.name !== "master") {
return -10;
} else if (a.name !== "master" && b.name === "master") {
return 10;
} else if (a.name === "default" && b.name !== "default") {
return -10;
} else if (a.name !== "default" && b.name === "default") {
return 10;
} else if (a.name === "develop" && b.name !== "develop") {
return -5;
} else if (a.name !== "develop" && b.name === "develop") {
return 5;
} else if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
}
return 0;
});
}

View File

@@ -0,0 +1,51 @@
import { orderBranches } from "./orderBranches";
const branch1 = { name: "branch1", revision: "revision1" };
const branch2 = { name: "branch2", revision: "revision2" };
const branch3 = { name: "branch3", revision: "revision3", defaultBranch: true };
const defaultBranch = {
name: "default",
revision: "revision4",
defaultBranch: false
};
const developBranch = {
name: "develop",
revision: "revision5",
defaultBranch: false
};
const masterBranch = {
name: "master",
revision: "revision6",
defaultBranch: false
};
describe("order branches", () => {
it("should return branches", () => {
let branches = [branch1, branch2];
orderBranches(branches);
expect(branches).toEqual([branch1, branch2]);
});
it("should return defaultBranch first", () => {
let branches = [branch1, branch2, branch3];
orderBranches(branches);
expect(branches).toEqual([branch3, branch1, branch2]);
});
it("should order special branches as follows: master > default > develop", () => {
let branches = [defaultBranch, developBranch, masterBranch];
orderBranches(branches);
expect(branches).toEqual([masterBranch, defaultBranch, developBranch]);
});
it("should order special branches but starting with defaultBranch", () => {
let branches = [masterBranch, developBranch, defaultBranch, branch3];
orderBranches(branches);
expect(branches).toEqual([
branch3,
masterBranch,
defaultBranch,
developBranch
]);
});
});

View File

@@ -40,7 +40,7 @@ type Props = {
t: string => string t: string => string
}; };
class BranchRoot extends React.Component<Props> { class ChangesetsRoot extends React.Component<Props> {
componentDidMount() { componentDidMount() {
this.props.fetchBranches(this.props.repository); this.props.fetchBranches(this.props.repository);
} }
@@ -146,4 +146,4 @@ export default compose(
mapStateToProps, mapStateToProps,
mapDispatchToProps mapDispatchToProps
) )
)(BranchRoot); )(ChangesetsRoot);

View File

@@ -24,14 +24,13 @@ import { translate } from "react-i18next";
import RepositoryDetails from "../components/RepositoryDetails"; import RepositoryDetails from "../components/RepositoryDetails";
import EditRepo from "./EditRepo"; import EditRepo from "./EditRepo";
import BranchesOverview from "../branches/containers/BranchesOverview"; import BranchesOverview from "../branches/containers/BranchesOverview";
import BranchView from "../branches/containers/BranchView";
import CreateBranch from "../branches/containers/CreateBranch"; import CreateBranch from "../branches/containers/CreateBranch";
import Permissions from "../permissions/containers/Permissions"; import Permissions from "../permissions/containers/Permissions";
import type { History } from "history"; import type { History } from "history";
import EditRepoNavLink from "../components/EditRepoNavLink"; import EditRepoNavLink from "../components/EditRepoNavLink";
import BranchRoot from "../branches/containers/BranchRoot";
import BranchRoot from "./ChangesetsRoot"; import ChangesetsRoot from "./ChangesetsRoot";
import ChangesetView from "./ChangesetView"; import ChangesetView from "./ChangesetView";
import PermissionsNavLink from "../components/PermissionsNavLink"; import PermissionsNavLink from "../components/PermissionsNavLink";
import Sources from "../sources/containers/Sources"; import Sources from "../sources/containers/Sources";
@@ -168,7 +167,7 @@ class RepositoryRoot extends React.Component<Props> {
<Route <Route
path={`${url}/changesets`} path={`${url}/changesets`}
render={() => ( render={() => (
<BranchRoot <ChangesetsRoot
repository={repository} repository={repository}
baseUrlWithBranch={`${url}/branch`} baseUrlWithBranch={`${url}/branch`}
baseUrlWithoutBranch={`${url}/changesets`} baseUrlWithoutBranch={`${url}/changesets`}
@@ -176,9 +175,9 @@ class RepositoryRoot extends React.Component<Props> {
)} )}
/> />
<Route <Route
path={`${url}/branch/:branch/info`} path={`${url}/branch/:branch`}
render={() => ( render={() => (
<BranchView <BranchRoot
repository={repository} repository={repository}
baseUrl={`${url}/branch`} baseUrl={`${url}/branch`}
/> />
@@ -187,7 +186,7 @@ class RepositoryRoot extends React.Component<Props> {
<Route <Route
path={`${url}/branch/:branch/changesets`} path={`${url}/branch/:branch/changesets`}
render={() => ( render={() => (
<BranchRoot <ChangesetsRoot
repository={repository} repository={repository}
baseUrlWithBranch={`${url}/branch`} baseUrlWithBranch={`${url}/branch`}
baseUrlWithoutBranch={`${url}/changesets`} baseUrlWithoutBranch={`${url}/changesets`}