BigW Consortium Gitlab

api.js 5.48 KB
Newer Older
1
import _ from 'underscore';
2
import axios from './lib/utils/axios_utils';
3 4 5

const Api = {
  groupsPath: '/api/:version/groups.json',
6
  groupPath: '/api/:version/groups/:id',
7 8
  namespacesPath: '/api/:version/namespaces.json',
  groupProjectsPath: '/api/:version/groups/:id/projects.json',
9
  projectsPath: '/api/:version/projects.json',
10
  projectPath: '/api/:version/projects/:id',
11 12
  projectLabelsPath: '/:namespace_path/:project_path/labels',
  groupLabelsPath: '/groups/:namespace_path/labels',
13 14 15 16 17 18
  licensePath: '/api/:version/templates/licenses/:key',
  gitignorePath: '/api/:version/templates/gitignores/:key',
  gitlabCiYmlPath: '/api/:version/templates/gitlab_ci_ymls/:key',
  dockerfilePath: '/api/:version/templates/dockerfiles/:key',
  issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key',
  usersPath: '/api/:version/users.json',
Jacob Schatz committed
19
  commitPath: '/api/:version/projects/:id/repository/commits',
20
  branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
21
  createBranchPath: '/api/:version/projects/:id/repository/branches',
22 23 24 25

  group(groupId, callback) {
    const url = Api.buildUrl(Api.groupPath)
      .replace(':id', groupId);
26
    return axios.get(url)
27 28 29 30 31
      .then(({ data }) => {
        callback(data);

        return data;
      });
32
  },
33

34
  // Return groups list. Filtered by query
35 36
  groups(query, options, callback) {
    const url = Api.buildUrl(Api.groupsPath);
37 38
    return axios.get(url, {
      params: Object.assign({
39
        search: query,
40
        per_page: 20,
41
      }, options),
42
    })
Phil Hughes committed
43 44 45 46 47
      .then(({ data }) => {
        callback(data);

        return data;
      });
48
  },
49

50
  // Return namespaces list. Filtered by query
51 52
  namespaces(query, callback) {
    const url = Api.buildUrl(Api.namespacesPath);
53 54
    return axios.get(url, {
      params: {
55
        search: query,
56
        per_page: 20,
57
      },
58 59
    })
      .then(({ data }) => callback(data));
60
  },
61

62
  // Return projects list. Filtered by query
63
  projects(query, options, callback = _.noop) {
64
    const url = Api.buildUrl(Api.projectsPath);
65 66 67
    const defaults = {
      search: query,
      per_page: 20,
68
      simple: true,
69 70 71 72 73 74
    };

    if (gon.current_user_id) {
      defaults.membership = true;
    }

75 76
    return axios.get(url, {
      params: Object.assign(defaults, options),
77
    })
Phil Hughes committed
78 79 80 81 82
      .then(({ data }) => {
        callback(data);

        return data;
      });
83
  },
84

85 86 87 88 89 90 91 92
  // Return single project
  project(projectPath) {
    const url = Api.buildUrl(Api.projectPath)
            .replace(':id', encodeURIComponent(projectPath));

    return axios.get(url);
  },

93
  newLabel(namespacePath, projectPath, data, callback) {
94 95 96 97 98 99 100 101 102 103
    let url;

    if (projectPath) {
      url = Api.buildUrl(Api.projectLabelsPath)
        .replace(':namespace_path', namespacePath)
        .replace(':project_path', projectPath);
    } else {
      url = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespacePath);
    }

104 105
    return axios.post(url, {
      label: data,
106
    })
107 108
      .then(res => callback(res.data))
      .catch(e => callback(e.response.data));
109
  },
110

111
  // Return group projects list. Filtered by query
112 113 114
  groupProjects(groupId, query, callback) {
    const url = Api.buildUrl(Api.groupProjectsPath)
      .replace(':id', groupId);
115 116
    return axios.get(url, {
      params: {
117
        search: query,
118
        per_page: 20,
119
      },
120
    })
121
      .then(({ data }) => callback(data));
122
  },
123

124 125
  commitMultiple(id, data) {
    // see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
Jacob Schatz committed
126
    const url = Api.buildUrl(Api.commitPath)
127
      .replace(':id', encodeURIComponent(id));
128 129 130 131
    return axios.post(url, JSON.stringify(data), {
      headers: {
        'Content-Type': 'application/json; charset=utf-8',
      },
132
    });
133 134 135 136
  },

  branchSingle(id, branch) {
    const url = Api.buildUrl(Api.branchSinglePath)
137
      .replace(':id', encodeURIComponent(id))
138 139
      .replace(':branch', branch);

140
    return axios.get(url);
Jacob Schatz committed
141 142
  },

143
  // Return text for a specific license
144 145
  licenseText(key, data, callback) {
    const url = Api.buildUrl(Api.licensePath)
146
      .replace(':key', key);
147 148
    return axios.get(url, {
      params: data,
149
    })
150
      .then(res => callback(res.data));
151
  },
152 153 154

  gitignoreText(key, callback) {
    const url = Api.buildUrl(Api.gitignorePath)
155
      .replace(':key', key);
156 157
    return axios.get(url)
      .then(({ data }) => callback(data));
158
  },
159 160 161

  gitlabCiYml(key, callback) {
    const url = Api.buildUrl(Api.gitlabCiYmlPath)
162
      .replace(':key', key);
163 164
    return axios.get(url)
      .then(({ data }) => callback(data));
165
  },
166 167 168

  dockerfileYml(key, callback) {
    const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
169 170
    return axios.get(url)
      .then(({ data }) => callback(data));
171
  },
172 173 174

  issueTemplate(namespacePath, projectPath, key, type, callback) {
    const url = Api.buildUrl(Api.issuableTemplatePath)
175
      .replace(':key', encodeURIComponent(key))
176 177 178
      .replace(':type', type)
      .replace(':project_path', projectPath)
      .replace(':namespace_path', namespacePath);
179 180 181
    return axios.get(url)
      .then(({ data }) => callback(null, data))
      .catch(callback);
182
  },
183 184 185

  users(query, options) {
    const url = Api.buildUrl(this.usersPath);
186 187
    return axios.get(url, {
      params: Object.assign({
188 189 190 191 192 193 194 195
        search: query,
        per_page: 20,
      }, options),
    });
  },

  buildUrl(url) {
    let urlRoot = '';
196
    if (gon.relative_url_root != null) {
197
      urlRoot = gon.relative_url_root;
Fatih Acet committed
198
    }
199 200
    return urlRoot + url.replace(':version', gon.api_version);
  },
201
};
Fatih Acet committed
202

203
export default Api;