From 2edacc4d67242d1763a546137d3324e388e54ff5 Mon Sep 17 00:00:00 2001 From: Hannes Papenberg Date: Thu, 12 Nov 2026 22:07:17 +0100 Subject: [PATCH] Add the Projects (v2) API package Adds Joomla\Github\Package\Projects ($github->projects) with the sub-packages Projects\Drafts, Projects\Fields, Projects\Items and Projects\Views, covering all 26 operations of the GitHub REST projects category (projectsV2 endpoints of organisations and users). Co-Authored-By: Claude Opus 5.5 --- Tests/Package/Projects/DraftsTest.php | 118 +++++++ Tests/Package/Projects/FieldsTest.php | 262 ++++++++++++++ Tests/Package/Projects/ItemsTest.php | 478 ++++++++++++++++++++++++++ Tests/Package/Projects/ViewsTest.php | 118 +++++++ Tests/Package/ProjectsTest.php | 184 ++++++++++ docs/projects.md | 96 ++++++ src/Package/Projects.php | 128 +++++++ src/Package/Projects/Drafts.php | 73 ++++ src/Package/Projects/Fields.php | 177 ++++++++++ src/Package/Projects/Items.php | 355 +++++++++++++++++++ src/Package/Projects/Views.php | 75 ++++ 11 files changed, 2064 insertions(+) create mode 100644 Tests/Package/Projects/DraftsTest.php create mode 100644 Tests/Package/Projects/FieldsTest.php create mode 100644 Tests/Package/Projects/ItemsTest.php create mode 100644 Tests/Package/Projects/ViewsTest.php create mode 100644 Tests/Package/ProjectsTest.php create mode 100644 docs/projects.md create mode 100644 src/Package/Projects.php create mode 100644 src/Package/Projects/Drafts.php create mode 100644 src/Package/Projects/Fields.php create mode 100644 src/Package/Projects/Items.php create mode 100644 src/Package/Projects/Views.php diff --git a/Tests/Package/Projects/DraftsTest.php b/Tests/Package/Projects/DraftsTest.php new file mode 100644 index 00000000..1a11480d --- /dev/null +++ b/Tests/Package/Projects/DraftsTest.php @@ -0,0 +1,118 @@ +object = new Drafts($this->options, $this->client); + } + + /** + * Tests the createForOrg method. + * + * @return void + */ + public function testCreateForOrg() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"body":"Details","title":"Write the docs"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/projectsV2/6/drafts', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForOrg('joomla', 6, 'Write the docs', ['body' => 'Details']) + ); + } + + /** + * Tests the createForOrg method - failure. + * + * @return void + */ + public function testCreateForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->createForOrg('joomla', 6, 'Write the docs', ['body' => 'Details']); + } + + /** + * Tests the createForUser method. + * + * @return void + */ + public function testCreateForUser() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"body":"Details","title":"Write the docs"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/user/583231/projectsV2/6/drafts', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForUser('583231', 6, 'Write the docs', ['body' => 'Details']) + ); + } + + /** + * Tests the createForUser method - failure. + * + * @return void + */ + public function testCreateForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->createForUser('583231', 6, 'Write the docs', ['body' => 'Details']); + } +} diff --git a/Tests/Package/Projects/FieldsTest.php b/Tests/Package/Projects/FieldsTest.php new file mode 100644 index 00000000..1427d99a --- /dev/null +++ b/Tests/Package/Projects/FieldsTest.php @@ -0,0 +1,262 @@ +object = new Fields($this->options, $this->client); + } + + /** + * Tests the getListForOrg method. + * + * @return void + */ + public function testGetListForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6/fields?page=2&per_page=10&before=Y3Vyc29yOnYyOpK5', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForOrg('joomla', 6, ['before' => 'Y3Vyc29yOnYyOpK5'], 2, 10) + ); + } + + /** + * Tests the getListForOrg method - failure. + * + * @return void + */ + public function testGetListForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForOrg('joomla', 6, ['before' => 'Y3Vyc29yOnYyOpK5'], 2, 10); + } + + /** + * Tests the addForOrg method. + * + * @return void + */ + public function testAddForOrg() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"name":"Priority","data_type":"text"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/projectsV2/6/fields', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addForOrg('joomla', 6, ['name' => 'Priority', 'data_type' => 'text']) + ); + } + + /** + * Tests the addForOrg method - failure. + * + * @return void + */ + public function testAddForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->addForOrg('joomla', 6, ['name' => 'Priority', 'data_type' => 'text']); + } + + /** + * Tests the getForOrg method. + * + * @return void + */ + public function testGetForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6/fields/8', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForOrg('joomla', 6, 8) + ); + } + + /** + * Tests the getForOrg method - failure. + * + * @return void + */ + public function testGetForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForOrg('joomla', 6, 8); + } + + /** + * Tests the getListForUser method. + * + * @return void + */ + public function testGetListForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6/fields?page=2&per_page=10&after=Y3Vyc29yOnYyOpK5', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForUser('octocat', 6, ['after' => 'Y3Vyc29yOnYyOpK5'], 2, 10) + ); + } + + /** + * Tests the getListForUser method - failure. + * + * @return void + */ + public function testGetListForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForUser('octocat', 6, ['after' => 'Y3Vyc29yOnYyOpK5'], 2, 10); + } + + /** + * Tests the addForUser method. + * + * @return void + */ + public function testAddForUser() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"single_select_options":[{"name":"High","color":"RED"}],"name":"Priority","data_type":"single_select"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/users/octocat/projectsV2/6/fields', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addForUser('octocat', 6, 'Priority', 'single_select', ['single_select_options' => [['name' => 'High', 'color' => 'RED']]]) + ); + } + + /** + * Tests the addForUser method - failure. + * + * @return void + */ + public function testAddForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->addForUser('octocat', 6, 'Priority', 'single_select', ['single_select_options' => [['name' => 'High', 'color' => 'RED']]]); + } + + /** + * Tests the getForUser method. + * + * @return void + */ + public function testGetForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6/fields/8', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForUser('octocat', 6, 8) + ); + } + + /** + * Tests the getForUser method - failure. + * + * @return void + */ + public function testGetForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForUser('octocat', 6, 8); + } +} diff --git a/Tests/Package/Projects/ItemsTest.php b/Tests/Package/Projects/ItemsTest.php new file mode 100644 index 00000000..96ed6fd1 --- /dev/null +++ b/Tests/Package/Projects/ItemsTest.php @@ -0,0 +1,478 @@ +object = new Items($this->options, $this->client); + } + + /** + * Tests the getListForOrg method. + * + * @return void + */ + public function testGetListForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6/items?page=2&per_page=10&q=is:open&fields=1,2', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForOrg('joomla', 6, ['q' => 'is:open', 'fields' => '1,2'], 2, 10) + ); + } + + /** + * Tests the getListForOrg method - failure. + * + * @return void + */ + public function testGetListForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForOrg('joomla', 6, ['q' => 'is:open', 'fields' => '1,2'], 2, 10); + } + + /** + * Tests the addForOrg method. + * + * @return void + */ + public function testAddForOrg() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"id":42,"type":"Issue"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/projectsV2/6/items', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addForOrg('joomla', 6, 'Issue', ['id' => 42]) + ); + } + + /** + * Tests the addForOrg method - failure. + * + * @return void + */ + public function testAddForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->addForOrg('joomla', 6, 'Issue', ['id' => 42]); + } + + /** + * Tests the getForOrg method. + * + * @return void + */ + public function testGetForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6/items/11?fields=1,2', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForOrg('joomla', 6, 11, ['fields' => '1,2']) + ); + } + + /** + * Tests the getForOrg method - failure. + * + * @return void + */ + public function testGetForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForOrg('joomla', 6, 11, ['fields' => '1,2']); + } + + /** + * Tests the editForOrg method. + * + * @return void + */ + public function testEditForOrg() + { + $body = '{"fields":[{"id":1,"value":"Done"}]}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/orgs/joomla/projectsV2/6/items/11', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->editForOrg('joomla', 6, 11, [['id' => 1, 'value' => 'Done']]) + ); + } + + /** + * Tests the editForOrg method - failure. + * + * @return void + */ + public function testEditForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('patch') + ->willReturn($this->response); + + $this->object->editForOrg('joomla', 6, 11, [['id' => 1, 'value' => 'Done']]); + } + + /** + * Tests the deleteForOrg method. + * + * @return void + */ + public function testDeleteForOrg() + { + $this->response = $this->getResponseObject('', 204); + $this->client->expects($this->once()) + ->method('delete') + ->with('/orgs/joomla/projectsV2/6/items/11', [], 0) + ->willReturn($this->response); + + $this->assertNull($this->object->deleteForOrg('joomla', 6, 11)); + } + + /** + * Tests the deleteForOrg method - failure. + * + * @return void + */ + public function testDeleteForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('delete') + ->willReturn($this->response); + + $this->object->deleteForOrg('joomla', 6, 11); + } + + /** + * Tests the getListForOrgView method. + * + * @return void + */ + public function testGetListForOrgView() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6/views/3/items?page=2&per_page=10&fields=1,2', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForOrgView('joomla', 6, 3, ['fields' => '1,2'], 2, 10) + ); + } + + /** + * Tests the getListForOrgView method - failure. + * + * @return void + */ + public function testGetListForOrgViewFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForOrgView('joomla', 6, 3, ['fields' => '1,2'], 2, 10); + } + + /** + * Tests the getListForUser method. + * + * @return void + */ + public function testGetListForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6/items?page=2&per_page=10&q=is:open', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForUser('octocat', 6, ['q' => 'is:open'], 2, 10) + ); + } + + /** + * Tests the getListForUser method - failure. + * + * @return void + */ + public function testGetListForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForUser('octocat', 6, ['q' => 'is:open'], 2, 10); + } + + /** + * Tests the addForUser method. + * + * @return void + */ + public function testAddForUser() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"owner":"joomla","repo":"joomla-cms","number":1,"type":"Issue"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/users/octocat/projectsV2/6/items', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->addForUser('octocat', 6, 'Issue', ['owner' => 'joomla', 'repo' => 'joomla-cms', 'number' => 1]) + ); + } + + /** + * Tests the addForUser method - failure. + * + * @return void + */ + public function testAddForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->addForUser('octocat', 6, 'Issue', ['owner' => 'joomla', 'repo' => 'joomla-cms', 'number' => 1]); + } + + /** + * Tests the getForUser method. + * + * @return void + */ + public function testGetForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6/items/11?fields=1,2', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForUser('octocat', 6, 11, ['fields' => '1,2']) + ); + } + + /** + * Tests the getForUser method - failure. + * + * @return void + */ + public function testGetForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForUser('octocat', 6, 11, ['fields' => '1,2']); + } + + /** + * Tests the editForUser method. + * + * @return void + */ + public function testEditForUser() + { + $body = '{"fields":[{"id":1,"value":"Done"}]}'; + + $this->client->expects($this->once()) + ->method('patch') + ->with('/users/octocat/projectsV2/6/items/11', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->editForUser('octocat', 6, 11, [['id' => 1, 'value' => 'Done']]) + ); + } + + /** + * Tests the editForUser method - failure. + * + * @return void + */ + public function testEditForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('patch') + ->willReturn($this->response); + + $this->object->editForUser('octocat', 6, 11, [['id' => 1, 'value' => 'Done']]); + } + + /** + * Tests the deleteForUser method. + * + * @return void + */ + public function testDeleteForUser() + { + $this->response = $this->getResponseObject('', 204); + $this->client->expects($this->once()) + ->method('delete') + ->with('/users/octocat/projectsV2/6/items/11', [], 0) + ->willReturn($this->response); + + $this->assertNull($this->object->deleteForUser('octocat', 6, 11)); + } + + /** + * Tests the deleteForUser method - failure. + * + * @return void + */ + public function testDeleteForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('delete') + ->willReturn($this->response); + + $this->object->deleteForUser('octocat', 6, 11); + } + + /** + * Tests the getListForUserView method. + * + * @return void + */ + public function testGetListForUserView() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6/views/3/items?page=2&per_page=10&fields=1,2', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForUserView('octocat', 6, 3, ['fields' => '1,2'], 2, 10) + ); + } + + /** + * Tests the getListForUserView method - failure. + * + * @return void + */ + public function testGetListForUserViewFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForUserView('octocat', 6, 3, ['fields' => '1,2'], 2, 10); + } +} diff --git a/Tests/Package/Projects/ViewsTest.php b/Tests/Package/Projects/ViewsTest.php new file mode 100644 index 00000000..19b73100 --- /dev/null +++ b/Tests/Package/Projects/ViewsTest.php @@ -0,0 +1,118 @@ +object = new Views($this->options, $this->client); + } + + /** + * Tests the createForOrg method. + * + * @return void + */ + public function testCreateForOrg() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"filter":"is:issue","name":"Board","layout":"board"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/orgs/joomla/projectsV2/6/views', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForOrg('joomla', 6, 'Board', 'board', ['filter' => 'is:issue']) + ); + } + + /** + * Tests the createForOrg method - failure. + * + * @return void + */ + public function testCreateForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->createForOrg('joomla', 6, 'Board', 'board', ['filter' => 'is:issue']); + } + + /** + * Tests the createForUser method. + * + * @return void + */ + public function testCreateForUser() + { + $this->response = $this->getResponseObject($this->sampleString, 201); + $body = '{"visible_fields":[1,2],"name":"Board","layout":"board"}'; + + $this->client->expects($this->once()) + ->method('post') + ->with('/users/583231/projectsV2/6/views', $body, [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->createForUser('583231', 6, 'Board', 'board', ['visible_fields' => [1, 2]]) + ); + } + + /** + * Tests the createForUser method - failure. + * + * @return void + */ + public function testCreateForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('post') + ->willReturn($this->response); + + $this->object->createForUser('583231', 6, 'Board', 'board', ['visible_fields' => [1, 2]]); + } +} diff --git a/Tests/Package/ProjectsTest.php b/Tests/Package/ProjectsTest.php new file mode 100644 index 00000000..a80917f0 --- /dev/null +++ b/Tests/Package/ProjectsTest.php @@ -0,0 +1,184 @@ +object = new Projects($this->options, $this->client); + } + + /** + * Tests the getListForOrg method. + * + * @return void + */ + public function testGetListForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2?page=2&per_page=10&q=roadmap', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForOrg('joomla', ['q' => 'roadmap'], 2, 10) + ); + } + + /** + * Tests the getListForOrg method - failure. + * + * @return void + */ + public function testGetListForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForOrg('joomla', ['q' => 'roadmap'], 2, 10); + } + + /** + * Tests the getForOrg method. + * + * @return void + */ + public function testGetForOrg() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/orgs/joomla/projectsV2/6', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForOrg('joomla', 6) + ); + } + + /** + * Tests the getForOrg method - failure. + * + * @return void + */ + public function testGetForOrgFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForOrg('joomla', 6); + } + + /** + * Tests the getListForUser method. + * + * @return void + */ + public function testGetListForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2?page=2&per_page=10&after=Y3Vyc29yOnYyOpK5', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getListForUser('octocat', ['after' => 'Y3Vyc29yOnYyOpK5'], 2, 10) + ); + } + + /** + * Tests the getListForUser method - failure. + * + * @return void + */ + public function testGetListForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getListForUser('octocat', ['after' => 'Y3Vyc29yOnYyOpK5'], 2, 10); + } + + /** + * Tests the getForUser method. + * + * @return void + */ + public function testGetForUser() + { + $this->client->expects($this->once()) + ->method('get') + ->with('/users/octocat/projectsV2/6', [], 0) + ->willReturn($this->response); + + $this->assertEquals( + json_decode($this->sampleString), + $this->object->getForUser('octocat', 6) + ); + } + + /** + * Tests the getForUser method - failure. + * + * @return void + */ + public function testGetForUserFailure() + { + $this->expectException(UnexpectedResponseException::class); + + $this->response = $this->getResponseObject($this->errorString, 500); + + $this->client->expects($this->once()) + ->method('get') + ->willReturn($this->response); + + $this->object->getForUser('octocat', 6); + } +} diff --git a/docs/projects.md b/docs/projects.md new file mode 100644 index 00000000..66973f87 --- /dev/null +++ b/docs/projects.md @@ -0,0 +1,96 @@ +# Projects + +The Projects package covers the [REST API for Projects](https://docs.github.com/rest/projects) (Projects v2, +the `projectsV2` endpoints) of organisations and users: projects, draft items, fields, items and views. It is +reached as `$github->projects`; the project operations live on that class, the others in the sub-packages +`$github->projects->drafts`, `$github->projects->fields`, `$github->projects->items` and +`$github->projects->views`, for example `$github->projects->items->getListForOrg('joomla', 5)`. Methods +for organisation-owned projects end in `ForOrg`, those for user-owned projects in `ForUser`. + +```php +use Joomla\Github\Github; +use Joomla\Registry\Registry; + +$options = new Registry(); +$options->set('gh.token', $token); + +$github = new Github($options); + +// Find a project and list its open items with two field values +$projects = $github->projects->getListForOrg('joomla', ['q' => 'roadmap']); +$items = $github->projects->items->getListForOrg('joomla', $projects[0]->number, ['q' => 'is:open', 'fields' => '123,456']); + +// Add an issue and set a field value +$item = $github->projects->items->addForOrg('joomla', 5, 'Issue', ['owner' => 'joomla', 'repo' => 'joomla-cms', 'number' => 42]); +$github->projects->items->editForOrg('joomla', 5, $item->id, [['id' => 123, 'value' => 'Done']]); +``` + +Notes: + +* Optional request body fields are passed in `$options` and optional query parameters in `$filters`; the + docblock of each method lists the accepted keys. +* The list endpoints use cursor-based pagination: pass the `before` / `after` filters taken from the `Link` + response header. GitHub only uses `$limit` (`per_page`); `$page` is accepted for consistency with the + rest of the library but has no effect. +* The `fields` filter limits the returned field values by field ID; pass it as a comma-separated string + (`'123,456'`). +* Most user-scoped paths take the user name (`{username}`), but GitHub addresses the user by numeric ID + (`{user_id}`) when creating draft items (`POST /user/{user_id}/...`, singular `user`) and views + (`POST /users/{user_id}/...`). The methods follow the API exactly, so `Drafts::createForUser()` and + `Views::createForUser()` expect the user ID. +* `Fields::addForOrg()` takes the whole field definition as an array because the request body has several + variants without a common required field (an existing issue field via `issue_field_id`, or a new field via + `name` and `data_type`). +* The classic Projects API (projects v1, `/projects/columns` etc.) is no longer part of GitHub's REST API description and is not included; + GraphQL is available through `$github->graphql`. + +## Projects (`$github->projects`) + +| Method | Endpoint | +| --- | --- | +| getListForOrg($org, $filters, $page, $limit) | GET /orgs/{org}/projectsV2 | +| getForOrg($org, $projectNumber) | GET /orgs/{org}/projectsV2/{project_number} | +| getListForUser($username, $filters, $page, $limit) | GET /users/{username}/projectsV2 | +| getForUser($username, $projectNumber) | GET /users/{username}/projectsV2/{project_number} | + +## Drafts (`$github->projects->drafts`) + +| Method | Endpoint | +| --- | --- | +| createForOrg($org, $projectNumber, $title, $options) | POST /orgs/{org}/projectsV2/{project_number}/drafts | +| createForUser($userId, $projectNumber, $title, $options) | POST /user/{user_id}/projectsV2/{project_number}/drafts | + +## Fields (`$github->projects->fields`) + +| Method | Endpoint | +| --- | --- | +| getListForOrg($org, $projectNumber, $filters, $page, $limit) | GET /orgs/{org}/projectsV2/{project_number}/fields | +| addForOrg($org, $projectNumber, $field) | POST /orgs/{org}/projectsV2/{project_number}/fields | +| getForOrg($org, $projectNumber, $fieldId) | GET /orgs/{org}/projectsV2/{project_number}/fields/{field_id} | +| getListForUser($username, $projectNumber, $filters, $page, $limit) | GET /users/{username}/projectsV2/{project_number}/fields | +| addForUser($username, $projectNumber, $name, $dataType, $options) | POST /users/{username}/projectsV2/{project_number}/fields | +| getForUser($username, $projectNumber, $fieldId) | GET /users/{username}/projectsV2/{project_number}/fields/{field_id} | + +## Items (`$github->projects->items`) + +| Method | Endpoint | +| --- | --- | +| getListForOrg($org, $projectNumber, $filters, $page, $limit) | GET /orgs/{org}/projectsV2/{project_number}/items | +| addForOrg($org, $projectNumber, $type, $options) | POST /orgs/{org}/projectsV2/{project_number}/items | +| getForOrg($org, $projectNumber, $itemId, $filters) | GET /orgs/{org}/projectsV2/{project_number}/items/{item_id} | +| editForOrg($org, $projectNumber, $itemId, $fields) | PATCH /orgs/{org}/projectsV2/{project_number}/items/{item_id} | +| deleteForOrg($org, $projectNumber, $itemId) | DELETE /orgs/{org}/projectsV2/{project_number}/items/{item_id} | +| getListForOrgView($org, $projectNumber, $viewNumber, $filters, $page, $limit) | GET /orgs/{org}/projectsV2/{project_number}/views/{view_number}/items | +| getListForUser($username, $projectNumber, $filters, $page, $limit) | GET /users/{username}/projectsV2/{project_number}/items | +| addForUser($username, $projectNumber, $type, $options) | POST /users/{username}/projectsV2/{project_number}/items | +| getForUser($username, $projectNumber, $itemId, $filters) | GET /users/{username}/projectsV2/{project_number}/items/{item_id} | +| editForUser($username, $projectNumber, $itemId, $fields) | PATCH /users/{username}/projectsV2/{project_number}/items/{item_id} | +| deleteForUser($username, $projectNumber, $itemId) | DELETE /users/{username}/projectsV2/{project_number}/items/{item_id} | +| getListForUserView($username, $projectNumber, $viewNumber, $filters, $page, $limit) | GET /users/{username}/projectsV2/{project_number}/views/{view_number}/items | + +## Views (`$github->projects->views`) + +| Method | Endpoint | +| --- | --- | +| createForOrg($org, $projectNumber, $name, $layout, $options) | POST /orgs/{org}/projectsV2/{project_number}/views | +| createForUser($userId, $projectNumber, $name, $layout, $options) | POST /users/{user_id}/projectsV2/{project_number}/views | diff --git a/src/Package/Projects.php b/src/Package/Projects.php new file mode 100644 index 00000000..5ef04cb8 --- /dev/null +++ b/src/Package/Projects.php @@ -0,0 +1,128 @@ +fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get project for organization. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * + * @return object + * + * @link https://docs.github.com/rest/projects/projects#get-project-for-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForOrg(string $org, int $projectNumber) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List projects for user. + * + * Cursor-based pagination: pass the `before` / `after` filters; GitHub uses only `$limit` (per_page), not `$page`. + * + * @param string $username GitHub user name. + * @param array $filters Optional query parameters: q, before, after. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @link https://docs.github.com/rest/projects/projects#list-projects-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForUser(string $username, array $filters = [], int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2'; + + $uri = $this->fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Get project for user. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * + * @return object + * + * @link https://docs.github.com/rest/projects/projects#get-project-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForUser(string $username, int $projectNumber) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } +} diff --git a/src/Package/Projects/Drafts.php b/src/Package/Projects/Drafts.php new file mode 100644 index 00000000..c07d5edf --- /dev/null +++ b/src/Package/Projects/Drafts.php @@ -0,0 +1,73 @@ + $title])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Create draft item for user owned project. + * + * Note that GitHub addresses the user by numeric ID here, under `/user/` (singular). + * + * @param string $userId The unique (numeric) identifier of the user. + * @param integer $projectNumber Project number. + * @param string $title The title of the draft issue item. + * @param array $options Optional request body fields: body. + * + * @return object + * + * @link https://docs.github.com/rest/projects/drafts#create-draft-item-for-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createForUser(string $userId, int $projectNumber, string $title, array $options = []) + { + // Build the request path. + $path = '/user/' . $userId . '/projectsV2/' . $projectNumber . '/drafts'; + + $data = json_encode(array_merge($options, ['title' => $title])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } +} diff --git a/src/Package/Projects/Fields.php b/src/Package/Projects/Fields.php new file mode 100644 index 00000000..884e4387 --- /dev/null +++ b/src/Package/Projects/Fields.php @@ -0,0 +1,177 @@ +fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Add a field to an organization-owned project. + * + * The request body is one of several variants without a common required field, so it is passed as a whole. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param array $field The field definition: either `issue_field_id`, or `name` and `data_type` plus `single_select_options` / `iteration_configuration` where the type needs them. + * + * @return object + * + * @link https://docs.github.com/rest/projects/fields#add-a-field-to-an-organization-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addForOrg(string $org, int $projectNumber, array $field) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/fields'; + + $data = json_encode($field); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Get project field for organization. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param integer $fieldId Field identifier. + * + * @return object + * + * @link https://docs.github.com/rest/projects/fields#get-project-field-for-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForOrg(string $org, int $projectNumber, int $fieldId) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/fields/' . $fieldId; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } + + /** + * List project fields for user. + * + * Cursor-based pagination: pass the `before` / `after` filters; GitHub uses only `$limit` (per_page), not `$page`. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param array $filters Optional query parameters: before, after. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @link https://docs.github.com/rest/projects/fields#list-project-fields-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForUser(string $username, int $projectNumber, array $filters = [], int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/fields'; + + $uri = $this->fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Add field to user owned project. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param string $name The name of the field. + * @param string $dataType The field type: text, number, date, single_select or iteration. + * @param array $options Optional request body fields: single_select_options, iteration_configuration. + * + * @return object + * + * @link https://docs.github.com/rest/projects/fields#add-field-to-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addForUser(string $username, int $projectNumber, string $name, string $dataType, array $options = []) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/fields'; + + $data = json_encode(array_merge($options, ['name' => $name, 'data_type' => $dataType])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Get project field for user. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param integer $fieldId Field identifier. + * + * @return object + * + * @link https://docs.github.com/rest/projects/fields#get-project-field-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForUser(string $username, int $projectNumber, int $fieldId) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/fields/' . $fieldId; + + return $this->processResponse($this->client->get($this->fetchUrl($path))); + } +} diff --git a/src/Package/Projects/Items.php b/src/Package/Projects/Items.php new file mode 100644 index 00000000..b8ee1189 --- /dev/null +++ b/src/Package/Projects/Items.php @@ -0,0 +1,355 @@ +fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Add item to organization owned project. + * + * Identify the issue or pull request either by `id`, or by `owner`, `repo` and `number`. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param string $type The type of item to add: Issue or PullRequest. + * @param array $options Optional request body fields: id, owner, repo, number. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#add-item-to-organization-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addForOrg(string $org, int $projectNumber, string $type, array $options = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/items'; + + $data = json_encode(array_merge($options, ['type' => $type])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Get an item for an organization owned project. + * + * `fields` limits the returned fields by ID (comma-separated string, e.g. "123,456"). + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * @param array $filters Optional query parameters: fields. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#get-an-item-for-an-organization-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForOrg(string $org, int $projectNumber, int $itemId, array $filters = []) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $uri = $this->fetchUrl($path); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Update project item for organization. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * @param array $fields The field values to set, each as ['id' => fieldId, 'value' => value]. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#update-project-item-for-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function editForOrg(string $org, int $projectNumber, int $itemId, array $fields) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $data = json_encode(['fields' => $fields]); + + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Delete project item for organization. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * + * @return void + * + * @link https://docs.github.com/rest/projects/items#delete-project-item-for-organization + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function deleteForOrg(string $org, int $projectNumber, int $itemId) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * List items for an organization project view. + * + * `fields` limits the returned fields by ID (comma-separated string, e.g. "123,456"). + * Cursor-based pagination: pass the `before` / `after` filters; GitHub uses only `$limit` (per_page), not `$page`. + * + * @param string $org Organisation name. + * @param integer $projectNumber Project number. + * @param integer $viewNumber View number. + * @param array $filters Optional query parameters: fields, before, after. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @link https://docs.github.com/rest/projects/items#list-items-for-an-organization-project-view + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForOrgView(string $org, int $projectNumber, int $viewNumber, array $filters = [], int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/orgs/' . $org . '/projectsV2/' . $projectNumber . '/views/' . $viewNumber . '/items'; + + $uri = $this->fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * List items for a user owned project. + * + * `fields` limits the returned fields by ID (comma-separated string, e.g. "123,456"). + * Cursor-based pagination: pass the `before` / `after` filters; GitHub uses only `$limit` (per_page), not `$page`. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param array $filters Optional query parameters: before, after, q, fields. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @link https://docs.github.com/rest/projects/items#list-items-for-a-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForUser(string $username, int $projectNumber, array $filters = [], int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/items'; + + $uri = $this->fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Add item to user owned project. + * + * Identify the issue or pull request either by `id`, or by `owner`, `repo` and `number`. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param string $type The type of item to add: Issue or PullRequest. + * @param array $options Optional request body fields: id, owner, repo, number. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#add-item-to-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function addForUser(string $username, int $projectNumber, string $type, array $options = []) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/items'; + + $data = json_encode(array_merge($options, ['type' => $type])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Get an item for a user owned project. + * + * `fields` limits the returned fields by ID (comma-separated string, e.g. "123,456"). + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * @param array $filters Optional query parameters: fields. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#get-an-item-for-a-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getForUser(string $username, int $projectNumber, int $itemId, array $filters = []) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $uri = $this->fetchUrl($path); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } + + /** + * Update project item for user. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * @param array $fields The field values to set, each as ['id' => fieldId, 'value' => value]. + * + * @return object + * + * @link https://docs.github.com/rest/projects/items#update-project-item-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function editForUser(string $username, int $projectNumber, int $itemId, array $fields) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $data = json_encode(['fields' => $fields]); + + return $this->processResponse($this->client->patch($this->fetchUrl($path), $data)); + } + + /** + * Delete project item for user. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param integer $itemId Item identifier. + * + * @return void + * + * @link https://docs.github.com/rest/projects/items#delete-project-item-for-user + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function deleteForUser(string $username, int $projectNumber, int $itemId) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/items/' . $itemId; + + $this->processResponse($this->client->delete($this->fetchUrl($path)), 204); + } + + /** + * List items for a user project view. + * + * `fields` limits the returned fields by ID (comma-separated string, e.g. "123,456"). + * Cursor-based pagination: pass the `before` / `after` filters; GitHub uses only `$limit` (per_page), not `$page`. + * + * @param string $username GitHub user name. + * @param integer $projectNumber Project number. + * @param integer $viewNumber View number. + * @param array $filters Optional query parameters: fields, before, after. + * @param integer $page The page number from which to get items. + * @param integer $limit The number of items on a page. + * + * @return array + * + * @link https://docs.github.com/rest/projects/items#list-items-for-a-user-project-view + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function getListForUserView(string $username, int $projectNumber, int $viewNumber, array $filters = [], int $page = 0, int $limit = 0) + { + // Build the request path. + $path = '/users/' . $username . '/projectsV2/' . $projectNumber . '/views/' . $viewNumber . '/items'; + + $uri = $this->fetchUrl($path, $page, $limit); + + foreach ($filters as $key => $value) { + $uri->setVar($key, $value); + } + + return $this->processResponse($this->client->get($uri)); + } +} diff --git a/src/Package/Projects/Views.php b/src/Package/Projects/Views.php new file mode 100644 index 00000000..3451dbac --- /dev/null +++ b/src/Package/Projects/Views.php @@ -0,0 +1,75 @@ + $name, 'layout' => $layout])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } + + /** + * Create a view for a user-owned project. + * + * Note that GitHub addresses the user by numeric ID here. + * + * @param string $userId The unique (numeric) identifier of the user. + * @param integer $projectNumber Project number. + * @param string $name The name of the view. + * @param string $layout The layout of the view: table, board or roadmap. + * @param array $options Optional request body fields: filter, visible_fields, sort_by, group_by, vertical_group_by. + * + * @return object + * + * @link https://docs.github.com/rest/projects/views#create-a-view-for-a-user-owned-project + * @since __DEPLOY_VERSION__ + * @throws UnexpectedResponseException + */ + public function createForUser(string $userId, int $projectNumber, string $name, string $layout, array $options = []) + { + // Build the request path. + $path = '/users/' . $userId . '/projectsV2/' . $projectNumber . '/views'; + + $data = json_encode(array_merge($options, ['name' => $name, 'layout' => $layout])); + + return $this->processResponse($this->client->post($this->fetchUrl($path), $data), 201); + } +}