BigW Consortium Gitlab

api.js 5.79 KB
Newer Older
1 2 3 4 5 6 7
import $ from 'jquery';

const Api = {
  groupsPath: '/api/:version/groups.json',
  groupPath: '/api/:version/groups/:id.json',
  namespacesPath: '/api/:version/namespaces.json',
  groupProjectsPath: '/api/:version/groups/:id/projects.json',
8
  projectsPath: '/api/:version/projects.json',
9 10
  projectLabelsPath: '/:namespace_path/:project_path/labels',
  groupLabelsPath: '/groups/:namespace_path/labels',
11 12 13 14 15 16
  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
17
  commitPath: '/api/:version/projects/:id/repository/commits',
18
  branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
19
  createBranchPath: '/api/:version/projects/:id/repository/branches',
20 21 22 23

  group(groupId, callback) {
    const url = Api.buildUrl(Api.groupPath)
      .replace(':id', groupId);
24
    return $.ajax({
25 26 27 28
      url,
      dataType: 'json',
    })
      .done(group => callback(group));
29
  },
30

31
  // Return groups list. Filtered by query
32 33
  groups(query, options, callback) {
    const url = Api.buildUrl(Api.groupsPath);
34
    return $.ajax({
35 36
      url,
      data: Object.assign({
37
        search: query,
38
        per_page: 20,
39
      }, options),
40 41 42
      dataType: 'json',
    })
      .done(groups => callback(groups));
43
  },
44

45
  // Return namespaces list. Filtered by query
46 47
  namespaces(query, callback) {
    const url = Api.buildUrl(Api.namespacesPath);
48
    return $.ajax({
49
      url,
50 51
      data: {
        search: query,
52
        per_page: 20,
53
      },
54 55
      dataType: 'json',
    }).done(namespaces => callback(namespaces));
56
  },
57

58
  // Return projects list. Filtered by query
59 60
  projects(query, options, callback) {
    const url = Api.buildUrl(Api.projectsPath);
61 62 63
    const defaults = {
      search: query,
      per_page: 20,
64
      simple: true,
65 66 67 68 69 70
    };

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

71
    return $.ajax({
72
      url,
73
      data: Object.assign(defaults, options),
74 75 76
      dataType: 'json',
    })
      .done(projects => callback(projects));
77
  },
78 79

  newLabel(namespacePath, projectPath, data, callback) {
80 81 82 83 84 85 86 87 88 89
    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);
    }

90
    return $.ajax({
91 92 93 94 95 96
      url,
      type: 'POST',
      data: { label: data },
      dataType: 'json',
    })
      .done(label => callback(label))
97
      .fail(message => callback(message.responseJSON));
98
  },
99

100
  // Return group projects list. Filtered by query
101 102 103
  groupProjects(groupId, query, callback) {
    const url = Api.buildUrl(Api.groupProjectsPath)
      .replace(':id', groupId);
104
    return $.ajax({
105
      url,
106 107
      data: {
        search: query,
108
        per_page: 20,
109
      },
110 111 112
      dataType: 'json',
    })
      .done(projects => callback(projects));
113
  },
114

115 116
  commitMultiple(id, data) {
    // see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
Jacob Schatz committed
117 118
    const url = Api.buildUrl(Api.commitPath)
      .replace(':id', id);
119
    return this.wrapAjaxCall({
Jacob Schatz committed
120 121
      url,
      type: 'POST',
122
      contentType: 'application/json; charset=utf-8',
123
      data: JSON.stringify(data),
Jacob Schatz committed
124
      dataType: 'json',
125
    });
126 127 128 129 130 131 132 133 134 135 136 137 138
  },

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

    return this.wrapAjaxCall({
      url,
      type: 'GET',
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
    });
Jacob Schatz committed
139 140
  },

141
  // Return text for a specific license
142 143
  licenseText(key, data, callback) {
    const url = Api.buildUrl(Api.licensePath)
144 145
      .replace(':key', key);
    return $.ajax({
146 147 148 149
      url,
      data,
    })
      .done(license => callback(license));
150
  },
151 152 153

  gitignoreText(key, callback) {
    const url = Api.buildUrl(Api.gitignorePath)
154
      .replace(':key', key);
155
    return $.get(url, gitignore => callback(gitignore));
156
  },
157 158 159

  gitlabCiYml(key, callback) {
    const url = Api.buildUrl(Api.gitlabCiYmlPath)
160
      .replace(':key', key);
161
    return $.get(url, file => callback(file));
162
  },
163 164 165

  dockerfileYml(key, callback) {
    const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
166 167
    $.get(url, callback);
  },
168 169 170

  issueTemplate(namespacePath, projectPath, key, type, callback) {
    const url = Api.buildUrl(Api.issuableTemplatePath)
171 172 173 174 175
      .replace(':key', key)
      .replace(':type', type)
      .replace(':project_path', projectPath)
      .replace(':namespace_path', namespacePath);
    $.ajax({
176 177 178 179
      url,
      dataType: 'json',
    })
      .done(file => callback(null, file))
180
      .fail(callback);
181
  },
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

  users(query, options) {
    const url = Api.buildUrl(this.usersPath);
    return Api.wrapAjaxCall({
      url,
      data: Object.assign({
        search: query,
        per_page: 20,
      }, options),
      dataType: 'json',
    });
  },

  buildUrl(url) {
    let urlRoot = '';
197
    if (gon.relative_url_root != null) {
198
      urlRoot = gon.relative_url_root;
Fatih Acet committed
199
    }
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    return urlRoot + url.replace(':version', gon.api_version);
  },

  wrapAjaxCall(options) {
    return new Promise((resolve, reject) => {
      // jQuery 2 is not Promises/A+ compatible (missing catch)
      $.ajax(options) // eslint-disable-line promise/catch-or-return
      .then(data => resolve(data),
        (jqXHR, textStatus, errorThrown) => {
          const error = new Error(`${options.url}: ${errorThrown}`);
          error.textStatus = textStatus;
          reject(error);
        },
      );
    });
  },
216
};
Fatih Acet committed
217

218
export default Api;