Issue.js

  1. /**
  2. * @file
  3. * @copyright 2013 Michael Aufreiter (Development Seed) and 2016 Yahoo Inc.
  4. * @license Licensed under {@link https://spdx.org/licenses/BSD-3-Clause-Clear.html BSD-3-Clause-Clear}.
  5. * Github.js is freely distributable.
  6. */
  7. import Requestable from './Requestable';
  8. /**
  9. * Issue wraps the functionality to get issues for repositories
  10. */
  11. class Issue extends Requestable {
  12. /**
  13. * Create a new Issue
  14. * @param {string} repository - the full name of the repository (`:user/:repo`) to get issues for
  15. * @param {Requestable.auth} [auth] - information required to authenticate to Github
  16. * @param {string} [apiBase=https://api.github.com] - the base Github API URL
  17. */
  18. constructor(repository, auth, apiBase) {
  19. super(auth, apiBase);
  20. this.__repository = repository;
  21. }
  22. /**
  23. * Create a new issue
  24. * @see https://developer.github.com/v3/issues/#create-an-issue
  25. * @param {Object} issueData - the issue to create
  26. * @param {Requestable.callback} [cb] - will receive the created issue
  27. * @return {Promise} - the promise for the http request
  28. */
  29. createIssue(issueData, cb) {
  30. return this._request('POST', `/repos/${this.__repository}/issues`, issueData, cb);
  31. }
  32. /**
  33. * List the issues for the repository
  34. * @see https://developer.github.com/v3/issues/#list-issues-for-a-repository
  35. * @param {Object} options - filtering options
  36. * @param {Requestable.callback} [cb] - will receive the array of issues
  37. * @return {Promise} - the promise for the http request
  38. */
  39. listIssues(options, cb) {
  40. return this._requestAllPages(`/repos/${this.__repository}/issues`, options, cb);
  41. }
  42. /**
  43. * List the events for an issue
  44. * @see https://developer.github.com/v3/issues/events/#list-events-for-an-issue
  45. * @param {number} issue - the issue to get events for
  46. * @param {Requestable.callback} [cb] - will receive the list of events
  47. * @return {Promise} - the promise for the http request
  48. */
  49. listIssueEvents(issue, cb) {
  50. return this._request('GET', `/repos/${this.__repository}/issues/${issue}/events`, null, cb);
  51. }
  52. /**
  53. * List comments on an issue
  54. * @see https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
  55. * @param {number} issue - the id of the issue to get comments from
  56. * @param {Requestable.callback} [cb] - will receive the comments
  57. * @return {Promise} - the promise for the http request
  58. */
  59. listIssueComments(issue, cb) {
  60. return this._request('GET', `/repos/${this.__repository}/issues/${issue}/comments`, null, cb);
  61. }
  62. /**
  63. * Get a single comment on an issue
  64. * @see https://developer.github.com/v3/issues/comments/#get-a-single-comment
  65. * @param {number} id - the comment id to get
  66. * @param {Requestable.callback} [cb] - will receive the comment
  67. * @return {Promise} - the promise for the http request
  68. */
  69. getIssueComment(id, cb) {
  70. return this._request('GET', `/repos/${this.__repository}/issues/comments/${id}`, null, cb);
  71. }
  72. /**
  73. * Comment on an issue
  74. * @see https://developer.github.com/v3/issues/comments/#create-a-comment
  75. * @param {number} issue - the id of the issue to comment on
  76. * @param {string} comment - the comment to add
  77. * @param {Requestable.callback} [cb] - will receive the created comment
  78. * @return {Promise} - the promise for the http request
  79. */
  80. createIssueComment(issue, comment, cb) {
  81. return this._request('POST', `/repos/${this.__repository}/issues/${issue}/comments`, {body: comment}, cb);
  82. }
  83. /**
  84. * Edit a comment on an issue
  85. * @see https://developer.github.com/v3/issues/comments/#edit-a-comment
  86. * @param {number} id - the comment id to edit
  87. * @param {string} comment - the comment to edit
  88. * @param {Requestable.callback} [cb] - will receive the edited comment
  89. * @return {Promise} - the promise for the http request
  90. */
  91. editIssueComment(id, comment, cb) {
  92. return this._request('PATCH', `/repos/${this.__repository}/issues/comments/${id}`, {body: comment}, cb);
  93. }
  94. /**
  95. * Delete a comment on an issue
  96. * @see https://developer.github.com/v3/issues/comments/#delete-a-comment
  97. * @param {number} id - the comment id to delete
  98. * @param {Requestable.callback} [cb] - will receive true if the request is successful
  99. * @return {Promise} - the promise for the http request
  100. */
  101. deleteIssueComment(id, cb) {
  102. return this._request('DELETE', `/repos/${this.__repository}/issues/comments/${id}`, null, cb);
  103. }
  104. /**
  105. * Edit an issue
  106. * @see https://developer.github.com/v3/issues/#edit-an-issue
  107. * @param {number} issue - the issue number to edit
  108. * @param {Object} issueData - the new issue data
  109. * @param {Requestable.callback} [cb] - will receive the modified issue
  110. * @return {Promise} - the promise for the http request
  111. */
  112. editIssue(issue, issueData, cb) {
  113. return this._request('PATCH', `/repos/${this.__repository}/issues/${issue}`, issueData, cb);
  114. }
  115. /**
  116. * Get a particular issue
  117. * @see https://developer.github.com/v3/issues/#get-a-single-issue
  118. * @param {number} issue - the issue number to fetch
  119. * @param {Requestable.callback} [cb] - will receive the issue
  120. * @return {Promise} - the promise for the http request
  121. */
  122. getIssue(issue, cb) {
  123. return this._request('GET', `/repos/${this.__repository}/issues/${issue}`, null, cb);
  124. }
  125. /**
  126. * List the milestones for the repository
  127. * @see https://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
  128. * @param {Object} options - filtering options
  129. * @param {Requestable.callback} [cb] - will receive the array of milestones
  130. * @return {Promise} - the promise for the http request
  131. */
  132. listMilestones(options, cb) {
  133. return this._request('GET', `/repos/${this.__repository}/milestones`, options, cb);
  134. }
  135. /**
  136. * Get a milestone
  137. * @see https://developer.github.com/v3/issues/milestones/#get-a-single-milestone
  138. * @param {string} milestone - the id of the milestone to fetch
  139. * @param {Requestable.callback} [cb] - will receive the array of milestones
  140. * @return {Promise} - the promise for the http request
  141. */
  142. getMilestone(milestone, cb) {
  143. return this._request('GET', `/repos/${this.__repository}/milestones/${milestone}`, null, cb);
  144. }
  145. /**
  146. * Create a new milestone
  147. * @see https://developer.github.com/v3/issues/milestones/#create-a-milestone
  148. * @param {Object} milestoneData - the milestone definition
  149. * @param {Requestable.callback} [cb] - will receive the array of milestones
  150. * @return {Promise} - the promise for the http request
  151. */
  152. createMilestone(milestoneData, cb) {
  153. return this._request('POST', `/repos/${this.__repository}/milestones`, milestoneData, cb);
  154. }
  155. /**
  156. * Edit a milestone
  157. * @see https://developer.github.com/v3/issues/milestones/#update-a-milestone
  158. * @param {string} milestone - the id of the milestone to edit
  159. * @param {Object} milestoneData - the updates to make to the milestone
  160. * @param {Requestable.callback} [cb] - will receive the array of milestones
  161. * @return {Promise} - the promise for the http request
  162. */
  163. editMilestone(milestone, milestoneData, cb) {
  164. return this._request('PATCH', `/repos/${this.__repository}/milestones/${milestone}`, milestoneData, cb);
  165. }
  166. /**
  167. * Delete a milestone (this is distinct from closing a milestone)
  168. * @see https://developer.github.com/v3/issues/milestones/#delete-a-milestone
  169. * @param {string} milestone - the id of the milestone to delete
  170. * @param {Requestable.callback} [cb] - will receive the array of milestones
  171. * @return {Promise} - the promise for the http request
  172. */
  173. deleteMilestone(milestone, cb) {
  174. return this._request('DELETE', `/repos/${this.__repository}/milestones/${milestone}`, null, cb);
  175. }
  176. }
  177. module.exports = Issue;