1
|
|
|
<?php |
2
|
|
|
namespace FlexyProject\GitHub\Receiver\Issues; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The Trees API class provides access to Issues's labels. |
8
|
|
|
* |
9
|
|
|
* @link https://developer.github.com/v3/issues/labels/ |
10
|
|
|
* @package FlexyProject\GitHub\Receiver\Issues |
11
|
|
|
*/ |
12
|
|
|
class Labels extends AbstractIssues |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* List all labels for this repository |
17
|
|
|
* |
18
|
|
|
* @link https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository |
19
|
|
|
* @return array |
20
|
|
|
*/ |
21
|
|
|
public function listRepositoryLabels(): array |
22
|
|
|
{ |
23
|
|
|
return $this->getApi()->request($this->getApi() |
24
|
|
|
->sprintf('/repos/:owner/:repo/labels', $this->getIssues()->getOwner(), |
25
|
|
|
$this->getIssues()->getRepo())); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get a single label |
30
|
|
|
* |
31
|
|
|
* @link https://developer.github.com/v3/issues/labels/#get-a-single-label |
32
|
|
|
* |
33
|
|
|
* @param string $name |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getLabel(string $name): array |
38
|
|
|
{ |
39
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/labels/:name', |
40
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $name)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a label |
45
|
|
|
* |
46
|
|
|
* @link https://developer.github.com/v3/issues/labels/#create-a-label |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @param string $color |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function createLabel(string $name, string $color): array |
54
|
|
|
{ |
55
|
|
|
return $this->getApi()->request($this->getApi() |
56
|
|
|
->sprintf('/repos/:owner/:repo/labels', $this->getIssues()->getOwner(), |
57
|
|
|
$this->getIssues()->getRepo()), Request::METHOD_POST, [ |
58
|
|
|
'name' => $name, |
59
|
|
|
'color' => $color |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Update a label |
65
|
|
|
* |
66
|
|
|
* @link https://developer.github.com/v3/issues/labels/#update-a-label |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @param string $color |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function updateLabel(string $name, string $color): array |
74
|
|
|
{ |
75
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/labels/:name', |
76
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $name), Request::METHOD_PATCH, [ |
77
|
|
|
'color' => $color |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete a label |
83
|
|
|
* |
84
|
|
|
* @link https://developer.github.com/v3/issues/labels/#delete-a-label |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function deleteLabel(string $name): bool |
91
|
|
|
{ |
92
|
|
|
$this->getApi()->request($this->getApi() |
93
|
|
|
->sprintf('/repos/:owner/:repo/labels/:name', $this->getIssues()->getOwner(), |
94
|
|
|
$this->getIssues()->getRepo(), $name)); |
95
|
|
|
|
96
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* List labels on an issue |
105
|
|
|
* |
106
|
|
|
* @link https://developer.github.com/v3/issues/labels/#list-labels-on-an-issue |
107
|
|
|
* |
108
|
|
|
* @param int $number |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function listIssueLabels(int $number): array |
113
|
|
|
{ |
114
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/issues/:number/labels', |
115
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number)); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Add labels to an issue |
120
|
|
|
* |
121
|
|
|
* @link https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue |
122
|
|
|
* |
123
|
|
|
* @param int $number |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function addIssueLabels(int $number): array |
128
|
|
|
{ |
129
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/issues/:number/labels', |
130
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number), Request::METHOD_POST); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Remove a label from an issue |
135
|
|
|
* |
136
|
|
|
* @link https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue |
137
|
|
|
* |
138
|
|
|
* @param int $number |
139
|
|
|
* @param string $name |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
View Code Duplication |
public function removeIssueLabel(int $number, string $name): bool |
|
|
|
|
144
|
|
|
{ |
145
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/issues/:number/labels/:name', |
146
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number, $name), Request::METHOD_DELETE); |
147
|
|
|
|
148
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
149
|
|
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Replace all labels for an issue |
157
|
|
|
* |
158
|
|
|
* @link https://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue |
159
|
|
|
* |
160
|
|
|
* @param int $number |
161
|
|
|
* |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public function replaceIssuesLabels(int $number): array |
165
|
|
|
{ |
166
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/issues/:number/labels', |
167
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number), Request::METHOD_PUT); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Remove all labels from an issue |
172
|
|
|
* |
173
|
|
|
* @link https://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue |
174
|
|
|
* |
175
|
|
|
* @param int $number |
176
|
|
|
* |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function removeIssueLabels(int $number): bool |
180
|
|
|
{ |
181
|
|
|
$this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/issues/:number/labels', |
182
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number), Request::METHOD_DELETE); |
183
|
|
|
|
184
|
|
|
if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { |
185
|
|
|
return true; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get labels for every issue in a milestone |
193
|
|
|
* |
194
|
|
|
* @link https://developer.github.com/v3/issues/labels/#get-labels-for-every-issue-in-a-milestone |
195
|
|
|
* |
196
|
|
|
* @param int $number |
197
|
|
|
* |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function getIssueLabelsInMilestone(int $number): array |
201
|
|
|
{ |
202
|
|
|
return $this->getApi()->request($this->getApi()->sprintf('/repos/:owner/:repo/milestones/:number/labels', |
203
|
|
|
$this->getIssues()->getOwner(), $this->getIssues()->getRepo(), $number)); |
204
|
|
|
} |
205
|
|
|
} |
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.