PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/assets/javascripts/api.js

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