Skip to content

Commit 7882713

Browse files
committed
feat(api): add job token api
Added endpoints as described in https://docs.gitlab.com/api/project_job_token_scopes
1 parent 1bf5326 commit 7882713

File tree

3 files changed

+201
-1
lines changed

3 files changed

+201
-1
lines changed

src/Api/Projects.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,4 +1280,80 @@ public function search(int|string $id, array $parameters = []): mixed
12801280

12811281
return $this->get('projects/'.self::encodePath($id).'/search', $resolver->resolve($parameters));
12821282
}
1283+
1284+
/**
1285+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-access-settings
1286+
*/
1287+
public function jobTokenScope(int|string $project_id): mixed
1288+
{
1289+
return $this->get($this->getProjectPath($project_id, 'job_token_scope'));
1290+
}
1291+
1292+
/**
1293+
* @param bool $enabled Restricts job token access to allowlisted projects only. Set to false to allow access from all projects. This parameter can be overridden by the Enforce job token allowlist instance setting.
1294+
*
1295+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#patch-a-projects-cicd-job-token-access-settings
1296+
*/
1297+
public function setJobTokenAllowListEnabled(int|string $project_id, bool $enabled): mixed
1298+
{
1299+
return $this->patch($this->getProjectPath($project_id, 'job_token_scope'), ['enabled' => $enabled]);
1300+
}
1301+
1302+
/**
1303+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-inbound-allowlist
1304+
*/
1305+
public function jobTokenAllowList(int|string $project_id): mixed
1306+
{
1307+
return $this->get($this->getProjectPath($project_id, 'job_token_scope/allowlist'));
1308+
}
1309+
1310+
/**
1311+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#add-a-project-to-a-cicd-job-token-inbound-allowlist
1312+
*/
1313+
public function addJobTokenAllowList(int|string $project_id, int $target_project_id): mixed
1314+
{
1315+
return $this->post(
1316+
$this->getProjectPath($project_id, 'job_token_scope/allowlist'),
1317+
['target_project_id' => $target_project_id]
1318+
);
1319+
}
1320+
1321+
/**
1322+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#remove-a-project-from-a-cicd-job-token-inbound-allowlist
1323+
*/
1324+
public function removeJobTokenAllowList(int|string $project_id, int $target_project_id): mixed
1325+
{
1326+
return $this->delete(
1327+
$this->getProjectPath($project_id, 'job_token_scope/allowlist/'.self::encodePath($target_project_id))
1328+
);
1329+
}
1330+
1331+
/**
1332+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#get-a-projects-cicd-job-token-allowlist-of-groups
1333+
*/
1334+
public function jobTokenGroupAllowList(int|string $project_id): mixed
1335+
{
1336+
return $this->get($this->getProjectPath($project_id, 'job_token_scope/groups_allowlist'));
1337+
}
1338+
1339+
/**
1340+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#add-a-group-to-a-cicd-job-token-allowlist
1341+
*/
1342+
public function addJobTokenGroupAllowList(int|string $project_id, int $target_group_id): mixed
1343+
{
1344+
return $this->post(
1345+
$this->getProjectPath($project_id, 'job_token_scope/groups_allowlist'),
1346+
['target_group_id' => $target_group_id]
1347+
);
1348+
}
1349+
1350+
/**
1351+
* @see https://docs.gitlab.com/api/project_job_token_scopes/#remove-a-group-from-a-cicd-job-token-allowlist
1352+
*/
1353+
public function removeJobTokenGroupAllowList(int|string $project_id, int $target_group_id): mixed
1354+
{
1355+
return $this->delete(
1356+
$this->getProjectPath($project_id, 'job_token_scope/groups_allowlist/'.self::encodePath($target_group_id))
1357+
);
1358+
}
12831359
}

tests/Api/ProjectsTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,4 +2765,128 @@ public function shouldSearchGroups(): void
27652765
'sort' => 'desc',
27662766
]));
27672767
}
2768+
2769+
#[Test]
2770+
public function shouldGetJobTokenSettings(): void
2771+
{
2772+
$expectedArray = ['inbound_enabled' => true, 'outbound_enabled' => false];
2773+
2774+
$api = $this->getApiMock();
2775+
$api->expects($this->once())
2776+
->method('get')
2777+
->with('projects/1/job_token_scope')
2778+
->willReturn($expectedArray);
2779+
2780+
$this->assertEquals($expectedArray, $api->jobTokenScope(1));
2781+
}
2782+
2783+
#[Test]
2784+
public function shouldUpdateJobTokenSettings(): void
2785+
{
2786+
$expectedBool = true;
2787+
2788+
$api = $this->getApiMock();
2789+
$api->expects($this->once())
2790+
->method('patch')
2791+
->with('projects/1/job_token_scope', ['enabled' => false])
2792+
->willReturn($expectedBool);
2793+
2794+
$this->assertEquals($expectedBool, $api->setJobTokenAllowListEnabled(1, false));
2795+
}
2796+
2797+
#[Test]
2798+
public function shouldGetJobTokenAllowList(): void
2799+
{
2800+
$expectedArray = [[
2801+
'id' => 1,
2802+
]];
2803+
2804+
$api = $this->getApiMock();
2805+
$api->expects($this->once())
2806+
->method('get')
2807+
->with('projects/1/job_token_scope/allowlist')
2808+
->willReturn($expectedArray);
2809+
2810+
$this->assertEquals($expectedArray, $api->jobTokenAllowList(1));
2811+
}
2812+
2813+
#[Test]
2814+
public function shouldAddToJobTokenAllowList(): void
2815+
{
2816+
$expectedArray = [
2817+
'source_project_id' => 1,
2818+
'target_project_id' => 42,
2819+
];
2820+
2821+
$api = $this->getApiMock();
2822+
$api->expects($this->once())
2823+
->method('post')
2824+
->with('projects/1/job_token_scope/allowlist', ['target_project_id' => 42])
2825+
->willReturn($expectedArray);
2826+
2827+
$this->assertEquals($expectedArray, $api->addJobTokenAllowList(1, 42));
2828+
}
2829+
2830+
#[Test]
2831+
public function shouldRemoveToJobTokenAllowList(): void
2832+
{
2833+
$expectedBool = true;
2834+
2835+
$api = $this->getApiMock();
2836+
$api->expects($this->once())
2837+
->method('delete')
2838+
->with('projects/1/job_token_scope/allowlist/42')
2839+
->willReturn($expectedBool);
2840+
2841+
$this->assertEquals($expectedBool, $api->removeJobTokenAllowList(1, 42));
2842+
}
2843+
2844+
#[Test]
2845+
public function shouldGetJobTokenGroupAllowList(): void
2846+
{
2847+
$expectedArray = [[
2848+
'id' => 1,
2849+
'name' => 'my-group',
2850+
'web_url' => 'https://gitlab.example.com/groups/my-group',
2851+
]];
2852+
2853+
$api = $this->getApiMock();
2854+
$api->expects($this->once())
2855+
->method('get')
2856+
->with('projects/1/job_token_scope/groups_allowlist')
2857+
->willReturn($expectedArray);
2858+
2859+
$this->assertEquals($expectedArray, $api->jobTokenGroupAllowList(1));
2860+
}
2861+
2862+
#[Test]
2863+
public function shouldAddToJobTokenGroupsAllowList(): void
2864+
{
2865+
$expectedArray = [
2866+
'source_project_id' => 1,
2867+
'target_group_id' => 42,
2868+
];
2869+
2870+
$api = $this->getApiMock();
2871+
$api->expects($this->once())
2872+
->method('post')
2873+
->with('projects/1/job_token_scope/groups_allowlist', ['target_group_id' => 42])
2874+
->willReturn($expectedArray);
2875+
2876+
$this->assertEquals($expectedArray, $api->addJobTokenGroupAllowList(1, 42));
2877+
}
2878+
2879+
#[Test]
2880+
public function shouldRemoveToJobTokenGroupsAllowList(): void
2881+
{
2882+
$expectedBool = true;
2883+
2884+
$api = $this->getApiMock();
2885+
$api->expects($this->once())
2886+
->method('delete')
2887+
->with('projects/1/job_token_scope/groups_allowlist/42')
2888+
->willReturn($expectedBool);
2889+
2890+
$this->assertEquals($expectedBool, $api->removeJobTokenGroupAllowList(1, 42));
2891+
}
27682892
}

tests/Api/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function getApiMock(array $methods = []): MockObject
3535
$client = Client::createWithHttpClient($httpClient);
3636

3737
return $this->getMockBuilder($this->getApiClass())
38-
->onlyMethods(\array_merge(['getAsResponse', 'get', 'post', 'delete', 'put'], $methods))
38+
->onlyMethods(\array_merge(['getAsResponse', 'get', 'post', 'delete', 'put', 'patch'], $methods))
3939
->setConstructorArgs([$client, null])
4040
->getMock();
4141
}

0 commit comments

Comments
 (0)