diff --git a/scm-ui-components/packages/ui-types/src/Branches.js b/scm-ui-components/packages/ui-types/src/Branches.js index 2dee365424..6d501af593 100644 --- a/scm-ui-components/packages/ui-types/src/Branches.js +++ b/scm-ui-components/packages/ui-types/src/Branches.js @@ -7,3 +7,8 @@ export type Branch = { defaultBranch?: boolean, _links: Links } + +export type BranchRequest = { + name: string, + parent: string +} diff --git a/scm-ui-components/packages/ui-types/src/index.js b/scm-ui-components/packages/ui-types/src/index.js index 02e88f12e1..c57a3c6792 100644 --- a/scm-ui-components/packages/ui-types/src/index.js +++ b/scm-ui-components/packages/ui-types/src/index.js @@ -9,7 +9,7 @@ export type { Group, Member } from "./Group"; export type { Repository, RepositoryCollection, RepositoryGroup } from "./Repositories"; export type { RepositoryType, RepositoryTypeCollection } from "./RepositoryTypes"; -export type { Branch } from "./Branches"; +export type { Branch, BranchRequest } from "./Branches"; export type { Changeset } from "./Changesets"; diff --git a/scm-ui/src/repos/branches/components/BranchForm.js b/scm-ui/src/repos/branches/components/BranchForm.js index 2938b06a2a..d72e2196ba 100644 --- a/scm-ui/src/repos/branches/components/BranchForm.js +++ b/scm-ui/src/repos/branches/components/BranchForm.js @@ -1,7 +1,7 @@ // @flow import React from "react"; import { translate } from "react-i18next"; -import type { Repository, Branch } from "@scm-manager/ui-types"; +import type { Repository, Branch, BranchRequest } from "@scm-manager/ui-types"; import { Select, InputField, @@ -11,7 +11,7 @@ import { import { orderBranches } from "../util/orderBranches"; type Props = { - submitForm: Branch => void, + submitForm: BranchRequest => void, repository: Repository, branches: Branch[], loading?: boolean, @@ -51,7 +51,10 @@ class BranchForm extends React.Component { submit = (event: Event) => { event.preventDefault(); if (this.isValid()) { - this.props.submitForm(this.state.branch); + this.props.submitForm({ + name: this.state.name, + parent: this.state.source + }); } }; diff --git a/scm-ui/src/repos/branches/containers/CreateBranch.js b/scm-ui/src/repos/branches/containers/CreateBranch.js index 1b0ef76408..a38d483af7 100644 --- a/scm-ui/src/repos/branches/containers/CreateBranch.js +++ b/scm-ui/src/repos/branches/containers/CreateBranch.js @@ -7,10 +7,11 @@ import { } from "@scm-manager/ui-components"; import { translate } from "react-i18next"; import BranchForm from "../components/BranchForm"; -import type { Repository, Branch } from "@scm-manager/ui-types"; +import type { Repository, Branch, BranchRequest } from "@scm-manager/ui-types"; import { fetchBranches, getBranches, + getBrancheCreateLink, createBranch, createBranchReset, isCreateBranchPending, @@ -28,10 +29,16 @@ type Props = { error?: Error, repository: Repository, branches: Branch[], + createBranchesLink: string, // dispatcher functions fetchBranches: Repository => void, - createBranch: (branch: Branch, callback?: () => void) => void, + createBranch: ( + createLink: string, + repository: Repository, + branch: BranchRequest, + callback?: (Branch) => void + ) => void, resetForm: () => void, // context objects @@ -48,12 +55,21 @@ class CreateBranch extends React.Component { } branchCreated = (branch: Branch) => { - const { history } = this.props; - history.push("/branch/" + encodeURIComponent(branch.name) + "/info"); + const { history, repository } = this.props; + history.push( + `/repo/${repository.namespace}/${ + repository.name + }/branch/${encodeURIComponent(branch.name)}/info` + ); }; - createBranch = (branch: Branch) => { - this.props.createBranch(branch, () => this.branchCreated(branch)); + createBranch = (branch: BranchRequest) => { + this.props.createBranch( + this.props.createBranchesLink, + this.props.repository, + branch, + newBranch => this.branchCreated(newBranch) + ); }; transmittedName = (url: string) => { @@ -76,7 +92,7 @@ class CreateBranch extends React.Component { <> this.createBranch(branch)} + submitForm={branchRequest => this.createBranch(branchRequest)} loading={loading} repository={repository} branches={branches} @@ -93,11 +109,12 @@ const mapDispatchToProps = dispatch => { dispatch(fetchBranches(repository)); }, createBranch: ( + createLink: string, repository: Repository, - branch: Branch, - callback?: () => void + branchRequest: BranchRequest, + callback?: (newBranch: Branch) => void ) => { - dispatch(createBranch("INSERTLINK", repository, branch, callback)); //TODO + dispatch(createBranch(createLink, repository, branchRequest, callback)); }, resetForm: () => { dispatch(createBranchReset()); @@ -111,11 +128,13 @@ const mapStateToProps = (state, ownProps) => { const error = getFetchBranchesFailure(state, repository) || getCreateBranchFailure(state); const branches = getBranches(state, repository); + const createBranchesLink = getBrancheCreateLink(state, repository); return { repository, loading, error, - branches + branches, + createBranchesLink }; }; diff --git a/scm-ui/src/repos/branches/modules/branches.js b/scm-ui/src/repos/branches/modules/branches.js index c6cb7a8215..5fb3da5994 100644 --- a/scm-ui/src/repos/branches/modules/branches.js +++ b/scm-ui/src/repos/branches/modules/branches.js @@ -5,7 +5,12 @@ import { SUCCESS_SUFFIX } from "../../../modules/types"; import { apiClient } from "@scm-manager/ui-components"; -import type { Action, Branch, Repository } from "@scm-manager/ui-types"; +import type { + Action, + Branch, + BranchRequest, + Repository +} from "@scm-manager/ui-types"; import { isPending } from "../../../modules/pending"; import { getFailure } from "../../../modules/failure"; @@ -25,7 +30,8 @@ export const CREATE_BRANCH_SUCCESS = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_FAILURE = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; export const CREATE_BRANCH_RESET = `${CREATE_BRANCH}_${PENDING_SUFFIX}`; -const CONTENT_TYPE_BRANCH = "application/vnd.scmm-branch+json;v=2"; +const CONTENT_TYPE_BRANCH_REQUEST = + "application/vnd.scmm-branchRequest+json;v=2"; // Fetching branches @@ -79,13 +85,13 @@ export function fetchBranch(repository: Repository, name: string) { export function createBranch( link: string, repository: Repository, - branch: Branch, + branchRequest: BranchRequest, callback?: (branch: Branch) => void ) { return function(dispatch: any) { - dispatch(createBranchPending(repository, branch.name)); + dispatch(createBranchPending(repository, branchRequest.name)); return apiClient - .post(link, branch, CONTENT_TYPE_BRANCH) + .post(link, branchRequest, CONTENT_TYPE_BRANCH_REQUEST) .then(response => response.headers.get("Location")) .then(location => apiClient.get(location)) .then(response => response.json()) @@ -110,6 +116,13 @@ export function getBranches(state: Object, repository: Repository) { } } +export function getBrancheCreateLink(state: Object, repository: Repository) { + const repoState = getRepoState(state, repository); + if (repoState && repoState.list) { + return repoState.list._links.create.href; + } +} + function getRepoState(state: Object, repository: Repository) { const key = createKey(repository); const repoState = state.branches[key];