Skip to content

Commit 02d6c81

Browse files
authored
feature #994 Add create a repository using a template endpoint (martinbean)
This PR was squashed before being merged into the 3.3.x-dev branch. Discussion ---------- Adds methods to [create a repository using a template][1], and also contributes a few test cases for the various options. Resolves #976 [1]: https://docs.github.com/en/rest/reference/repos#create-a-repository-using-a-template Commits ------- 58b294f Add create a repository using a template endpoint 9fabcf7 Make parameters array instead c70f39f Document create repository from template 79ebd98 Type-hint parameters
1 parent 4b368a1 commit 02d6c81

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

doc/repos.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,14 @@ Example when you want to configure custom github action workflows.
370370
```php
371371
$client->api('repo')->dispatch('KnpLabs', 'php-github-api', 'acme-event', ['foo'=>'bar']);
372372
```
373+
374+
### Create a repository using a template
375+
376+
Create a new repository using a repository template.
377+
378+
```php
379+
$client->api('repo')->createFromTemplate('template-owner', 'template-repo', [
380+
'name' => 'name-of-new-repo',
381+
'owner' => 'name-of-new-repo-owner', // can be user or org
382+
]);
383+
```

lib/Github/Api/Repo.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,4 +793,16 @@ public function transfer($username, $repository, $newOwner, $teamId = [])
793793
{
794794
return $this->post('/repos/'.rawurldecode($username).'/'.rawurldecode($repository).'/transfer', ['new_owner' => $newOwner, 'team_id' => $teamId]);
795795
}
796+
797+
/**
798+
* Create a repository using a template.
799+
*
800+
* @link https://developer.github.com/v3/repos/#create-a-repository-using-a-template
801+
*
802+
* @return array
803+
*/
804+
public function createFromTemplate(string $templateOwner, string $templateRepo, array $parameters = [])
805+
{
806+
return $this->post('/repos/'.rawurldecode($templateOwner).'/'.rawurldecode($templateRepo).'/generate', $parameters);
807+
}
796808
}

test/Github/Tests/Api/RepoTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,32 @@ public function shouldTransferRepository()
626626
$this->assertEquals($expectedArray, $api->transfer('KnpLabs', 'php-github-api', 'github', [1234, 1235]));
627627
}
628628

629+
/**
630+
* @test
631+
*/
632+
public function shouldCreateRepositoryUsingTemplate()
633+
{
634+
$expectedArray = [
635+
'id' => 1,
636+
'name' => 'newrepo',
637+
'full_name' => 'johndoe/newrepo',
638+
];
639+
640+
$api = $this->getApiMock();
641+
$api->expects($this->once())
642+
->method('post')
643+
->with('/repos/acme/template/generate', [
644+
'name' => 'newrepo',
645+
'owner' => 'johndoe',
646+
])
647+
->will($this->returnValue($expectedArray));
648+
649+
$this->assertEquals($expectedArray, $api->createFromTemplate('acme', 'template', [
650+
'name' => 'newrepo',
651+
'owner' => 'johndoe',
652+
]));
653+
}
654+
629655
/**
630656
* @return string
631657
*/

0 commit comments

Comments
 (0)