1
|
|
|
<?php |
2
|
|
|
namespace FlexyProject\GitHub\Receiver; |
3
|
|
|
|
4
|
|
|
use FlexyProject\GitHub\AbstractApi; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* This class give you access to the Pull Request API who allows you to list, view, edit, create, and even merge pull |
9
|
|
|
* requests. |
10
|
|
|
* |
11
|
|
|
* @link https://developer.github.com/v3/pulls/ |
12
|
|
|
* @package FlexyProject\GitHub\Receiver |
13
|
|
|
*/ |
14
|
|
|
class PullRequests extends AbstractReceiver |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** Available sub-Receiver */ |
18
|
|
|
const REVIEW_COMMENTS = 'ReviewComments'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* List pull requests |
22
|
|
|
* |
23
|
|
|
* @link https://developer.github.com/v3/pulls/#list-pull-requests |
24
|
|
|
* |
25
|
|
|
* @param string $state |
26
|
|
|
* @param string $head |
27
|
|
|
* @param string $base |
28
|
|
|
* @param string $sort |
29
|
|
|
* @param string $direction |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
* @throws \Exception |
33
|
|
|
*/ |
34
|
|
|
public function listPullRequests(string $state = AbstractApi::STATE_OPEN, string $head = null, string $base = null, |
35
|
|
|
string $sort = AbstractApi::SORT_CREATED, |
36
|
|
|
string $direction = AbstractApi::DIRECTION_ASC): array |
37
|
|
|
{ |
38
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls?:args', $this->getOwner(), |
39
|
|
|
$this->getRepo(), http_build_query([ |
40
|
|
|
'state' => $state, |
41
|
|
|
'head' => $head, |
42
|
|
|
'base' => $base, |
43
|
|
|
'sort' => $sort, |
44
|
|
|
'direction' => $direction |
45
|
|
|
]))); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get a single pull request |
50
|
|
|
* |
51
|
|
|
* @link https://developer.github.com/v3/pulls/#get-a-single-pull-request |
52
|
|
|
* |
53
|
|
|
* @param int $number |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
* @throws \Exception |
57
|
|
|
*/ |
58
|
|
|
public function getSinglePullRequest(int $number): array |
59
|
|
|
{ |
60
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
61
|
|
|
$this->getRepo(), $number)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a pull request |
66
|
|
|
* |
67
|
|
|
* @link https://developer.github.com/v3/pulls/#create-a-pull-request |
68
|
|
|
* |
69
|
|
|
* @param string $title |
70
|
|
|
* @param string $head |
71
|
|
|
* @param string $base |
72
|
|
|
* @param string $body |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* @throws \Exception |
76
|
|
|
*/ |
77
|
|
|
public function createPullrequest(string $title, string $head, string $base, string $body): array |
78
|
|
|
{ |
79
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls', $this->getOwner(), |
80
|
|
|
$this->getRepo()), Request::METHOD_POST, [ |
81
|
|
|
'title' => $title, |
82
|
|
|
'head' => $head, |
83
|
|
|
'base' => $base, |
84
|
|
|
'body' => $body |
85
|
|
|
]); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Update a pull request |
90
|
|
|
* |
91
|
|
|
* @link https://developer.github.com/v3/pulls/#update-a-pull-request |
92
|
|
|
* |
93
|
|
|
* @param int $number |
94
|
|
|
* @param string $title |
95
|
|
|
* @param string $body |
96
|
|
|
* @param string $state |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function updatePullRequest(int $number, string $title = null, string $body = null, |
|
|
|
|
102
|
|
|
string $state = null): array |
103
|
|
|
{ |
104
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number', $this->getOwner(), |
105
|
|
|
$this->getRepo(), $number), Request::METHOD_PATCH, [ |
106
|
|
|
'title' => $title, |
107
|
|
|
'body' => $body, |
108
|
|
|
'state' => $state |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* List commits on a pull request |
114
|
|
|
* |
115
|
|
|
* @link https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request |
116
|
|
|
* |
117
|
|
|
* @param int $number |
118
|
|
|
* |
119
|
|
|
* @return array |
120
|
|
|
* @throws \Exception |
121
|
|
|
*/ |
122
|
|
|
public function listCommits(int $number): array |
123
|
|
|
{ |
124
|
|
|
return $this->getApi()->request($this->getApi() |
125
|
|
|
->sprintf('/repos/:owner/:repo/pulls/:number/commits', $this->getOwner(), |
126
|
|
|
$this->getRepo(), $number)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* List pull requests files |
131
|
|
|
* |
132
|
|
|
* @link https://developer.github.com/v3/pulls/#list-pull-requests-files |
133
|
|
|
* |
134
|
|
|
* @param int $number |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
* @throws \Exception |
138
|
|
|
*/ |
139
|
|
|
public function listPullRequestsFiles(int $number): array |
140
|
|
|
{ |
141
|
|
|
return $this->getApi()->request($this->getApi() |
142
|
|
|
->sprintf('/repos/:owner/:repo/pulls/:number/files', $this->getOwner(), |
143
|
|
|
$this->getRepo(), $number)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get if a pull request has been merged |
148
|
|
|
* |
149
|
|
|
* @link https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged |
150
|
|
|
* |
151
|
|
|
* @param int $number |
152
|
|
|
* |
153
|
|
|
* @return bool |
154
|
|
|
* @throws \Exception |
155
|
|
|
*/ |
156
|
|
|
public function checkPullRequestsMerged(int $number): bool |
157
|
|
|
{ |
158
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
159
|
|
|
$this->getRepo(), $number)); |
160
|
|
|
|
161
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
162
|
|
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Merge a pull request (Merge Button) |
170
|
|
|
* |
171
|
|
|
* @link https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-button |
172
|
|
|
* |
173
|
|
|
* @param int $number |
174
|
|
|
* @param string $commitMessage |
175
|
|
|
* @param string $sha |
176
|
|
|
* |
177
|
|
|
* @return array |
178
|
|
|
* @throws \Exception |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function mergePullRequest(int $number, string $commitMessage = null, string $sha = null): array |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
return $this->getApi()->request($this->getApi() |
183
|
|
|
->sprintf('/repos/:owner/:repo/pulls/:number/merge', $this->getOwner(), |
184
|
|
|
$this->getRepo(), $number), Request::METHOD_PUT, [ |
185
|
|
|
'commit_message' => $commitMessage, |
186
|
|
|
'sha' => $sha |
187
|
|
|
]); |
188
|
|
|
} |
189
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.