-
-
Notifications
You must be signed in to change notification settings - Fork 456
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[12.0] Support for Personal Access Token endpoints #827
Open
fjgarlin
wants to merge
8
commits into
GitLabPHP:12.0
Choose a base branch
from
fjgarlin:12.0
base: 12.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0da32e7
Recreate PR 797.
fjgarlin 40918e5
Spacing.
fjgarlin 576561d
Coding standards.
fjgarlin c8a6d98
Function signature.
fjgarlin 61a742e
Test annotation.
fjgarlin 8eeee25
Remove unneeded comments.
fjgarlin 32377ce
Add return type.
fjgarlin 3edb162
Add namespace.
fjgarlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Gitlab API library. | ||
* | ||
* (c) Matt Humphrey <matth@windsor-telecom.co.uk> | ||
* (c) Graham Campbell <hello@gjcampbell.co.uk> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gitlab\Api; | ||
|
||
use Symfony\Component\OptionsResolver\Options; | ||
|
||
class PersonalAccessTokens extends AbstractApi | ||
{ | ||
/** | ||
* @param array $parameters { | ||
* | ||
* @var string $search search text | ||
* @var string $state state of the token | ||
* @var int $user_id tokens belonging to the given user | ||
* @var bool $revoked whether the token is revoked or not | ||
* @var \DateTimeInterface $created_before return tokens created before the given time (inclusive) | ||
* @var \DateTimeInterface $created_after return tokens created after the given time (inclusive) | ||
* @var \DateTimeInterface $last_used_after return tokens used before the given time (inclusive) | ||
* @var \DateTimeInterface $last_used_before return tokens used after the given time (inclusive) | ||
* } | ||
* | ||
* @return mixed | ||
*/ | ||
public function all(array $parameters = []) | ||
{ | ||
$resolver = $this->createOptionsResolver(); | ||
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string { | ||
return $value->format('c'); | ||
}; | ||
$booleanNormalizer = function (Options $resolver, $value): string { | ||
return $value ? 'true' : 'false'; | ||
}; | ||
|
||
$resolver->setDefined('search'); | ||
$resolver->setDefined('state') | ||
->setAllowedValues('state', ['all', 'active', 'inactive']); | ||
$resolver->setDefined('user_id') | ||
->setAllowedTypes('user_id', 'int') | ||
->setAllowedValues('user_id', function ($value): bool { | ||
return $value > 0; | ||
}) | ||
; | ||
$resolver->setDefined('created_before') | ||
->setAllowedTypes('created_before', \DateTimeInterface::class) | ||
->setNormalizer('created_before', $datetimeNormalizer) | ||
; | ||
$resolver->setDefined('created_after') | ||
->setAllowedTypes('created_after', \DateTimeInterface::class) | ||
->setNormalizer('created_after', $datetimeNormalizer) | ||
; | ||
$resolver->setDefined('last_used_after') | ||
->setAllowedTypes('last_used_after', \DateTimeInterface::class) | ||
->setNormalizer('last_used_after', $datetimeNormalizer) | ||
; | ||
$resolver->setDefined('last_used_before') | ||
->setAllowedTypes('last_used_before', \DateTimeInterface::class) | ||
->setNormalizer('last_used_before', $datetimeNormalizer) | ||
; | ||
$resolver->setDefined('revoked') | ||
->setAllowedTypes('revoked', 'bool') | ||
->setNormalizer('revoked', $booleanNormalizer) | ||
; | ||
|
||
return $this->get('personal_access_tokens', $resolver->resolve($parameters)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function show(int $id) | ||
{ | ||
return $this->get('personal_access_tokens/'.self::encodePath($id)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function current() | ||
{ | ||
return $this->get('personal_access_tokens/self'); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function rotate(int $id, array $params = []) | ||
{ | ||
$resolver = $this->createOptionsResolver(); | ||
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string { | ||
return $value->format('c'); | ||
}; | ||
$resolver->setDefined('expires_at') | ||
->setAllowedTypes('expires_at', \DateTimeInterface::class) | ||
->setNormalizer('expires_at', $datetimeNormalizer) | ||
; | ||
|
||
return $this->post('personal_access_tokens/'.self::encodePath($id).'/rotate', $resolver->resolve($params)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function rotateCurrent(array $params = []) | ||
{ | ||
$resolver = $this->createOptionsResolver(); | ||
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string { | ||
return $value->format('c'); | ||
}; | ||
$resolver->setDefined('expires_at') | ||
->setAllowedTypes('expires_at', \DateTimeInterface::class) | ||
->setNormalizer('expires_at', $datetimeNormalizer) | ||
; | ||
|
||
return $this->post('personal_access_tokens/self/rotate', $resolver->resolve($params)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function remove(int $id) | ||
{ | ||
return $this->delete('personal_access_tokens/'.self::encodePath($id)); | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function removeCurrent() | ||
{ | ||
return $this->delete('personal_access_tokens/self'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the Gitlab API library. | ||
* | ||
* (c) Matt Humphrey <matth@windsor-telecom.co.uk> | ||
* (c) Graham Campbell <hello@gjcampbell.co.uk> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Gitlab\Tests\Api; | ||
|
||
use Gitlab\Api\PersonalAccessTokens; | ||
|
||
class PersonalAccessTokensTest extends TestCase | ||
{ | ||
protected function getApiClass(): string | ||
{ | ||
return PersonalAccessTokens::class; | ||
} | ||
|
||
/** | ||
* @test | ||
GrahamCampbell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function shouldGetAllTokens(): void | ||
{ | ||
$expectedArray = [ | ||
['id' => 1, 'name' => 'Token 1', 'state' => 'active'], | ||
['id' => 2, 'name' => 'Token 2', 'state' => 'revoked'], | ||
]; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('personal_access_tokens', []) | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->all()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldGetActiveTokens(): void | ||
{ | ||
$expectedArray = [ | ||
['id' => 1, 'name' => 'Token 1', 'state' => 'active'], | ||
]; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('personal_access_tokens', ['state' => 'active']) | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->all(['state' => 'active'])); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldShowToken(): void | ||
{ | ||
$expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active']; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('personal_access_tokens/1') | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->show(1)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldShowCurrent(): void | ||
{ | ||
$expectedArray = ['id' => 1, 'name' => 'Token 1', 'state' => 'active']; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('get') | ||
->with('personal_access_tokens/self') | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->current()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldRotate(): void | ||
{ | ||
$expectedArray = ['id' => 4, 'name' => 'Token 4']; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('post') | ||
->with('personal_access_tokens/3/rotate') | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->rotate(3)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldRotateCurrent(): void | ||
{ | ||
$expectedArray = ['id' => 4, 'name' => 'Token 4']; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('post') | ||
->with('personal_access_tokens/self/rotate') | ||
->will($this->returnValue($expectedArray)) | ||
; | ||
|
||
$this->assertEquals($expectedArray, $api->rotateCurrent()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldRemoveToken(): void | ||
{ | ||
$expectedBool = true; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('delete') | ||
->with('personal_access_tokens/1') | ||
->will($this->returnValue($expectedBool)) | ||
; | ||
|
||
$this->assertEquals($expectedBool, $api->remove(1)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function shouldRemoveCurrentToken(): void | ||
{ | ||
$expectedBool = true; | ||
|
||
$api = $this->getApiMock(); | ||
$api->expects($this->once()) | ||
->method('delete') | ||
->with('personal_access_tokens/self') | ||
->will($this->returnValue($expectedBool)) | ||
; | ||
|
||
$this->assertEquals($expectedBool, $api->removeCurrent()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move all return types to be real return types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.