Skip to content

feat: Support for Organization Runners #1101

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

Merged
merged 14 commits into from
Apr 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions lib/Github/Api/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github\Api;

use Github\Api\Organization\Actions\Secrets;
use Github\Api\Organization\Actions\SelfHostedRunners;
use Github\Api\Organization\Hooks;
use Github\Api\Organization\Members;
use Github\Api\Organization\OutsideCollaborators;
Expand Down Expand Up @@ -131,4 +132,12 @@ public function issues($organization, array $params = [], $page = 1)
{
return $this->get('/orgs/'.rawurlencode($organization).'/issues', array_merge(['page' => $page], $params));
}

/**
* @return SelfHostedRunners
*/
public function runners(): SelfHostedRunners
{
return new SelfHostedRunners($this->getClient());
}
}
65 changes: 65 additions & 0 deletions lib/Github/Api/Organization/Actions/SelfHostedRunners.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Github\Api\Organization\Actions;

use Github\Api\AbstractApi;

class SelfHostedRunners extends AbstractApi
{
/**
* @link https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#list-self-hosted-runners-for-an-organization
*
* @param string $organization
* @param string $type
* @param int $page
*
* @return array|string
*/
public function all(string $organization, string $type = 'all', int $page = 1)
{
$parameters = [
'type' => $type,
'page' => $page,
];

return $this->get('/orgs/'.rawurlencode($organization).'/actions/runners', $parameters);
}

/**
* @link https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#get-a-self-hosted-runner-for-an-organization
*
* @param string $organization
* @param int $runnerId
*
* @return array|string
*/
public function show(string $organization, int $runnerId)
{
return $this->get('/orgs/'.rawurlencode($organization).'/actions/runners/'.$runnerId);
}

/**
* @link https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#delete-a-self-hosted-runner-from-an-organization
*
* @param string $organization
* @param int $runnerId
*
* @return array|string
*/
public function remove(string $organization, int $runnerId)
{
return $this->delete('/orgs/'.rawurlencode($organization).'/actions/runners/'.$runnerId);
}

/**
* @link https://docs.github.com/en/rest/actions/self-hosted-runners?apiVersion=2022-11-28#list-runner-applications-for-an-organization
*
* @param string $organization
*
* @return array|string
*/
public function applications(string $organization)
{
return $this->get('/orgs/'.rawurlencode($organization).'/actions/runners/downloads');
}
}
116 changes: 116 additions & 0 deletions test/Github/Tests/Api/Organization/Actions/SelfHostedRunnersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace Github\Tests\Api\Organization\Actions;

use Github\Api\Organization\Actions\SelfHostedRunners;
use Github\Tests\Api\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

class SelfHostedRunnersTest extends TestCase
{
/**
* @test
*/
public function shouldGetSelfHostedRunners()
{
$expectedArray = [
[
'id' => 1,
'name' => 'MBP',
'os' => 'macos',
'status' => 'online',
],
[
'id' => 2,
'name' => 'iMac',
'os' => 'macos',
'status' => 'offline',
],
];

/** @var SelfHostedRunners|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/orgs/KnpLabs/actions/runners')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->all('KnpLabs'));
}

/**
* @test
*/
public function shouldGetSelfHostedRunner()
{
$expectedArray = [
'id' => 1,
'name' => 'MBP',
'os' => 'macos',
'status' => 'online',
];

/** @var SelfHostedRunners|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/orgs/KnpLabs/actions/runners/1')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->show('KnpLabs', 1));
}

/**
* @test
*/
public function shouldRemoveSelfHostedRunner()
{
$expectedValue = 'response';

/** @var SelfHostedRunners|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('delete')
->with('/orgs/KnpLabs/actions/runners/1')
->will($this->returnValue($expectedValue));

$this->assertEquals($expectedValue, $api->remove('KnpLabs', 1));
}

/**
* @test
*/
public function shouldGetSelfHostedRunnerApps()
{
$expectedArray = [
['os' => 'osx', 'architecture' => 'x64', 'download_url' => 'download_url', 'filename' => 'filename'],
['os' => 'linux', 'architecture' => 'x64', 'download_url' => 'download_url', 'filename' => 'filename'],
['os' => 'linux', 'architecture' => 'arm', 'download_url' => 'download_url', 'filename' => 'filename'],
['os' => 'win', 'architecture' => 'x64', 'download_url' => 'download_url', 'filename' => 'filename'],
['os' => 'linux', 'architecture' => 'arm64', 'download_url' => 'download_url', 'filename' => 'filename'],
];

/** @var SelfHostedRunners|MockObject $api */
$api = $this->getApiMock();

$api
->expects($this->once())
->method('get')
->with('/orgs/KnpLabs/actions/runners/downloads')
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->applications('KnpLabs'));
}

protected function getApiClass()
{
return SelfHostedRunners::class;
}
}

10 changes: 10 additions & 0 deletions test/Github/Tests/Api/OrganizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ public function shouldGetTeamsApiObject()
$this->assertInstanceOf(\Github\Api\Organization\Teams::class, $api->teams());
}

/**
* @test
*/
public function shouldGetSelfHostedRunnersApiObject()
{
$api = $this->getApiMock();

$this->assertInstanceOf(\Github\Api\Organization\Actions\SelfHostedRunners::class, $api->runners());
}

/**
* @return string
*/
Expand Down