2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2019-10-20 18:02:52 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import BranchView from "../components/BranchView";
|
|
|
|
|
import { connect } from "react-redux";
|
2020-01-08 13:40:07 +01:00
|
|
|
import { compose } from "redux";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { Redirect, Route, Switch, withRouter } from "react-router-dom";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { Branch, Repository } from "@scm-manager/ui-types";
|
2019-10-21 10:57:56 +02:00
|
|
|
import { fetchBranch, getBranch, getFetchBranchFailure, isFetchBranchPending } from "../modules/branches";
|
2020-01-08 15:57:13 +01:00
|
|
|
import { ErrorNotification, Loading, NotFoundError } from "@scm-manager/ui-components";
|
2019-10-20 18:02:52 +02:00
|
|
|
import { History } from "history";
|
|
|
|
|
import queryString from "query-string";
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
repository: Repository;
|
|
|
|
|
branchName: string;
|
|
|
|
|
branch: Branch;
|
|
|
|
|
loading: boolean;
|
|
|
|
|
error?: Error;
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
// context props
|
2019-10-19 16:38:07 +02:00
|
|
|
history: History;
|
|
|
|
|
match: any;
|
|
|
|
|
location: any;
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
// dispatch functions
|
2019-10-19 16:38:07 +02:00
|
|
|
fetchBranch: (repository: Repository, branchName: string) => void;
|
2019-04-03 15:19:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BranchRoot extends React.Component<Props> {
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
const { fetchBranch, repository, branchName } = this.props;
|
|
|
|
|
fetchBranch(repository, branchName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stripEndingSlash = (url: string) => {
|
2019-10-20 18:02:52 +02:00
|
|
|
if (url.endsWith("/")) {
|
2019-04-03 15:19:54 +02:00
|
|
|
return url.substring(0, url.length - 1);
|
|
|
|
|
}
|
|
|
|
|
return url;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
matchedUrl = () => {
|
|
|
|
|
return this.stripEndingSlash(this.props.match.url);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2019-04-04 11:55:01 +02:00
|
|
|
const { repository, branch, loading, error, match, location } = this.props;
|
2019-04-03 16:28:54 +02:00
|
|
|
|
|
|
|
|
const url = this.matchedUrl();
|
2019-04-03 15:19:54 +02:00
|
|
|
|
|
|
|
|
if (error) {
|
2019-10-21 10:57:56 +02:00
|
|
|
if (error instanceof NotFoundError && queryString.parse(location.search).create === "true") {
|
2019-04-04 11:55:01 +02:00
|
|
|
return (
|
|
|
|
|
<Redirect
|
2019-10-19 16:38:07 +02:00
|
|
|
to={`/repo/${repository.namespace}/${repository.name}/branches/create?name=${match.params.branch}`}
|
2019-04-04 11:55:01 +02:00
|
|
|
/>
|
|
|
|
|
);
|
2019-04-03 16:28:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-04 11:55:01 +02:00
|
|
|
return <ErrorNotification error={error} />;
|
2019-04-03 15:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading || !branch) {
|
|
|
|
|
return <Loading />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Switch>
|
|
|
|
|
<Redirect exact from={url} to={`${url}/info`} />
|
2019-10-21 10:57:56 +02:00
|
|
|
<Route path={`${url}/info`} component={() => <BranchView repository={repository} branch={branch} />} />
|
2019-04-03 15:19:54 +02:00
|
|
|
</Switch>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const mapStateToProps = (state: any, ownProps: Props) => {
|
2019-04-03 15:19:54 +02:00
|
|
|
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,
|
2019-10-20 18:02:52 +02:00
|
|
|
error
|
2019-04-03 15:19:54 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 10:31:43 +01:00
|
|
|
const mapDispatchToProps = (dispatch: any) => {
|
2019-04-03 15:19:54 +02:00
|
|
|
return {
|
|
|
|
|
fetchBranch: (repository: Repository, branchName: string) => {
|
|
|
|
|
dispatch(fetchBranch(repository, branchName));
|
2019-10-20 18:02:52 +02:00
|
|
|
}
|
2019-04-03 15:19:54 +02:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-08 13:40:07 +01:00
|
|
|
export default compose(withRouter, connect(mapStateToProps, mapDispatchToProps))(BranchRoot);
|