Notification Setup Error

We have detected an error in your notification set-up (Event-ID dab39dc24f564ec7bd4628d1305fd03c). Currently, we cannot inform you about inspection progress. Please check that the user 557058:bca11929-8c2d-43f2-8a82-c5416880d395 still has access to your repository or update the API account.

PullRequests::get()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * Manage the comments on pull requests.
19
 *
20
 * @author  Alexandru G.    <[email protected]>
21
 */
22
class PullRequests extends API\Api
23
{
24
    /**
25
     * Get comments
26
     *
27
     * @access public
28
     * @return PullRequests\Comments
29
     *
30
     * @throws \InvalidArgumentException
31
     * @codeCoverageIgnore
32
     */
33
    public function comments()
34
    {
35
        return $this->api('Repositories\\PullRequests\\Comments');
36
    }
37
38
    /**
39
     * Get a list of pull requests
40
     *
41
     * @access public
42
     * @param  string           $account The team or individual account owning the repository.
43
     * @param  string           $repo    The repository identifier.
44
     * @param  array            $params  Additional parameters
45
     * @return ResponseInterface
46
     *
47
     * @throws \InvalidArgumentException
48
     */
49 1
    public function all($account, $repo, array $params = array())
50
    {
51 1
        $states = array('OPEN', 'MERGED', 'DECLINED');
52 1
        $params = array_merge(
53
            array(
54 1
                'state' => 'OPEN'
55
            ),
56 1
            $params
57
        );
58
59 1
        if (!is_array($params['state'])) {
60 1
            $params['state'] = array($params['state']);
61
        }
62
63 1
        array_walk(
64 1
            $params['state'],
65 1
            function ($state) use ($states) {
66 1
                if (!in_array($state, $states)) {
67
                    throw new \InvalidArgumentException(sprintf('Unknown `state` %s', $state));
68
                }
69 1
            }
70
        );
71
72 1
        return $this->getClient()->setApiVersion('2.0')->get(
73 1
            sprintf('/repositories/%s/%s/pullrequests', $account, $repo),
74 1
            $params
75
        );
76
    }
77
78
    /**
79
     * Create a new pull request
80
     *
81
     * @access public
82
     * @param  string           $account The team or individual account owning the repository.
83
     * @param  string           $repo    The repository identifier.
84
     * @param  array|string     $params  Additional parameters as array or JSON string
85
     * @return ResponseInterface
86
     *
87
     * @throws \InvalidArgumentException
88
     * @see https://confluence.atlassian.com/x/XAZAGQ
89
     */
90 3
    public function create($account, $repo, $params = array())
91
    {
92
        $defaults = array(
93 3
            'title' => 'New pull request',
94
            'source' => array(
95
                'branch' => array(
96
                    'name'  => 'develop'
97
                )
98
            )
99
        );
100
101
        // allow developer to directly specify params as json if (s)he wants.
102 3
        if ('array' !== gettype($params)) {
103 2
            if (empty($params)) {
104 1
                throw new \InvalidArgumentException('Invalid JSON provided.');
105
            }
106
107 1
            $params = $this->decodeJSON($params);
108
        }
109
110 2
        $params = array_merge($defaults, $params);
111
112 2
        if (empty($params['title'])) {
113
            throw new \InvalidArgumentException('Pull request\'s title must be specified.');
114
        }
115
116 2
        if (empty($params['source']['branch']['name'])) {
117
            throw new \InvalidArgumentException('Pull request\'s source branch name must be specified.');
118
        }
119
120 2
        return $this->getClient()->setApiVersion('2.0')->post(
121 2
            sprintf('/repositories/%s/%s/pullrequests', $account, $repo),
122 2
            json_encode($params),
123 2
            array('Content-Type' => 'application/json')
124
        );
125
    }
126
127
    /**
128
     * Update a pull request
129
     *
130
     * @access public
131
     * @param  string           $account The team or individual account owning the repository.
132
     * @param  string           $repo    The repository identifier.
133
     * @param  int              $id      ID of the pull request that will be updated
134
     * @param  array|string     $params  Additional parameters as array or JSON string
135
     * @return ResponseInterface
136
     *
137
     * @throws \InvalidArgumentException
138
     */
139 3
    public function update($account, $repo, $id, $params = array())
140
    {
141
        $defaults = array(
142 3
            'title' => 'Updated pull request',
143
            'destination' => array(
144
                'branch' => array(
145
                    'name'  => 'develop'
146
                )
147
            )
148
        );
149
150
        // allow developer to directly specify params as json if (s)he wants.
151 3
        if ('array' !== gettype($params)) {
152 2
            if (empty($params)) {
153 1
                throw new \InvalidArgumentException('Invalid JSON provided.');
154
            }
155
156 1
            $params = $this->decodeJSON($params);
157
        }
158
159 2
        $params = array_merge($defaults, $params);
160
161 2
        if (empty($params['title'])) {
162
            throw new \InvalidArgumentException('Pull request\'s title must be specified.');
163
        }
164
165 2
        if (empty($params['destination']['branch']['name'])) {
166
            throw new \InvalidArgumentException('Pull request\'s destination branch name must be specified.');
167
        }
168
169 2
        return $this->getClient()->setApiVersion('2.0')->put(
170 2
            sprintf('/repositories/%s/%s/pullrequests/%d', $account, $repo, $id),
171 2
            json_encode($params),
172 2
            array('Content-Type' => 'application/json')
173
        );
174
    }
175
176
    /**
177
     * Get a specific pull request
178
     *
179
     * @access public
180
     * @param  string           $account The team or individual account owning the repository.
181
     * @param  string           $repo    The repository identifier.
182
     * @param  int              $id      ID of the pull request
183
     * @return ResponseInterface
184
     */
185 1
    public function get($account, $repo, $id)
186
    {
187 1
        return $this->getClient()->setApiVersion('2.0')->get(
188 1
            sprintf('/repositories/%s/%s/pullrequests/%d', $account, $repo, $id)
189
        );
190
    }
191
192
    /**
193
     * Get the commits for a pull request
194
     *
195
     * @access public
196
     * @param  string           $account The team or individual account owning the repository.
197
     * @param  string           $repo    The repository identifier.
198
     * @param  int              $id      ID of the pull request
199
     * @return ResponseInterface
200
     */
201 1
    public function commits($account, $repo, $id)
202
    {
203 1
        return $this->getClient()->setApiVersion('2.0')->get(
204 1
            sprintf('/repositories/%s/%s/pullrequests/%d/commits', $account, $repo, $id)
205
        );
206
    }
207
208
    /**
209
     * Approve a pull request
210
     *
211
     * @access public
212
     * @param  string           $account The team or individual account owning the repository.
213
     * @param  string           $repo    The repository identifier.
214
     * @param  int              $id      ID of the pull request
215
     * @return ResponseInterface
216
     */
217 1
    public function approve($account, $repo, $id)
218
    {
219 1
        return $this->getClient()->setApiVersion('2.0')->post(
220 1
            sprintf('/repositories/%s/%s/pullrequests/%d/approve', $account, $repo, $id)
221
        );
222
    }
223
224
    /**
225
     * Delete a pull request approval
226
     *
227
     * NOTE: On success returns `HTTP/1.1 204 NO CONTENT`
228
     *
229
     * @access public
230
     * @param  string           $account The team or individual account owning the repository.
231
     * @param  string           $repo    The repository identifier.
232
     * @param  int              $id      ID of the pull request
233
     * @return ResponseInterface
234
     */
235 1
    public function deleteApproval($account, $repo, $id)
236
    {
237 1
        return $this->getClient()->setApiVersion('2.0')->delete(
238 1
            sprintf('/repositories/%s/%s/pullrequests/%d/approve', $account, $repo, $id)
239
        );
240
    }
241
242
    /**
243
     * Get the diff for a pull request
244
     *
245
     * @access public
246
     * @param  string           $account The team or individual account owning the repository.
247
     * @param  string           $repo    The repository identifier.
248
     * @param  int              $id      ID of the pull request
249
     * @return ResponseInterface
250
     */
251 1
    public function diff($account, $repo, $id)
252
    {
253 1
        return $this->getClient()->setApiVersion('2.0')->get(
254 1
            sprintf('/repositories/%s/%s/pullrequests/%d/diff', $account, $repo, $id)
255
        );
256
    }
257
258
    /**
259
     * Get the log of all of a repository's pull request activity
260
     *
261
     * If `$id` is omitted the repository's pull request activity is returned.
262
     * If `$id` is not omitted the pull request activity is returned.
263
     *
264
     * @access public
265
     * @param  string           $account The team or individual account owning the repository.
266
     * @param  string           $repo    The repository identifier.
267
     * @param  int              $id      (Optional) ID of the pull request
268
     * @return ResponseInterface
269
     */
270 2
    public function activity($account, $repo, $id = 0)
271
    {
272 2
        $endpoint = sprintf('/repositories/%s/%s/pullrequests/', $account, $repo);
273
274 2
        if ($id === 0) {
275 1
            $endpoint .= 'activity';
276
        } else {
277 1
            $endpoint .= $id.'/activity';
278
        }
279
280 2
        return $this->getClient()->setApiVersion('2.0')->get($endpoint);
281
    }
282
283
    /**
284
     * Accept and merge a pull request
285
     *
286
     * @access public
287
     * @param  string           $account The team or individual account owning the repository.
288
     * @param  string           $repo    The repository identifier.
289
     * @param  int              $id      (Optional) ID of the pull request
290
     * @param  array            $params  Additional parameters.
291
     * @return ResponseInterface
292
     */
293 1
    public function accept($account, $repo, $id, $params = array())
294
    {
295 1
        return $this->getClient()->setApiVersion('2.0')->post(
296 1
            sprintf('/repositories/%s/%s/pullrequests/%d/merge', $account, $repo, $id),
297 1
            json_encode($params),
298 1
            array('Content-Type' => 'application/json')
299
        );
300
    }
301
302
    /**
303
     * Decline a pull request
304
     *
305
     * @access public
306
     * @param  string           $account The team or individual account owning the repository.
307
     * @param  string           $repo    The repository identifier.
308
     * @param  int              $id      (Optional) ID of the pull request
309
     * @param  array            $params  Additional parameters.
310
     * @return ResponseInterface
311
     */
312 2
    public function decline($account, $repo, $id, $params = array())
313
    {
314 2
        if (false === array_key_exists('message', $params)) {
315 1
            $params['message'] = '';
316
        }
317
318 2
        return $this->getClient()->setApiVersion('2.0')->post(
319 2
            sprintf('/repositories/%s/%s/pullrequests/%d/decline', $account, $repo, $id),
320 2
            json_encode($params),
321 2
            array('Content-Type' => 'application/json')
322
        );
323
    }
324
}
325