PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/app/assets/javascripts/api.js

https://gitlab.com/artofhuman/gitlab-ce
JavaScript | 326 lines | 265 code | 54 blank | 7 comment | 7 complexity | ae92f1eaa7f034fcce116acbbc97e7b4 MD5 | raw file
  1. import $ from 'jquery';
  2. import _ from 'underscore';
  3. import axios from './lib/utils/axios_utils';
  4. const Api = {
  5. groupsPath: '/api/:version/groups.json',
  6. groupPath: '/api/:version/groups/:id',
  7. subgroupsPath: '/api/:version/groups/:id/subgroups',
  8. namespacesPath: '/api/:version/namespaces.json',
  9. groupProjectsPath: '/api/:version/groups/:id/projects.json',
  10. projectsPath: '/api/:version/projects.json',
  11. projectPath: '/api/:version/projects/:id',
  12. projectLabelsPath: '/:namespace_path/:project_path/labels',
  13. projectMergeRequestPath: '/api/:version/projects/:id/merge_requests/:mrid',
  14. projectMergeRequestChangesPath: '/api/:version/projects/:id/merge_requests/:mrid/changes',
  15. projectMergeRequestVersionsPath: '/api/:version/projects/:id/merge_requests/:mrid/versions',
  16. projectRunnersPath: '/api/:version/projects/:id/runners',
  17. mergeRequestsPath: '/api/:version/merge_requests',
  18. groupLabelsPath: '/groups/:namespace_path/-/labels',
  19. issuableTemplatePath: '/:namespace_path/:project_path/templates/:type/:key',
  20. projectTemplatePath: '/api/:version/projects/:id/templates/:type/:key',
  21. projectTemplatesPath: '/api/:version/projects/:id/templates/:type',
  22. usersPath: '/api/:version/users.json',
  23. userPath: '/api/:version/users/:id',
  24. userStatusPath: '/api/:version/users/:id/status',
  25. userPostStatusPath: '/api/:version/user/status',
  26. commitPath: '/api/:version/projects/:id/repository/commits',
  27. applySuggestionPath: '/api/:version/suggestions/:id/apply',
  28. commitPipelinesPath: '/:project_id/commit/:sha/pipelines',
  29. branchSinglePath: '/api/:version/projects/:id/repository/branches/:branch',
  30. createBranchPath: '/api/:version/projects/:id/repository/branches',
  31. releasesPath: '/api/:version/projects/:id/releases',
  32. group(groupId, callback) {
  33. const url = Api.buildUrl(Api.groupPath).replace(':id', groupId);
  34. return axios.get(url).then(({ data }) => {
  35. callback(data);
  36. return data;
  37. });
  38. },
  39. // Return groups list. Filtered by query
  40. groups(query, options, callback = $.noop) {
  41. const url = Api.buildUrl(Api.groupsPath);
  42. return axios
  43. .get(url, {
  44. params: Object.assign(
  45. {
  46. search: query,
  47. per_page: 20,
  48. },
  49. options,
  50. ),
  51. })
  52. .then(({ data }) => {
  53. callback(data);
  54. return data;
  55. });
  56. },
  57. // Return namespaces list. Filtered by query
  58. namespaces(query, callback) {
  59. const url = Api.buildUrl(Api.namespacesPath);
  60. return axios
  61. .get(url, {
  62. params: {
  63. search: query,
  64. per_page: 20,
  65. },
  66. })
  67. .then(({ data }) => callback(data));
  68. },
  69. // Return projects list. Filtered by query
  70. projects(query, options, callback = _.noop) {
  71. const url = Api.buildUrl(Api.projectsPath);
  72. const defaults = {
  73. search: query,
  74. per_page: 20,
  75. simple: true,
  76. };
  77. if (gon.current_user_id) {
  78. defaults.membership = true;
  79. }
  80. return axios
  81. .get(url, {
  82. params: Object.assign(defaults, options),
  83. })
  84. .then(({ data }) => {
  85. callback(data);
  86. return data;
  87. });
  88. },
  89. // Return single project
  90. project(projectPath) {
  91. const url = Api.buildUrl(Api.projectPath).replace(':id', encodeURIComponent(projectPath));
  92. return axios.get(url);
  93. },
  94. // Return Merge Request for project
  95. projectMergeRequest(projectPath, mergeRequestId, params = {}) {
  96. const url = Api.buildUrl(Api.projectMergeRequestPath)
  97. .replace(':id', encodeURIComponent(projectPath))
  98. .replace(':mrid', mergeRequestId);
  99. return axios.get(url, { params });
  100. },
  101. projectMergeRequestChanges(projectPath, mergeRequestId) {
  102. const url = Api.buildUrl(Api.projectMergeRequestChangesPath)
  103. .replace(':id', encodeURIComponent(projectPath))
  104. .replace(':mrid', mergeRequestId);
  105. return axios.get(url);
  106. },
  107. projectMergeRequestVersions(projectPath, mergeRequestId) {
  108. const url = Api.buildUrl(Api.projectMergeRequestVersionsPath)
  109. .replace(':id', encodeURIComponent(projectPath))
  110. .replace(':mrid', mergeRequestId);
  111. return axios.get(url);
  112. },
  113. projectRunners(projectPath, config = {}) {
  114. const url = Api.buildUrl(Api.projectRunnersPath).replace(
  115. ':id',
  116. encodeURIComponent(projectPath),
  117. );
  118. return axios.get(url, config);
  119. },
  120. mergeRequests(params = {}) {
  121. const url = Api.buildUrl(Api.mergeRequestsPath);
  122. return axios.get(url, { params });
  123. },
  124. newLabel(namespacePath, projectPath, data, callback) {
  125. let url;
  126. if (projectPath) {
  127. url = Api.buildUrl(Api.projectLabelsPath)
  128. .replace(':namespace_path', namespacePath)
  129. .replace(':project_path', projectPath);
  130. } else {
  131. url = Api.buildUrl(Api.groupLabelsPath).replace(':namespace_path', namespacePath);
  132. }
  133. return axios
  134. .post(url, {
  135. label: data,
  136. })
  137. .then(res => callback(res.data))
  138. .catch(e => callback(e.response.data));
  139. },
  140. // Return group projects list. Filtered by query
  141. groupProjects(groupId, query, options, callback) {
  142. const url = Api.buildUrl(Api.groupProjectsPath).replace(':id', groupId);
  143. const defaults = {
  144. search: query,
  145. per_page: 20,
  146. };
  147. return axios
  148. .get(url, {
  149. params: Object.assign({}, defaults, options),
  150. })
  151. .then(({ data }) => callback(data));
  152. },
  153. commitMultiple(id, data) {
  154. // see https://docs.gitlab.com/ce/api/commits.html#create-a-commit-with-multiple-files-and-actions
  155. const url = Api.buildUrl(Api.commitPath).replace(':id', encodeURIComponent(id));
  156. return axios.post(url, JSON.stringify(data), {
  157. headers: {
  158. 'Content-Type': 'application/json; charset=utf-8',
  159. },
  160. });
  161. },
  162. applySuggestion(id) {
  163. const url = Api.buildUrl(Api.applySuggestionPath).replace(':id', encodeURIComponent(id));
  164. return axios.put(url);
  165. },
  166. commitPipelines(projectId, sha) {
  167. const encodedProjectId = projectId
  168. .split('/')
  169. .map(fragment => encodeURIComponent(fragment))
  170. .join('/');
  171. const url = Api.buildUrl(Api.commitPipelinesPath)
  172. .replace(':project_id', encodedProjectId)
  173. .replace(':sha', encodeURIComponent(sha));
  174. return axios.get(url);
  175. },
  176. branchSingle(id, branch) {
  177. const url = Api.buildUrl(Api.branchSinglePath)
  178. .replace(':id', encodeURIComponent(id))
  179. .replace(':branch', encodeURIComponent(branch));
  180. return axios.get(url);
  181. },
  182. projectTemplate(id, type, key, options, callback) {
  183. const url = Api.buildUrl(this.projectTemplatePath)
  184. .replace(':id', encodeURIComponent(id))
  185. .replace(':type', type)
  186. .replace(':key', encodeURIComponent(key));
  187. return axios.get(url, { params: options }).then(res => {
  188. if (callback) callback(res.data);
  189. return res;
  190. });
  191. },
  192. projectTemplates(id, type, params = {}, callback) {
  193. const url = Api.buildUrl(this.projectTemplatesPath)
  194. .replace(':id', encodeURIComponent(id))
  195. .replace(':type', type);
  196. return axios.get(url, { params }).then(res => {
  197. if (callback) callback(res.data);
  198. return res;
  199. });
  200. },
  201. issueTemplate(namespacePath, projectPath, key, type, callback) {
  202. const url = Api.buildUrl(Api.issuableTemplatePath)
  203. .replace(':key', encodeURIComponent(key))
  204. .replace(':type', type)
  205. .replace(':project_path', projectPath)
  206. .replace(':namespace_path', namespacePath);
  207. return axios
  208. .get(url)
  209. .then(({ data }) => callback(null, data))
  210. .catch(callback);
  211. },
  212. users(query, options) {
  213. const url = Api.buildUrl(this.usersPath);
  214. return axios.get(url, {
  215. params: Object.assign(
  216. {
  217. search: query,
  218. per_page: 20,
  219. },
  220. options,
  221. ),
  222. });
  223. },
  224. user(id, options) {
  225. const url = Api.buildUrl(this.userPath).replace(':id', encodeURIComponent(id));
  226. return axios.get(url, {
  227. params: options,
  228. });
  229. },
  230. userStatus(id, options) {
  231. const url = Api.buildUrl(this.userStatusPath).replace(':id', encodeURIComponent(id));
  232. return axios.get(url, {
  233. params: options,
  234. });
  235. },
  236. branches(id, query = '', options = {}) {
  237. const url = Api.buildUrl(this.createBranchPath).replace(':id', encodeURIComponent(id));
  238. return axios.get(url, {
  239. params: {
  240. search: query,
  241. per_page: 20,
  242. ...options,
  243. },
  244. });
  245. },
  246. createBranch(id, { ref, branch }) {
  247. const url = Api.buildUrl(this.createBranchPath).replace(':id', encodeURIComponent(id));
  248. return axios.post(url, {
  249. ref,
  250. branch,
  251. });
  252. },
  253. postUserStatus({ emoji, message }) {
  254. const url = Api.buildUrl(this.userPostStatusPath);
  255. return axios.put(url, {
  256. emoji,
  257. message,
  258. });
  259. },
  260. releases(id) {
  261. const url = Api.buildUrl(this.releasesPath).replace(':id', encodeURIComponent(id));
  262. return axios.get(url);
  263. },
  264. buildUrl(url) {
  265. let urlRoot = '';
  266. if (gon.relative_url_root != null) {
  267. urlRoot = gon.relative_url_root;
  268. }
  269. return urlRoot + url.replace(':version', gon.api_version);
  270. },
  271. };
  272. export default Api;