summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repository/log_tree.js
blob: 6498725adb6609739b5c3df2b3e11ca9c5c56d90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { normalizeData } from 'ee_else_ce/repository/utils/commit';
import axios from '~/lib/utils/axios_utils';
import getCommits from './queries/getCommits.query.graphql';
import getProjectPath from './queries/getProjectPath.query.graphql';
import getRef from './queries/getRef.query.graphql';

let fetchpromise;
let resolvers = [];

export function resolveCommit(commits, path, { resolve, entry }) {
  const commit = commits.find(c => c.filePath === `${path}/${entry.name}` && c.type === entry.type);

  if (commit) {
    resolve(commit);
  }
}

export function fetchLogsTree(client, path, offset, resolver = null) {
  if (resolver) {
    resolvers.push(resolver);
  }

  if (fetchpromise) return fetchpromise;

  const { projectPath } = client.readQuery({ query: getProjectPath });
  const { ref } = client.readQuery({ query: getRef });

  fetchpromise = axios
    .get(
      `${gon.relative_url_root}/${projectPath}/refs/${ref}/logs_tree/${path.replace(/^\//, '')}`,
      {
        params: { format: 'json', offset },
      },
    )
    .then(({ data, headers }) => {
      const headerLogsOffset = headers['more-logs-offset'];
      const { commits } = client.readQuery({ query: getCommits });
      const newCommitData = [...commits, ...normalizeData(data, path)];
      client.writeQuery({
        query: getCommits,
        data: { commits: newCommitData },
      });

      resolvers.forEach(r => resolveCommit(newCommitData, path, r));

      fetchpromise = null;

      if (headerLogsOffset) {
        fetchLogsTree(client, path, headerLogsOffset);
      } else {
        resolvers = [];
      }
    });

  return fetchpromise;
}