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 Repositories Comments class for the Joomla Platform.
 14  *
 15  * @documentation https://developer.github.com/v3/repos/comments
 16  *
 17  * @since       11.3
 18  * @deprecated  4.0  Use the `joomla/github` package via Composer instead
 19  */
 20 class JGithubPackageRepositoriesComments extends JGithubPackage
 21 {
 22     /**
 23      * Method to get a list of commit comments for a repository.
 24      *
 25      * @param   string   $user   The name of the owner of the GitHub repository.
 26      * @param   string   $repo   The name of the GitHub repository.
 27      * @param   integer  $page   Page to request
 28      * @param   integer  $limit  Number of results to return per page
 29      *
 30      * @return  array
 31      *
 32      * @since   12.1
 33      */
 34     public function getListRepository($user, $repo, $page = 0, $limit = 0)
 35     {
 36         // Build the request path.
 37         $path = '/repos/' . $user . '/' . $repo . '/comments';
 38 
 39         // Send the request.
 40         return $this->processResponse(
 41             $this->client->get($this->fetchUrl($path, $page, $limit))
 42         );
 43     }
 44 
 45     /**
 46      * Method to get a list of comments for a single commit for a repository.
 47      *
 48      * @param   string   $user   The name of the owner of the GitHub repository.
 49      * @param   string   $repo   The name of the GitHub repository.
 50      * @param   string   $sha    The SHA of the commit to retrieve.
 51      * @param   integer  $page   Page to request
 52      * @param   integer  $limit  Number of results to return per page
 53      *
 54      * @return  array
 55      *
 56      * @since   12.1
 57      */
 58     public function getList($user, $repo, $sha, $page = 0, $limit = 0)
 59     {
 60         // Build the request path.
 61         $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
 62 
 63         // Send the request.
 64         return $this->processResponse(
 65             $this->client->get($this->fetchUrl($path, $page, $limit))
 66         );
 67     }
 68 
 69     /**
 70      * Method to get a single comment on a commit.
 71      *
 72      * @param   string   $user  The name of the owner of the GitHub repository.
 73      * @param   string   $repo  The name of the GitHub repository.
 74      * @param   integer  $id    ID of the comment to retrieve
 75      *
 76      * @return  array
 77      *
 78      * @since   12.1
 79      */
 80     public function get($user, $repo, $id)
 81     {
 82         // Build the request path.
 83         $path = '/repos/' . $user . '/' . $repo . '/comments/' . (int) $id;
 84 
 85         // Send the request.
 86         return $this->processResponse(
 87             $this->client->get($this->fetchUrl($path))
 88         );
 89     }
 90 
 91     /**
 92      * Method to edit a comment on a commit.
 93      *
 94      * @param   string  $user     The name of the owner of the GitHub repository.
 95      * @param   string  $repo     The name of the GitHub repository.
 96      * @param   string  $id       The ID of the comment to edit.
 97      * @param   string  $comment  The text of the comment.
 98      *
 99      * @return  object
100      *
101      * @since   12.1
102      */
103     public function edit($user, $repo, $id, $comment)
104     {
105         // Build the request path.
106         $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
107 
108         $data = json_encode(
109             array(
110                 'body' => $comment,
111             )
112         );
113 
114         // Send the request.
115         return $this->processResponse(
116             $this->client->patch($this->fetchUrl($path), $data)
117         );
118     }
119 
120     /**
121      * Method to delete a comment on a commit.
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   string  $id    The ID of the comment to edit.
126      *
127      * @return  object
128      *
129      * @since   12.1
130      */
131     public function delete($user, $repo, $id)
132     {
133         // Build the request path.
134         $path = '/repos/' . $user . '/' . $repo . '/comments/' . $id;
135 
136         // Send the request.
137         return $this->processResponse(
138             $this->client->delete($this->fetchUrl($path)),
139             204
140         );
141     }
142 
143     /**
144      * Method to create a comment on a commit.
145      *
146      * @param   string   $user      The name of the owner of the GitHub repository.
147      * @param   string   $repo      The name of the GitHub repository.
148      * @param   string   $sha       The SHA of the commit to comment on.
149      * @param   string   $comment   The text of the comment.
150      * @param   integer  $line      The line number of the commit to comment on.
151      * @param   string   $filepath  A relative path to the file to comment on within the commit.
152      * @param   integer  $position  Line index in the diff to comment on.
153      *
154      * @return  object
155      *
156      * @since   12.1
157      */
158     public function create($user, $repo, $sha, $comment, $line, $filepath, $position)
159     {
160         // Build the request path.
161         $path = '/repos/' . $user . '/' . $repo . '/commits/' . $sha . '/comments';
162 
163         $data = json_encode(
164             array(
165                 'body' => $comment,
166                 'path' => $filepath,
167                 'position' => (int) $position,
168                 'line' => (int) $line,
169             )
170         );
171 
172         // Send the request.
173         return $this->processResponse(
174             $this->client->post($this->fetchUrl($path), $data),
175             201
176         );
177     }
178 }
179