api.js 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
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',

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

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

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

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

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

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

98
  // Return text for a specific license
99 100
  licenseText(key, data, callback) {
    const url = Api.buildUrl(Api.licensePath)
101 102
      .replace(':key', key);
    return $.ajax({
103 104 105 106
      url,
      data,
    })
      .done(license => callback(license));
107
  },
108 109 110

  gitignoreText(key, callback) {
    const url = Api.buildUrl(Api.gitignorePath)
111
      .replace(':key', key);
112
    return $.get(url, gitignore => callback(gitignore));
113
  },
114 115 116

  gitlabCiYml(key, callback) {
    const url = Api.buildUrl(Api.gitlabCiYmlPath)
117
      .replace(':key', key);
118
    return $.get(url, file => callback(file));
119
  },
120 121 122

  dockerfileYml(key, callback) {
    const url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
123 124
    $.get(url, callback);
  },
125 126 127

  issueTemplate(namespacePath, projectPath, key, type, callback) {
    const url = Api.buildUrl(Api.issuableTemplatePath)
128 129 130 131 132
      .replace(':key', key)
      .replace(':type', type)
      .replace(':project_path', projectPath)
      .replace(':namespace_path', namespacePath);
    $.ajax({
133 134 135 136
      url,
      dataType: 'json',
    })
      .done(file => callback(null, file))
137
      .fail(callback);
138
  },
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  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 = '';
154
    if (gon.relative_url_root != null) {
155
      urlRoot = gon.relative_url_root;
F
Fatih Acet 已提交
156
    }
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    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);
        },
      );
    });
  },
173
};
F
Fatih Acet 已提交
174

175
export default Api;