summaryrefslogtreecommitdiff
path: root/spec/frontend_integration/test_helpers/mock_server/routes/repository.js
blob: ba36463cad8156ded136e9168b9a7921eb05b0be (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
57
58
59
60
import { createNewCommit, createCommitIdGenerator } from 'test_helpers/factories';

export default (server) => {
  const commitIdGenerator = createCommitIdGenerator();

  server.get('/api/v4/projects/:id/repository/branches', (schema) => {
    return schema.db.branches;
  });

  server.get('/api/v4/projects/:id/repository/branches/:name', (schema, request) => {
    const { name } = request.params;

    const branch = schema.branches.findBy({ name });

    return branch.attrs;
  });

  server.get('*/-/files/:id', (schema) => {
    return schema.db.files.map(({ path }) => path);
  });

  server.get('/:namespace/:project/-/blob/:sha/*path', (schema, request) => {
    const { path } = schema.db.files.findBy({ path: request.params.path });

    return { path, rawPath: request.url.replace('/-/blob', '/-/raw') };
  });

  server.get('/:namespace/:project/-/raw/:sha/*path', (schema, request) => {
    const { path } = request.params;

    return schema.db.filesRaw.findBy({ path })?.raw || 'Sample content';
  });

  server.post('/api/v4/projects/:id/repository/commits', (schema, request) => {
    const { branch: branchName, commit_message: message, actions } = JSON.parse(
      request.requestBody,
    );

    const branch = schema.branches.findBy({ name: branchName });
    const prevCommit = branch
      ? branch.attrs.commit
      : schema.branches.findBy({ name: 'master' }).attrs.commit;

    const commit = {
      ...createNewCommit({ id: commitIdGenerator.next(), message }, prevCommit),
      __actions: actions,
    };

    if (branch) {
      branch.update({ commit });
    } else {
      schema.branches.create({
        name: branchName,
        commit,
      });
    }

    return commit;
  });
};