mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-16 02:06:18 +01:00
merged for orderbranches and routing
This commit is contained in:
32
scm-ui/src/repos/branches/components/BranchView.js
Normal file
32
scm-ui/src/repos/branches/components/BranchView.js
Normal 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;
|
||||
@@ -1,11 +1,10 @@
|
||||
// @flow
|
||||
//@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 BranchView from "../components/BranchView";
|
||||
import { connect } from "react-redux";
|
||||
import { Redirect, Route, Switch, withRouter } from "react-router-dom";
|
||||
import { translate } from "react-i18next";
|
||||
import { withRouter } from "react-router-dom";
|
||||
import type { Repository, Branch } from "@scm-manager/ui-types";
|
||||
import {
|
||||
fetchBranch,
|
||||
getBranch,
|
||||
@@ -13,32 +12,61 @@ import {
|
||||
isFetchBranchPending
|
||||
} from "../modules/branches";
|
||||
import { ErrorPage, Loading } from "@scm-manager/ui-components";
|
||||
import type { History } from "history";
|
||||
|
||||
type Props = {
|
||||
repository: Repository,
|
||||
branchName: string,
|
||||
branch: Branch,
|
||||
loading: boolean,
|
||||
error?: Error,
|
||||
branch?: Branch,
|
||||
|
||||
// dispatch functions
|
||||
fetchBranch: (repository: Repository, branchName: string) => void,
|
||||
|
||||
// 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() {
|
||||
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 { loading, error, t, repository, branch } = 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 <Redirect to={`/repo/${repository.namespace}/${repository.name}/branches/create?name=${match.params.branch}`} />;
|
||||
}
|
||||
|
||||
return (
|
||||
<ErrorPage
|
||||
title={t("branches.errorTitle")}
|
||||
@@ -48,22 +76,20 @@ class BranchView extends React.Component<Props> {
|
||||
);
|
||||
}
|
||||
|
||||
if (!branch || loading) {
|
||||
if (loading || !branch) {
|
||||
return <Loading />;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<BranchDetail repository={repository} branch={branch} />
|
||||
<hr />
|
||||
<div className="content">
|
||||
<ExtensionPoint
|
||||
name="repos.branch-details.information"
|
||||
renderAll={true}
|
||||
props={{ repository, branch }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Switch>
|
||||
<Redirect exact from={url} to={`${url}/info`} />
|
||||
<Route
|
||||
path={`${url}/info`}
|
||||
component={() => (
|
||||
<BranchView repository={repository} branch={branch} />
|
||||
)}
|
||||
/>
|
||||
</Switch>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -95,5 +121,5 @@ export default withRouter(
|
||||
connect(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)(translate("repos")(BranchView))
|
||||
)(translate("repos")(BranchRoot))
|
||||
);
|
||||
@@ -4,9 +4,9 @@ import {
|
||||
fetchBranches,
|
||||
getBranches,
|
||||
getFetchBranchesFailure,
|
||||
isFetchBranchesPending,
|
||||
orderBranches
|
||||
isFetchBranchesPending
|
||||
} from "../modules/branches";
|
||||
import { orderBranches } from "../util/orderBranches";
|
||||
import { connect } from "react-redux";
|
||||
import type { Branch, Repository } from "@scm-manager/ui-types";
|
||||
import { compose } from "redux";
|
||||
|
||||
@@ -310,32 +310,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;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
32
scm-ui/src/repos/branches/util/orderBranches.js
Normal file
32
scm-ui/src/repos/branches/util/orderBranches.js
Normal 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;
|
||||
});
|
||||
}
|
||||
51
scm-ui/src/repos/branches/util/orderBranches.test.js
Normal file
51
scm-ui/src/repos/branches/util/orderBranches.test.js
Normal 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
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -40,7 +40,7 @@ type Props = {
|
||||
t: string => string
|
||||
};
|
||||
|
||||
class BranchRoot extends React.Component<Props> {
|
||||
class ChangesetsRoot extends React.Component<Props> {
|
||||
componentDidMount() {
|
||||
this.props.fetchBranches(this.props.repository);
|
||||
}
|
||||
@@ -146,4 +146,4 @@ export default compose(
|
||||
mapStateToProps,
|
||||
mapDispatchToProps
|
||||
)
|
||||
)(BranchRoot);
|
||||
)(ChangesetsRoot);
|
||||
|
||||
@@ -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<Props> {
|
||||
<Route
|
||||
path={`${url}/changesets`}
|
||||
render={() => (
|
||||
<BranchRoot
|
||||
<ChangesetsRoot
|
||||
repository={repository}
|
||||
baseUrlWithBranch={`${url}/branch`}
|
||||
baseUrlWithoutBranch={`${url}/changesets`}
|
||||
@@ -176,9 +175,9 @@ class RepositoryRoot extends React.Component<Props> {
|
||||
)}
|
||||
/>
|
||||
<Route
|
||||
path={`${url}/branch/:branch/info`}
|
||||
path={`${url}/branch/:branch`}
|
||||
render={() => (
|
||||
<BranchView
|
||||
<BranchRoot
|
||||
repository={repository}
|
||||
baseUrl={`${url}/branch`}
|
||||
/>
|
||||
@@ -187,7 +186,7 @@ class RepositoryRoot extends React.Component<Props> {
|
||||
<Route
|
||||
path={`${url}/branch/:branch/changesets`}
|
||||
render={() => (
|
||||
<BranchRoot
|
||||
<ChangesetsRoot
|
||||
repository={repository}
|
||||
baseUrlWithBranch={`${url}/branch`}
|
||||
baseUrlWithoutBranch={`${url}/changesets`}
|
||||
|
||||
Reference in New Issue
Block a user