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 Merging class for the Joomla Platform.
14 *
15 * @documentation https://developer.github.com/v3/repos/merging
16 *
17 * @since 11.3
18 * @deprecated 4.0 Use the `joomla/github` package via Composer instead
19 */
20 class JGithubPackageRepositoriesMerging extends JGithubPackage
21 {
22 /**
23 * Perform a merge.
24 *
25 * @param string $owner The name of the owner of the GitHub repository.
26 * @param string $repo The name of the GitHub repository.
27 * @param string $base The name of the base branch that the head will be merged into.
28 * @param string $head The head to merge. This can be a branch name or a commit SHA1.
29 * @param string $commit_message Commit message to use for the merge commit.
30 * If omitted, a default message will be used.
31 *
32 * @throws UnexpectedValueException
33 * @since 12.4
34 *
35 * @return boolean
36 */
37 public function perform($owner, $repo, $base, $head, $commit_message = '')
38 {
39 // Build the request path.
40 $path = '/repos/' . $owner . '/' . $repo . '/merges';
41
42 $data = new stdClass;
43
44 $data->base = $base;
45 $data->head = $head;
46
47 if ($commit_message)
48 {
49 $data->commit_message = $commit_message;
50 }
51
52 // Send the request.
53 $response = $this->client->post($this->fetchUrl($path), json_encode($data));
54
55 switch ($response->code)
56 {
57 case '201':
58 // Success
59 return json_decode($response->body);
60 break;
61
62 case '204':
63 // No-op response (base already contains the head, nothing to merge)
64 throw new UnexpectedValueException('Nothing to merge');
65 break;
66
67 case '404':
68 // Missing base or Missing head response
69 $error = json_decode($response->body);
70
71 $message = (isset($error->message)) ? $error->message : 'Missing base or head: ' . $response->code;
72
73 throw new UnexpectedValueException($message);
74 break;
75
76 case '409':
77 // Merge conflict response
78 $error = json_decode($response->body);
79
80 $message = (isset($error->message)) ? $error->message : 'Merge conflict ' . $response->code;
81
82 throw new UnexpectedValueException($message);
83 break;
84
85 default :
86 throw new UnexpectedValueException('Unexpected response code: ' . $response->code);
87 break;
88 }
89 }
90 }
91