Skip to content

Commit 0da32e7

Browse files
committed
Recreate PR 797.
1 parent 362450f commit 0da32e7

File tree

3 files changed

+326
-0
lines changed

3 files changed

+326
-0
lines changed

src/Api/PersonalAccessTokens.php

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Matt Humphrey <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Gitlab\Api;
16+
17+
use Symfony\Component\OptionsResolver\Options;
18+
19+
class PersonalAccessTokens extends AbstractApi
20+
{
21+
/**
22+
* @param array $parameters {
23+
*
24+
* @var string $search search text
25+
* @var string $state state of the token
26+
* @var int $user_id tokens belonging to the given user
27+
* @var bool $revoked whether the token is revoked or not
28+
* @var \DateTimeInterface $created_before return tokens created before the given time (inclusive)
29+
* @var \DateTimeInterface $created_after return tokens created after the given time (inclusive)
30+
* @var \DateTimeInterface $last_used_after return tokens used before the given time (inclusive)
31+
* @var \DateTimeInterface $last_used_before return tokens used after the given time (inclusive)
32+
* }
33+
*
34+
* @return mixed
35+
*/
36+
public function all(array $parameters = [])
37+
{
38+
$resolver = $this->createOptionsResolver();
39+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
40+
return $value->format('c');
41+
};
42+
$booleanNormalizer = function (Options $resolver, $value): string {
43+
return $value ? 'true' : 'false';
44+
};
45+
46+
$resolver->setDefined('search');
47+
$resolver->setDefined('state')
48+
->setAllowedValues('state', ['all', 'active', 'inactive']);
49+
$resolver->setDefined('user_id')
50+
->setAllowedTypes('user_id', 'int')
51+
->setAllowedValues('user_id', function ($value): bool {
52+
return $value > 0;
53+
})
54+
;
55+
$resolver->setDefined('created_before')
56+
->setAllowedTypes('created_before', \DateTimeInterface::class)
57+
->setNormalizer('created_before', $datetimeNormalizer)
58+
;
59+
$resolver->setDefined('created_after')
60+
->setAllowedTypes('created_after', \DateTimeInterface::class)
61+
->setNormalizer('created_after', $datetimeNormalizer)
62+
;
63+
$resolver->setDefined('last_used_after')
64+
->setAllowedTypes('last_used_after', \DateTimeInterface::class)
65+
->setNormalizer('last_used_after', $datetimeNormalizer)
66+
;
67+
$resolver->setDefined('last_used_before')
68+
->setAllowedTypes('last_used_before', \DateTimeInterface::class)
69+
->setNormalizer('last_used_before', $datetimeNormalizer)
70+
;
71+
$resolver->setDefined('revoked')
72+
->setAllowedTypes('revoked', 'bool')
73+
->setNormalizer('revoked', $booleanNormalizer);
74+
;
75+
76+
return $this->get('personal_access_tokens', $resolver->resolve($parameters));
77+
}
78+
79+
/**
80+
* @param int $id
81+
*
82+
* @return mixed
83+
*/
84+
public function show(int $id)
85+
{
86+
return $this->get('personal_access_tokens/'.self::encodePath($id));
87+
}
88+
89+
/**
90+
* @return mixed
91+
*/
92+
public function current()
93+
{
94+
return $this->get('personal_access_tokens/self');
95+
}
96+
97+
98+
/**
99+
* @param int $id
100+
*
101+
* @param array $params
102+
*
103+
* @return mixed
104+
*/
105+
public function rotate(int $id, array $params = [])
106+
{
107+
$resolver = $this->createOptionsResolver();
108+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
109+
return $value->format('c');
110+
};
111+
$resolver->setDefined('expires_at')
112+
->setAllowedTypes('expires_at', \DateTimeInterface::class)
113+
->setNormalizer('expires_at', $datetimeNormalizer)
114+
;
115+
return $this->post('personal_access_tokens/'.self::encodePath($id).'/rotate', $resolver->resolve($params));
116+
}
117+
118+
/**
119+
* @param array $params
120+
*
121+
* @return mixed
122+
*/
123+
public function rotateCurrent(array $params = [])
124+
{
125+
$resolver = $this->createOptionsResolver();
126+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
127+
return $value->format('c');
128+
};
129+
$resolver->setDefined('expires_at')
130+
->setAllowedTypes('expires_at', \DateTimeInterface::class)
131+
->setNormalizer('expires_at', $datetimeNormalizer)
132+
;
133+
return $this->post('personal_access_tokens/self/rotate', $resolver->resolve($params));
134+
}
135+
136+
/**
137+
* @param int $id
138+
*
139+
* @return mixed
140+
*/
141+
public function remove(int $id)
142+
{
143+
return $this->delete('personal_access_tokens/'.self::encodePath($id));
144+
}
145+
146+
/**
147+
* @return mixed
148+
*/
149+
public function removeCurrent()
150+
{
151+
return $this->delete('personal_access_tokens/self');
152+
}
153+
}

src/Client.php

+6
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Gitlab\Api\Keys;
3131
use Gitlab\Api\MergeRequests;
3232
use Gitlab\Api\Milestones;
33+
use Gitlab\Api\PersonalAccessTokens;
3334
use Gitlab\Api\ProjectNamespaces;
3435
use Gitlab\Api\Projects;
3536
use Gitlab\Api\Repositories;
@@ -240,6 +241,11 @@ public function namespaces(): ProjectNamespaces
240241
return new ProjectNamespaces($this);
241242
}
242243

244+
public function personal_access_tokens(): PersonalAccessTokens
245+
{
246+
return new PersonalAccessTokens($this);
247+
}
248+
243249
public function projects(): Projects
244250
{
245251
return new Projects($this);
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Gitlab API library.
7+
*
8+
* (c) Matt Humphrey <[email protected]>
9+
* (c) Graham Campbell <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Gitlab\Tests\Api;
16+
17+
use Gitlab\Api\PersonalAccessTokens;
18+
19+
class PersonalAccessTokensTest extends TestCase
20+
{
21+
protected function getApiClass()
22+
{
23+
return PersonalAccessTokens::class;
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function shouldGetAllTokens(): void
30+
{
31+
$expectedArray = [
32+
['id' => 1, 'name' => 'Token 1', 'state' => 'active'],
33+
['id' => 2, 'name' => 'Token 2', 'state' => 'revoked'],
34+
];
35+
36+
$api = $this->getApiMock();
37+
$api->expects($this->once())
38+
->method('get')
39+
->with('personal_access_tokens', [])
40+
->will($this->returnValue($expectedArray))
41+
;
42+
43+
$this->assertEquals($expectedArray, $api->all());
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function shouldGetActiveTokens(): void
50+
{
51+
$expectedArray = [
52+
['id' => 1, 'name' => 'Token 1', 'state' => 'active'],
53+
];
54+
55+
$api = $this->getApiMock();
56+
$api->expects($this->once())
57+
->method('get')
58+
->with('personal_access_tokens', ['state' => 'active'])
59+
->will($this->returnValue($expectedArray))
60+
;
61+
62+
$this->assertEquals($expectedArray, $api->all(['state' => 'active']));
63+
}
64+
65+
/**
66+
* @test
67+
*/
68+
public function shouldShowToken(): void
69+
{
70+
$expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active'];
71+
72+
$api = $this->getApiMock();
73+
$api->expects($this->once())
74+
->method('get')
75+
->with('personal_access_tokens/1')
76+
->will($this->returnValue($expectedArray))
77+
;
78+
79+
$this->assertEquals($expectedArray, $api->show(1));
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function shouldShowCurrent(): void
86+
{
87+
$expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active'];
88+
89+
$api = $this->getApiMock();
90+
$api->expects($this->once())
91+
->method('get')
92+
->with('personal_access_tokens/self')
93+
->will($this->returnValue($expectedArray))
94+
;
95+
96+
$this->assertEquals($expectedArray, $api->current());
97+
}
98+
99+
/**
100+
* @test
101+
*/
102+
public function shouldRotate(): void
103+
{
104+
$expectedArray = ['id' => 4, 'name' => 'Token 4'];
105+
106+
$api = $this->getApiMock();
107+
$api->expects($this->once())
108+
->method('post')
109+
->with('personal_access_tokens/3/rotate')
110+
->will($this->returnValue($expectedArray))
111+
;
112+
113+
$this->assertEquals($expectedArray, $api->rotate(3));
114+
}
115+
116+
/**
117+
* @test
118+
*/
119+
public function shouldRotateCurrent(): void
120+
{
121+
$expectedArray = ['id' => 4, 'name' => 'Token 4'];
122+
123+
$api = $this->getApiMock();
124+
$api->expects($this->once())
125+
->method('post')
126+
->with('personal_access_tokens/self/rotate')
127+
->will($this->returnValue($expectedArray))
128+
;
129+
130+
$this->assertEquals($expectedArray, $api->rotateCurrent());
131+
}
132+
133+
/**
134+
* @test
135+
*/
136+
public function shouldRemoveToken(): void
137+
{
138+
$expectedBool = true;
139+
140+
$api = $this->getApiMock();
141+
$api->expects($this->once())
142+
->method('delete')
143+
->with('personal_access_tokens/1')
144+
->will($this->returnValue($expectedBool))
145+
;
146+
147+
$this->assertEquals($expectedBool, $api->remove(1));
148+
}
149+
150+
/**
151+
* @test
152+
*/
153+
public function shouldRemoveCurrentToken(): void
154+
{
155+
$expectedBool = true;
156+
157+
$api = $this->getApiMock();
158+
$api->expects($this->once())
159+
->method('delete')
160+
->with('personal_access_tokens/self')
161+
->will($this->returnValue($expectedBool))
162+
;
163+
164+
$this->assertEquals($expectedBool, $api->removeCurrent());
165+
}
166+
167+
}

0 commit comments

Comments
 (0)