|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the bitbucket-api package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandru G. <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bitbucket\API\Repositories; |
|
13
|
|
|
|
|
14
|
|
|
use Bitbucket\API\Api; |
|
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Manage branch restrictions on a repository |
|
19
|
|
|
* |
|
20
|
|
|
* @author Alexandru G. <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class BranchRestrictions extends Api |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Allowed restrictions to create a new branch permission for |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $allowedRestrictionTypes = array( |
|
29
|
|
|
'require_tasks_to_be_completed', |
|
30
|
|
|
'require_passing_builds_to_merge', |
|
31
|
|
|
'force', |
|
32
|
|
|
'require_all_dependencies_merged', |
|
33
|
|
|
'push', |
|
34
|
|
|
'require_approvals_to_merge', |
|
35
|
|
|
'enforce_merge_checks', |
|
36
|
|
|
'restrict_merges', |
|
37
|
|
|
'reset_pullrequest_approvals_on_change', |
|
38
|
|
|
'delete' |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Get the information associated with a repository's branch restrictions |
|
43
|
|
|
* |
|
44
|
|
|
* @access public |
|
45
|
|
|
* @param string $account The team or individual account owning the repository. |
|
46
|
|
|
* @param string $repo The repository identifier. |
|
47
|
|
|
* @return ResponseInterface |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function all($account, $repo) |
|
50
|
|
|
{ |
|
51
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
|
52
|
1 |
|
sprintf('/repositories/%s/%s/branch-restrictions', $account, $repo) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Creates restrictions for the specified repository. |
|
58
|
|
|
* |
|
59
|
|
|
* @access public |
|
60
|
|
|
* @param string $account The team or individual account owning the repository. |
|
61
|
|
|
* @param string $repo The repository identifier. |
|
62
|
|
|
* @param array|string $params Additional parameters as array or JSON string |
|
63
|
|
|
* @return ResponseInterface |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \InvalidArgumentException |
|
66
|
|
|
*/ |
|
67
|
6 |
|
public function create($account, $repo, $params = array()) |
|
68
|
|
|
{ |
|
69
|
|
|
$defaults = array( |
|
70
|
6 |
|
'kind' => 'push' |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
// allow developer to directly specify params as json if (s)he wants. |
|
74
|
6 |
|
if ('array' !== gettype($params)) { |
|
75
|
3 |
|
if (empty($params)) { |
|
76
|
1 |
|
throw new \InvalidArgumentException('Invalid JSON provided.'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
$params = $this->decodeJSON($params); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
5 |
|
$params = array_merge($defaults, $params); |
|
83
|
|
|
|
|
84
|
5 |
|
if (empty($params['kind']) || !in_array($params['kind'], $this->allowedRestrictionTypes)) { |
|
85
|
2 |
|
throw new \InvalidArgumentException('Invalid restriction kind.'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
return $this->getClient()->setApiVersion('2.0')->post( |
|
89
|
3 |
|
sprintf('/repositories/%s/%s/branch-restrictions', $account, $repo), |
|
90
|
3 |
|
json_encode($params), |
|
91
|
3 |
|
array('Content-Type' => 'application/json') |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Get a specific restriction |
|
97
|
|
|
* |
|
98
|
|
|
* @access public |
|
99
|
|
|
* @param string $account The team or individual account owning the repository. |
|
100
|
|
|
* @param string $repo The repository identifier. |
|
101
|
|
|
* @param int $id The restriction's identifier. |
|
102
|
|
|
* @return ResponseInterface |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function get($account, $repo, $id) |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->get( |
|
107
|
1 |
|
sprintf('/repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Updates a specific branch restriction. |
|
113
|
|
|
* |
|
114
|
|
|
* @access public |
|
115
|
|
|
* @param string $account The team or individual account owning the repository. |
|
116
|
|
|
* @param string $repo The repository identifier. |
|
117
|
|
|
* @param int $id The restriction's identifier. |
|
118
|
|
|
* @param array|string $params Additional parameters as array or JSON string |
|
119
|
|
|
* @return ResponseInterface |
|
120
|
|
|
* |
|
121
|
|
|
* @throws \InvalidArgumentException |
|
122
|
|
|
*/ |
|
123
|
4 |
|
public function update($account, $repo, $id, $params = array()) |
|
124
|
|
|
{ |
|
125
|
|
|
// allow developer to directly specify params as json if (s)he wants. |
|
126
|
4 |
|
if ('array' !== gettype($params)) { |
|
127
|
2 |
|
if (empty($params)) { |
|
128
|
1 |
|
throw new \InvalidArgumentException('Invalid JSON provided.'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
$params = $this->decodeJSON($params); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
3 |
|
if (!empty($params['kind'])) { |
|
135
|
1 |
|
throw new \InvalidArgumentException('You cannot change the "kind" value.'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
return $this->getClient()->setApiVersion('2.0')->put( |
|
139
|
2 |
|
sprintf('/repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id), |
|
140
|
2 |
|
json_encode($params), |
|
141
|
2 |
|
array('Content-Type' => 'application/json') |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Delete a specific branch restriction. |
|
147
|
|
|
* |
|
148
|
|
|
* @access public |
|
149
|
|
|
* @param string $account The team or individual account owning the repository. |
|
150
|
|
|
* @param string $repo The repository identifier. |
|
151
|
|
|
* @param int $id The restriction's identifier. |
|
152
|
|
|
* @return ResponseInterface |
|
153
|
|
|
* |
|
154
|
|
|
* @throws \InvalidArgumentException |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function delete($account, $repo, $id) |
|
157
|
|
|
{ |
|
158
|
1 |
|
return $this->getClient()->setApiVersion('2.0')->delete( |
|
159
|
1 |
|
sprintf('/repositories/%s/%s/branch-restrictions/%d', $account, $repo, $id) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Add allowed permission types |
|
165
|
|
|
* |
|
166
|
|
|
* @param array $restrictions |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function addAllowedRestrictionType($restrictions = array()) |
|
169
|
|
|
{ |
|
170
|
1 |
|
$this->allowedRestrictionTypes = array_merge($this->allowedRestrictionTypes, $restrictions); |
|
171
|
1 |
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|