Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.15] Support for Personal Access Token endpoints #797

Open
wants to merge 5 commits into
base: 11.15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions src/Api/PersonalAccessTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <[email protected]>
* (c) Graham Campbell <[email protected]>
*
* 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));
}

/**
* @param int $id
*
* @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');
}


/**
* @param int $id
*
* @param array $params
*
* @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));
}

/**
* @param array $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));
}

/**
* @param int $id
*
* @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');
}
}
9 changes: 9 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Gitlab\Api\Keys;
use Gitlab\Api\MergeRequests;
use Gitlab\Api\Milestones;
use Gitlab\Api\PersonalAccessTokens;
use Gitlab\Api\ProjectNamespaces;
use Gitlab\Api\Projects;
use Gitlab\Api\Repositories;
Expand Down Expand Up @@ -400,6 +401,14 @@ public function users(): Users
return new Users($this);
}

/**
* @return PersonalAccessTokens
*/
public function personal_access_tokens(): PersonalAccessTokens
{
return new PersonalAccessTokens($this);
}

/**
* @return Version
*/
Expand Down
167 changes: 167 additions & 0 deletions tests/Api/PersonalAccessTokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Gitlab API library.
*
* (c) Matt Humphrey <[email protected]>
* (c) Graham Campbell <[email protected]>
*
* 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()
{
return PersonalAccessTokens::class;
}

/**
* @test
*/
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());
}

}
Loading