Moved changeset code to repos

This commit is contained in:
Philipp Czora
2018-09-19 14:10:50 +02:00
parent 10b120c862
commit c853a3eb80
9 changed files with 21 additions and 21 deletions

View File

@@ -0,0 +1,257 @@
// @flow
import {
FAILURE_SUFFIX,
PENDING_SUFFIX,
SUCCESS_SUFFIX
} from "../../modules/types";
import { apiClient } from "@scm-manager/ui-components";
import { isPending } from "../../modules/pending";
import { getFailure } from "../../modules/failure";
import { combineReducers } from "redux";
import type { Action, PagedCollection } from "@scm-manager/ui-types";
export const FETCH_CHANGESETS = "scm/repos/FETCH_CHANGESETS";
export const FETCH_CHANGESETS_PENDING = `${FETCH_CHANGESETS}_${PENDING_SUFFIX}`;
export const FETCH_CHANGESETS_SUCCESS = `${FETCH_CHANGESETS}_${SUCCESS_SUFFIX}`;
export const FETCH_CHANGESETS_FAILURE = `${FETCH_CHANGESETS}_${FAILURE_SUFFIX}`;
const REPO_URL = "repositories";
//TODO: Content type
// actions
export function fetchChangesetsWithOptions(
namespace: string,
name: string,
branch?: string,
suffix?: string
) {
let link = REPO_URL + `/${namespace}/${name}`;
if (branch && branch !== "") {
link = link + `/branches/${branch}`;
}
link = link + "/changesets";
if (suffix) {
link = link + `${suffix}`;
}
return function(dispatch: any) {
dispatch(fetchChangesetsPending(namespace, name, branch));
return apiClient
.get(link)
.then(response => response.json())
.then(data => {
dispatch(fetchChangesetsSuccess(data, namespace, name, branch));
})
.catch(cause => {
dispatch(fetchChangesetsFailure(namespace, name, cause, branch));
});
};
}
export function fetchChangesets(namespace: string, name: string) {
return fetchChangesetsWithOptions(namespace, name);
}
export function fetchChangesetsByPage(
namespace: string,
name: string,
page: number
) {
return fetchChangesetsWithOptions(namespace, name, "", `?page=${page}`);
}
export function fetchChangesetsByBranchAndPage(
namespace: string,
name: string,
branch: string,
page: number
) {
return fetchChangesetsWithOptions(namespace, name, branch, `?page=${page}`);
}
export function fetchChangesetsByNamespaceNameAndBranch(
namespace: string,
name: string,
branch: string
) {
return fetchChangesetsWithOptions(namespace, name, branch);
}
export function fetchChangesetsPending(
namespace: string,
name: string,
branch?: string
): Action {
const itemId = createItemId(namespace, name, branch);
return {
type: FETCH_CHANGESETS_PENDING,
payload: itemId,
itemId
};
}
export function fetchChangesetsSuccess(
changesets: any,
namespace: string,
name: string,
branch?: string
): Action {
return {
type: FETCH_CHANGESETS_SUCCESS,
payload: changesets,
itemId: createItemId(namespace, name, branch)
};
}
function fetchChangesetsFailure(
namespace: string,
name: string,
error: Error,
branch?: string
): Action {
return {
type: FETCH_CHANGESETS_FAILURE,
payload: {
namespace,
name,
branch,
error
},
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
function byKeyReducer(
state: any = {},
action: Action = { type: "UNKNOWN" }
): Object {
switch (action.type) {
case FETCH_CHANGESETS_SUCCESS:
const key = action.itemId;
let oldChangesets = { [key]: {} };
if (state[key] !== undefined) {
oldChangesets[key] = state[key];
}
return {
...state,
[key]: {
byId: extractChangesetsByIds(action.payload, oldChangesets[key].byId)
}
};
default:
return state;
}
}
function listReducer(
state: any = {},
action: Action = { type: "UNKNOWN" }
): Object {
switch (action.type) {
case FETCH_CHANGESETS_SUCCESS:
const changesets = action.payload._embedded.changesets;
const changesetIds = changesets.map(c => c.id);
return {
entries: changesetIds,
entry: {
page: action.payload.page,
pageTotal: action.payload.pageTotal,
_links: action.payload._links
}
};
default:
return state;
}
}
export default combineReducers({
list: listReducer,
byKey: byKeyReducer
});
function extractChangesetsByIds(data: any, oldChangesetsByIds: any) {
const changesets = data._embedded.changesets;
const changesetsByIds = {};
for (let changeset of changesets) {
changesetsByIds[changeset.id] = changeset;
}
for (let id in oldChangesetsByIds) {
changesetsByIds[id] = oldChangesetsByIds[id];
}
return changesetsByIds;
}
//selectors
export function getChangesets(
state: Object,
namespace: string,
name: string,
branch?: string
) {
const key = createItemId(namespace, name, branch);
if (!state.changesets.byKey[key]) {
return null;
}
return Object.values(state.changesets.byKey[key].byId);
}
export function isFetchChangesetsPending(
state: Object,
namespace: string,
name: string,
branch?: string
) {
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)
);
}
const selectList = (state: Object) => {
if (state.changesets && state.changesets.list) {
return state.changesets.list;
}
return {};
};
const selectListEntry = (state: Object): Object => {
const list = selectList(state);
if (list.entry) {
return list.entry;
}
return {};
};
export const selectListAsCollection = (state: Object): PagedCollection => {
return selectListEntry(state);
};

View File

@@ -0,0 +1,301 @@
// @flow
import configureMockStore from "redux-mock-store";
import thunk from "redux-thunk";
import fetchMock from "fetch-mock";
import {
FETCH_CHANGESETS,
FETCH_CHANGESETS_FAILURE,
FETCH_CHANGESETS_PENDING,
FETCH_CHANGESETS_SUCCESS,
fetchChangesets,
fetchChangesetsByBranchAndPage,
fetchChangesetsByNamespaceNameAndBranch,
fetchChangesetsByPage,
fetchChangesetsSuccess,
getChangesets,
getFetchChangesetsFailure,
isFetchChangesetsPending
} from "./changesets";
import reducer from "./changesets";
const changesets = {};
describe("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]);
afterEach(() => {
fetchMock.reset();
fetchMock.restore();
});
it("should fetch changesets for default branch", () => {
fetchMock.getOnce(DEFAULT_BRANCH_URL, "{}");
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: "foo/bar",
itemId: "foo/bar"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: changesets,
itemId: "foo/bar"
}
];
const store = mockStore({});
return store.dispatch(fetchChangesets("foo", "bar")).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should fetch changesets for specific branch", () => {
const itemId = "foo/bar/specific";
fetchMock.getOnce(SPECIFIC_BRANCH_URL, "{}");
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: itemId,
itemId
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: changesets,
itemId
}
];
const store = mockStore({});
return store
.dispatch(
fetchChangesetsByNamespaceNameAndBranch("foo", "bar", "specific")
)
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should fail fetching changesets on error", () => {
const itemId = "foo/bar";
fetchMock.getOnce(DEFAULT_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: itemId,
itemId
}
];
const store = mockStore({});
return store.dispatch(fetchChangesets("foo", "bar")).then(() => {
expect(store.getActions()[0]).toEqual(expectedActions[0]);
expect(store.getActions()[1].type).toEqual(FETCH_CHANGESETS_FAILURE);
expect(store.getActions()[1].payload).toBeDefined();
});
});
it("should fail fetching changesets for specific branch on error", () => {
const itemId = "foo/bar/specific";
fetchMock.getOnce(SPECIFIC_BRANCH_URL, 500);
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: itemId,
itemId
}
];
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();
});
});
it("should fetch changesets by page", () => {
fetchMock.getOnce(DEFAULT_BRANCH_URL + "?page=5", "{}");
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: "foo/bar",
itemId: "foo/bar"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: changesets,
itemId: "foo/bar"
}
];
const store = mockStore({});
return store.dispatch(fetchChangesetsByPage("foo", "bar", 5)).then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
it("should fetch changesets by branch and page", () => {
fetchMock.getOnce(SPECIFIC_BRANCH_URL + "?page=5", "{}");
const expectedActions = [
{
type: FETCH_CHANGESETS_PENDING,
payload: "foo/bar/specific",
itemId: "foo/bar/specific"
},
{
type: FETCH_CHANGESETS_SUCCESS,
payload: changesets,
itemId: "foo/bar/specific"
}
];
const store = mockStore({});
return store
.dispatch(fetchChangesetsByBranchAndPage("foo", "bar", "specific", 5))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});
});
describe("changesets reducer", () => {
const responseBody = {
page: 1,
pageTotal: 10,
_links: {},
_embedded: {
changesets: [
{ id: "changeset1", author: { mail: "z@phod.com", name: "zaphod" } },
{ id: "changeset2", description: "foo" },
{ id: "changeset3", description: "bar" }
],
_embedded: {
tags: [],
branches: [],
parents: []
}
}
};
it("should set state to received changesets", () => {
const newState = reducer(
{},
fetchChangesetsSuccess(responseBody, "foo", "bar")
);
expect(newState).toBeDefined();
expect(newState.byKey["foo/bar"].byId["changeset1"].author.mail).toEqual(
"z@phod.com"
);
expect(newState.byKey["foo/bar"].byId["changeset2"].description).toEqual(
"foo"
);
expect(newState.byKey["foo/bar"].byId["changeset3"].description).toEqual(
"bar"
);
expect(newState.list).toEqual({
entry: {
page: 1,
pageTotal: 10,
_links: {}
},
entries: ["changeset1", "changeset2", "changeset3"]
});
});
it("should not delete existing changesets from state", () => {
const responseBody = {
_embedded: {
changesets: [
{ id: "changeset1", author: { mail: "z@phod.com", name: "zaphod" } }
],
_embedded: {
tags: [],
branches: [],
parents: []
}
}
};
const newState = reducer(
{
byKey: {
"foo/bar": {
byId: {
["changeset2"]: {
id: "changeset2",
author: { mail: "mail@author.com", name: "author" }
}
}
}
}
},
fetchChangesetsSuccess(responseBody, "foo", "bar")
);
expect(newState.byKey["foo/bar"].byId["changeset2"]).toBeDefined();
expect(newState.byKey["foo/bar"].byId["changeset1"]).toBeDefined();
});
});
describe("changeset selectors", () => {
const error = new Error("Something went wrong");
it("should get all changesets for a given namespace and name", () => {
const state = {
changesets: {
byKey: {
"foo/bar": {
byId: {
id1: { id: "id1" },
id2: { id: "id2" }
}
}
}
}
};
const result = getChangesets(state, "foo", "bar");
expect(result).toContainEqual({ id: "id1" });
});
it("should return true, when fetching changesets is pending", () => {
const state = {
pending: {
[FETCH_CHANGESETS + "/foo/bar"]: true
}
};
expect(isFetchChangesetsPending(state, "foo", "bar")).toBeTruthy();
});
it("should return false, when fetching changesets is not pending", () => {
expect(isFetchChangesetsPending({}, "foo", "bar")).toEqual(false);
});
it("should return error if fetching changesets failed", () => {
const state = {
failure: {
[FETCH_CHANGESETS + "/foo/bar"]: error
}
};
expect(getFetchChangesetsFailure(state, "foo", "bar")).toEqual(error);
});
it("should return false if fetching changesets did not fail", () => {
expect(getFetchChangesetsFailure({}, "foo", "bar")).toBeUndefined();
});
});
});