Added possibility to fetch changesets by branches

This commit is contained in:
Philipp Czora
2018-09-14 16:15:13 +02:00
parent 75002ffcff
commit 5687a552b8
6 changed files with 359 additions and 148 deletions

View File

@@ -3,8 +3,12 @@ import { connect } from "react-redux";
import ChangesetRow from "./ChangesetRow"; import ChangesetRow from "./ChangesetRow";
import type {Changeset} from "@scm-manager/ui-types"; import type {Changeset} from "@scm-manager/ui-types";
import { fetchChangesetsByNamespaceAndName, getChangesetsForNameAndNamespaceFromState } from "../modules/changesets"; import {
fetchChangesetsByNamespaceAndName,
getChangesets,
} from "../modules/changesets";
import { translate } from "react-i18next"; import { translate } from "react-i18next";
import {fetchBranchesByNamespaceAndName} from "../../repos/modules/branches";
type Props = { type Props = {
changesets: Changeset[], changesets: Changeset[],
@@ -15,6 +19,7 @@ class Changesets extends React.Component<Props> {
componentDidMount() { componentDidMount() {
const {namespace, name} = this.props.repository; const {namespace, name} = this.props.repository;
this.props.fetchChangesetsByNamespaceAndName(namespace, name); this.props.fetchChangesetsByNamespaceAndName(namespace, name);
this.props.fetchBranchesByNamespaceAndName(namespace, name);
} }
render() { render() {
@@ -24,7 +29,7 @@ class Changesets extends React.Component<Props> {
} }
return <table className="table is-hoverable is-fullwidth is-striped is-bordered"> return <table className="table is-hoverable is-fullwidth is-striped is-bordered">
<thead> <thead>
<tr>Changesets</tr> <th>Changesets</th>
</thead> </thead>
<tbody> <tbody>
{changesets.map((changeset, index) => { {changesets.map((changeset, index) => {
@@ -36,15 +41,20 @@ class Changesets extends React.Component<Props> {
} }
const mapStateToProps = (state, ownProps) => { const mapStateToProps = (state, ownProps) => {
const {namespace, name} = ownProps.repository;
return { return {
changesets: getChangesetsForNameAndNamespaceFromState(ownProps.repository.namespace, ownProps.repository.name, state) changesets: getChangesets(namespace, name, "", state)
} }
}; };
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
fetchChangesetsByNamespaceAndName: (namespace: string, name: string) => { fetchChangesetsByNamespaceAndName: (namespace: string, name: string) => {
dispatch(fetchChangesetsByNamespaceAndName(namespace, name)) dispatch(fetchChangesetsByNamespaceAndName(namespace, name));
},
fetchBranchesByNamespaceAndName: (namespace: string, name: string) => {
dispatch(fetchBranchesByNamespaceAndName(namespace, name));
} }
} }
}; };

View File

@@ -26,39 +26,61 @@ export function fetchChangesetsByNamespaceAndName(namespace: string, name: strin
} }
} }
export function fetchChangesetsPending(namespace: string, name: string): Action { export function fetchChangesetsByNamespaceNameAndBranch(namespace: string, name: string, branch: string) {
return function (dispatch: any) {
dispatch(fetchChangesetsPending(namespace, name, branch));
return apiClient.get(REPO_URL + "/" + namespace + "/" + name + "/branches/" + branch + "/changesets").then(response => response.json())
.then(data => {
dispatch(fetchChangesetsSuccess(data, namespace, name, branch))
}).catch(cause => {
dispatch(fetchChangesetsFailure(namespace, name, branch, cause))
})
}
}
export function fetchChangesetsPending(namespace: string, name: string, branch?: string): Action {
return { return {
type: FETCH_CHANGESETS_PENDING, type: FETCH_CHANGESETS_PENDING,
payload: { payload: {
namespace, namespace,
name name,
branch
}, },
itemId: namespace + "/" + name itemId: createItemId(namespace, name, branch)
} }
} }
export function fetchChangesetsSuccess(collection: any, namespace: string, name: string): Action { export function fetchChangesetsSuccess(collection: any, namespace: string, name: string, branch?: string): Action {
return { return {
type: FETCH_CHANGESETS_SUCCESS, type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace, name}, payload: {collection, namespace, name, branch},
itemId: namespace + "/" + name itemId: createItemId(namespace, name, branch)
} }
} }
function fetchChangesetsFailure(namespace: string, name: string, error: Error): Action { function fetchChangesetsFailure(namespace: string, name: string, branch?: string, error: Error): Action {
return { return {
type: FETCH_CHANGESETS_FAILURE, type: FETCH_CHANGESETS_FAILURE,
payload: { payload: {
namespace, namespace,
name, name,
branch,
error error
}, },
itemId: namespace + "/" + name itemId: createItemId(namespace, name, branch)
} }
} }
function createItemId(namespace: string, name: string, branch?: string): string {
let itemId = namespace + "/" + name;
if (branch && branch !== "") {
itemId = itemId + "/" + branch;
}
return itemId;
}
// reducer // reducer
export default function reducer(state: any = {}, action: any = {}) { export default function reducer(state: any = {}, action: Action = {type: "UNKNOWN"}): Object {
switch (action.type) { switch (action.type) {
case FETCH_CHANGESETS_SUCCESS: case FETCH_CHANGESETS_SUCCESS:
const {namespace, name} = action.payload; const {namespace, name} = action.payload;
@@ -90,7 +112,7 @@ function extractChangesetsByIds(data: any, oldChangesetsByIds: any) {
} }
//selectors //selectors
export function getChangesetsForNameAndNamespaceFromState(namespace: string, name: string, state: Object) { export function getChangesetsForNamespaceAndNameFromState(namespace: string, name: string, state: Object) {
const key = namespace + "/" + name; const key = namespace + "/" + name;
if (!state.changesets[key]) { if (!state.changesets[key]) {
return null; return null;
@@ -98,11 +120,19 @@ export function getChangesetsForNameAndNamespaceFromState(namespace: string, nam
return Object.values(state.changesets[key].byId); return Object.values(state.changesets[key].byId);
} }
export function isFetchChangesetsPending( state: Object, namespace: string, name: string) { export function getChangesets(namespace: string, name: string, branch: string, state: Object) {
return isPending(state, FETCH_CHANGESETS, namespace + "/" + name) const key = createItemId(namespace, name, branch);
if (!state.changesets[key]) {
return null;
}
return Object.values(state.changesets[key].byId);
} }
export function getFetchChangesetsFailure( state: Object, namespace: string, name: string) { export function isFetchChangesetsPending(state: Object, namespace: string, name: string, branch?: string) {
return getFailure(state, FETCH_CHANGESETS, namespace + "/" + name); return isPending(state, FETCH_CHANGESETS, createItemId(namespace, name, branch))
}
export function getFetchChangesetsFailure(state: Object, namespace: string, name: string, branch?: string) {
return getFailure(state, FETCH_CHANGESETS, createItemId(namespace, name, branch));
} }

View File

@@ -4,18 +4,26 @@ import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk"; import thunk from "redux-thunk";
import fetchMock from "fetch-mock"; import fetchMock from "fetch-mock";
import { import {
FETCH_CHANGESETS, FETCH_CHANGESETS_FAILURE, FETCH_CHANGESETS,
FETCH_CHANGESETS_FAILURE,
FETCH_CHANGESETS_PENDING, FETCH_CHANGESETS_PENDING,
FETCH_CHANGESETS_SUCCESS, FETCH_CHANGESETS_SUCCESS,
fetchChangesetsByNamespaceAndName, fetchChangesetsByNamespaceAndName,
fetchChangesetsSuccess, getChangesetsForNameAndNamespaceFromState, getFetchChangesetsFailure, isFetchChangesetsPending fetchChangesetsByNamespaceNameAndBranch,
fetchChangesetsSuccess,
getChangesets,
getChangesetsForNamespaceAndNameFromState,
getFetchChangesetsFailure,
isFetchChangesetsPending
} from "./changesets"; } from "./changesets";
import reducer from "./changesets"; import reducer from "./changesets";
const collection = {}; const collection = {};
describe("fetching of changesets", () => { describe("changesets", () => {
const URL = "/api/rest/v2/repositories/foo/bar/changesets"; describe("fetching of changesets", () => {
const DEFAULT_BRANCH_URL = "/api/rest/v2/repositories/foo/bar/changesets";
const SPECIFIC_BRANCH_URL = "/api/rest/v2/repositories/foo/bar/branches/specific/changesets";
const mockStore = configureMockStore([thunk]); const mockStore = configureMockStore([thunk]);
afterEach(() => { afterEach(() => {
@@ -23,12 +31,14 @@ describe("fetching of changesets", () => {
fetchMock.restore(); fetchMock.restore();
}); });
it("should fetch changesets", () => { it("should fetch changesets for default branch", () => {
fetchMock.getOnce(URL, "{}"); fetchMock.getOnce(DEFAULT_BRANCH_URL, "{}");
const expectedActions = [ const expectedActions = [
{type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"}, {
itemId: "foo/bar"}, type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"},
itemId: "foo/bar"
},
{ {
type: FETCH_CHANGESETS_SUCCESS, type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar"}, payload: {collection, namespace: "foo", name: "bar"},
@@ -42,12 +52,35 @@ describe("fetching of changesets", () => {
}); });
}); });
it("should fail fetching changesets on error", () => { it("should fetch changesets for specific branch", () => {
fetchMock.getOnce(URL, 500); fetchMock.getOnce(SPECIFIC_BRANCH_URL, "{}");
const expectedActions = [ const expectedActions = [
{type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"}, {
itemId: "foo/bar"}, type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
}
];
const store = mockStore({});
return store.dispatch(fetchChangesetsByNamespaceNameAndBranch("foo", "bar", "specific")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should fail fetching changesets on error", () => {
fetchMock.getOnce(DEFAULT_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar"},
itemId: "foo/bar"
},
{ {
type: FETCH_CHANGESETS_SUCCESS, type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar"}, payload: {collection, namespace: "foo", name: "bar"},
@@ -62,9 +95,32 @@ describe("fetching of changesets", () => {
expect(store.getActions()[1].payload).toBeDefined(); expect(store.getActions()[1].payload).toBeDefined();
}); });
}) })
});
describe("changesets reducer", () => { it("should fail fetching changesets for specific branch on error", () => {
fetchMock.getOnce(SPECIFIC_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING, payload: {namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: {collection, namespace: "foo", name: "bar", branch: "specific"},
itemId: "foo/bar/specific"
}
];
const store = mockStore({});
return store.dispatch(fetchChangesetsByNamespaceNameAndBranch("foo", "bar", "specific")).then(() => {
expect(store.getActions()[0]).toEqual(expectedActions[0]);
expect(store.getActions()[1].type).toEqual(FETCH_CHANGESETS_FAILURE);
expect(store.getActions()[1].payload).toBeDefined();
});
})
});
describe("changesets reducer", () => {
const responseBody = { const responseBody = {
_embedded: { _embedded: {
changesets: [ changesets: [
@@ -114,9 +170,9 @@ describe("changesets reducer", () => {
expect(newState["foo/bar"].byId["changeset2"]).toBeDefined(); expect(newState["foo/bar"].byId["changeset2"]).toBeDefined();
expect(newState["foo/bar"].byId["changeset1"]).toBeDefined(); expect(newState["foo/bar"].byId["changeset1"]).toBeDefined();
}) })
}); });
describe("changeset selectors", () => { describe("changeset selectors", () => {
const error = new Error("Something went wrong"); const error = new Error("Something went wrong");
it("should get all changesets for a given namespace and name", () => { it("should get all changesets for a given namespace and name", () => {
@@ -130,7 +186,8 @@ describe("changeset selectors", () => {
} }
} }
}; };
const result = getChangesetsForNameAndNamespaceFromState("foo", "bar", state); // const result = getChangesetsForNamespaceAndNameFromState("foo", "bar", state);
const result = getChangesets("foo", "bar", "", state);
expect(result).toContainEqual({id: "id1"}) expect(result).toContainEqual({id: "id1"})
}); });
@@ -162,4 +219,5 @@ describe("changeset selectors", () => {
expect(getFetchChangesetsFailure({}, "foo", "bar")).toBeUndefined(); expect(getFetchChangesetsFailure({}, "foo", "bar")).toBeUndefined();
}) })
});
}); });

View File

@@ -15,6 +15,7 @@ import failure from "./modules/failure";
import config from "./config/modules/config"; import config from "./config/modules/config";
import type { BrowserHistory } from "history/createBrowserHistory"; import type { BrowserHistory } from "history/createBrowserHistory";
import branches from "./repos/modules/branches";
function createReduxStore(history: BrowserHistory) { function createReduxStore(history: BrowserHistory) {
const composeEnhancers = const composeEnhancers =
@@ -28,6 +29,7 @@ function createReduxStore(history: BrowserHistory) {
repos, repos,
repositoryTypes, repositoryTypes,
changesets, changesets,
branches,
groups, groups,
auth, auth,
config config

View File

@@ -50,4 +50,45 @@ export function fetchBranchesFailure(namespace: string, name: string, error: Err
// Reducers // Reducers
export default function reducer(state: Object = {}, action: Action = {type: "UNKNOWN"}): Object {
switch (action.type) {
case FETCH_BRANCHES_SUCCESS:
const key = action.payload.namespace + "/" + action.payload.name;
let oldBranchesByNames = {[key]: {}};
if (state[key] !== undefined) {
oldBranchesByNames[key] = state[key]
}
return {
[key]: {
byNames: extractBranchesByNames(action.payload.data, oldBranchesByNames[key].byNames)
}
};
default:
return state;
}
}
function extractBranchesByNames(data: any, oldBranchesByNames: any): Branch[] {
const branches = data._embedded.branches;
const branchesByNames = {};
for (let branch of branches) {
branchesByNames[branch.name] = branch;
}
for (let name in oldBranchesByNames) {
branchesByNames[name] = oldBranchesByNames[name]
}
return branchesByNames;
}
// Selectors // Selectors
export function getBranchesForNamespaceAndNameFromState(namespace: string, name: string, state: Object) {
const key = namespace + "/" + name;
if (!state.branches[key]) {
return null;
}
return Object.values(state.branches[key].byNames);
}

View File

@@ -5,13 +5,25 @@ import {
FETCH_BRANCHES_FAILURE, FETCH_BRANCHES_FAILURE,
FETCH_BRANCHES_PENDING, FETCH_BRANCHES_PENDING,
FETCH_BRANCHES_SUCCESS, FETCH_BRANCHES_SUCCESS,
fetchBranchesByNamespaceAndName fetchBranchesByNamespaceAndName, getBranchesForNamespaceAndNameFromState
} from "./branches"; } from "./branches";
import reducer from "./branches";
const namespace = "foo";
const name = "bar";
const key = namespace + "/" + name;
const branch1 = {name: "branch1", revision: "revision1"};
const branch2 = {name: "branch2", revision: "revision2"};
const branch3 = {name: "branch3", revision: "revision3"};
describe("fetch branches", () => { describe("fetch branches", () => {
const URL = "/api/rest/v2/repositories/foo/bar/branches"; const URL = "/api/rest/v2/repositories/foo/bar/branches";
const mockStore = configureMockStore([thunk]); const mockStore = configureMockStore([thunk]);
afterEach(() => { afterEach(() => {
fetchMock.reset(); fetchMock.reset();
fetchMock.restore(); fetchMock.restore();
@@ -24,17 +36,19 @@ describe("fetch branches", () => {
fetchMock.getOnce(URL, "{}"); fetchMock.getOnce(URL, "{}");
const expectedActions = [ const expectedActions = [
{type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"}, {
itemId: "foo/bar"}, type: FETCH_BRANCHES_PENDING, payload: {namespace, name},
itemId: key
},
{ {
type: FETCH_BRANCHES_SUCCESS, type: FETCH_BRANCHES_SUCCESS,
payload: {data: collection, namespace: "foo", name: "bar"}, payload: {data: collection, namespace, name},
itemId: "foo/bar" itemId: key
} }
]; ];
const store = mockStore({}); const store = mockStore({});
return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => { return store.dispatch(fetchBranchesByNamespaceAndName(namespace, name)).then(() => {
expect(store.getActions()).toEqual(expectedActions); expect(store.getActions()).toEqual(expectedActions);
}); });
}); });
@@ -45,19 +59,75 @@ describe("fetch branches", () => {
fetchMock.getOnce(URL, 500); fetchMock.getOnce(URL, 500);
const expectedActions = [ const expectedActions = [
{type: FETCH_BRANCHES_PENDING, payload: {namespace: "foo", name: "bar"}, {
itemId: "foo/bar"}, type: FETCH_BRANCHES_PENDING, payload: {namespace, name},
itemId: key
},
{ {
type: FETCH_BRANCHES_FAILURE, type: FETCH_BRANCHES_FAILURE,
payload: {error: collection, namespace: "foo", name: "bar"}, payload: {error: collection, namespace, name},
itemId: "foo/bar" itemId: key
} }
]; ];
const store = mockStore({}); const store = mockStore({});
return store.dispatch(fetchBranchesByNamespaceAndName("foo", "bar")).then(() => { return store.dispatch(fetchBranchesByNamespaceAndName(namespace, name)).then(() => {
expect(store.getActions()[0]).toEqual(expectedActions[0]); expect(store.getActions()[0]).toEqual(expectedActions[0]);
expect(store.getActions()[1].type).toEqual(FETCH_BRANCHES_FAILURE); expect(store.getActions()[1].type).toEqual(FETCH_BRANCHES_FAILURE);
}); });
}) })
}); });
describe("branches reducer", () => {
const branches = {
_embedded: {
branches: [branch1, branch2]
}
};
const action = {
type: FETCH_BRANCHES_SUCCESS,
payload: {
namespace,
name,
data: branches
}
};
it("should update state according to successful fetch", () => {
const newState = reducer({}, action);
expect(newState).toBeDefined();
expect(newState[key]).toBeDefined();
expect(newState[key].byNames["branch1"]).toEqual(branch1);
expect(newState[key].byNames["branch2"]).toEqual(branch2);
});
it("should not delete existing branches from state", () => {
const oldState = {
"foo/bar": { byNames: {
"branch3": branch3
}}
};
const newState = reducer(oldState, action);
console.log(newState);
expect(newState[key].byNames["branch1"]).toEqual(branch1);
expect(newState[key].byNames["branch2"]).toEqual(branch2);
expect(newState[key].byNames["branch3"]).toEqual(branch3);
});
});
describe("branch selectors", () => {
it("should get branches for namespace and name", () => {
const state = {
branches: {
[key]: {
byNames: {
"branch1": branch1
}
}
}
};
getBranchesForNamespaceAndNameFromState(namespace, name, state);
})
});