api.js 4.7 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, quotes, object-shorthand, camelcase, no-var, comma-dangle, prefer-arrow-callback, quote-props, no-param-reassign, max-len */
2

F
Fatih Acet 已提交
3
(function() {
4
  var Api = {
F
Fatih Acet 已提交
5 6 7 8 9
    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",
10
    labelsPath: "/:namespace_path/:project_path/labels",
11 12 13
    licensePath: "/api/:version/templates/licenses/:key",
    gitignorePath: "/api/:version/templates/gitignores/:key",
    gitlabCiYmlPath: "/api/:version/templates/gitlab_ci_ymls/:key",
14
    dockerfilePath: "/api/:version/templates/dockerfiles/:key",
15
    issuableTemplatePath: "/:namespace_path/:project_path/templates/:type/:key",
F
Fatih Acet 已提交
16
    group: function(group_id, callback) {
17 18
      var url = Api.buildUrl(Api.groupPath)
        .replace(':id', group_id);
F
Fatih Acet 已提交
19 20 21 22 23 24 25
      return $.ajax({
        url: url,
        dataType: "json"
      }).done(function(group) {
        return callback(group);
      });
    },
26
    // Return groups list. Filtered by query
27
    groups: function(query, options, callback) {
28
      var url = Api.buildUrl(Api.groupsPath);
F
Fatih Acet 已提交
29 30
      return $.ajax({
        url: url,
31
        data: $.extend({
32 33 34
          search: query,
          per_page: 20
        }, options),
F
Fatih Acet 已提交
35 36 37 38 39
        dataType: "json"
      }).done(function(groups) {
        return callback(groups);
      });
    },
40
    // Return namespaces list. Filtered by query
F
Fatih Acet 已提交
41
    namespaces: function(query, callback) {
42
      var url = Api.buildUrl(Api.namespacesPath);
F
Fatih Acet 已提交
43 44 45 46 47 48 49 50 51 52 53
      return $.ajax({
        url: url,
        data: {
          search: query,
          per_page: 20
        },
        dataType: "json"
      }).done(function(namespaces) {
        return callback(namespaces);
      });
    },
54
    // Return projects list. Filtered by query
F
Fatih Acet 已提交
55
    projects: function(query, order, callback) {
56
      var url = Api.buildUrl(Api.projectsPath);
F
Fatih Acet 已提交
57 58 59 60 61 62 63 64 65 66 67 68
      return $.ajax({
        url: url,
        data: {
          search: query,
          order_by: order,
          per_page: 20
        },
        dataType: "json"
      }).done(function(projects) {
        return callback(projects);
      });
    },
69
    newLabel: function(namespace_path, project_path, data, callback) {
70
      var url = Api.buildUrl(Api.labelsPath)
71 72
        .replace(':namespace_path', namespace_path)
        .replace(':project_path', project_path);
F
Fatih Acet 已提交
73 74 75
      return $.ajax({
        url: url,
        type: "POST",
76
        data: { 'label': data },
F
Fatih Acet 已提交
77 78 79 80 81 82 83
        dataType: "json"
      }).done(function(label) {
        return callback(label);
      }).error(function(message) {
        return callback(message.responseJSON);
      });
    },
84
    // Return group projects list. Filtered by query
F
Fatih Acet 已提交
85
    groupProjects: function(group_id, query, callback) {
86 87
      var url = Api.buildUrl(Api.groupProjectsPath)
        .replace(':id', group_id);
F
Fatih Acet 已提交
88 89 90 91 92 93 94 95 96 97 98
      return $.ajax({
        url: url,
        data: {
          search: query,
          per_page: 20
        },
        dataType: "json"
      }).done(function(projects) {
        return callback(projects);
      });
    },
99
    // Return text for a specific license
F
Fatih Acet 已提交
100
    licenseText: function(key, data, callback) {
101 102
      var url = Api.buildUrl(Api.licensePath)
        .replace(':key', key);
F
Fatih Acet 已提交
103 104 105 106 107 108 109 110
      return $.ajax({
        url: url,
        data: data
      }).done(function(license) {
        return callback(license);
      });
    },
    gitignoreText: function(key, callback) {
111 112
      var url = Api.buildUrl(Api.gitignorePath)
        .replace(':key', key);
F
Fatih Acet 已提交
113 114 115 116 117
      return $.get(url, function(gitignore) {
        return callback(gitignore);
      });
    },
    gitlabCiYml: function(key, callback) {
118 119
      var url = Api.buildUrl(Api.gitlabCiYmlPath)
        .replace(':key', key);
F
Fatih Acet 已提交
120 121 122 123
      return $.get(url, function(file) {
        return callback(file);
      });
    },
124 125 126 127
    dockerfileYml: function(key, callback) {
      var url = Api.buildUrl(Api.dockerfilePath).replace(':key', key);
      $.get(url, callback);
    },
128 129 130 131 132 133 134 135 136 137 138 139 140
    issueTemplate: function(namespacePath, projectPath, key, type, callback) {
      var url = Api.buildUrl(Api.issuableTemplatePath)
        .replace(':key', key)
        .replace(':type', type)
        .replace(':project_path', projectPath)
        .replace(':namespace_path', namespacePath);
      $.ajax({
        url: url,
        dataType: 'json'
      }).done(function(file) {
        callback(null, file);
      }).error(callback);
    },
F
Fatih Acet 已提交
141 142 143 144 145 146 147 148
    buildUrl: function(url) {
      if (gon.relative_url_root != null) {
        url = gon.relative_url_root + url;
      }
      return url.replace(':version', gon.api_version);
    }
  };

149
  window.Api = Api;
150
}).call(window);