2018-07-31 16:32:16 +02:00
|
|
|
// @flow
|
|
|
|
|
import { apiClient } from "../../apiclient";
|
|
|
|
|
import * as types from "../../modules/types";
|
|
|
|
|
import type { Action } from "../../types/Action";
|
2018-08-03 08:52:02 +02:00
|
|
|
import type { Repository, RepositoryCollection } from "../types/Repositories";
|
|
|
|
|
import { isPending } from "../../modules/pending";
|
|
|
|
|
import { getFailure } from "../../modules/failure";
|
2018-07-31 16:32:16 +02:00
|
|
|
|
|
|
|
|
export const FETCH_REPOS = "scm/repos/FETCH_REPOS";
|
|
|
|
|
export const FETCH_REPOS_PENDING = `${FETCH_REPOS}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_REPOS_SUCCESS = `${FETCH_REPOS}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_REPOS_FAILURE = `${FETCH_REPOS}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2018-08-01 18:23:16 +02:00
|
|
|
export const FETCH_REPO = "scm/repos/FETCH_REPO";
|
|
|
|
|
export const FETCH_REPO_PENDING = `${FETCH_REPO}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_REPO_SUCCESS = `${FETCH_REPO}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_REPO_FAILURE = `${FETCH_REPO}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2018-08-03 09:54:04 +02:00
|
|
|
export const CREATE_REPO = "scm/repos/CREATE_REPO";
|
2018-08-03 08:52:02 +02:00
|
|
|
export const CREATE_REPO_PENDING = `${CREATE_REPO}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const CREATE_REPO_SUCCESS = `${CREATE_REPO}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const CREATE_REPO_FAILURE = `${CREATE_REPO}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
export const CREATE_REPO_RESET = `${CREATE_REPO}_${types.RESET_SUFFIX}`;
|
|
|
|
|
|
2018-08-03 09:54:04 +02:00
|
|
|
export const DELETE_REPO = "scm/repos/DELETE_REPO";
|
|
|
|
|
export const DELETE_REPO_PENDING = `${DELETE_REPO}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const DELETE_REPO_SUCCESS = `${DELETE_REPO}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const DELETE_REPO_FAILURE = `${DELETE_REPO}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
const REPOS_URL = "repositories";
|
2018-08-01 18:23:16 +02:00
|
|
|
|
2018-08-03 08:52:02 +02:00
|
|
|
const CONTENT_TYPE = "application/vnd.scmm-repository+json;v=2";
|
|
|
|
|
|
2018-08-01 18:23:16 +02:00
|
|
|
// fetch repos
|
|
|
|
|
|
2018-08-01 09:44:34 +02:00
|
|
|
const SORT_BY = "sortBy=namespaceAndName";
|
2018-07-31 16:32:16 +02:00
|
|
|
|
|
|
|
|
export function fetchRepos() {
|
2018-08-01 14:56:24 +02:00
|
|
|
return fetchReposByLink(REPOS_URL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchReposByPage(page: number) {
|
|
|
|
|
return fetchReposByLink(`${REPOS_URL}?page=${page - 1}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appendSortByLink(url: string) {
|
|
|
|
|
if (url.includes(SORT_BY)) {
|
|
|
|
|
return url;
|
|
|
|
|
}
|
|
|
|
|
let urlWithSortBy = url;
|
|
|
|
|
if (url.includes("?")) {
|
|
|
|
|
urlWithSortBy += "&";
|
|
|
|
|
} else {
|
|
|
|
|
urlWithSortBy += "?";
|
|
|
|
|
}
|
|
|
|
|
return urlWithSortBy + SORT_BY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchReposByLink(link: string) {
|
|
|
|
|
const url = appendSortByLink(link);
|
2018-07-31 16:32:16 +02:00
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchReposPending());
|
|
|
|
|
return apiClient
|
2018-08-01 14:56:24 +02:00
|
|
|
.get(url)
|
2018-07-31 16:32:16 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(repositories => {
|
|
|
|
|
dispatch(fetchReposSuccess(repositories));
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(fetchReposFailure(err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchReposPending(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_REPOS_PENDING
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchReposSuccess(repositories: RepositoryCollection): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_REPOS_SUCCESS,
|
|
|
|
|
payload: repositories
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchReposFailure(err: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_REPOS_FAILURE,
|
|
|
|
|
payload: err
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 18:23:16 +02:00
|
|
|
// fetch repo
|
|
|
|
|
|
|
|
|
|
export function fetchRepo(namespace: string, name: string) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(fetchRepoPending(namespace, name));
|
2018-08-03 08:52:02 +02:00
|
|
|
return apiClient
|
|
|
|
|
.get(`${REPOS_URL}/${namespace}/${name}`)
|
2018-08-01 18:23:16 +02:00
|
|
|
.then(response => response.json())
|
2018-08-03 08:52:02 +02:00
|
|
|
.then(repository => {
|
|
|
|
|
dispatch(fetchRepoSuccess(repository));
|
|
|
|
|
})
|
2018-08-01 18:23:16 +02:00
|
|
|
.catch(err => {
|
2018-08-03 08:52:02 +02:00
|
|
|
dispatch(fetchRepoFailure(namespace, name, err));
|
2018-08-01 18:23:16 +02:00
|
|
|
});
|
2018-08-03 08:52:02 +02:00
|
|
|
};
|
2018-08-01 18:23:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchRepoPending(namespace: string, name: string): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_REPO_PENDING,
|
|
|
|
|
payload: {
|
|
|
|
|
namespace,
|
|
|
|
|
name
|
|
|
|
|
},
|
|
|
|
|
itemId: namespace + "/" + name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchRepoSuccess(repository: Repository): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_REPO_SUCCESS,
|
|
|
|
|
payload: repository,
|
|
|
|
|
itemId: createIdentifier(repository)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:52:02 +02:00
|
|
|
export function fetchRepoFailure(
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string,
|
|
|
|
|
error: Error
|
|
|
|
|
): Action {
|
2018-08-01 18:23:16 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_REPO_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
namespace,
|
|
|
|
|
name,
|
|
|
|
|
error
|
|
|
|
|
},
|
|
|
|
|
itemId: namespace + "/" + name
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:52:02 +02:00
|
|
|
// create repo
|
|
|
|
|
|
|
|
|
|
export function createRepo(repository: Repository, callback?: () => void) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(createRepoPending());
|
|
|
|
|
return apiClient
|
|
|
|
|
.post(REPOS_URL, repository, CONTENT_TYPE)
|
|
|
|
|
.then(() => {
|
|
|
|
|
dispatch(createRepoSuccess());
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(createRepoFailure(err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createRepoPending(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: CREATE_REPO_PENDING
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createRepoSuccess(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: CREATE_REPO_SUCCESS
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createRepoFailure(err: Error): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: CREATE_REPO_FAILURE,
|
|
|
|
|
payload: err
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createRepoReset(): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: CREATE_REPO_RESET
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 09:54:04 +02:00
|
|
|
// delete
|
|
|
|
|
|
|
|
|
|
export function deleteRepo(repository: Repository, callback?: () => void) {
|
|
|
|
|
return function(dispatch: any) {
|
|
|
|
|
dispatch(deleteRepoPending(repository));
|
|
|
|
|
return apiClient
|
|
|
|
|
.delete(repository._links.delete.href)
|
|
|
|
|
.then(() => {
|
|
|
|
|
dispatch(deleteRepoSuccess(repository));
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
dispatch(deleteRepoFailure(repository, err));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteRepoPending(repository: Repository): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: DELETE_REPO_PENDING,
|
|
|
|
|
payload: repository,
|
|
|
|
|
itemId: createIdentifier(repository)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteRepoSuccess(repository: Repository): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: DELETE_REPO_SUCCESS,
|
|
|
|
|
payload: repository,
|
|
|
|
|
itemId: createIdentifier(repository)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function deleteRepoFailure(
|
|
|
|
|
repository: Repository,
|
|
|
|
|
error: Error
|
|
|
|
|
): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: DELETE_REPO_FAILURE,
|
|
|
|
|
payload: {
|
|
|
|
|
error,
|
|
|
|
|
repository
|
|
|
|
|
},
|
|
|
|
|
itemId: createIdentifier(repository)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
// reducer
|
|
|
|
|
|
2018-08-01 18:23:16 +02:00
|
|
|
function createIdentifier(repository: Repository) {
|
|
|
|
|
return repository.namespace + "/" + repository.name;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
function normalizeByNamespaceAndName(
|
|
|
|
|
repositoryCollection: RepositoryCollection
|
|
|
|
|
) {
|
|
|
|
|
const names = [];
|
|
|
|
|
const byNames = {};
|
|
|
|
|
for (const repository of repositoryCollection._embedded.repositories) {
|
2018-08-01 18:23:16 +02:00
|
|
|
const identifier = createIdentifier(repository);
|
2018-07-31 16:32:16 +02:00
|
|
|
names.push(identifier);
|
|
|
|
|
byNames[identifier] = repository;
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
list: {
|
|
|
|
|
...repositoryCollection,
|
|
|
|
|
_embedded: {
|
|
|
|
|
repositories: names
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
byNames: byNames
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 18:23:16 +02:00
|
|
|
const reducerByNames = (state: Object, repository: Repository) => {
|
|
|
|
|
const identifier = createIdentifier(repository);
|
|
|
|
|
const newState = {
|
|
|
|
|
...state,
|
|
|
|
|
byNames: {
|
|
|
|
|
...state.byNames,
|
|
|
|
|
[identifier]: repository
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return newState;
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-31 16:32:16 +02:00
|
|
|
export default function reducer(
|
|
|
|
|
state: Object = {},
|
|
|
|
|
action: Action = { type: "UNKNOWN" }
|
|
|
|
|
): Object {
|
2018-08-01 18:23:16 +02:00
|
|
|
if (!action.payload) {
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
case FETCH_REPOS_SUCCESS:
|
2018-08-01 14:56:24 +02:00
|
|
|
return normalizeByNamespaceAndName(action.payload);
|
2018-08-01 18:23:16 +02:00
|
|
|
case FETCH_REPO_SUCCESS:
|
|
|
|
|
return reducerByNames(state, action.payload);
|
|
|
|
|
default:
|
2018-08-03 08:52:02 +02:00
|
|
|
return state;
|
2018-07-31 16:32:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// selectors
|
|
|
|
|
|
|
|
|
|
export function getRepositoryCollection(state: Object) {
|
|
|
|
|
if (state.repos && state.repos.list && state.repos.byNames) {
|
|
|
|
|
const repositories = [];
|
|
|
|
|
for (let repositoryName of state.repos.list._embedded.repositories) {
|
|
|
|
|
repositories.push(state.repos.byNames[repositoryName]);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
...state.repos.list,
|
|
|
|
|
_embedded: {
|
|
|
|
|
repositories
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-01 10:00:53 +02:00
|
|
|
|
|
|
|
|
export function isFetchReposPending(state: Object) {
|
|
|
|
|
return isPending(state, FETCH_REPOS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFetchReposFailure(state: Object) {
|
|
|
|
|
return getFailure(state, FETCH_REPOS);
|
|
|
|
|
}
|
2018-08-01 18:23:16 +02:00
|
|
|
|
|
|
|
|
export function getRepository(state: Object, namespace: string, name: string) {
|
|
|
|
|
if (state.repos && state.repos.byNames) {
|
2018-08-03 08:52:02 +02:00
|
|
|
return state.repos.byNames[namespace + "/" + name];
|
2018-08-01 18:23:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:52:02 +02:00
|
|
|
export function isFetchRepoPending(
|
|
|
|
|
state: Object,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) {
|
2018-08-01 18:23:16 +02:00
|
|
|
return isPending(state, FETCH_REPO, namespace + "/" + name);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:52:02 +02:00
|
|
|
export function getFetchRepoFailure(
|
|
|
|
|
state: Object,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) {
|
2018-08-01 18:23:16 +02:00
|
|
|
return getFailure(state, FETCH_REPO, namespace + "/" + name);
|
|
|
|
|
}
|
2018-08-03 08:52:02 +02:00
|
|
|
|
|
|
|
|
export function isAbleToCreateRepos(state: Object) {
|
2018-08-03 09:54:04 +02:00
|
|
|
return !!(
|
2018-08-03 08:52:02 +02:00
|
|
|
state.repos &&
|
|
|
|
|
state.repos.list &&
|
|
|
|
|
state.repos.list._links &&
|
|
|
|
|
state.repos.list._links.create
|
2018-08-03 09:54:04 +02:00
|
|
|
);
|
2018-08-03 08:52:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isCreateRepoPending(state: Object) {
|
|
|
|
|
return isPending(state, CREATE_REPO);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getCreateRepoFailure(state: Object) {
|
|
|
|
|
return getFailure(state, CREATE_REPO);
|
|
|
|
|
}
|
2018-08-03 09:54:04 +02:00
|
|
|
|
|
|
|
|
export function isDeleteRepoPending(
|
|
|
|
|
state: Object,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) {
|
|
|
|
|
return isPending(state, DELETE_REPO, namespace + "/" + name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getDeleteRepoFailure(
|
|
|
|
|
state: Object,
|
|
|
|
|
namespace: string,
|
|
|
|
|
name: string
|
|
|
|
|
) {
|
|
|
|
|
return getFailure(state, DELETE_REPO, namespace + "/" + name);
|
|
|
|
|
}
|