Skip to content

Commit ff191bb

Browse files
Add project access token rotation API (#884)
Co-authored-by: Dorian Boulc'h <5851082+dorianboulch@users.noreply.github.com>
1 parent c165244 commit ff191bb

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
* Add support for personal access tokens
1414
* Add support for `job_inputs` and `job_variables_attributes` in `Jobs::play`
1515
* Add support for filters in `Projects::projectAccessTokens`
16+
* Add support for `Projects::rotateProjectAccessToken`
1617
* Add support for listing merge requests associated with a commit
1718
* Add support for `without_project_bots` in `Users::all`
1819
* Add support for date filters and `finished_at` ordering in `Deployments::all`

‎src/Api/Projects.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,26 @@ public function createProjectAccessToken(int|string $project_id, array $paramete
12701270
return $this->post($this->getProjectPath($project_id, 'access_tokens'), $resolver->resolve($parameters));
12711271
}
12721272

1273+
/**
1274+
* @param array $parameters {
1275+
*
1276+
* @var \DateTimeInterface $expires_at expiration date of the access token
1277+
* }
1278+
*/
1279+
public function rotateProjectAccessToken(int|string $project_id, int|string $token_id, array $parameters = []): mixed
1280+
{
1281+
$resolver = new OptionsResolver();
1282+
$dateNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
1283+
return $value->format('Y-m-d');
1284+
};
1285+
$resolver->setDefined('expires_at')
1286+
->setAllowedTypes('expires_at', \DateTimeInterface::class)
1287+
->setNormalizer('expires_at', $dateNormalizer)
1288+
;
1289+
1290+
return $this->post($this->getProjectPath($project_id, 'access_tokens/'.self::encodePath($token_id).'/rotate'), $resolver->resolve($parameters));
1291+
}
1292+
12731293
public function deleteProjectAccessToken(int|string $project_id, int|string $token_id): mixed
12741294
{
12751295
return $this->delete($this->getProjectPath($project_id, 'access_tokens/'.$token_id));

‎tests/Api/ProjectsTest.php‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,6 +2769,61 @@ public function shouldCreateProjectAccessToken(): void
27692769
]));
27702770
}
27712771

2772+
#[Test]
2773+
public function shouldRotateProjectAccessToken(): void
2774+
{
2775+
$expectedArray = [
2776+
'scopes' => [
2777+
'api',
2778+
'read_repository',
2779+
],
2780+
'active' => true,
2781+
'name' => 'test',
2782+
'revoked' => false,
2783+
'created_at' => '2021-01-21T19:35:37.921Z',
2784+
'user_id' => 166,
2785+
'id' => 58,
2786+
'expires_at' => '2021-01-31',
2787+
'token' => 'D4y...Wzr',
2788+
];
2789+
$expiresAt = new DateTime('2021-01-31');
2790+
2791+
$api = $this->getApiMock();
2792+
$api->expects($this->once())
2793+
->method('post')
2794+
->with('projects/1/access_tokens/2/rotate', ['expires_at' => '2021-01-31'])
2795+
->willReturn($expectedArray);
2796+
2797+
$this->assertEquals($expectedArray, $api->rotateProjectAccessToken(1, 2, ['expires_at' => $expiresAt]));
2798+
}
2799+
2800+
#[Test]
2801+
public function shouldRotateCurrentProjectAccessToken(): void
2802+
{
2803+
$expectedArray = [
2804+
'scopes' => [
2805+
'api',
2806+
'read_repository',
2807+
],
2808+
'active' => true,
2809+
'name' => 'test',
2810+
'revoked' => false,
2811+
'created_at' => '2021-01-21T19:35:37.921Z',
2812+
'user_id' => 166,
2813+
'id' => 58,
2814+
'expires_at' => '2021-01-31',
2815+
'token' => 'D4y...Wzr',
2816+
];
2817+
2818+
$api = $this->getApiMock();
2819+
$api->expects($this->once())
2820+
->method('post')
2821+
->with('projects/1/access_tokens/self/rotate', [])
2822+
->willReturn($expectedArray);
2823+
2824+
$this->assertEquals($expectedArray, $api->rotateProjectAccessToken(1, 'self'));
2825+
}
2826+
27722827
#[Test]
27732828
public function shouldDeleteProjectAccessToken(): void
27742829
{

0 commit comments

Comments
 (0)