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 References class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/git/refs/
16 *
17 * @since 11.3
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageDataRefs extends JGithubPackage
21 {
22 /**
23 * Method to get a reference.
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 string $ref The reference to get.
28 *
29 * @return object
30 *
31 * @since 11.3
32 */
33 public function get($user, $repo, $ref)
34 {
35 // Build the request path.
36 $path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref;
37
38 // Send the request.
39 $response = $this->client->get($this->fetchUrl($path));
40
41 // Validate the response code.
42 if ($response->code != 200)
43 {
44 // Decode the error response and throw an exception.
45 $error = json_decode($response->body);
46 throw new DomainException($error->message, $response->code);
47 }
48
49 return json_decode($response->body);
50 }
51
52 /**
53 * Method to list references for a repository.
54 *
55 * @param string $user The name of the owner of the GitHub repository.
56 * @param string $repo The name of the GitHub repository.
57 * @param string $namespace Optional sub-namespace to limit the returned references.
58 * @param integer $page Page to request
59 * @param integer $limit Number of results to return per page
60 *
61 * @return array
62 *
63 * @since 11.3
64 */
65 public function getList($user, $repo, $namespace = '', $page = 0, $limit = 0)
66 {
67 // Build the request path.
68 $path = '/repos/' . $user . '/' . $repo . '/git/refs' . $namespace;
69
70 // Send the request.
71 $response = $this->client->get($this->fetchUrl($path, $page, $limit));
72
73 // Validate the response code.
74 if ($response->code != 200)
75 {
76 // Decode the error response and throw an exception.
77 $error = json_decode($response->body);
78 throw new DomainException($error->message, $response->code);
79 }
80
81 return json_decode($response->body);
82 }
83
84 /**
85 * Method to create a ref.
86 *
87 * @param string $user The name of the owner of the GitHub repository.
88 * @param string $repo The name of the GitHub repository.
89 * @param string $ref The name of the fully qualified reference.
90 * @param string $sha The SHA1 value to set this reference to.
91 *
92 * @throws DomainException
93 * @since 11.3
94 *
95 * @return object
96 */
97 public function create($user, $repo, $ref, $sha)
98 {
99 // Build the request path.
100 $path = '/repos/' . $user . '/' . $repo . '/git/refs';
101
102 // Build the request data.
103 $data = json_encode(
104 array(
105 'ref' => $ref,
106 'sha' => $sha,
107 )
108 );
109
110 // Send the request.
111 $response = $this->client->post($this->fetchUrl($path), $data);
112
113 // Validate the response code.
114 if ($response->code != 201)
115 {
116 // Decode the error response and throw an exception.
117 $error = json_decode($response->body);
118 throw new DomainException($error->message, $response->code);
119 }
120
121 return json_decode($response->body);
122 }
123
124 /**
125 * Method to update a reference.
126 *
127 * @param string $user The name of the owner of the GitHub repository.
128 * @param string $repo The name of the GitHub repository.
129 * @param string $ref The reference to update.
130 * @param string $sha The SHA1 value to set the reference to.
131 * @param boolean $force Whether the update should be forced. Default to false.
132 *
133 * @throws DomainException
134 * @since 11.3
135 *
136 * @return object
137 */
138 public function edit($user, $repo, $ref, $sha, $force = false)
139 {
140 // Build the request path.
141 $path = '/repos/' . $user . '/' . $repo . '/git/refs/' . $ref;
142
143 // Craete the data object.
144 $data = new stdClass;
145
146 // If a title is set add it to the data object.
147 if ($force)
148 {
149 $data->force = true;
150 }
151
152 $data->sha = $sha;
153
154 // Encode the request data.
155 $data = json_encode($data);
156
157 // Send the request.
158 $response = $this->client->patch($this->fetchUrl($path), $data);
159
160 // Validate the response code.
161 if ($response->code != 200)
162 {
163 // Decode the error response and throw an exception.
164 $error = json_decode($response->body);
165 throw new DomainException($error->message, $response->code);
166 }
167
168 return json_decode($response->body);
169 }
170
171 /**
172 * Delete a Reference
173 *
174 * @param string $owner The name of the owner of the GitHub repository.
175 * @param string $repo The name of the GitHub repository.
176 * @param string $ref The reference to update.
177 *
178 * @since 3.3 (CMS)
179 * @return object
180 */
181 public function delete($owner, $repo, $ref)
182 {
183 // Build the request path.
184 $path = '/repos/' . $owner . '/' . $repo . '/git/refs/' . $ref;
185
186 return $this->processResponse(
187 $this->client->delete($this->fetchUrl($path)),
188 204
189 );
190 }
191 }
192