2018-09-27 16:29:33 +02:00
|
|
|
// @flow
|
2018-10-05 09:55:17 +02:00
|
|
|
import {
|
|
|
|
|
FAILURE_SUFFIX,
|
|
|
|
|
PENDING_SUFFIX,
|
|
|
|
|
SUCCESS_SUFFIX
|
|
|
|
|
} from "../../modules/types";
|
|
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
|
|
|
|
import type { Repository } from "@scm-manager/ui-types";
|
2018-09-12 17:15:16 +02:00
|
|
|
|
|
|
|
|
export const FETCH_BRANCHES = "scm/repos/FETCH_BRANCHES";
|
|
|
|
|
export const FETCH_BRANCHES_PENDING = `${FETCH_BRANCHES}_${PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_BRANCHES_SUCCESS = `${FETCH_BRANCHES}_${SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_BRANCHES_FAILURE = `${FETCH_BRANCHES}_${FAILURE_SUFFIX}`;
|
|
|
|
|
|
|
|
|
|
// Fetching branches
|
2018-10-04 17:12:38 +02:00
|
|
|
|
|
|
|
|
export function fetchBranches(repository: Repository) {
|
2018-09-27 16:29:33 +02:00
|
|
|
return function(dispatch: any) {
|
2018-10-04 17:12:38 +02:00
|
|
|
dispatch(fetchBranchesPending(repository));
|
2018-09-27 16:29:33 +02:00
|
|
|
return apiClient
|
2018-10-04 17:12:38 +02:00
|
|
|
.get(repository._links.branches.href)
|
2018-09-12 17:15:16 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(data => {
|
2018-10-04 17:12:38 +02:00
|
|
|
dispatch(fetchBranchesSuccess(data, repository));
|
2018-09-12 17:15:16 +02:00
|
|
|
})
|
2018-09-27 16:29:33 +02:00
|
|
|
.catch(error => {
|
2018-10-04 17:12:38 +02:00
|
|
|
dispatch(fetchBranchesFailure(repository, error));
|
2018-09-27 16:29:33 +02:00
|
|
|
});
|
|
|
|
|
};
|
2018-09-12 17:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Action creators
|
2018-10-04 17:12:38 +02:00
|
|
|
export function fetchBranchesPending(repository: Repository) {
|
2018-09-12 17:15:16 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_BRANCHES_PENDING,
|
2018-10-04 17:12:38 +02:00
|
|
|
payload: { repository },
|
2018-10-05 09:55:17 +02:00
|
|
|
itemId: createKey(repository)
|
2018-09-27 16:29:33 +02:00
|
|
|
};
|
2018-09-12 17:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 17:12:38 +02:00
|
|
|
export function fetchBranchesSuccess(data: string, repository: Repository) {
|
2018-09-12 17:15:16 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_BRANCHES_SUCCESS,
|
2018-10-04 17:12:38 +02:00
|
|
|
payload: { data, repository },
|
2018-10-05 09:55:17 +02:00
|
|
|
itemId: createKey(repository)
|
2018-09-27 16:29:33 +02:00
|
|
|
};
|
2018-09-12 17:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-04 17:12:38 +02:00
|
|
|
export function fetchBranchesFailure(repository: Repository, error: Error) {
|
2018-09-12 17:15:16 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_BRANCHES_FAILURE,
|
2018-10-04 17:12:38 +02:00
|
|
|
payload: { error, repository },
|
2018-10-05 09:55:17 +02:00
|
|
|
itemId: createKey(repository)
|
2018-09-27 16:29:33 +02:00
|
|
|
};
|
2018-09-12 17:15:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reducers
|
|
|
|
|
|
2018-09-27 16:29:33 +02:00
|
|
|
export default function reducer(
|
|
|
|
|
state: Object = {},
|
|
|
|
|
action: Action = { type: "UNKNOWN" }
|
|
|
|
|
): Object {
|
2018-09-14 16:15:13 +02:00
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_BRANCHES_SUCCESS:
|
2018-10-05 09:55:17 +02:00
|
|
|
const key = createKey(action.payload.repository);
|
2018-09-27 16:29:33 +02:00
|
|
|
let oldBranchesByNames = { [key]: {} };
|
2018-09-14 16:15:13 +02:00
|
|
|
if (state[key] !== undefined) {
|
2018-09-27 16:29:33 +02:00
|
|
|
oldBranchesByNames[key] = state[key];
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
[key]: {
|
2018-09-27 16:29:33 +02:00
|
|
|
byNames: extractBranchesByNames(
|
|
|
|
|
action.payload.data,
|
|
|
|
|
oldBranchesByNames[key].byNames
|
|
|
|
|
)
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
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) {
|
2018-09-27 16:29:33 +02:00
|
|
|
branchesByNames[name] = oldBranchesByNames[name];
|
2018-09-14 16:15:13 +02:00
|
|
|
}
|
|
|
|
|
return branchesByNames;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 17:15:16 +02:00
|
|
|
// Selectors
|
2018-09-14 16:15:13 +02:00
|
|
|
|
2018-10-04 17:12:38 +02:00
|
|
|
export function getBranchNames(state: Object, repository: Repository) {
|
2018-10-05 09:55:17 +02:00
|
|
|
const key = createKey(repository);
|
2018-09-18 15:07:30 +02:00
|
|
|
if (!state.branches[key] || !state.branches[key].byNames) {
|
2018-09-17 14:03:13 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return Object.keys(state.branches[key].byNames);
|
|
|
|
|
}
|
2018-10-04 20:02:18 +02:00
|
|
|
|
|
|
|
|
export function getBranches(state: Object, repository: Repository) {
|
2018-10-05 09:55:17 +02:00
|
|
|
const key = createKey(repository);
|
|
|
|
|
return Object.values(state.branches[key].byNames);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getBranch(state: Object, repository: Repository, name: string) {
|
|
|
|
|
const key = createKey(repository);
|
|
|
|
|
return state.branches[key].byNames[name];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createKey(repository: Repository) {
|
|
|
|
|
const { namespace, name } = repository;
|
|
|
|
|
return `${namespace}/${name}`;
|
2018-10-04 20:02:18 +02:00
|
|
|
}
|