Skip to content

Commit 61366b2

Browse files
committed
Add workflow dispatch and allow workflow names.
1 parent 79a0993 commit 61366b2

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

doc/repo/actions/workflow_runs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $workflowRuns = $client->api('repo')->workflowRuns()->all('KnpLabs', 'php-github
1414
https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-workflow-runs
1515

1616
```php
17-
$runs = $client->api('repo')->workflowRuns()->listRuns('KnpLabs', 'php-github-api', $workflowId);
17+
$runs = $client->api('repo')->workflowRuns()->listRuns('KnpLabs', 'php-github-api', $workflow);
1818
```
1919

2020
### Get a workflow run

doc/repo/actions/workflows.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ $workflows = $client->api('repo')->workflows()->all('KnpLabs', 'php-github-api')
1414
https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-a-workflow
1515

1616
```php
17-
$workflow = $client->api('repo')->workflows()->show('KnpLabs', 'php-github-api', $workflowId);
17+
$workflow = $client->api('repo')->workflows()->show('KnpLabs', 'php-github-api', $workflow);
1818
```
1919

2020
### Get workflow usage
2121

2222
https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-workflow-usage
2323

2424
```php
25-
$usage = $client->api('repo')->workflows()->usage('KnpLabs', 'php-github-api', $workflowId);
25+
$usage = $client->api('repo')->workflows()->usage('KnpLabs', 'php-github-api', $workflow);
26+
```
27+
28+
### Dispatch a workflow
29+
30+
https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event
31+
32+
```php
33+
$client->api('repo')->workflows()->dispatches('KnpLabs', 'php-github-api', $workflow, 'main');
2634
```

lib/Github/Api/Repository/Actions/WorkflowRuns.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public function all(string $username, string $repository, array $parameters = []
2828
*
2929
* @param string $username
3030
* @param string $repository
31-
* @param string $workflowId
31+
* @param string $workflow
3232
* @param array $parameters
3333
*
3434
* @return array
3535
*/
36-
public function listRuns(string $username, string $repository, string $workflowId, array $parameters = [])
36+
public function listRuns(string $username, string $repository, string $workflow, array $parameters = [])
3737
{
38-
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId.'/runs', $parameters);
38+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.rawurlencode($workflow).'/runs', $parameters);
3939
}
4040

4141
/**

lib/Github/Api/Repository/Actions/Workflows.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,57 @@ public function all(string $username, string $repository, array $parameters = []
2626
/**
2727
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-a-workflow
2828
*
29-
* @param string $username
30-
* @param string $repository
31-
* @param int $workflowId
29+
* @param string $username
30+
* @param string $repository
31+
* @param string|int $workflow
3232
*
3333
* @return array
3434
*/
35-
public function show(string $username, string $repository, int $workflowId)
35+
public function show(string $username, string $repository, $workflow)
3636
{
37-
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId);
37+
if (is_string($workflow)) {
38+
$workflow = rawurlencode($workflow);
39+
}
40+
41+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow);
3842
}
3943

4044
/**
4145
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#get-workflow-usage
4246
*
43-
* @param string $username
44-
* @param string $repository
45-
* @param int $workflowId
47+
* @param string $username
48+
* @param string $repository
49+
* @param string|int $workflow
4650
*
4751
* @return array|string
4852
*/
49-
public function usage(string $username, string $repository, int $workflowId)
53+
public function usage(string $username, string $repository, $workflow)
54+
{
55+
if (is_string($workflow)) {
56+
$workflow = rawurlencode($workflow);
57+
}
58+
59+
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow.'/timing');
60+
}
61+
62+
/**
63+
* @link https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#create-a-workflow-dispatch-event
64+
*
65+
* @param string $username
66+
* @param string $repository
67+
* @param string|int $workflow
68+
* @param string $ref
69+
* @param array $inputs
70+
*
71+
* @return array|string empty
72+
*/
73+
public function dispatches(string $username, string $repository, $workflow, string $ref, array $inputs = null)
5074
{
51-
return $this->get('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflowId.'/timing');
75+
if (is_string($workflow)) {
76+
$workflow = rawurlencode($workflow);
77+
}
78+
$parameters = array_filter(['ref' => $ref, 'inputs' => $inputs]);
79+
80+
return $this->post('/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/actions/workflows/'.$workflow.'/dispatches', $parameters);
5281
}
5382
}

test/Github/Tests/Api/Repository/Actions/WorkflowsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ public function shouldGetWorkflowUsage()
8989
$this->assertEquals($expectedArray, $api->usage('KnpLabs', 'php-github-api', 1));
9090
}
9191

92+
/**
93+
* @test
94+
*/
95+
public function shouldDispatchWorkflow()
96+
{
97+
// empty
98+
$expectedArray = [];
99+
100+
/** @var Workflows|MockObject $api */
101+
$api = $this->getApiMock();
102+
103+
$api
104+
->expects($this->once())
105+
->method('post')
106+
->with('/repos/KnpLabs/php-github-api/actions/workflows/1/dispatches')
107+
->will($this->returnValue($expectedArray));
108+
109+
$this->assertEquals($expectedArray, $api->dispatches('KnpLabs', 'php-github-api', 1, 'main'));
110+
}
111+
92112
protected function getApiClass()
93113
{
94114
return Workflows::class;

0 commit comments

Comments
 (0)