2019-10-20 18:02:52 +02:00
|
|
|
import * as types from "../../../modules/types";
|
|
|
|
|
import { Repository, File, Action } from "@scm-manager/ui-types";
|
|
|
|
|
import { apiClient } from "@scm-manager/ui-components";
|
|
|
|
|
import { isPending } from "../../../modules/pending";
|
|
|
|
|
import { getFailure } from "../../../modules/failure";
|
2018-09-27 16:32:37 +02:00
|
|
|
|
2019-10-20 18:02:52 +02:00
|
|
|
export const FETCH_SOURCES = "scm/repos/FETCH_SOURCES";
|
2018-09-27 16:32:37 +02:00
|
|
|
export const FETCH_SOURCES_PENDING = `${FETCH_SOURCES}_${types.PENDING_SUFFIX}`;
|
|
|
|
|
export const FETCH_SOURCES_SUCCESS = `${FETCH_SOURCES}_${types.SUCCESS_SUFFIX}`;
|
|
|
|
|
export const FETCH_SOURCES_FAILURE = `${FETCH_SOURCES}_${types.FAILURE_SUFFIX}`;
|
|
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
export function fetchSources(
|
|
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2018-09-28 11:31:38 +02:00
|
|
|
) {
|
2018-09-27 16:32:37 +02:00
|
|
|
return function(dispatch: any) {
|
2018-09-28 11:31:38 +02:00
|
|
|
dispatch(fetchSourcesPending(repository, revision, path));
|
2018-09-27 16:32:37 +02:00
|
|
|
return apiClient
|
2018-09-28 11:31:38 +02:00
|
|
|
.get(createUrl(repository, revision, path))
|
2018-09-27 16:32:37 +02:00
|
|
|
.then(response => response.json())
|
|
|
|
|
.then(sources => {
|
2018-09-28 11:31:38 +02:00
|
|
|
dispatch(fetchSourcesSuccess(repository, revision, path, sources));
|
2018-09-27 16:32:37 +02:00
|
|
|
})
|
|
|
|
|
.catch(err => {
|
2018-12-12 09:40:37 +01:00
|
|
|
dispatch(fetchSourcesFailure(repository, revision, path, err));
|
2018-09-27 16:32:37 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
function createUrl(repository: Repository, revision: string, path: string) {
|
|
|
|
|
const base = repository._links.sources.href;
|
|
|
|
|
if (!revision && !path) {
|
|
|
|
|
return base;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO handle trailing slash
|
2019-10-20 18:02:52 +02:00
|
|
|
const pathDefined = path ? path : "";
|
2018-10-23 13:03:15 +02:00
|
|
|
return `${base}${encodeURIComponent(revision)}/${pathDefined}`;
|
2018-09-28 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchSourcesPending(
|
|
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2018-09-28 11:31:38 +02:00
|
|
|
): Action {
|
2018-09-27 16:32:37 +02:00
|
|
|
return {
|
|
|
|
|
type: FETCH_SOURCES_PENDING,
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: createItemId(repository, revision, path)
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchSourcesSuccess(
|
|
|
|
|
repository: Repository,
|
2018-09-28 11:31:38 +02:00
|
|
|
revision: string,
|
|
|
|
|
path: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
sources: File
|
2018-09-27 16:32:37 +02:00
|
|
|
) {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_SOURCES_SUCCESS,
|
|
|
|
|
payload: sources,
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: createItemId(repository, revision, path)
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function fetchSourcesFailure(
|
|
|
|
|
repository: Repository,
|
2018-09-28 11:31:38 +02:00
|
|
|
revision: string,
|
|
|
|
|
path: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
error: Error
|
2018-09-27 16:32:37 +02:00
|
|
|
): Action {
|
|
|
|
|
return {
|
|
|
|
|
type: FETCH_SOURCES_FAILURE,
|
|
|
|
|
payload: error,
|
2019-10-20 18:02:52 +02:00
|
|
|
itemId: createItemId(repository, revision, path)
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 11:31:38 +02:00
|
|
|
function createItemId(repository: Repository, revision: string, path: string) {
|
2019-10-20 18:02:52 +02:00
|
|
|
const revPart = revision ? revision : "_";
|
|
|
|
|
const pathPart = path ? path : "";
|
2018-09-28 11:31:38 +02:00
|
|
|
return `${repository.namespace}/${repository.name}/${revPart}/${pathPart}`;
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// reducer
|
|
|
|
|
|
|
|
|
|
export default function reducer(
|
|
|
|
|
state: any = {},
|
2019-10-19 16:38:07 +02:00
|
|
|
action: Action = {
|
2019-10-20 18:02:52 +02:00
|
|
|
type: "UNKNOWN"
|
|
|
|
|
}
|
2018-09-27 16:32:37 +02:00
|
|
|
): any {
|
2018-11-07 16:42:26 +01:00
|
|
|
if (action.itemId && action.type === FETCH_SOURCES_SUCCESS) {
|
2018-09-27 16:32:37 +02:00
|
|
|
return {
|
2018-12-21 13:39:24 +01:00
|
|
|
...state,
|
2019-10-20 18:02:52 +02:00
|
|
|
[action.itemId]: action.payload
|
2018-09-27 16:32:37 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// selectors
|
|
|
|
|
|
2018-10-25 11:27:31 +02:00
|
|
|
export function isDirectory(
|
|
|
|
|
state: any,
|
|
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2018-10-25 11:27:31 +02:00
|
|
|
): boolean {
|
|
|
|
|
const currentFile = getSources(state, repository, revision, path);
|
|
|
|
|
if (currentFile && !currentFile.directory) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true; //also return true if no currentFile is found since it is the "default" path
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 16:32:37 +02:00
|
|
|
export function getSources(
|
|
|
|
|
state: any,
|
2018-09-28 11:31:38 +02:00
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2019-10-19 16:38:07 +02:00
|
|
|
): File | null | undefined {
|
2018-09-27 16:32:37 +02:00
|
|
|
if (state.sources) {
|
2018-09-28 11:31:38 +02:00
|
|
|
return state.sources[createItemId(repository, revision, path)];
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function isFetchSourcesPending(
|
|
|
|
|
state: any,
|
2018-09-28 11:31:38 +02:00
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2018-09-27 16:32:37 +02:00
|
|
|
): boolean {
|
2018-09-28 11:31:38 +02:00
|
|
|
return isPending(
|
|
|
|
|
state,
|
|
|
|
|
FETCH_SOURCES,
|
2019-10-20 18:02:52 +02:00
|
|
|
createItemId(repository, revision, path)
|
2018-09-28 11:31:38 +02:00
|
|
|
);
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getFetchSourcesFailure(
|
|
|
|
|
state: any,
|
2018-09-28 11:31:38 +02:00
|
|
|
repository: Repository,
|
|
|
|
|
revision: string,
|
2019-10-20 18:02:52 +02:00
|
|
|
path: string
|
2019-10-19 16:38:07 +02:00
|
|
|
): Error | null | undefined {
|
2018-09-28 11:31:38 +02:00
|
|
|
return getFailure(
|
|
|
|
|
state,
|
|
|
|
|
FETCH_SOURCES,
|
2019-10-20 18:02:52 +02:00
|
|
|
createItemId(repository, revision, path)
|
2018-09-28 11:31:38 +02:00
|
|
|
);
|
2018-09-27 16:32:37 +02:00
|
|
|
}
|