debian-mirror-gitlab/app/assets/javascripts/repository/log_tree.js

96 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import produce from 'immer';
2019-12-26 22:10:19 +05:30
import { normalizeData } from 'ee_else_ce/repository/utils/commit';
2020-01-01 13:55:28 +05:30
import axios from '~/lib/utils/axios_utils';
2020-10-24 23:57:45 +05:30
import commitsQuery from './queries/commits.query.graphql';
import projectPathQuery from './queries/project_path.query.graphql';
import refQuery from './queries/ref.query.graphql';
2019-09-30 21:07:59 +05:30
2021-01-03 14:25:43 +05:30
const fetchpromises = {};
const resolvers = {};
2021-09-04 01:27:46 +05:30
let maxOffset;
let nextOffset;
let currentPath;
function setNextOffset(offset) {
nextOffset = offset || null;
}
2019-09-30 21:07:59 +05:30
2020-01-01 13:55:28 +05:30
export function resolveCommit(commits, path, { resolve, entry }) {
2021-03-08 18:12:59 +05:30
const commit = commits.find(
(c) => c.filePath === `${path}/${entry.name}` && c.type === entry.type,
);
2019-09-30 21:07:59 +05:30
if (commit) {
resolve(commit);
}
}
2021-09-04 01:27:46 +05:30
export function fetchLogsTree(client, path, offset, resolver = null, _maxOffset = null) {
if (_maxOffset) {
maxOffset = _maxOffset;
}
if (!currentPath || currentPath !== path) {
// ensures the nextOffset is reset if the user changed directories
setNextOffset(null);
}
currentPath = path;
const offsetNumber = Number(offset);
if (!nextOffset && offsetNumber > maxOffset) {
setNextOffset(offsetNumber - 25); // ensures commit data is fetched for newly added rows that need data from the previous request (requests are made in batches of 25).
return Promise.resolve();
}
2019-09-30 21:07:59 +05:30
if (resolver) {
2021-01-03 14:25:43 +05:30
if (!resolvers[path]) {
resolvers[path] = [resolver];
} else {
resolvers[path].push(resolver);
}
2019-09-30 21:07:59 +05:30
}
2021-01-03 14:25:43 +05:30
if (fetchpromises[path]) return fetchpromises[path];
2019-09-30 21:07:59 +05:30
2020-10-24 23:57:45 +05:30
const { projectPath } = client.readQuery({ query: projectPathQuery });
const { escapedRef } = client.readQuery({ query: refQuery });
2019-09-30 21:07:59 +05:30
2021-01-03 14:25:43 +05:30
fetchpromises[path] = axios
2019-12-26 22:10:19 +05:30
.get(
2020-05-05 14:28:15 +05:30
`${gon.relative_url_root}/${projectPath}/-/refs/${escapedRef}/logs_tree/${encodeURIComponent(
path.replace(/^\//, ''),
)}`,
2019-12-26 22:10:19 +05:30
{
2021-09-04 01:27:46 +05:30
params: { format: 'json', offset: nextOffset || offset },
2019-12-26 22:10:19 +05:30
},
)
2020-11-24 15:15:51 +05:30
.then(({ data: newData, headers }) => {
2019-09-30 21:07:59 +05:30
const headerLogsOffset = headers['more-logs-offset'];
2020-11-24 15:15:51 +05:30
const sourceData = client.readQuery({ query: commitsQuery });
2021-03-08 18:12:59 +05:30
const data = produce(sourceData, (draftState) => {
2020-11-24 15:15:51 +05:30
draftState.commits.push(...normalizeData(newData, path));
});
2019-09-30 21:07:59 +05:30
client.writeQuery({
2020-10-24 23:57:45 +05:30
query: commitsQuery,
2020-11-24 15:15:51 +05:30
data,
2019-09-30 21:07:59 +05:30
});
2021-03-08 18:12:59 +05:30
resolvers[path].forEach((r) => resolveCommit(data.commits, path, r));
2019-09-30 21:07:59 +05:30
2021-01-03 14:25:43 +05:30
delete fetchpromises[path];
2019-09-30 21:07:59 +05:30
if (headerLogsOffset) {
2021-09-04 01:27:46 +05:30
setNextOffset(null);
2019-09-30 21:07:59 +05:30
fetchLogsTree(client, path, headerLogsOffset);
} else {
2021-01-03 14:25:43 +05:30
delete resolvers[path];
2021-09-04 01:27:46 +05:30
maxOffset = null;
setNextOffset(null);
2019-09-30 21:07:59 +05:30
}
});
2021-01-03 14:25:43 +05:30
return fetchpromises[path];
2019-09-30 21:07:59 +05:30
}