Skip to content

Commit 315c0cb

Browse files
authored
1 parent dbaaa90 commit 315c0cb

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed

src/Api/Projects.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,4 +1217,70 @@ public function deleteAllMergedBranches($project_id)
12171217
{
12181218
return $this->delete($this->getProjectPath($project_id, 'repository/merged_branches'));
12191219
}
1220+
1221+
/**
1222+
* @param int|string $project_id
1223+
*
1224+
* @return mixed
1225+
*/
1226+
public function projectAccessTokens($project_id)
1227+
{
1228+
return $this->get($this->getProjectPath($project_id, 'access_tokens'));
1229+
}
1230+
1231+
/**
1232+
* @param int|string $project_id
1233+
* @param array $parameters {
1234+
*
1235+
* @var string $name the name of the project access token
1236+
* @var array $scopes the scopes, one or many of: api, read_api, read_registry, write_registry, read_repository, write_repository
1237+
* @var \DateTimeInterface $expires_at the token expires at midnight UTC on that date
1238+
* }
1239+
*
1240+
* @return mixed
1241+
*/
1242+
public function createProjectAccessToken($project_id, array $parameters = [])
1243+
{
1244+
$resolver = $this->createOptionsResolver();
1245+
$datetimeNormalizer = function (Options $resolver, \DateTimeInterface $value): string {
1246+
return $value->format('Y-m-d');
1247+
};
1248+
1249+
$resolver->define('name')
1250+
->required()
1251+
;
1252+
1253+
$resolver->define('scopes')
1254+
->required()
1255+
->allowedTypes('array')
1256+
->allowedValues(function ($scopes) {
1257+
$allowed = ['api', 'read_api', 'read_registry', 'write_registry', 'read_repository', 'write_repository'];
1258+
foreach ($scopes as $scope) {
1259+
if (!\in_array($scope, $allowed, true)) {
1260+
return false;
1261+
}
1262+
}
1263+
1264+
return true;
1265+
})
1266+
;
1267+
1268+
$resolver->setDefined('expires_at')
1269+
->setAllowedTypes('expires_at', \DateTimeInterface::class)
1270+
->setNormalizer('expires_at', $datetimeNormalizer)
1271+
;
1272+
1273+
return $this->post($this->getProjectPath($project_id, 'access_tokens'), $resolver->resolve($parameters));
1274+
}
1275+
1276+
/**
1277+
* @param int|string $project_id
1278+
* @param int|string $token_id
1279+
*
1280+
* @return mixed
1281+
*/
1282+
public function deleteProjectAccessToken($project_id, $token_id)
1283+
{
1284+
return $this->delete($this->getProjectPath($project_id, 'access_tokens/'.$token_id));
1285+
}
12201286
}

tests/Api/ProjectsTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2321,6 +2321,96 @@ public function shouldGetProtectedBranches(): void
23212321
$this->assertEquals($expectedArray, $api->protectedBranches(1));
23222322
}
23232323

2324+
/**
2325+
* @test
2326+
*/
2327+
public function shouldGetProjectAccessTokens(): void
2328+
{
2329+
$expectedArray = [
2330+
[
2331+
'user_id' => 141,
2332+
'scopes' => [
2333+
'api',
2334+
],
2335+
'name' => 'token',
2336+
'expires_at' => '2021-01-31',
2337+
'id' => 42,
2338+
'active' => true,
2339+
'created_at' => '2021-01-20T22:11:48.151Z',
2340+
'revoked' => false,
2341+
]
2342+
];
2343+
2344+
$api = $this->getApiMock();
2345+
$api->expects($this->once())
2346+
->method('get')
2347+
->with('projects/1/access_tokens')
2348+
->will($this->returnValue($expectedArray));
2349+
2350+
$this->assertEquals($expectedArray, $api->projectAccessTokens(1));
2351+
}
2352+
2353+
/**
2354+
* @test
2355+
*/
2356+
public function shouldCreateProjectAccessToken(): void
2357+
{
2358+
$expectedArray = [
2359+
"scopes" => [
2360+
'api',
2361+
'read_repository',
2362+
],
2363+
'active' => true,
2364+
'name' => "test",
2365+
'revoked' => false,
2366+
'created_at' => '2021-01-21T19:35:37.921Z',
2367+
'user_id' => 166,
2368+
'id' => 58,
2369+
'expires_at' => '2021-01-31',
2370+
'token' => "D4y...Wzr",
2371+
];
2372+
$api = $this->getApiMock();
2373+
$api->expects($this->once())
2374+
->method('post')
2375+
->with(
2376+
'projects/1/access_tokens',
2377+
[
2378+
'name' => 'test_token',
2379+
'scopes' => [
2380+
'api',
2381+
'read_repository',
2382+
],
2383+
'expires_at' => '2021-01-31'
2384+
]
2385+
)
2386+
->will($this->returnValue($expectedArray));
2387+
2388+
$this->assertEquals($expectedArray, $api->createProjectAccessToken(1, [
2389+
'name' => 'test_token',
2390+
'scopes' => [
2391+
'api',
2392+
'read_repository'
2393+
],
2394+
'expires_at' => new DateTime('2021-01-31'),
2395+
]));
2396+
}
2397+
2398+
/**
2399+
* @test
2400+
*/
2401+
public function shouldDeleteProjectAccessToken(): void
2402+
{
2403+
$expectedBool = true;
2404+
2405+
$api = $this->getApiMock();
2406+
$api->expects($this->once())
2407+
->method('delete')
2408+
->with('projects/1/access_tokens/2')
2409+
->will($this->returnValue($expectedBool));
2410+
2411+
$this->assertEquals($expectedBool, $api->deleteProjectAccessToken(1, 2));
2412+
}
2413+
23242414
protected function getApiClass()
23252415
{
23262416
return Projects::class;

0 commit comments

Comments
 (0)