2021-11-18 22:05:49 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import { joinPaths } from '~/lib/utils/url_utility';
|
|
|
|
import { normalizeData } from 'ee_else_ce/repository/utils/commit';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2021-11-18 22:05:49 +05:30
|
|
|
import { COMMIT_BATCH_SIZE, I18N_COMMIT_DATA_FETCH_ERROR } from './constants';
|
|
|
|
|
|
|
|
let requestedOffsets = [];
|
|
|
|
let fetchedBatches = [];
|
|
|
|
|
|
|
|
export const isRequested = (offset) => requestedOffsets.includes(offset);
|
|
|
|
|
|
|
|
export const resetRequestedCommits = () => {
|
|
|
|
requestedOffsets = [];
|
|
|
|
fetchedBatches = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const addRequestedOffset = (offset) => {
|
|
|
|
if (isRequested(offset) || offset < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
requestedOffsets.push(offset);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeLeadingSlash = (path) => path.replace(/^\//, '');
|
|
|
|
|
|
|
|
const fetchData = (projectPath, path, ref, offset) => {
|
|
|
|
if (fetchedBatches.includes(offset) || offset < 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchedBatches.push(offset);
|
|
|
|
|
|
|
|
const url = joinPaths(
|
|
|
|
gon.relative_url_root || '/',
|
|
|
|
projectPath,
|
|
|
|
'/-/refs/',
|
2023-03-17 16:20:25 +05:30
|
|
|
encodeURIComponent(ref),
|
2021-11-18 22:05:49 +05:30
|
|
|
'/logs_tree/',
|
|
|
|
encodeURIComponent(removeLeadingSlash(path)),
|
|
|
|
);
|
|
|
|
|
|
|
|
return axios
|
|
|
|
.get(url, { params: { format: 'json', offset } })
|
|
|
|
.then(({ data }) => normalizeData(data, path))
|
2022-11-25 23:54:43 +05:30
|
|
|
.catch(() => createAlert({ message: I18N_COMMIT_DATA_FETCH_ERROR }));
|
2021-11-18 22:05:49 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
export const loadCommits = async (projectPath, path, ref, offset) => {
|
|
|
|
if (isRequested(offset)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// We fetch in batches of 25, so this ensures we don't refetch
|
2022-01-26 12:08:38 +05:30
|
|
|
Array.from(Array(COMMIT_BATCH_SIZE)).forEach((_, i) => addRequestedOffset(offset + i));
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
const commits = await fetchData(projectPath, path, ref, offset);
|
2021-11-18 22:05:49 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
return commits;
|
2021-11-18 22:05:49 +05:30
|
|
|
};
|