From 61366b26679156c4a71b1157ffe83bee6519cb89 Mon Sep 17 00:00:00 2001 From: Joachim Meyer Date: Mon, 15 Feb 2021 12:56:18 +0100 Subject: [PATCH] Add workflow dispatch and allow workflow names. --- doc/repo/actions/workflow_runs.md | 2 +- doc/repo/actions/workflows.md | 12 ++++- .../Api/Repository/Actions/WorkflowRuns.php | 6 +-- .../Api/Repository/Actions/Workflows.php | 49 +++++++++++++++---- .../Api/Repository/Actions/WorkflowsTest.php | 20 ++++++++ 5 files changed, 73 insertions(+), 16 deletions(-) diff --git a/doc/repo/actions/workflow_runs.md b/doc/repo/actions/workflow_runs.md index 88c6da7c621..6da31b62735 100644 --- a/doc/repo/actions/workflow_runs.md +++ b/doc/repo/actions/workflow_runs.md @@ -14,7 +14,7 @@ $workflowRuns = $client->api('repo')->workflowRuns()->all('KnpLabs', 'php-github https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-workflow-runs ```php -$runs = $client->api('repo')->workflowRuns()->listRuns('KnpLabs', 'php-github-api', $workflowId); +$runs = $client->api('repo')->workflowRuns()->listRuns('KnpLabs', 'php-github-api', $workflow); ``` ### Get a workflow run diff --git a/doc/repo/actions/workflows.md b/doc/repo/actions/workflows.md index d6689d90aa2..1d86f9efb09 100644 --- a/doc/repo/actions/workflows.md +++ b/doc/repo/actions/workflows.md @@ -14,7 +14,7 @@ $workflows = $client->api('repo')->workflows()->all('KnpLabs', 'php-github-api') https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-a-workflow ```php -$workflow = $client->api('repo')->workflows()->show('KnpLabs', 'php-github-api', $workflowId); +$workflow = $client->api('repo')->workflows()->show('KnpLabs', 'php-github-api', $workflow); ``` ### Get workflow usage @@ -22,5 +22,13 @@ $workflow = $client->api('repo')->workflows()->show('KnpLabs', 'php-github-api', https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-workflow-usage ```php -$usage = $client->api('repo')->workflows()->usage('KnpLabs', 'php-github-api', $workflowId); +$usage = $client->api('repo')->workflows()->usage('KnpLabs', 'php-github-api', $workflow); +``` + +### Dispatch a workflow + +https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event + +```php +$client->api('repo')->workflows()->dispatches('KnpLabs', 'php-github-api', $workflow, 'main'); ``` diff --git a/lib/Github/Api/Repository/Actions/WorkflowRuns.php b/lib/Github/Api/Repository/Actions/WorkflowRuns.php index 27213826207..cc5f1a117c3 100644 --- a/lib/Github/Api/Repository/Actions/WorkflowRuns.php +++ b/lib/Github/Api/Repository/Actions/WorkflowRuns.php @@ -28,14 +28,14 @@ public function all(string $username, string $repository, array $parameters = [] * * @param string $username * @param string $repository - * @param string $workflowId + * @param string $workflow * @param array $parameters * * @return array */ - public function listRuns(string $username, string $repository, string $workflowId, array $parameters = []) + public function listRuns(string $username, string $repository, string $workflow, array $parameters = []) { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId.'/runs', $parameters); + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.rawurlencode($workflow).'/runs', $parameters); } /** diff --git a/lib/Github/Api/Repository/Actions/Workflows.php b/lib/Github/Api/Repository/Actions/Workflows.php index 3843c59a0ba..cf067d4ac17 100644 --- a/lib/Github/Api/Repository/Actions/Workflows.php +++ b/lib/Github/Api/Repository/Actions/Workflows.php @@ -26,28 +26,57 @@ public function all(string $username, string $repository, array $parameters = [] /** * @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-a-workflow * - * @param string $username - * @param string $repository - * @param int $workflowId + * @param string $username + * @param string $repository + * @param string|int $workflow * * @return array */ - public function show(string $username, string $repository, int $workflowId) + public function show(string $username, string $repository, $workflow) { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId); + if (is_string($workflow)) { + $workflow = rawurlencode($workflow); + } + + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow); } /** * @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-workflow-usage * - * @param string $username - * @param string $repository - * @param int $workflowId + * @param string $username + * @param string $repository + * @param string|int $workflow * * @return array|string */ - public function usage(string $username, string $repository, int $workflowId) + public function usage(string $username, string $repository, $workflow) + { + if (is_string($workflow)) { + $workflow = rawurlencode($workflow); + } + + return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow.'/timing'); + } + + /** + * @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event + * + * @param string $username + * @param string $repository + * @param string|int $workflow + * @param string $ref + * @param array $inputs + * + * @return array|string empty + */ + public function dispatches(string $username, string $repository, $workflow, string $ref, array $inputs = null) { - return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId.'/timing'); + if (is_string($workflow)) { + $workflow = rawurlencode($workflow); + } + $parameters = array_filter(['ref' => $ref, 'inputs' => $inputs]); + + return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow.'/dispatches', $parameters); } } diff --git a/test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php b/test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php index a018f396531..7413fc5d7c0 100644 --- a/test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php +++ b/test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php @@ -89,6 +89,26 @@ public function shouldGetWorkflowUsage() $this->assertEquals($expectedArray, $api->usage('KnpLabs', 'php-github-api', 1)); } + /** + * @test + */ + public function shouldDispatchWorkflow() + { + // empty + $expectedArray = []; + + /** @var Workflows|MockObject $api */ + $api = $this->getApiMock(); + + $api + ->expects($this->once()) + ->method('post') + ->with('/repos/KnpLabs/php-github-api/actions/workflows/1/dispatches') + ->will($this->returnValue($expectedArray)); + + $this->assertEquals($expectedArray, $api->dispatches('KnpLabs', 'php-github-api', 1, 'main')); + } + protected function getApiClass() { return Workflows::class;