Source: api/workflowScheme.js

  1. "use strict";
  2. module.exports = WorkflowSchemeClient;
  3. /**
  4. * Used to access Jira REST endpoints in '/rest/api/2/workflowscheme'
  5. * @param {JiraClient} jiraClient
  6. * @constructor WorkflowSchemeClient
  7. */
  8. function WorkflowSchemeClient(jiraClient) {
  9. this.jiraClient = jiraClient;
  10. /**
  11. * Create a new workflow scheme. The body contains a representation of the new scheme. Values not passed are
  12. * assumed to be set to their defaults.
  13. *
  14. * @method createWorkflowScheme
  15. * @memberOf WorkflowSchemeClient#
  16. * @param opts The request options sent to the Jira API.
  17. * @param opts.workflowScheme See {@link https://docs.atlassian.com/jira/REST/latest/#d2e2196}
  18. * @param [callback] Called when the workflow scheme has been created.
  19. * @return {Promise} Resolved when the workflow scheme has been created.
  20. */
  21. this.createWorkflowScheme = function (opts, callback) {
  22. var options = {
  23. uri: this.jiraClient.buildURL('/workflowscheme'),
  24. method: 'POST',
  25. json: true,
  26. followAllRedirects: true,
  27. body: opts.workflowScheme
  28. };
  29. return this.jiraClient.makeRequest(options, callback);
  30. };
  31. /**
  32. * Update the passed workflow scheme. The body of the request is a representation of the workflow scheme. Values
  33. * not passed are assumed to indicate no change for that field. The passed representation can have its
  34. * updateDraftIfNeeded flag set to true to indicate that the draft should be created and/or updated when the actual
  35. * scheme cannot be edited (e.g. when the scheme is being used by a project). Values not appearing the body will
  36. * not be touched.
  37. *
  38. * @method editWorkflowScheme
  39. * @memberOf WorkflowSchemeClient#
  40. * @param opts The request options sent to the Jira API.
  41. * @param opts.workflowSchemeId The id of the workflow scheme.
  42. * @param opts.workflowScheme See {@link https://docs.atlassian.com/jira/REST/latest/#d2e2305}
  43. * @param [callback] Called when the workflow scheme has been edited.
  44. * @return {Promise} Resolved when the workflow scheme has been edited.
  45. */
  46. this.editWorkflowScheme = function (opts, callback) {
  47. var options = this.buildRequestOptions(opts, '', 'PUT', opts.workflowScheme);
  48. return this.jiraClient.makeRequest(options, callback);
  49. };
  50. /**
  51. * Get the requested workflow scheme
  52. *
  53. * @method getWorkflowScheme
  54. * @memberOf WorkflowSchemeClient#
  55. * @param opts The request options sent to the Jira API.
  56. * @param opts.workflowSchemeId The id of the workflow scheme.
  57. * @param [opts.returnDraftIfExists=false] when true indicates that a scheme's draft, if it exists, should be
  58. * queried instead of the scheme itself.
  59. * @param [callback] Called when the workflow scheme has been retrieved.
  60. * @return {Promise} Resolved when the workflow scheme has been retrieved.
  61. */
  62. this.getWorkflowScheme = function (opts, callback) {
  63. var options = this.buildRequestOptions(opts, '', 'GET', null, {returnDraftIfExists: opts.returnDraftIfExists});
  64. return this.jiraClient.makeRequest(options, callback);
  65. };
  66. /**
  67. * Delete the passed workflow scheme.
  68. *
  69. * @method deleteWorkflowScheme
  70. * @memberOf WorkflowSchemeClient#
  71. * @param opts The request options sent to the Jira API.
  72. * @param opts.workflowSchemeId The id of the workflow scheme.
  73. * @param [callback] Called when the workflow scheme has been deleted.
  74. * @return {Promise} Resolved when the workflow scheme has been deleted.
  75. */
  76. this.deleteWorkflowScheme = function (opts, callback) {
  77. var options = this.buildRequestOptions(opts, '', 'DELETE');
  78. return this.jiraClient.makeRequest(options, callback, 'Workflow Scheme Deleted');
  79. };
  80. /**
  81. * Create a draft for the passed scheme. The draft will be a copy of the state of the parent.
  82. *
  83. * @method createDraft
  84. * @memberOf WorkflowSchemeClient#
  85. * @param opts The request options sent to the Jira API.
  86. * @param opts.workflowSchemeId The id of the workflow scheme.
  87. * @param [callback] Called when the draft has been created.
  88. * @return {Promise} Resolved when the draft has been created.
  89. */
  90. this.createDraft = function (opts, callback) {
  91. var options = this.buildRequestOptions(opts, '/createdraft', 'POST');
  92. return this.jiraClient.makeRequest(options, callback);
  93. };
  94. /**
  95. * Get the default workflow from the passed workflow scheme.
  96. *
  97. * @method getDefaultWorkflow
  98. * @memberOf WorkflowSchemeClient#
  99. * @param opts The request options sent to the Jira API.
  100. * @param opts.workflowSchemeId The id of the workflow scheme.
  101. * @param opts.returnDraftIfExists when true indicates that a scheme's draft, if it exists, should be queried
  102. * instead of the scheme itself.
  103. * @param [callback] Called when the default workflow is returned.
  104. * @return {Promise} Resolved when the default workflow is returned.
  105. */
  106. this.getDefaultWorkflow = function (opts, callback) {
  107. var options = this.buildRequestOptions(opts, '/default', 'GET', null, {returnDraftIfExists: opts.returnDraftIfExists});
  108. return this.jiraClient.makeRequest(options, callback);
  109. };
  110. /**
  111. * Remove the default workflow from the passed workflow scheme.
  112. *
  113. * @method removeDefaultWorkflow
  114. * @memberOf WorkflowSchemeClient#
  115. * @param opts The request options sent to the Jira API.
  116. * @param opts.workflowSchemeId The id of the workflow scheme.
  117. * @param opts.updateDraftIfNeeded when true will create and return a draft when the workflow scheme cannot be
  118. * edited (e.g. when it is being used by a project).
  119. * @param [callback] Called when the defaul workflow has been removed.
  120. * @return {Promise} Resolved when the defaul workflow has been removed.
  121. */
  122. this.removeDefaultWorkflow = function (opts, callback) {
  123. var options = this.buildRequestOptions(opts, '/default', 'DELETE', null, {updateDraftIfNeeded: opts.updateDraftIfNeeded});
  124. return this.jiraClient.makeRequest(options, callback);
  125. };
  126. /**
  127. * Remove the default workflow from the passed workflow scheme.
  128. *
  129. * @method setDefaultWorkflow
  130. * @memberOf WorkflowSchemeClient#
  131. * @param opts The request options sent to the Jira API.
  132. * @param opts.workflowSchemeId The id of the workflow scheme.
  133. * @param opts.workflowName The name of the new deafault workflow
  134. * @param opts.updateDraftIfNeeded when true will create and return a draft when the workflow scheme cannot be
  135. * edited (e.g. when it is being used by a project).
  136. * @param [callback] Called when the default workflow has been updated.
  137. * @return {Promise} Resolved when the default workflow has been updated.
  138. */
  139. this.setDefaultWorkflow = function (opts, callback) {
  140. var options = this.buildRequestOptions(opts, '/default', 'PUT', {
  141. workflow: opts.workflowName,
  142. updateDraftIfNeeded: opts.updateDraftIfNeeded
  143. });
  144. return this.jiraClient.makeRequest(options, callback);
  145. };
  146. /**
  147. * Get the requested draft workflow scheme
  148. *
  149. * @method getDraft
  150. * @memberOf WorkflowSchemeClient#
  151. * @param opts The request options sent to the Jira API.
  152. * @param opts.workflowSchemeId The id of the workflow scheme.
  153. * @param [callback] Called when the draft has been retrieved.
  154. * @return {Promise} Resolved when the draft has been retrieved.
  155. */
  156. this.getDraft = function (opts, callback) {
  157. var options = this.buildRequestOptions(opts, '/draft', 'GET');
  158. return this.jiraClient.makeRequest(options, callback);
  159. };
  160. /**
  161. * Update a draft workflow scheme. The draft will created if necessary. The body is a representation of the
  162. * workflow scheme. Values not passed are assumed to indicate no change for that field.
  163. *
  164. * @method editDraft
  165. * @memberOf WorkflowSchemeClient#
  166. * @param opts The request options sent to the Jira API.
  167. * @param opts.workflowSchemeId The id of the workflow scheme.
  168. * @param opts.draft See {@link https://docs.atlassian.com/jira/REST/latest/#d2e2575}
  169. * @param [callback] Called when the draft has been edited.
  170. * @return {Promise} Resolved when the draft has been edited.
  171. */
  172. this.editDraft = function (opts, callback) {
  173. var options = this.buildRequestOptions(opts, '/draft', 'PUT', opts.draft);
  174. return this.jiraClient.makeRequest(options, callback);
  175. };
  176. /**
  177. * Delete the passed draft workflow scheme.
  178. *
  179. * @method deleteDraft
  180. * @memberOf WorkflowSchemeClient#
  181. * @param opts The request options sent to the Jira API.
  182. * @param opts.workflowSchemeId The id of the workflow scheme.
  183. * @param [callback] Called when the draft has been deleted.
  184. * @return {Promise} Resolved when the draft has been deleted.
  185. */
  186. this.deleteDraft = function (opts, callback) {
  187. var options = this.buildRequestOptions(opts, '/draft', 'DELETE');
  188. return this.jiraClient.makeRequest(options, callback);
  189. };
  190. /**
  191. * Get the default workflow from the passed draft workflow scheme
  192. *
  193. * @method getDraftDefaultWorkflow
  194. * @memberOf WorkflowSchemeClient#
  195. * @param opts The request options sent to the Jira API.
  196. * @param opts.workflowSchemeId The id of the workflow scheme.
  197. * @param [callback] Called when the default workflow is returned.
  198. * @return {Promise} Resolved when the default workflow is returned.
  199. */
  200. this.getDraftDefaultWorkflow = function (opts, callback) {
  201. var options = this.buildRequestOptions(opts, '/draft/default', 'GET');
  202. return this.jiraClient.makeRequest(options, callback);
  203. };
  204. /**
  205. * Remove the default workflow from the passed workflow scheme.
  206. *
  207. * @method setDraftDefaultWorkflow
  208. * @memberOf WorkflowSchemeClient#
  209. * @param opts The request options sent to the Jira API.
  210. * @param opts.workflowSchemeId The id of the workflow scheme.
  211. * @param opts.workflowName The name of the new default workflow
  212. * @param [callback] Called when the default workflow has been updated.
  213. * @return {Promise} Resolved when the default workflow has been updated.
  214. */
  215. this.setDraftDefaultWorkflow = function (opts, callback) {
  216. var options = this.buildRequestOptions(opts, '/draft/default', 'PUT', {
  217. workflow: opts.workflowName,
  218. updateDraftIfNeeded: opts.updateDraftIfNeeded
  219. });
  220. return this.jiraClient.makeRequest(options, callback);
  221. };
  222. /**
  223. * Remove the default workflow from the passed draft workflow scheme.
  224. *
  225. * @method removeDraftDefaultWorkflow
  226. * @memberOf WorkflowSchemeClient#
  227. * @param opts The request options sent to the Jira API.
  228. * @param opts.workflowSchemeId The id of the workflow scheme.
  229. * @param [callback] Called when the defaul workflow has been removed.
  230. * @return {Promise} Resolved when the defaul workflow has been removed.
  231. */
  232. this.removeDraftDefaultWorkflow = function (opts, callback) {
  233. var options = this.buildRequestOptions(opts, '/draft/default', 'DELETE');
  234. return this.jiraClient.makeRequest(options, callback);
  235. };
  236. /**
  237. * Returns the issue type mapping for the passed workflow scheme.
  238. *
  239. * @method getIssueType
  240. * @memberOf WorkflowSchemeClient#
  241. * @param opts The request options sent to the Jira API.
  242. * @param opts.workflowSchemeId The id of the workflow scheme.
  243. * @param opts.issueType The issue type
  244. * @param opts.returnDraftIfExists when true indicates that a scheme's draft, if it exists, should be queried
  245. * instead of the scheme itself.
  246. * @param [callback] Called when the issue type has been retrieved.
  247. * @return {Promise} Resolved when the issue type has been retrieved.
  248. */
  249. this.getIssueType = function (opts, callback) {
  250. var options = this.buildRequestOptions(opts, '/issuetype/' + opts.issueType, 'GET', null,
  251. {returnDraftIfExists: opts.returnDraftIfExists});
  252. return this.jiraClient.makeRequest(options, callback);
  253. };
  254. /**
  255. * Set the issue type mapping for the passed scheme. The passed representation can have its updateDraftIfNeeded
  256. * flag set to true to indicate that the draft should be created/updated when the actual scheme cannot be edited.
  257. *
  258. * @method editIssueType
  259. * @memberOf WorkflowSchemeClient#
  260. * @param opts The request options sent to the Jira API
  261. * @param opts.workflowSchemeId The id of the workflow scheme.
  262. * @param opts.issueType The issue type
  263. * @param opts.workflow The new workflow
  264. * @param opts.updateDraftIfNeeded when true will create and return a draft when the workflow scheme cannot be
  265. * edited (e.g. when it is being used by a project).
  266. * @param [callback] Called when the issue type has been edited
  267. * @return {Promise} Resolved when the issue type has been edited
  268. */
  269. this.editIssueType = function (opts, callback) {
  270. var options = this.buildRequestOptions(opts, '/issuetype/' + opts.issueType, 'PUT', {
  271. workflow: opts.workflow,
  272. updateDraftIfNeeded: opts.updateDraftIfNeeded
  273. });
  274. return this.jiraClient.makeRequest(options, callback);
  275. };
  276. /**
  277. * Remove the specified issue type mapping from the scheme.
  278. *
  279. * @method removeIssueType
  280. * @memberOf WorkflowSchemeClient#
  281. * @param opts The request options sent to the Jira API
  282. * @param opts.workflowSchemeId The id of the workflow scheme.
  283. * @param opts.issueType The issue type
  284. * @param [callback] Called when the issue type mapping has been removed.
  285. * @return {Promise} Resolved when the issue type mapping has been removed.
  286. */
  287. this.removeIssueType = function (opts, callback) {
  288. var options = this.buildRequestOptions(opts, '/issuetype/' + opts.issueType, 'DELETE', null, {
  289. updateDraftIfNeeded: opts.updateDraftIfNeeded
  290. });
  291. return this.jiraClient.makeRequest(options, callback);
  292. };
  293. /**
  294. * Returns the issue type mapping for the passed draft workflow scheme.
  295. *
  296. * @method getDraftIssueType
  297. * @memberOf WorkflowSchemeClient#
  298. * @param opts The request options sent to the Jira API.
  299. * @param opts.workflowSchemeId The id of the workflow scheme.
  300. * @param opts.issueType The issue type
  301. * @param [callback] Called when the issue type has been retrieved.
  302. * @return {Promise} Resolved when the issue type has been retrieved.
  303. */
  304. this.getDraftIssueType = function (opts, callback) {
  305. var options = this.buildRequestOptions(opts, '/draft/issuetype/' + opts.issueType, 'GET');
  306. return this.jiraClient.makeRequest(options, callback);
  307. };
  308. /**
  309. * Set the issue type mapping for the passed draft scheme.
  310. *
  311. * @method editDraftIssueType
  312. * @memberOf WorkflowSchemeClient#
  313. * @param opts The request options sent to the Jira API
  314. * @param opts.workflowSchemeId The id of the workflow scheme.
  315. * @param opts.issueType The issue type
  316. * @param opts.workflow The new workflow
  317. * @param [callback] Called when the issue type has been edited
  318. * @return {Promise} Resolved when the issue type has been edited
  319. */
  320. this.editDraftIssueType = function (opts, callback) {
  321. var options = this.buildRequestOptions(opts, '/draft/issuetype/' + opts.issueType, 'PUT', {workflow: opts.workflow});
  322. return this.jiraClient.makeRequest(options, callback);
  323. };
  324. /**
  325. * Remove the specified issue type mapping from the scheme.
  326. *
  327. * @method removeDraftIssueType
  328. * @memberOf WorkflowSchemeClient#
  329. * @param opts The request options sent to the Jira API
  330. * @param opts.workflowSchemeId The id of the workflow scheme.
  331. * @param opts.issueType The issue type
  332. * @param [callback] Called when the issue type mapping has been removed.
  333. * @return {Promise} Resolved when the issue type mapping has been removed.
  334. */
  335. this.removeDraftIssueType = function (opts, callback) {
  336. var options = this.buildRequestOptions(opts, '/draft/issuetype/' + opts.issueType, 'DELETE');
  337. return this.jiraClient.makeRequest(options, callback);
  338. };
  339. /**
  340. * Returns the workflow mappings or requested mapping to the caller for the passed scheme.
  341. *
  342. * @method getWorkflow
  343. * @memberOf WorkflowSchemeClient#
  344. * @param opts The request options sent to the Jira API
  345. * @param opts.workflowSchemeId The id of the workflow scheme.
  346. * @param opts.workflowName The name of the workflow.
  347. * @param [callback] Called when the workflow has been retrieved.
  348. * @return {Promise} Resolved when the workflow has been retrieved.
  349. */
  350. this.getWorkflow = function (opts, callback) {
  351. var options = this.buildRequestOptions(opts, '/workflow', 'GET', null, {
  352. workflowName: opts.workflowName
  353. });
  354. return this.jiraClient.makeRequest(options, callback);
  355. };
  356. /**
  357. * Returns the workflow mappings or requested mapping to the caller for the passed draft scheme.
  358. *
  359. * @method getDraftWorkflow
  360. * @memberOf WorkflowSchemeClient#
  361. * @param opts The request options sent to the Jira API
  362. * @param opts.workflowSchemeId The id of the workflow scheme.
  363. * @param opts.workflowName The name of the workflow.
  364. * @param [callback] Called when the workflow has been retrieved.
  365. * @return {Promise} Resolved when the workflow has been retrieved.
  366. */
  367. this.getDraftWorkflow = function (opts, callback) {
  368. var options = this.buildRequestOptions(opts, '/draft/workflow', 'GET', null, {
  369. workflowName: opts.workflowName
  370. });
  371. return this.jiraClient.makeRequest(options, callback);
  372. };
  373. /**
  374. * Update the scheme to include the passed mapping. The body is a representation of the workflow mapping. Values
  375. * not passed are assumed to indicate no change for that field.
  376. *
  377. * @method editWorkflow
  378. * @memberOf WorkflowSchemeClient#
  379. * @param {Object} opts The request options sent to the Jira API
  380. * @param {number} opts.workflowSchemeId The id of the workflow scheme.
  381. * @param {string} opts.workflowName The name of the workflow.
  382. * @param {Array} opts.issueTypes The new issue types to inclue in the workflow.
  383. * See {@link https://docs.atlassian.com/jira/REST/latest/#d2e2509}
  384. * @param [callback] Called when the workflow has been edited.
  385. * @return {Promise} Resolved when the workflow has been edited.
  386. */
  387. this.editWorkflow = function (opts, callback) {
  388. var options = this.buildRequestOptions(opts, '/workflow', 'PUT', {
  389. workflow: opts.workflowName,
  390. issueTypes: opts.issueTypes
  391. }, {workflowName: opts.workflowName});
  392. return this.jiraClient.makeRequest(options, callback);
  393. };
  394. /**
  395. * Update the draft scheme to include the passed mapping. The body is a representation of the workflow mapping.
  396. * Values not passed are assumed to indicate no change for that field.
  397. *
  398. * @method editDraftWorkflow
  399. * @memberOf WorkflowSchemeClient#
  400. * @param {Object} opts The request options sent to the Jira API
  401. * @param {number} opts.workflowSchemeId The id of the workflow scheme.
  402. * @param {string} opts.workflowName The name of the workflow.
  403. * @param {Array} opts.issueTypes The new issue types to inclue in the workflow.
  404. * See {@link https://docs.atlassian.com/jira/REST/latest/#d2e2670 }
  405. * @param [callback] Called when the workflow has been edited.
  406. * @return {Promise} Resolved when the workflow has been edited.
  407. */
  408. this.editDraftWorkflow = function (opts, callback) {
  409. var options = this.buildRequestOptions(opts, '/draft/workflow', 'PUT', {
  410. workflow: opts.workflowName,
  411. issueTypes: opts.issueTypes
  412. }, {workflowName: opts.workflowName});
  413. return this.jiraClient.makeRequest(options, callback);
  414. };
  415. /**
  416. * Delete the passed workflow from the workflow scheme.
  417. *
  418. * @method deleteWorkflow
  419. * @memberOf WorkflowSchemeClient#
  420. * @param {Object} opts The request options sent to the Jira API
  421. * @param {number} opts.workflowSchemeId The id of the workflow scheme.
  422. * @param {string} opts.workflowName The name of the workflow.
  423. * @param [callback] Called when the workflow has been edited.
  424. * @return {Promise} Resolved when the workflow has been edited.
  425. */
  426. this.deleteWorkflow = function (opts, callback) {
  427. var options = this.buildRequestOptions(opts, '/workflow', 'DELETE', null, {workflowName: opts.workflowName});
  428. return this.jiraClient.makeRequest(options, callback);
  429. };
  430. /**
  431. * Delete the passed workflow from the workflow draft scheme.
  432. *
  433. * @method deleteDraftWorkflow
  434. * @memberOf WorkflowSchemeClient#
  435. * @param {Object} opts The request options sent to the Jira API
  436. * @param {number} opts.workflowSchemeId The id of the workflow scheme.
  437. * @param {string} opts.workflowName The name of the workflow.
  438. * @param [callback] Called when the workflow has been edited.
  439. * @return {Promise} Resolved when the workflow has been edited.
  440. */
  441. this.deleteDraftWorkflow = function (opts, callback) {
  442. var options = this.buildRequestOptions(opts, '/draft/workflow', 'DELETE', null, {workflowName: opts.workflowName});
  443. return this.jiraClient.makeRequest(options, callback);
  444. };
  445. /**
  446. * Build out the request options necessary to make a particular API call.
  447. *
  448. * @private
  449. * @method buildRequestOptions
  450. * @memberOf WorkflowSchemeClient#
  451. * @param {Object} opts The arguments passed to the method.
  452. * @param {number} opts.workflowSchemeId The id of the workflowScheme to use in the path.
  453. * @param {Array} [opts.fields] The fields to include
  454. * @param {Array} [opts.expand] The fields to expand
  455. * @param {string} path The path of the endpoint following /workflowScheme/{id}
  456. * @param {string} method The request method.
  457. * @param {Object} [body] The request body, if any.
  458. * @param {Object} [qs] The querystring, if any. opts.expand and opts.fields arrays will be automagically added.
  459. * @returns {{uri: string, method: string, body: Object, qs: Object, followAllRedirects: boolean, json: boolean}}
  460. */
  461. this.buildRequestOptions = function (opts, path, method, body, qs) {
  462. var basePath = '/workflowscheme/' + opts.workflowSchemeId;
  463. if (!qs) qs = {};
  464. if (!body) body = {};
  465. if (opts.fields) {
  466. qs.fields = '';
  467. opts.fields.forEach(function (field) {
  468. qs.fields += field + ','
  469. });
  470. qs.fields = qs.fields.slice(0, -1);
  471. }
  472. if (opts.expand) {
  473. qs.expand = '';
  474. opts.expand.forEach(function (ex) {
  475. qs.expand += ex + ','
  476. });
  477. qs.expand = qs.expand.slice(0, -1);
  478. }
  479. return {
  480. uri: this.jiraClient.buildURL(basePath + path),
  481. method: method,
  482. body: body,
  483. qs: qs,
  484. followAllRedirects: true,
  485. json: true
  486. };
  487. };
  488. }