Repository: jira-node/node-jira-client
Pull request #163 (2017-04-02 09:36:16)

Add field option API suite
Add fieldOption APIs base on [jira docs](https://docs.atlassian.com/jira/REST/cloud/)

P/s: How often do you push new versions to npm?


Patch:
diff --git a/src/jira.js b/src/jira.js
index dd57f1e..92de843 100644
--- a/src/jira.js
+++ b/src/jira.js
@@ -685,6 +685,22 @@ export default class JiraApi {
     }));
   }
 
+  /** Create custom Jira field
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field-createCustomField)
+   * @name createCustomField
+   * @function
+   * @param {object} field - Properly formatted Field object
+   */
+  createCustomField(field) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: '/field',
+    }), {
+      method: 'POST',
+      followAllRedirects: true,
+      body: field,
+    }));
+  }
+
   /** List all fields custom and not that jira knows about.
    * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290489)
    * @name listFields
@@ -696,6 +712,82 @@ export default class JiraApi {
     })));
   }
 
+   /** Add an option for a select list issue field.
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field/{fieldKey}/option-createOption)
+   * @name createFieldOption
+   * @function
+   * @param {string} fieldKey - the key of the select list field
+   * @param {object} option - properly formatted Option object
+   */
+  createFieldOption(fieldKey, option) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: `/field/${fieldKey}/option`,
+    }), {
+      method: 'POST',
+      followAllRedirects: true,
+      body: option,
+    }));
+  }
+
+  /** Returns all options defined for a select list issue field.
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field/{fieldKey}/option-getAllOptions)
+   * @name listFieldOptions
+   * @function
+   * @param {string} fieldKey - the key of the select list field
+   */
+  listFieldOptions(fieldKey) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: `/field/${fieldKey}/option`,
+    })));
+  }
+
+  /** Creates or updates an option for a select list issue field.
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field/{fieldKey}/option-putOption)
+   * @name upsertFieldOption
+   * @function
+   * @param {string} fieldKey - the key of the select list field
+   * @param {string} optionId - the id of the modified option
+   * @param {object} option - properly formatted Option object
+   */
+  upsertFieldOption(fieldKey, optionId, option) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: `/field/${fieldKey}/option/${optionId}`,
+    }), {
+      method: 'PUT',
+      followAllRedirects: true,
+      body: option,
+    }));
+  }
+
+  /** Returns an option for a select list issue field.
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field/{fieldKey}/option-getOption)
+   * @name getFieldOption
+   * @function
+   * @param {string} fieldKey - the key of the select list field
+   * @param {string} optionId - the id of the option
+   */
+  getFieldOption(fieldKey, optionId) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: `/field/${fieldKey}/option/${optionId}`,
+    })));
+  }
+
+  /** Deletes an option from a select list issue field.
+   * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#api/2/field/{fieldKey}/option-delete)
+   * @name deleteFieldOption
+   * @function
+   * @param {string} fieldKey - the key of the select list field
+   * @param {string} optionId - the id of the deleted option
+   */
+  deleteFieldOption(fieldKey, optionId) {
+    return this.doRequest(this.makeRequestHeader(this.makeUri({
+      pathname: `/field/${fieldKey}/option/${optionId}`,
+    }), {
+      method: 'DELETE',
+      followAllRedirects: true,
+    }));
+  }
+
   /** List all priorities jira knows about
    * [Jira Doc](http://docs.atlassian.com/jira/REST/latest/#id290489)
    * @name listPriorities


Test patch:
diff --git a/test/jira-tests.js b/test/jira-tests.js
index ac4ef29..2a9c744 100644
--- a/test/jira-tests.js
+++ b/test/jira-tests.js
@@ -539,9 +539,45 @@ describe('Jira API Tests', () => {
       result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/component/someComponentNumber');
     });
 
-    it('listFields hits proper url', async () => {
-      const result = await dummyURLCall('listFields', []);
-      result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field');
+    // Field APIs Suite Tests
+    describe('Field APIs Suite Tests', () => {
+      it('createCustomField hits proper url', async () => {
+        const result = await dummyURLCall('createCustomField', ['someField']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field');
+      });
+
+      it('listFields hits proper url', async () => {
+        const result = await dummyURLCall('listFields', []);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field');
+      });
+    });
+
+    // Field Option APIs Suite Tests
+    describe('Field Option APIs Suite Tests', () => {
+      it('createFieldOption hits proper url', async () => {
+        const result = await dummyURLCall('createFieldOption', ['someFieldKey', 'someOption']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field/someFieldKey/option');
+      });
+
+      it('listFieldOptions hits proper url', async () => {
+        const result = await dummyURLCall('listFieldOptions', ['someFieldKey']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field/someFieldKey/option');
+      });
+
+      it('upsertFieldOption hits proper url', async () => {
+        const result = await dummyURLCall('upsertFieldOption', ['someFieldKey', 'someOptionId', 'someOption']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field/someFieldKey/option/someOptionId');
+      });
+
+      it('getFieldOption hits proper url', async () => {
+        const result = await dummyURLCall('getFieldOption', ['someFieldKey', 'someOptionId']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field/someFieldKey/option/someOptionId');
+      });
+
+      it('deleteFieldOption hits proper url', async () => {
+        const result = await dummyURLCall('deleteFieldOption', ['someFieldKey', 'someOptionId']);
+        result.should.eql('http://jira.somehost.com:8080/rest/api/2.0/field/someFieldKey/option/someOptionId');
+      });
     });
 
     it('listPriorities hits proper url', async () => {