1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage GitHub
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * GitHub API Comments class for the Joomla Platform.
14 *
15 * The Issue Comments API supports listing, viewing, editing, and creating comments
16 * on issues and pull requests.
17 *
18 * @documentation https://developer.github.com/v3/issues/comments/
19 *
20 * @since 12.3
21 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
22 */
23 class JGithubPackageIssuesComments extends JGithubPackage
24 {
25 /**
26 * Method to get the list of comments on an issue.
27 *
28 * @param string $owner The name of the owner of the GitHub repository.
29 * @param string $repo The name of the GitHub repository.
30 * @param integer $issueId The issue number.
31 * @param integer $page The page number from which to get items.
32 * @param integer $limit The number of items on a page.
33 *
34 * @throws DomainException
35 * @since 11.3
36 *
37 * @return array
38 */
39 public function getList($owner, $repo, $issueId, $page = 0, $limit = 0)
40 {
41 // Build the request path.
42 $path = '/repos/' . $owner . '/' . $repo . '/issues/' . (int) $issueId . '/comments';
43
44 // Send the request.
45 return $this->processResponse(
46 $this->client->get($this->fetchUrl($path, $page, $limit))
47 );
48 }
49
50 /**
51 * Method to get the list of comments in a repository.
52 *
53 * @param string $owner The name of the owner of the GitHub repository.
54 * @param string $repo The name of the GitHub repository.
55 * @param string $sort The sort field - created or updated.
56 * @param string $direction The sort order- asc or desc. Ignored without sort parameter.
57 * @param JDate $since A timestamp in ISO 8601 format.
58 *
59 * @throws UnexpectedValueException
60 * @throws DomainException
61 * @since 11.3
62 *
63 * @return array
64 */
65 public function getRepositoryList($owner, $repo, $sort = 'created', $direction = 'asc', JDate $since = null)
66 {
67 // Build the request path.
68 $path = '/repos/' . $owner . '/' . $repo . '/issues/comments';
69
70 if (false == in_array($sort, array('created', 'updated')))
71 {
72 throw new UnexpectedValueException(
73 sprintf(
74 '%1$s - sort field must be "created" or "updated"', __METHOD__
75 )
76 );
77 }
78
79 if (false == in_array($direction, array('asc', 'desc')))
80 {
81 throw new UnexpectedValueException(
82 sprintf(
83 '%1$s - direction field must be "asc" or "desc"', __METHOD__
84 )
85 );
86 }
87
88 $path .= '?sort=' . $sort;
89 $path .= '&direction=' . $direction;
90
91 if ($since)
92 {
93 $path .= '&since=' . $since->toISO8601();
94 }
95
96 // Send the request.
97 return $this->processResponse($this->client->get($this->fetchUrl($path)));
98 }
99
100 /**
101 * Method to get a single comment.
102 *
103 * @param string $owner The name of the owner of the GitHub repository.
104 * @param string $repo The name of the GitHub repository.
105 * @param integer $id The comment id.
106 *
107 * @return mixed
108 */
109 public function get($owner, $repo, $id)
110 {
111 // Build the request path.
112 $path = '/repos/' . $owner . '/' . $repo . '/issues/comments/' . (int) $id;
113
114 // Send the request.
115 return $this->processResponse(
116 $this->client->get($this->fetchUrl($path))
117 );
118 }
119
120 /**
121 * Method to update a comment on an issue.
122 *
123 * @param string $user The name of the owner of the GitHub repository.
124 * @param string $repo The name of the GitHub repository.
125 * @param integer $commentId The id of the comment to update.
126 * @param string $body The new body text for the comment.
127 *
128 * @since 11.3
129 * @throws DomainException
130 *
131 * @return object
132 */
133 public function edit($user, $repo, $commentId, $body)
134 {
135 // Build the request path.
136 $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId;
137
138 // Build the request data.
139 $data = json_encode(
140 array(
141 'body' => $body,
142 )
143 );
144
145 // Send the request.
146 return $this->processResponse(
147 $this->client->patch($this->fetchUrl($path), $data)
148 );
149 }
150
151 /**
152 * Method to create a comment on an issue.
153 *
154 * @param string $user The name of the owner of the GitHub repository.
155 * @param string $repo The name of the GitHub repository.
156 * @param integer $issueId The issue number.
157 * @param string $body The comment body text.
158 *
159 * @throws DomainException
160 * @since 11.3
161 *
162 * @return object
163 */
164 public function create($user, $repo, $issueId, $body)
165 {
166 // Build the request path.
167 $path = '/repos/' . $user . '/' . $repo . '/issues/' . (int) $issueId . '/comments';
168
169 // Build the request data.
170 $data = json_encode(
171 array(
172 'body' => $body,
173 )
174 );
175
176 // Send the request.
177 return $this->processResponse(
178 $this->client->post($this->fetchUrl($path), $data),
179 201
180 );
181 }
182
183 /**
184 * Method to delete a comment on an issue.
185 *
186 * @param string $user The name of the owner of the GitHub repository.
187 * @param string $repo The name of the GitHub repository.
188 * @param integer $commentId The id of the comment to delete.
189 *
190 * @throws DomainException
191 * @since 11.3
192 *
193 * @return boolean
194 */
195 public function delete($user, $repo, $commentId)
196 {
197 // Build the request path.
198 $path = '/repos/' . $user . '/' . $repo . '/issues/comments/' . (int) $commentId;
199
200 // Send the request.
201 $this->processResponse(
202 $this->client->delete($this->fetchUrl($path)),
203 204
204 );
205
206 return true;
207 }
208 }
209