api.js 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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',
  projectsPath: '/api/:version/projects.json?simple=true',
  labelsPath: '/:namespace_path/:project_path/labels',
  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',
J
Jacob Schatz 已提交
16
  commitPath: '/api/:version/projects/:id/repository/commits',
17 18 19 20

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

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

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

55
  // Return projects list. Filtered by query
56 57
  projects(query, options, callback) {
    const url = Api.buildUrl(Api.projectsPath);
58
    return $.ajax({
59 60
      url,
      data: Object.assign({
61
        search: query,
S
Sam Rose 已提交
62
        per_page: 20,
63
        membership: true,
S
Sam Rose 已提交
64
      }, options),
65 66 67
      dataType: 'json',
    })
      .done(projects => callback(projects));
68
  },
69 70 71 72 73

  newLabel(namespacePath, projectPath, data, callback) {
    const url = Api.buildUrl(Api.labelsPath)
      .replace(':namespace_path', namespacePath)
      .replace(':project_path', projectPath);
74
    return $.ajax({
75 76 77 78 79 80
      url,
      type: 'POST',
      data: { label: data },
      dataType: 'json',
    })
      .done(label => callback(label))
81
      .fail(message => callback(message.responseJSON));
82
  },
83

84
  // Return group projects list. Filtered by query
85 86 87
  groupProjects(groupId, query, callback) {
    const url = Api.buildUrl(Api.groupProjectsPath)
      .replace(':id', groupId);
88
    return $.ajax({
89
      url,
90 91
      data: {
        search: query,
92
        per_page: 20,
93
      },
94 95 96
      dataType: 'json',
    })
      .done(projects => callback(projects));
97
  },
98

J
Jacob Schatz 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  commitMultiple(id, data, callback) {
    // see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
    const url = Api.buildUrl(Api.commitPath)
      .replace(':id', id);
    return $.ajax({
      url,
      type: 'POST',
      data: data,
      dataType: 'json',
    })
      .done(commitData => callback(commitData))
      .fail(message => callback(message.responseJSON));

  },

114
  // Return text for a specific license
115 116
  licenseText(key, data, callback) {
    const url = Api.buildUrl(Api.licensePath)
117 118
      .replace(':key', key);
    return $.ajax({
119 120 121 122
      url,
      data,
    })
      .done(license => callback(license));
123
  },
124 125 126

  gitignoreText(key, callback) {
    const url = Api.buildUrl(Api.gitignorePath)
127
      .replace(':key', key);
128
    return $.get(url, gitignore => callback(gitignore));
129
  },
130 131 132

  gitlabCiYml(key, callback) {
    const url = Api.buildUrl(Api.gitlabCiYmlPath)
133
      .replace(':key', key);
134
    return $.get(url, file => callback(file));
135
  },
136 137 138

  dockerfileYml(key, callback) {
    const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
139 140
    $.get(url, callback);
  },
141 142 143

  issueTemplate(namespacePath, projectPath, key, type, callback) {
    const url = Api.buildUrl(Api.issuableTemplatePath)
144 145 146 147 148
      .replace(':key', key)
      .replace(':type', type)
      .replace(':project_path', projectPath)
      .replace(':namespace_path', namespacePath);
    $.ajax({
149 150 151 152
      url,
      dataType: 'json',
    })
      .done(file => callback(null, file))
153
      .fail(callback);
154
  },
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

  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 = '';
170
    if (gon.relative_url_root != null) {
171
      urlRoot = gon.relative_url_root;
F
Fatih Acet 已提交
172
    }
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    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);
        },
      );
    });
  },
189
};
F
Fatih Acet 已提交
190

191
export default Api;