From 3e2cb0b2125191c093f6d42909d4169059a29a55 Mon Sep 17 00:00:00 2001 From: Florian Scholdei Date: Wed, 3 Apr 2019 13:24:41 +0200 Subject: [PATCH 1/4] moved orderbranches function and tests in seperate file --- .../branches/containers/BranchesOverview.js | 4 +- .../branches/containers/OrderBranches.js | 32 ++++++++++++ .../branches/containers/OrderBranches.test.js | 51 +++++++++++++++++++ scm-ui/src/repos/branches/modules/branches.js | 29 ----------- .../repos/branches/modules/branches.test.js | 43 +--------------- 5 files changed, 87 insertions(+), 72 deletions(-) create mode 100644 scm-ui/src/repos/branches/containers/OrderBranches.js create mode 100644 scm-ui/src/repos/branches/containers/OrderBranches.test.js diff --git a/scm-ui/src/repos/branches/containers/BranchesOverview.js b/scm-ui/src/repos/branches/containers/BranchesOverview.js index 3bc92deab4..bc7405f416 100644 --- a/scm-ui/src/repos/branches/containers/BranchesOverview.js +++ b/scm-ui/src/repos/branches/containers/BranchesOverview.js @@ -4,9 +4,9 @@ import { fetchBranches, getBranches, getFetchBranchesFailure, - isFetchBranchesPending, - orderBranches + isFetchBranchesPending } from "../modules/branches"; +import { orderBranches } from "./OrderBranches"; import { connect } from "react-redux"; import type { Branch, Repository } from "@scm-manager/ui-types"; import { compose } from "redux"; diff --git a/scm-ui/src/repos/branches/containers/OrderBranches.js b/scm-ui/src/repos/branches/containers/OrderBranches.js new file mode 100644 index 0000000000..a1c2f5e460 --- /dev/null +++ b/scm-ui/src/repos/branches/containers/OrderBranches.js @@ -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; + }); +} diff --git a/scm-ui/src/repos/branches/containers/OrderBranches.test.js b/scm-ui/src/repos/branches/containers/OrderBranches.test.js new file mode 100644 index 0000000000..76c4de0c4a --- /dev/null +++ b/scm-ui/src/repos/branches/containers/OrderBranches.test.js @@ -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 + ]); + }); +}); diff --git a/scm-ui/src/repos/branches/modules/branches.js b/scm-ui/src/repos/branches/modules/branches.js index a5f676eced..44543106ca 100644 --- a/scm-ui/src/repos/branches/modules/branches.js +++ b/scm-ui/src/repos/branches/modules/branches.js @@ -232,32 +232,3 @@ function createKey(repository: Repository): string { const { namespace, name } = repository; 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; - }); -} diff --git a/scm-ui/src/repos/branches/modules/branches.test.js b/scm-ui/src/repos/branches/modules/branches.test.js index 8db4f6f83a..99c9bba60a 100644 --- a/scm-ui/src/repos/branches/modules/branches.test.js +++ b/scm-ui/src/repos/branches/modules/branches.test.js @@ -6,7 +6,6 @@ import reducer, { FETCH_BRANCHES_FAILURE, FETCH_BRANCHES_PENDING, FETCH_BRANCHES_SUCCESS, - FETCH_BRANCH, FETCH_BRANCH_PENDING, FETCH_BRANCH_SUCCESS, FETCH_BRANCH_FAILURE, @@ -16,8 +15,7 @@ import reducer, { getBranch, getBranches, getFetchBranchesFailure, - isFetchBranchesPending, - orderBranches + isFetchBranchesPending } from "./branches"; const namespace = "foo"; @@ -35,18 +33,7 @@ const repository = { 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 }; +const branch3 = { name: "branch3", revision: "revision3" }; describe("branches", () => { describe("fetch branches", () => { @@ -293,30 +280,4 @@ describe("branches", () => { 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]); - }); - }); }); From 9a8fd459f875c5a582b8e8b4a755914744c140cd Mon Sep 17 00:00:00 2001 From: Florian Scholdei Date: Wed, 3 Apr 2019 13:39:16 +0200 Subject: [PATCH 2/4] renamed orderBranches path --- scm-ui/src/repos/branches/containers/BranchRoot.js | 0 scm-ui/src/repos/branches/containers/BranchesOverview.js | 2 +- .../{containers/OrderBranches.js => util/orderBranches.js} | 0 .../OrderBranches.test.js => util/orderBranches.test.js} | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 scm-ui/src/repos/branches/containers/BranchRoot.js rename scm-ui/src/repos/branches/{containers/OrderBranches.js => util/orderBranches.js} (100%) rename scm-ui/src/repos/branches/{containers/OrderBranches.test.js => util/orderBranches.test.js} (96%) diff --git a/scm-ui/src/repos/branches/containers/BranchRoot.js b/scm-ui/src/repos/branches/containers/BranchRoot.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/scm-ui/src/repos/branches/containers/BranchesOverview.js b/scm-ui/src/repos/branches/containers/BranchesOverview.js index bc7405f416..f8c8be7529 100644 --- a/scm-ui/src/repos/branches/containers/BranchesOverview.js +++ b/scm-ui/src/repos/branches/containers/BranchesOverview.js @@ -6,7 +6,7 @@ import { getFetchBranchesFailure, isFetchBranchesPending } from "../modules/branches"; -import { orderBranches } from "./OrderBranches"; +import { orderBranches } from "../util/orderBranches"; import { connect } from "react-redux"; import type { Branch, Repository } from "@scm-manager/ui-types"; import { compose } from "redux"; diff --git a/scm-ui/src/repos/branches/containers/OrderBranches.js b/scm-ui/src/repos/branches/util/orderBranches.js similarity index 100% rename from scm-ui/src/repos/branches/containers/OrderBranches.js rename to scm-ui/src/repos/branches/util/orderBranches.js diff --git a/scm-ui/src/repos/branches/containers/OrderBranches.test.js b/scm-ui/src/repos/branches/util/orderBranches.test.js similarity index 96% rename from scm-ui/src/repos/branches/containers/OrderBranches.test.js rename to scm-ui/src/repos/branches/util/orderBranches.test.js index 76c4de0c4a..557cc8a4c5 100644 --- a/scm-ui/src/repos/branches/containers/OrderBranches.test.js +++ b/scm-ui/src/repos/branches/util/orderBranches.test.js @@ -1,4 +1,4 @@ -import { orderBranches } from "./OrderBranches"; +import { orderBranches } from "./orderBranches"; const branch1 = { name: "branch1", revision: "revision1" }; const branch2 = { name: "branch2", revision: "revision2" }; From f940af3369d7907c81c6427a83ef519c3bac7e71 Mon Sep 17 00:00:00 2001 From: Florian Scholdei Date: Wed, 3 Apr 2019 15:19:54 +0200 Subject: [PATCH 3/4] outsourced single branch routing to branchroot --- .../repos/branches/components/BranchView.js | 32 +++++ .../repos/branches/containers/BranchRoot.js | 117 ++++++++++++++++++ .../repos/branches/containers/BranchView.js | 99 --------------- scm-ui/src/repos/containers/ChangesetsRoot.js | 4 +- scm-ui/src/repos/containers/RepositoryRoot.js | 13 +- 5 files changed, 157 insertions(+), 108 deletions(-) create mode 100644 scm-ui/src/repos/branches/components/BranchView.js delete mode 100644 scm-ui/src/repos/branches/containers/BranchView.js diff --git a/scm-ui/src/repos/branches/components/BranchView.js b/scm-ui/src/repos/branches/components/BranchView.js new file mode 100644 index 0000000000..a7dd7dbd4b --- /dev/null +++ b/scm-ui/src/repos/branches/components/BranchView.js @@ -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 { + render() { + const { repository, branch } = this.props; + + return ( +
+ +
+
+ +
+
+ ); + } +} + +export default BranchView; diff --git a/scm-ui/src/repos/branches/containers/BranchRoot.js b/scm-ui/src/repos/branches/containers/BranchRoot.js index e69de29bb2..22b7011968 100644 --- a/scm-ui/src/repos/branches/containers/BranchRoot.js +++ b/scm-ui/src/repos/branches/containers/BranchRoot.js @@ -0,0 +1,117 @@ +//@flow +import React from "react"; +import BranchView from "../components/BranchView"; +import { connect } from "react-redux"; +import { Redirect, Route, Switch, withRouter } from "react-router-dom"; +import { translate } from "react-i18next"; +import type { Repository, Branch } from "@scm-manager/ui-types"; +import { + fetchBranch, + getBranch, + getFetchBranchFailure, + isFetchBranchPending +} from "../modules/branches"; +import { ErrorPage, Loading } from "@scm-manager/ui-components"; +import CreateBranch from "./CreateBranch"; +import type { History } from "history"; + +type Props = { + repository: Repository, + branchName: string, + branch: Branch, + loading: boolean, + error?: Error, + + // context props + t: string => string, + history: History, + match: any, + + // dispatch functions + fetchBranch: (repository: Repository, branchName: string) => void +}; + +class BranchRoot extends React.Component { + componentDidMount() { + const { fetchBranch, repository, branchName } = this.props; + + 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() { + const { repository, branch, loading, error, t } = this.props; + + if (error) { + return ( + + ); + } + + if (loading || !branch) { + return ; + } + + const url = this.matchedUrl(); + + return ( + + + ( + + )} + /> + } + /> + + ); + } +} + +const mapStateToProps = (state, ownProps) => { + const { repository } = ownProps; + const branchName = decodeURIComponent(ownProps.match.params.branch); + const branch = getBranch(state, repository, branchName); + const loading = isFetchBranchPending(state, repository, branchName); + const error = getFetchBranchFailure(state, repository, branchName); + return { + repository, + branchName, + branch, + loading, + error + }; +}; + +const mapDispatchToProps = dispatch => { + return { + fetchBranch: (repository: Repository, branchName: string) => { + dispatch(fetchBranch(repository, branchName)); + } + }; +}; + +export default withRouter( + connect( + mapStateToProps, + mapDispatchToProps + )(translate("repos")(BranchRoot)) +); diff --git a/scm-ui/src/repos/branches/containers/BranchView.js b/scm-ui/src/repos/branches/containers/BranchView.js deleted file mode 100644 index 5e7fa9610a..0000000000 --- a/scm-ui/src/repos/branches/containers/BranchView.js +++ /dev/null @@ -1,99 +0,0 @@ -// @flow -import React from "react"; -import BranchDetail from "../components/BranchDetail"; -import { ExtensionPoint } from "@scm-manager/ui-extensions"; -import type { Repository, Branch } from "@scm-manager/ui-types"; -import { connect } from "react-redux"; -import { translate } from "react-i18next"; -import { withRouter } from "react-router-dom"; -import { - fetchBranch, - getBranch, - getFetchBranchFailure, - isFetchBranchPending -} from "../modules/branches"; -import { ErrorPage, Loading } from "@scm-manager/ui-components"; - -type Props = { - repository: Repository, - branchName: string, - loading: boolean, - error?: Error, - branch?: Branch, - - // dispatch functions - fetchBranch: (repository: Repository, branchName: string) => void, - - // context props - t: string => string -}; - -class BranchView extends React.Component { - componentDidMount() { - const { fetchBranch, repository, branchName } = this.props; - - fetchBranch(repository, branchName); - } - - render() { - const { loading, error, t, repository, branch } = this.props; - - if (error) { - return ( - - ); - } - - if (!branch || loading) { - return ; - } - - return ( -
- -
-
- -
-
- ); - } -} - -const mapStateToProps = (state, ownProps) => { - const { repository } = ownProps; - const branchName = decodeURIComponent(ownProps.match.params.branch); - const branch = getBranch(state, repository, branchName); - const loading = isFetchBranchPending(state, repository, branchName); - const error = getFetchBranchFailure(state, repository, branchName); - return { - repository, - branchName, - branch, - loading, - error - }; -}; - -const mapDispatchToProps = dispatch => { - return { - fetchBranch: (repository: Repository, branchName: string) => { - dispatch(fetchBranch(repository, branchName)); - } - }; -}; - -export default withRouter( - connect( - mapStateToProps, - mapDispatchToProps - )(translate("repos")(BranchView)) -); diff --git a/scm-ui/src/repos/containers/ChangesetsRoot.js b/scm-ui/src/repos/containers/ChangesetsRoot.js index c4f40e0d74..c4c310d94d 100644 --- a/scm-ui/src/repos/containers/ChangesetsRoot.js +++ b/scm-ui/src/repos/containers/ChangesetsRoot.js @@ -40,7 +40,7 @@ type Props = { t: string => string }; -class BranchRoot extends React.Component { +class ChangesetsRoot extends React.Component { componentDidMount() { this.props.fetchBranches(this.props.repository); } @@ -146,4 +146,4 @@ export default compose( mapStateToProps, mapDispatchToProps ) -)(BranchRoot); +)(ChangesetsRoot); diff --git a/scm-ui/src/repos/containers/RepositoryRoot.js b/scm-ui/src/repos/containers/RepositoryRoot.js index 8c481f1adc..418b43d144 100644 --- a/scm-ui/src/repos/containers/RepositoryRoot.js +++ b/scm-ui/src/repos/containers/RepositoryRoot.js @@ -24,14 +24,13 @@ import { translate } from "react-i18next"; import RepositoryDetails from "../components/RepositoryDetails"; import EditRepo from "./EditRepo"; import BranchesOverview from "../branches/containers/BranchesOverview"; -import BranchView from "../branches/containers/BranchView"; import CreateBranch from "../branches/containers/CreateBranch"; import Permissions from "../permissions/containers/Permissions"; import type { History } from "history"; import EditRepoNavLink from "../components/EditRepoNavLink"; - -import BranchRoot from "./ChangesetsRoot"; +import BranchRoot from "../branches/containers/BranchRoot"; +import ChangesetsRoot from "./ChangesetsRoot"; import ChangesetView from "./ChangesetView"; import PermissionsNavLink from "../components/PermissionsNavLink"; import Sources from "../sources/containers/Sources"; @@ -168,7 +167,7 @@ class RepositoryRoot extends React.Component { ( - { )} /> ( - @@ -187,7 +186,7 @@ class RepositoryRoot extends React.Component { ( - Date: Wed, 3 Apr 2019 16:28:54 +0200 Subject: [PATCH 4/4] added create branch redirect --- .../repos/branches/containers/BranchRoot.js | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/scm-ui/src/repos/branches/containers/BranchRoot.js b/scm-ui/src/repos/branches/containers/BranchRoot.js index 22b7011968..f1570735e2 100644 --- a/scm-ui/src/repos/branches/containers/BranchRoot.js +++ b/scm-ui/src/repos/branches/containers/BranchRoot.js @@ -12,7 +12,6 @@ import { isFetchBranchPending } from "../modules/branches"; import { ErrorPage, Loading } from "@scm-manager/ui-components"; -import CreateBranch from "./CreateBranch"; import type { History } from "history"; type Props = { @@ -26,6 +25,7 @@ type Props = { t: string => string, history: History, match: any, + location: any, // dispatch functions fetchBranch: (repository: Repository, branchName: string) => void @@ -50,9 +50,23 @@ class BranchRoot extends React.Component { }; render() { - const { repository, branch, loading, error, t } = this.props; + const { + repository, + branch, + loading, + error, + t, + match, + location + } = this.props; + + const url = this.matchedUrl(); if (error) { + if(location.search.indexOf("?create=true") > -1) { + return ; + } + return ( { return ; } - const url = this.matchedUrl(); - return ( - ( - - )} - /> } + component={() => ( + + )} /> );