Skip to content

Add workflow dispatch and allow workflow names. #969

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 1 commit into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion doc/repo/actions/workflow_runs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions doc/repo/actions/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ $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

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');
```
6 changes: 3 additions & 3 deletions lib/Github/Api/Repository/Actions/WorkflowRuns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
49 changes: 39 additions & 10 deletions lib/Github/Api/Repository/Actions/Workflows.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 20 additions & 0 deletions test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down