From 3643df16b9ff5832f72c1181c533f187983bea86 Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 13 Nov 2012 17:11:21 +0000 Subject: [PATCH 01/20] Start of model classes --- lib/Github/Model/AbstractModel.php | 25 +++++++++ lib/Github/Model/Issue.php | 85 ++++++++++++++++++++++++++++++ lib/Github/Model/Label.php | 38 +++++++++++++ lib/Github/Model/Org.php | 11 ++++ lib/Github/Model/Owner.php | 38 +++++++++++++ lib/Github/Model/Repo.php | 82 ++++++++++++++++++++++++++++ lib/Github/Model/User.php | 11 ++++ 7 files changed, 290 insertions(+) create mode 100644 lib/Github/Model/AbstractModel.php create mode 100644 lib/Github/Model/Issue.php create mode 100644 lib/Github/Model/Label.php create mode 100644 lib/Github/Model/Org.php create mode 100644 lib/Github/Model/Owner.php create mode 100644 lib/Github/Model/Repo.php create mode 100644 lib/Github/Model/User.php diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php new file mode 100644 index 00000000000..151cf249a61 --- /dev/null +++ b/lib/Github/Model/AbstractModel.php @@ -0,0 +1,25 @@ +api($api); + } + +} diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php new file mode 100644 index 00000000000..6631bc592c8 --- /dev/null +++ b/lib/Github/Model/Issue.php @@ -0,0 +1,85 @@ +owner = $owner; + $this->repo = $repo; + $this->number = $number; + } + + public function show() + { + return $this->api('issue')->show( + $this->owner->name, + $this->repo->name, + $this->number + ); + } + + public function update(array $params) + { + return $this->api('issue')->update( + $this->owner->name, + $this->repo->name, + $this->number, + $params + ); + } + + public function labels() + { + return $this->api('issue')->labels()->all( + $this->owner->name, + $this->repo->name, + $this->number + ); + } + + // todo: create label if not exists + public function addLabels(array $labels) + { + return $this->api('issue')->labels()->add( + $this->owner->name, + $this->repo->name, + $this->number, + $labels + ); + } + + public function removeLabel($name) + { + return $this->api('issue')->labels()->remove( + $this->owner->name, + $this->repo->name, + $this->number, + $name + ); + } + + public function replaceLabels(array $labels) + { + return $this->api('issue')->labels()->replace( + $this->owner->name, + $this->repo->name, + $this->number, + $labels + ); + } + + public function clearLabels() + { + return $this->api('issue')->labels()->clear( + $this->owner->name, + $this->repo->name, + $this->number + ); + } +} diff --git a/lib/Github/Model/Label.php b/lib/Github/Model/Label.php new file mode 100644 index 00000000000..6ba052be2ad --- /dev/null +++ b/lib/Github/Model/Label.php @@ -0,0 +1,38 @@ +repo = $repo; + $this->name = $name; + } + + public function update($name, $color) + { + return $this->api('repo')->labels()->update( + $this->owner->name, + $this->repo->name, + $this->name, + array( + 'name' => $name, + 'color' => $color + ) + ); + } + + public function remove() + { + return $this->api('repo')->labels()->remove( + $this->owner->name, + $this->repo->name, + $this->name + ); + } + +} diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php new file mode 100644 index 00000000000..620eb344eeb --- /dev/null +++ b/lib/Github/Model/Org.php @@ -0,0 +1,11 @@ +name.'/repos'; + } +} diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php new file mode 100644 index 00000000000..7bdec6b8b79 --- /dev/null +++ b/lib/Github/Model/Owner.php @@ -0,0 +1,38 @@ +name = $name; + } + + public function repo($name) + { + return new Repo($this, $name); + } + + public function createRepo($name, array $params = array()) + { + $params = array_merge(array( + 'name' => $name, + 'description' => null, + 'homepage' => null, + 'public' => true, + 'has_issues' => true, + 'has_wiki' => true, + 'has_downloads' => true, + 'team_id' => null, + 'auto_init' => false, + 'gitignore_template' => null + )); + + return $this->api('repo')->post($this->getRepoPath(), $params); + } + + abstract protected function getRepoPath(); +} diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php new file mode 100644 index 00000000000..4d380021412 --- /dev/null +++ b/lib/Github/Model/Repo.php @@ -0,0 +1,82 @@ +owner = $owner; + $this->name = $name; + } + + public function show() + { + return $this->api('repo')->show($this->owner->name, $this->name); + } + + public function createIssue($title, array $params) + { + $params['title'] = $title; + + $issue = $this->api('issue')->create( + $this->owner->name, + $this->name, + $params + ); + + return $this->getIssue($issue['number']); + } + + public function issue($number) + { + return new Issue($this->owner, $this, $number); + } + + public function labels() + { + return $this->api('repo')->labels()->all( + $this->owner->name, + $this->name + ); + } + + public function label($name) + { + return $this->api('repo')->labels()->show( + $this->owner->name, + $this->name, + $name + ); + } + + // Todo: check label doesnt exist + public function addLabel($name, $color) + { + return $this->api('repo')->labels()->create( + $this->owner->name, + $this->name, + array( + 'name' => $name, + 'color' => $color + ) + ); + } + + public function updateLabel($name, $color) + { + $label = new Label($this, $name); + + return $label->update($name, $color); + } + + public function removeLabel($name) + { + $label = new Label($this, $name); + + return $label->remove($name); + } +} diff --git a/lib/Github/Model/User.php b/lib/Github/Model/User.php new file mode 100644 index 00000000000..9586d40e17a --- /dev/null +++ b/lib/Github/Model/User.php @@ -0,0 +1,11 @@ + Date: Tue, 13 Nov 2012 17:15:22 +0000 Subject: [PATCH 02/20] Fixed missing param on array_merge --- lib/Github/Model/Owner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index 7bdec6b8b79..491713d367a 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -29,7 +29,7 @@ public function createRepo($name, array $params = array()) 'team_id' => null, 'auto_init' => false, 'gitignore_template' => null - )); + ), $params); return $this->api('repo')->post($this->getRepoPath(), $params); } From 1072b24d2260a9786f4fb22dd5a15d18acd38837 Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 13 Nov 2012 17:19:26 +0000 Subject: [PATCH 03/20] Update readme for model usage --- README.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.markdown b/README.markdown index 1ac462c0a20..be4f79da35e 100755 --- a/README.markdown +++ b/README.markdown @@ -58,6 +58,22 @@ $repositories = $client->api('user')->repositories('ornicar'); From `$client` object, you can access to all GitHub. +## Model usage (WIP) + +```php +createRepo('github-api'); +$issue = $repo->createIssue('This is a problem', array( + 'body' => 'Oh dear, now what?!' +)); + +$issue->addLabel('critical'); + +print_r($issue->labels()); +``` + ## Documentation See the `doc` directory for more detailed documentation. From 2187f8a618d4f7a9d6e57f993dcb465b4fc8082d Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 13 Nov 2012 17:46:01 +0000 Subject: [PATCH 04/20] Repo keys and tidying up Issue --- lib/Github/Model/Issue.php | 18 +++++++--------- lib/Github/Model/Repo.php | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 6631bc592c8..010ec4ba3c1 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -4,13 +4,11 @@ class Issue extends AbstractModel { - public $owner; public $repo; public $number; - public function __construct(Owner $owner, Repo $repo, $number = null) + public function __construct(Repo $repo, $number = null) { - $this->owner = $owner; $this->repo = $repo; $this->number = $number; } @@ -18,7 +16,7 @@ public function __construct(Owner $owner, Repo $repo, $number = null) public function show() { return $this->api('issue')->show( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number ); @@ -27,7 +25,7 @@ public function show() public function update(array $params) { return $this->api('issue')->update( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number, $params @@ -37,7 +35,7 @@ public function update(array $params) public function labels() { return $this->api('issue')->labels()->all( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number ); @@ -47,7 +45,7 @@ public function labels() public function addLabels(array $labels) { return $this->api('issue')->labels()->add( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number, $labels @@ -57,7 +55,7 @@ public function addLabels(array $labels) public function removeLabel($name) { return $this->api('issue')->labels()->remove( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number, $name @@ -67,7 +65,7 @@ public function removeLabel($name) public function replaceLabels(array $labels) { return $this->api('issue')->labels()->replace( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number, $labels @@ -77,7 +75,7 @@ public function replaceLabels(array $labels) public function clearLabels() { return $this->api('issue')->labels()->clear( - $this->owner->name, + $this->repo->owner->name, $this->repo->name, $this->number ); diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 4d380021412..88866ab976b 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -79,4 +79,47 @@ public function removeLabel($name) return $label->remove($name); } + + public function keys() + { + return $this->api('repo')->keys()->all( + $this->owner->name, + $this->name + ); + } + + public function key($title) + { + return $this->api('repo')->keys()->show( + $this->owner->name, + $this->name, + $title + ); + } + + public function addKey($title, $key) + { + return $this->api('repo')->keys()->create( + $this->owner->name, + $this->name, + array( + 'title' => $title, + 'key' => $key + ) + ); + } + + public function updateKey($id, $title, $key) + { + $deployKey = new DeployKey($this, $id); + + return $deployKey->update($title, $key); + } + + public function removeKey($id) + { + $deployKey = new DeployKey($this, $id); + + return $deployKey->remove(); + } } From 63e84f3f6fea1418edfba98ca3b82fa5387f732a Mon Sep 17 00:00:00 2001 From: matt Date: Tue, 13 Nov 2012 17:47:28 +0000 Subject: [PATCH 05/20] Add Key and DeployKey models --- lib/Github/Model/DeployKey.php | 36 ++++++++++++++++++++++++++++++++++ lib/Github/Model/Key.php | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 lib/Github/Model/DeployKey.php create mode 100644 lib/Github/Model/Key.php diff --git a/lib/Github/Model/DeployKey.php b/lib/Github/Model/DeployKey.php new file mode 100644 index 00000000000..5280f5e6102 --- /dev/null +++ b/lib/Github/Model/DeployKey.php @@ -0,0 +1,36 @@ +repo = $repo; + } + + public function update($title, $key) + { + return $this->api('repo')->keys()->update( + $this->repo->owner->name, + $this->repo->name, + $this->id, + array( + 'title' => $title, + 'key' => $key + ) + ); + } + + public function remove() + { + return $this->api('repo')->keys()->remove( + $this->repo->owner->name, + $this->repo->name, + $this->id + ); + } +} diff --git a/lib/Github/Model/Key.php b/lib/Github/Model/Key.php new file mode 100644 index 00000000000..2a2e10facb0 --- /dev/null +++ b/lib/Github/Model/Key.php @@ -0,0 +1,7 @@ + Date: Wed, 14 Nov 2012 13:16:41 +0000 Subject: [PATCH 06/20] More models --- lib/Github/Model/AbstractModel.php | 37 +++++++ lib/Github/Model/DeployKey.php | 20 +++- lib/Github/Model/Issue.php | 143 ++++++++++++++++++++++++---- lib/Github/Model/KeyInterface.php | 7 ++ lib/Github/Model/Label.php | 23 ++++- lib/Github/Model/Milestone.php | 72 ++++++++++++++ lib/Github/Model/Org.php | 4 +- lib/Github/Model/Owner.php | 46 +++++++-- lib/Github/Model/OwnerInterface.php | 8 ++ lib/Github/Model/Repo.php | 129 +++++++++++++++++++++---- lib/Github/Model/User.php | 4 +- 11 files changed, 438 insertions(+), 55 deletions(-) create mode 100644 lib/Github/Model/KeyInterface.php create mode 100644 lib/Github/Model/Milestone.php create mode 100644 lib/Github/Model/OwnerInterface.php diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php index 151cf249a61..0bb8da3ea54 100644 --- a/lib/Github/Model/AbstractModel.php +++ b/lib/Github/Model/AbstractModel.php @@ -3,9 +3,11 @@ namespace Github\Model; use Github\Client; +use Github\Exception; abstract class AbstractModel { + protected static $_properties; protected static $_client; public static function client(Client $client = null) @@ -17,9 +19,44 @@ public static function client(Client $client = null) return static::$_client; } + protected $_data = array(); + public function api($api) { return static::client()->api($api); } + public function hydrate(array $data = array()) + { + if (!empty($data)) { + foreach ($data as $k => $v) { + $this->$k = $v; + } + } + + return $this; + } + + public function __set($property, $value) + { + if (!in_array($property, static::$_properties)) { + throw new Exception\RuntimeException(sprintf('Property "%s" does not exist for %s object', $property, get_called_class())); + } + + $this->_data[$property] = $value; + } + + public function __get($property) + { + if (!in_array($property, static::$_properties)) { + throw new Exception\RuntimeException(sprintf('Property "%s" does not exist for %s object', $property, get_called_class())); + } + + if (isset($this->_data[$property])) { + return $this->_data[$property]; + } + + return null; + } + } diff --git a/lib/Github/Model/DeployKey.php b/lib/Github/Model/DeployKey.php index 5280f5e6102..63d05a20cb1 100644 --- a/lib/Github/Model/DeployKey.php +++ b/lib/Github/Model/DeployKey.php @@ -2,14 +2,26 @@ namespace Github\Model; -class DeployKey extends Key +class DeployKey extends Key implements KeyInterface { - public $repo; - public $id; + protected static $_properties = array( + 'id', + 'url', + 'title', + 'key' + ); + + public static function fromArray(Repo $repo, array $data) + { + $key = new DeployKey($repo, $data['id']); + + return $key->hydrate($data); + } public function __construct(Repo $repo, $id) { - $this->repo = $repo; + $this->repo = $repo; + $this->id = $id; } public function update($title, $key) diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 010ec4ba3c1..5c7f5d90c32 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -4,8 +4,58 @@ class Issue extends AbstractModel { - public $repo; - public $number; + protected static $_properties = array( + 'id', + 'repo', + 'url', + 'html_url', + 'number', + 'state', + 'title', + 'body', + 'user', + 'labels', + 'assignee', + 'milestone', + 'comments', + 'pull_request', + 'closed_at', + 'closed_by', + 'created_at', + 'updated_at' + ); + + public static function fromArray(Repo $repo, array $data) + { + if (isset($data['user'])) { + $data['user'] = User::fromArray($data['user']); + } + + if (isset($data['labels'])) { + $labels = array(); + foreach ($data['labels'] as $label) { + $labels[] = Label::fromArray($repo, $label); + } + + $data['labels'] = $labels; + } + + if (isset($data['assignee'])) { + $data['assignee'] = User::fromArray($data['assignee']); + } + + if (isset($data['milestone'])) { + + } + + if (isset($data['pull_request'])) { + + } + + $issue = new Issue($repo, $data['number']); + + return $issue->hydrate($data); + } public function __construct(Repo $repo, $number = null) { @@ -15,69 +65,130 @@ public function __construct(Repo $repo, $number = null) public function show() { - return $this->api('issue')->show( - $this->repo->owner->name, + $data = $this->api('issue')->show( + $this->repo->owner->login, $this->repo->name, $this->number ); + + return Issue::fromArray($this->repo, $data); } public function update(array $params) { - return $this->api('issue')->update( - $this->repo->owner->name, + $data = $this->api('issue')->update( + $this->repo->owner->login, $this->repo->name, $this->number, $params ); + + return Issue::fromArray($this->repo, $data); } public function labels() { - return $this->api('issue')->labels()->all( - $this->repo->owner->name, + $data = $this->api('issue')->labels()->all( + $this->repo->owner->login, $this->repo->name, $this->number ); + + $labels = array(); + foreach ($data as $label) { + $labels[] = Label::fromArray($this->repo, $label); + } + + return $labels; } // todo: create label if not exists public function addLabels(array $labels) { - return $this->api('issue')->labels()->add( - $this->repo->owner->name, + $data = $this->api('issue')->labels()->add( + $this->repo->owner->login, $this->repo->name, $this->number, $labels ); + + $labels = array(); + foreach ($data as $label) { + $labels[] = Label::fromArray($this->repo, $label); + } + + return $labels; + } + + public function addLabel($name) + { + return $this->addLabels(array($name)); } public function removeLabel($name) { - return $this->api('issue')->labels()->remove( - $this->repo->owner->name, + $data = $this->api('issue')->labels()->remove( + $this->repo->owner->login, $this->repo->name, $this->number, $name ); + + return Label::fromArray($this->repo, $data); } public function replaceLabels(array $labels) { - return $this->api('issue')->labels()->replace( - $this->repo->owner->name, + $data = $this->api('issue')->labels()->replace( + $this->repo->owner->login, $this->repo->name, $this->number, $labels ); + + $labels = array(); + foreach ($data as $label) { + $labels[] = Label::fromArray($this->repo, $label); + } + + return $labels; } public function clearLabels() { - return $this->api('issue')->labels()->clear( - $this->repo->owner->name, + $this->api('issue')->labels()->clear( + $this->repo->owner->login, $this->repo->name, $this->number ); + + return true; + } + + public function createMilestone($title, array $params = array()) + { + $params['title'] = $title; + + $data = $this->api('issue')->milestones()->create( + $this->repo->owner->login, + $this->repo->name, + $params + ); + + return Milestone::fromArray($this, $data); + } + + public function updateMilestone($number, array $data) + { + $milestone = new Milestone($this, $number); + + return $milestone->update($data); + } + + public function removeMilestone($number) + { + $milestone = new Milestone($this, $number); + + return $milestone->remove(); } } diff --git a/lib/Github/Model/KeyInterface.php b/lib/Github/Model/KeyInterface.php new file mode 100644 index 00000000000..f78740de1e5 --- /dev/null +++ b/lib/Github/Model/KeyInterface.php @@ -0,0 +1,7 @@ +hydrate($data); + } public function __construct(Repo $repo, $name) { @@ -15,7 +26,7 @@ public function __construct(Repo $repo, $name) public function update($name, $color) { - return $this->api('repo')->labels()->update( + $data = $this->api('repo')->labels()->update( $this->owner->name, $this->repo->name, $this->name, @@ -24,15 +35,19 @@ public function update($name, $color) 'color' => $color ) ); + + return Label::fromArray($this->repo, $data); } public function remove() { - return $this->api('repo')->labels()->remove( + $this->api('repo')->labels()->remove( $this->owner->name, $this->repo->name, $this->name ); + + return true; } } diff --git a/lib/Github/Model/Milestone.php b/lib/Github/Model/Milestone.php new file mode 100644 index 00000000000..7d447905207 --- /dev/null +++ b/lib/Github/Model/Milestone.php @@ -0,0 +1,72 @@ +hydrate($data); + } + + public function __construct(Issue $issue, $number) + { + $this->issue = $issue; + $this->number = $number; + } + + public function show() + { + $data = $this->api('issue')->milestones()->show( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->number + ); + + return Milestone::fromArray($this->issue, $data); + } + + public function update(array $data) + { + $data = $this->api('issue')->milestones()->update( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->number, + $data + ); + + return Milestone::fromArray($this->issue, $data); + } + + public function remove() + { + $this->api('issue')->milestones()->remove( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->number + ); + + return true; + } +} diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php index 620eb344eeb..472619056b0 100644 --- a/lib/Github/Model/Org.php +++ b/lib/Github/Model/Org.php @@ -2,9 +2,9 @@ namespace Github\Model; -class Org extends Owner +class Org extends Owner implements OwnerInterface { - public function getRepoPath() + public function getCreateRepoPath() { return 'orgs/'.$this->name.'/repos'; } diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index 491713d367a..ec4fc62db87 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -2,13 +2,39 @@ namespace Github\Model; -abstract class Owner extends AbstractModel +class Owner extends AbstractModel { - public $name; + protected static $_properties = array( + 'login', + 'id', + 'url', + 'avatar_url', + 'gravatar_id', + 'name', + 'company', + 'location', + 'email', + 'hireable', + 'bio', + 'public_repos', + 'public_gists', + 'followers', + 'following', + 'html_url', + 'created_at', + 'type' + ); - public function __construct($name) + public static function fromArray(array $data) { - $this->name = $name; + $owner = new Owner($data['login']); + + return $owner->hydrate($data); + } + + public function __construct($login) + { + $this->login = $login; } public function repo($name) @@ -31,8 +57,14 @@ public function createRepo($name, array $params = array()) 'gitignore_template' => null ), $params); - return $this->api('repo')->post($this->getRepoPath(), $params); - } + $data = $this->api('repo')->post( + $this->getCreateRepoPath(), + $params + ); + + $repo = new Repo($this, $name); + $repo->hydrate($data); - abstract protected function getRepoPath(); + return $repo; + } } diff --git a/lib/Github/Model/OwnerInterface.php b/lib/Github/Model/OwnerInterface.php new file mode 100644 index 00000000000..8721cd1d3ae --- /dev/null +++ b/lib/Github/Model/OwnerInterface.php @@ -0,0 +1,8 @@ +hydrate($data); + } public function __construct(Owner $owner, $name) { @@ -15,7 +77,12 @@ public function __construct(Owner $owner, $name) public function show() { - return $this->api('repo')->show($this->owner->name, $this->name); + $data = $this->api('repo')->show( + $this->owner->login, + $this->name + ); + + return Repo::fromArray($data); } public function createIssue($title, array $params) @@ -23,7 +90,7 @@ public function createIssue($title, array $params) $params['title'] = $title; $issue = $this->api('issue')->create( - $this->owner->name, + $this->owner->login, $this->name, $params ); @@ -33,37 +100,48 @@ public function createIssue($title, array $params) public function issue($number) { - return new Issue($this->owner, $this, $number); + return new Issue($this, $number); } public function labels() { - return $this->api('repo')->labels()->all( - $this->owner->name, + $data = $this->api('repo')->labels()->all( + $this->owner->login, $this->name ); + + $labels = array(); + foreach ($data as $label) { + $labels[] = Label::fromArray($this, $label); + } + + return $labels; } public function label($name) { - return $this->api('repo')->labels()->show( - $this->owner->name, + $data = $this->api('repo')->labels()->show( + $this->owner->login, $this->name, $name ); + + return Label::fromArray($this, $data); } // Todo: check label doesnt exist public function addLabel($name, $color) { - return $this->api('repo')->labels()->create( - $this->owner->name, + $data = $this->api('repo')->labels()->create( + $this->owner->login, $this->name, array( 'name' => $name, 'color' => $color ) - ); + ); + + return Label::fromArray($this, $data); } public function updateLabel($name, $color) @@ -82,31 +160,42 @@ public function removeLabel($name) public function keys() { - return $this->api('repo')->keys()->all( - $this->owner->name, + $data = $this->api('repo')->keys()->all( + $this->owner->login, $this->name ); + + $keys = array(); + foreach ($data as $key) { + $keys[] = DeployKey::fromArray($this, $key); + } + + return $keys; } - public function key($title) + public function key($id) { - return $this->api('repo')->keys()->show( - $this->owner->name, + $data = $this->api('repo')->keys()->show( + $this->owner->login, $this->name, - $title + $id ); + + return DeployKey::fromArray($this, $data); } public function addKey($title, $key) { - return $this->api('repo')->keys()->create( - $this->owner->name, + $data = $this->api('repo')->keys()->create( + $this->owner->login, $this->name, array( 'title' => $title, 'key' => $key ) ); + + return DeployKey::fromArray($this, $data); } public function updateKey($id, $title, $key) diff --git a/lib/Github/Model/User.php b/lib/Github/Model/User.php index 9586d40e17a..3578f886380 100644 --- a/lib/Github/Model/User.php +++ b/lib/Github/Model/User.php @@ -2,9 +2,9 @@ namespace Github\Model; -class User extends Owner +class User extends Owner implements OwnerInterface { - public function getRepoPath() + public function getCreateRepoPath() { return '/user/repos'; } From 6947ac6d5d6931217ed8240cb21a3abde53565fa Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 14 Nov 2012 14:47:10 +0000 Subject: [PATCH 07/20] Comments, events and milestones --- lib/Github/Model/Comment.php | 56 +++++++++++++++++++++++++++++++ lib/Github/Model/Event.php | 55 +++++++++++++++++++++++++++++++ lib/Github/Model/Issue.php | 64 ++++++++++++++++++++++++++++++++++-- lib/Github/Model/Repo.php | 26 +++++++++++++++ 4 files changed, 198 insertions(+), 3 deletions(-) create mode 100644 lib/Github/Model/Comment.php create mode 100644 lib/Github/Model/Event.php diff --git a/lib/Github/Model/Comment.php b/lib/Github/Model/Comment.php new file mode 100644 index 00000000000..cde3093758a --- /dev/null +++ b/lib/Github/Model/Comment.php @@ -0,0 +1,56 @@ +hydrate($data); + } + + public function __construct(Issue $issue, $id) + { + $this->id = $id; + $this->issue = $issue; + } + + public function update($body) + { + $data = $this->api('issue')->comments()->update( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->id, + array('body' => $body) + ); + + return Comment::fromArray($this->issue, $data); + } + + public function remove() + { + $this->api('issue')->comments()->remove( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->id + ); + + return true; + } +} diff --git a/lib/Github/Model/Event.php b/lib/Github/Model/Event.php new file mode 100644 index 00000000000..8e5b98f2a4d --- /dev/null +++ b/lib/Github/Model/Event.php @@ -0,0 +1,55 @@ +hydrate($data); + } + + public function __construct($repo, $event, $issue = null) + { + $this->repo = $repo; + $this->event = $event; + + if ($issue) { + $this->issue = $issue; + } + } + + public function show() + { + $data = $this->api('issue')->events()->show( + $this->repo->owner->login, + $this->repo->name, + $this->event + ); + + return Event::fromArray($this->repo, $data); + } +} diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 5c7f5d90c32..936d0afd138 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -27,6 +27,8 @@ class Issue extends AbstractModel public static function fromArray(Repo $repo, array $data) { + $issue = new Issue($repo, $data['number']); + if (isset($data['user'])) { $data['user'] = User::fromArray($data['user']); } @@ -45,15 +47,13 @@ public static function fromArray(Repo $repo, array $data) } if (isset($data['milestone'])) { - + $data['milestone'] = Milestone::fromArray($issue, $data['milestone']); } if (isset($data['pull_request'])) { } - $issue = new Issue($repo, $data['number']); - return $issue->hydrate($data); } @@ -165,6 +165,22 @@ public function clearLabels() return true; } + public function milestones(array $params = array()) + { + $data = $this->api('issue')->milestones()->all( + $this->repo->owner->login, + $this->repo->name, + $params + ); + + $milestones = array(); + foreach ($data as $milestone) { + $milestones[] = Milestone::fromArray($this, $milestone); + } + + return $milestones; + } + public function createMilestone($title, array $params = array()) { $params['title'] = $title; @@ -191,4 +207,46 @@ public function removeMilestone($number) return $milestone->remove(); } + + public function addComment($body) + { + $data = $this->api('issue')->comments()->create( + $this->repo->owner->login, + $this->repo->name, + $this->number, + array('body' => $body) + ); + + return Comment::fromArray($this, $data); + } + + public function updateComment($id, $body) + { + $comment = new Comment($this, $id); + + return $comment->update($body); + } + + public function removeComment($id) + { + $comment = new Comment($this, $id); + + return $comment->remove($id); + } + + public function events() + { + $data = $this->api('issue')->events()->all( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + + $events = array(); + foreach ($data as $event) { + $events[] = Event::fromArray($this->repo, $event, $this); + } + + return $events; + } } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 1db3601eeb7..02fff8ed0c5 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -211,4 +211,30 @@ public function removeKey($id) return $deployKey->remove(); } + + public function events() + { + $data = $this->api('issue')->events()->all( + $this->owner->login, + $this->name + ); + + $events = array(); + foreach ($data as $event) { + $events[] = Event::fromArray($this, $event); + } + + return $events; + } + + public function event($event) + { + $data = $this->api('issue')->events()->show( + $this->owner->login, + $this->name, + $event + ); + + return Event::fromArray($this, $data); + } } From f672c86a7c746d10b57a80d6b050bcab99ce7430 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Thu, 15 Nov 2012 13:57:10 +0000 Subject: [PATCH 08/20] Fix URL for show() method There is no need for `organization` name in the URL to fetch a team. --- lib/Github/Api/Organization/Teams.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Github/Api/Organization/Teams.php b/lib/Github/Api/Organization/Teams.php index 5310b5df0a6..3f231c13eaa 100644 --- a/lib/Github/Api/Organization/Teams.php +++ b/lib/Github/Api/Organization/Teams.php @@ -16,11 +16,6 @@ public function all($organization) return $this->get('orgs/'.urlencode($organization).'/teams'); } - public function show($organization, $team) - { - return $this->get('orgs/'.urlencode($organization).'/teams/'.urlencode($team)); - } - public function create($organization, array $params) { if (!isset($params['name'])) { @@ -36,6 +31,11 @@ public function create($organization, array $params) return $this->post('orgs/'.urlencode($organization).'/teams', $params); } + public function show($team) + { + return $this->get('teams/'.urlencode($team)); + } + public function update($team, array $params) { if (!isset($params['name'])) { From 6cb624e658d03664d2a3c59573baf0cf3c076816 Mon Sep 17 00:00:00 2001 From: matt Date: Thu, 15 Nov 2012 14:41:15 +0000 Subject: [PATCH 09/20] Org teams, issue comments and some others --- lib/Github/Model/Issue.php | 17 ++++ lib/Github/Model/Milestone.php | 16 ++++ lib/Github/Model/Org.php | 119 ++++++++++++++++++++++++++++ lib/Github/Model/Owner.php | 8 ++ lib/Github/Model/Repo.php | 18 ++++- lib/Github/Model/Team.php | 139 +++++++++++++++++++++++++++++++++ 6 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 lib/Github/Model/Team.php diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 936d0afd138..7984e42ba85 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -208,6 +208,23 @@ public function removeMilestone($number) return $milestone->remove(); } + public function comments($page = 1) + { + $data = $this->api('issue')->comments()->all( + $this->repo->owner->login, + $this->repo->name, + $this->number, + $page + ); + + $comments = array(); + foreach ($data as $comment) { + $comments[] = Comment::fromArray($this, $comment); + } + + return $comments; + } + public function addComment($body) { $data = $this->api('issue')->comments()->create( diff --git a/lib/Github/Model/Milestone.php b/lib/Github/Model/Milestone.php index 7d447905207..98e58947a02 100644 --- a/lib/Github/Model/Milestone.php +++ b/lib/Github/Model/Milestone.php @@ -69,4 +69,20 @@ public function remove() return true; } + + public function labels() + { + $data = $this->api('issue')->milestones()->labels( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->number + ); + + $labels = array(); + foreach ($data as $label) { + $labels[] = Label::fromArray($this->issue->repo, $label); + } + + return $labels; + } } diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php index 472619056b0..a4f1f5b69b2 100644 --- a/lib/Github/Model/Org.php +++ b/lib/Github/Model/Org.php @@ -8,4 +8,123 @@ public function getCreateRepoPath() { return 'orgs/'.$this->name.'/repos'; } + + public function show() + { + $data = $this->api('organization')->show( + $this->login + ); + + return Org::fromArray($data); + } + + public function update(array $params) + { + $data = $this->api('organization')->update( + $this->login, + $params + ); + + return Org::fromArray($data); + } + + public function members($type = null) + { + $data = $this->api('organization')->members()->all( + $this->login, + $type + ); + + $members = array(); + foreach ($data as $member) { + $members[] = User::fromArray($member); + } + + return $members; + } + + public function publicMembers() + { + return $this->members('public'); + } + + public function checkMembership($login) + { + return $this->api('organization')->members()->show( + $this->login, + $login + ); + } + + public function checkPublicMembership($login) + { + return $this->api('organization')->members()->check( + $this->login, + $login + ); + } + + public function publicizeMember($login) + { + return $this->api('organization')->members()->publicize( + $this->login, + $login + ); + } + + public function concealMember($login) + { + return $this->api('organization')->members()->conceal( + $this->login, + $login + ); + } + + public function removeMember($login) + { + return $this->api('organization')->members()->remove( + $this->login, + $login + ); + } + + public function createTeam($name, array $params = array()) + { + $params['name'] = $name; + + $data = $this->api('organization')->teams()->create( + $this->login, + $params + ); + + return Team::fromArray($this, $data); + } + + public function teams() + { + $data = $this->api('organization')->teams()->all( + $this->login + ); + + $teams = array(); + foreach ($data as $team) { + $teams[] = Team::fromArray($this, $team); + } + + return $teams; + } + + public function team($id) + { + $team = new Team($this, $id); + + return $team->show(); + } + + public function removeTeam($id) + { + $team = new Team($this, $id); + + return $team->remove(); + } } diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index ec4fc62db87..f0e8c2de476 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -12,8 +12,10 @@ class Owner extends AbstractModel 'gravatar_id', 'name', 'company', + 'blog', 'location', 'email', + 'billing_email', 'hireable', 'bio', 'public_repos', @@ -22,6 +24,12 @@ class Owner extends AbstractModel 'following', 'html_url', 'created_at', + 'total_private_repos', + 'owned_private_repos', + 'private_gists', + 'collaborators', + 'plan', + 'disk_usage', 'type' ); diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 02fff8ed0c5..7cb19a917d6 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -44,7 +44,7 @@ class Repo extends AbstractModel 'permissions' ); - protected static function fromArray(array $data) + public static function fromArray(array $data) { if (!isset($data['owner'])) { throw new Exception\MissingArgumentException(array('owner')); @@ -98,6 +98,22 @@ public function createIssue($title, array $params) return $this->getIssue($issue['number']); } + public function issues(array $params = array()) + { + $data = $this->api('issue')->all( + $this->owner->login, + $this->name, + $params + ); + + $issues = array(); + foreach ($data as $issue) { + $issues[] = Issue::fromArray($this, $issue); + } + + return $issues; + } + public function issue($number) { return new Issue($this, $number); diff --git a/lib/Github/Model/Team.php b/lib/Github/Model/Team.php new file mode 100644 index 00000000000..96bea8de136 --- /dev/null +++ b/lib/Github/Model/Team.php @@ -0,0 +1,139 @@ +hydrate($data); + } + + public function __construct(Org $org, $id) + { + $this->org = $org; + $this->id = $id; + } + + public function show() + { + $data = $this->api('organization')->teams()->show( + $this->id + ); + + return Team::fromArray($this->org, $data); + } + + public function update(array $params) + { + $data = $this-api('organization')->teams()->update( + $this->id, + $params + ); + + return Team::fromArray($this->org, $data); + } + + public function remove() + { + return $this->api('organization')->teams()->remove( + $this->id + ); + } + + public function members() + { + $data = $this->api('organization')->teams()->members( + $this->id + ); + + $members = array(); + foreach ($data as $member) { + $members[] = User::fromArray($member); + } + + return $member; + } + + public function check($login) + { + return $this->api('organization')->teams()->check( + $this->id, + $login + ); + } + + public function addMember($login) + { + return $this->api('organization')->teams()->addMember( + $this->id, + $login + ); + } + + public function removeMember($login) + { + return $this->api('organization')->teams()->removeMember( + $this->id, + $login + ); + } + + public function repositories() + { + $data = $this->api('organization')->teams()->repositories( + $this->id + ); + + $repos = array(); + foreach ($data as $repo) { + $repos[] = Repo::fromArray($repo); + } + + return $repos; + } + + public function repository($login, $repository) + { + $data = $this->api('organization')->teams()->repository( + $this->id, + $login, + $repository + ); + + return Repo::fromArray($data); + } + + public function addRepository($login, $repository) + { + $data = $this->api('organization')->teams()->addRepository( + $this->id, + $login, + $repository + ); + + return Repo::fromArray($data); + } + + public function removeRepository($login, $repository) + { + return $this->api('organization')->teams()->removeRepository( + $this->id, + $login, + $repository + ); + } + +} From eaa659b788d30d7a5ba0277da2a56bd8823e82c4 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Thu, 15 Nov 2012 16:15:32 +0000 Subject: [PATCH 10/20] Pull requests --- lib/Github/Model/Commit.php | 70 ++++++++++++++ lib/Github/Model/Issue.php | 2 +- lib/Github/Model/PullRequest.php | 155 +++++++++++++++++++++++++++++++ lib/Github/Model/Ref.php | 29 ++++++ lib/Github/Model/Repo.php | 30 ++++++ 5 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 lib/Github/Model/Commit.php create mode 100644 lib/Github/Model/PullRequest.php create mode 100644 lib/Github/Model/Ref.php diff --git a/lib/Github/Model/Commit.php b/lib/Github/Model/Commit.php new file mode 100644 index 00000000000..471c93ec81c --- /dev/null +++ b/lib/Github/Model/Commit.php @@ -0,0 +1,70 @@ +hydrate($data); + } + + public function __construct(Repo $repo, $sha) + { + $this->repo = $repo; + $this->sha = $sha; + } + + public function show() + { + $data = $this->api('git_data')->commits()->show( + $this->repo->owner->login, + $this->repo->name, + $this->sha + ); + + return Commit::fromArray($this->repo, $data); + } + +} diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 7984e42ba85..20b3dcc446e 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -57,7 +57,7 @@ public static function fromArray(Repo $repo, array $data) return $issue->hydrate($data); } - public function __construct(Repo $repo, $number = null) + public function __construct(Repo $repo, $number) { $this->repo = $repo; $this->number = $number; diff --git a/lib/Github/Model/PullRequest.php b/lib/Github/Model/PullRequest.php new file mode 100644 index 00000000000..cb59d160121 --- /dev/null +++ b/lib/Github/Model/PullRequest.php @@ -0,0 +1,155 @@ +hydrate($data); + } + + public function __construct(Repo $repo, $number) + { + $this->repo = $repo; + $this->number = $number; + } + + public function show() + { + $data = $this->api('pull_request')->show( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + + return PullRequest::fromArray($this->repo, $data); + } + + public function update(array $params) + { + $data = $this->api('pull_request')->update( + $this->repo->owner->login, + $this->repo->name, + $this->number, + $params + ); + + return PullRequest::fromArray($this->repo, $data); + } + + public function commits() + { + $data = $this->api('pull_request')->commits( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + + $commits = array(); + foreach ($data as $commit) { + $commits[] = Commit::fromArray($this->repo, $commit); + } + + return $commits; + } + + public function files() + { + $data = $this->api('pull_request')->files( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + + $files = array(); + foreach ($data as $file) { + $files[] = (object) $file; + } + + return $files; + } + + public function merged() + { + return $data = $this->api('pull_request')->merged( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + } + + // TODO: Handle result better + public function merge($message = null) + { + $data = $this->api('pull_request')->merged( + $this->repo->owner->login, + $this->repo->name, + $this->number, + $message + ); + + return (object) $data; + } +} diff --git a/lib/Github/Model/Ref.php b/lib/Github/Model/Ref.php new file mode 100644 index 00000000000..99cad53115a --- /dev/null +++ b/lib/Github/Model/Ref.php @@ -0,0 +1,29 @@ +hydrate($data); + } +} diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 7cb19a917d6..094312bb05e 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -253,4 +253,34 @@ public function event($event) return Event::fromArray($this, $data); } + + public function pullRequests($state = null) + { + $data = $this->api('pull_request')->all( + $this->owner->login, + $this->name, + $state + ); + + $pull_requests = array(); + foreach ($data as $pull_request) { + $pull_requests[] = PullRequest::fromArray($this, $pull_request); + } + + return $pull_requests; + } + + public function pullRequest($number) + { + $pull_request = new PullRequest($this, $number); + + return $pull_request->show(); + } + + public function updatePullRequest($number, array $params) + { + $pull_request = new PullRequest($this, $number); + + return $pull_request->update($params); + } } From d12195b3e6b742f81be0e176f47e0775e87cf4f6 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Thu, 15 Nov 2012 16:23:47 +0000 Subject: [PATCH 11/20] Create pull request --- lib/Github/Model/Repo.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 094312bb05e..9572ea5d433 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -254,6 +254,17 @@ public function event($event) return Event::fromArray($this, $data); } + public function createPullRequest(array $params) + { + $data = $this->api('pull_request')->create( + $this->owner->login, + $this->name, + $params + ); + + return PullRequest::fromArray($this, $data); + } + public function pullRequests($state = null) { $data = $this->api('pull_request')->all( From f0318eec5a131c942dfff3266962abb257ababe1 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Thu, 15 Nov 2012 16:41:01 +0000 Subject: [PATCH 12/20] Added factory method. Set owner to return correct object type --- lib/Github/Model/AbstractModel.php | 15 +++++++++++++++ lib/Github/Model/Owner.php | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php index 0bb8da3ea54..8e605817750 100644 --- a/lib/Github/Model/AbstractModel.php +++ b/lib/Github/Model/AbstractModel.php @@ -21,6 +21,21 @@ public static function client(Client $client = null) protected $_data = array(); + public static function factory() + { + $args = func_get_args(); + $last = end(array_values($args)); + + $obj = new \ReflectionClass(get_called_class()); + + if ($last instanceof Client) { + $last = array_pop($args); + static::client($last); + } + + return $obj->newInstanceArgs($args); + } + public function api($api) { return static::client()->api($api); diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index f0e8c2de476..8b0ca0634b9 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -35,7 +35,7 @@ class Owner extends AbstractModel public static function fromArray(array $data) { - $owner = new Owner($data['login']); + $owner = new static($data['login']); return $owner->hydrate($data); } From c59a863b02af82851d30097bacbe579bca0084f4 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 09:54:29 +0000 Subject: [PATCH 13/20] Factorize --- lib/Github/Model/AbstractModel.php | 4 ++-- lib/Github/Model/Comment.php | 4 ++-- lib/Github/Model/Commit.php | 2 +- lib/Github/Model/DeployKey.php | 2 +- lib/Github/Model/Event.php | 4 ++-- lib/Github/Model/Issue.php | 18 +++++------------ lib/Github/Model/Label.php | 2 +- lib/Github/Model/Milestone.php | 4 ++-- lib/Github/Model/Org.php | 8 ++------ lib/Github/Model/Owner.php | 11 +++------- lib/Github/Model/PullRequest.php | 2 +- lib/Github/Model/Ref.php | 4 ++-- lib/Github/Model/Repo.php | 32 ++++++++++-------------------- lib/Github/Model/Team.php | 2 +- 14 files changed, 35 insertions(+), 64 deletions(-) diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php index 8e605817750..c06a8763b43 100644 --- a/lib/Github/Model/AbstractModel.php +++ b/lib/Github/Model/AbstractModel.php @@ -19,8 +19,6 @@ public static function client(Client $client = null) return static::$_client; } - protected $_data = array(); - public static function factory() { $args = func_get_args(); @@ -36,6 +34,8 @@ public static function factory() return $obj->newInstanceArgs($args); } + protected $_data = array(); + public function api($api) { return static::client()->api($api); diff --git a/lib/Github/Model/Comment.php b/lib/Github/Model/Comment.php index cde3093758a..ae6f30c7911 100644 --- a/lib/Github/Model/Comment.php +++ b/lib/Github/Model/Comment.php @@ -16,12 +16,12 @@ class Comment extends AbstractModel public static function fromArray(Issue $issue, array $data) { + $comment = Comment::factory($issue, $data['id']); + if (isset($data['user'])) { $data['user'] = User::fromArray($data['user']); } - $comment = new Comment($issue, $data['id']); - return $comment->hydrate($data); } diff --git a/lib/Github/Model/Commit.php b/lib/Github/Model/Commit.php index 471c93ec81c..e0b61da33d6 100644 --- a/lib/Github/Model/Commit.php +++ b/lib/Github/Model/Commit.php @@ -18,7 +18,7 @@ class Commit extends AbstractModel public static function fromArray(Repo $repo, array $data) { - $commit = new Commit($repo, $data['sha']); + $commit = Commit::factory($repo, $data['sha']); if (isset($data['author'])) { $data['author'] = User::fromArray($data['author']); diff --git a/lib/Github/Model/DeployKey.php b/lib/Github/Model/DeployKey.php index 63d05a20cb1..1a7d0c74ed5 100644 --- a/lib/Github/Model/DeployKey.php +++ b/lib/Github/Model/DeployKey.php @@ -13,7 +13,7 @@ class DeployKey extends Key implements KeyInterface public static function fromArray(Repo $repo, array $data) { - $key = new DeployKey($repo, $data['id']); + $key = DeployKey::factory($repo, $data['id']); return $key->hydrate($data); } diff --git a/lib/Github/Model/Event.php b/lib/Github/Model/Event.php index 8e5b98f2a4d..85bba4ac7e5 100644 --- a/lib/Github/Model/Event.php +++ b/lib/Github/Model/Event.php @@ -19,6 +19,8 @@ class Event extends AbstractModel public static function fromArray(Repo $repo, array $data, Issue $issue = null) { + $event = Event::factory($repo, $data['event'], $issue); + if (isset($data['actor'])) { $data['actor'] = User::fromArray($data['actor']); } @@ -27,8 +29,6 @@ public static function fromArray(Repo $repo, array $data, Issue $issue = null) $data['issue'] = Issue::fromArray($repo, $data['issue']); } - $event = new Event($repo, $data['event'], $issue); - return $event->hydrate($data); } diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 20b3dcc446e..708ec4f6acb 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -27,7 +27,7 @@ class Issue extends AbstractModel public static function fromArray(Repo $repo, array $data) { - $issue = new Issue($repo, $data['number']); + $issue = Issue::factory($repo, $data['number']); if (isset($data['user'])) { $data['user'] = User::fromArray($data['user']); @@ -196,16 +196,12 @@ public function createMilestone($title, array $params = array()) public function updateMilestone($number, array $data) { - $milestone = new Milestone($this, $number); - - return $milestone->update($data); + return Milestone::factory($this, $number)->update($data); } public function removeMilestone($number) { - $milestone = new Milestone($this, $number); - - return $milestone->remove(); + return Milestone::factory($this, $number)->remove(); } public function comments($page = 1) @@ -239,16 +235,12 @@ public function addComment($body) public function updateComment($id, $body) { - $comment = new Comment($this, $id); - - return $comment->update($body); + return Comment::factory($this, $id)->update($body); } public function removeComment($id) { - $comment = new Comment($this, $id); - - return $comment->remove($id); + return Comment::factory($this, $id)->remove($id); } public function events() diff --git a/lib/Github/Model/Label.php b/lib/Github/Model/Label.php index 9d1b236f66f..6b82a8ef9a3 100644 --- a/lib/Github/Model/Label.php +++ b/lib/Github/Model/Label.php @@ -13,7 +13,7 @@ class Label extends AbstractModel public static function fromArray(Repo $repo, array $data) { - $label = new Label($repo, $data['name']); + $label = Label::factory($repo, $data['name']); return $label->hydrate($data); } diff --git a/lib/Github/Model/Milestone.php b/lib/Github/Model/Milestone.php index 98e58947a02..99bbd5508f1 100644 --- a/lib/Github/Model/Milestone.php +++ b/lib/Github/Model/Milestone.php @@ -21,12 +21,12 @@ class Milestone extends AbstractModel public static function fromArray($issue, array $data) { + $milestone = Milestone::factory($issue, $data['number']); + if (isset($data['creator'])) { $data['creator'] = User::fromArray($data['creator']); } - $milestone = new Milestone($issue, $data['number']); - return $milestone->hydrate($data); } diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php index a4f1f5b69b2..15a6d62e401 100644 --- a/lib/Github/Model/Org.php +++ b/lib/Github/Model/Org.php @@ -116,15 +116,11 @@ public function teams() public function team($id) { - $team = new Team($this, $id); - - return $team->show(); + return Team::factory($this, $id)->show(); } public function removeTeam($id) { - $team = new Team($this, $id); - - return $team->remove(); + return Team::factory($this, $id)->remove(); } } diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index 8b0ca0634b9..c4ac319bb76 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -35,9 +35,7 @@ class Owner extends AbstractModel public static function fromArray(array $data) { - $owner = new static($data['login']); - - return $owner->hydrate($data); + return static::factory($data['login'])->hydrate($data); } public function __construct($login) @@ -47,7 +45,7 @@ public function __construct($login) public function repo($name) { - return new Repo($this, $name); + return Repo::factory($this, $name)->show(); } public function createRepo($name, array $params = array()) @@ -70,9 +68,6 @@ public function createRepo($name, array $params = array()) $params ); - $repo = new Repo($this, $name); - $repo->hydrate($data); - - return $repo; + return Repo::factory($this, $name)->hydrate($data); } } diff --git a/lib/Github/Model/PullRequest.php b/lib/Github/Model/PullRequest.php index cb59d160121..a25f9c5c6b0 100644 --- a/lib/Github/Model/PullRequest.php +++ b/lib/Github/Model/PullRequest.php @@ -41,7 +41,7 @@ class PullRequest extends Issue public static function fromArray(Repo $repo, array $data) { - $pull_request = new PullRequest($repo, $data['number']); + $pull_request = PullRequest::factory($repo, $data['number']); if (isset($data['head'])) { $data['head'] = Ref::fromArray($data['head']); diff --git a/lib/Github/Model/Ref.php b/lib/Github/Model/Ref.php index 99cad53115a..696aa465689 100644 --- a/lib/Github/Model/Ref.php +++ b/lib/Github/Model/Ref.php @@ -14,6 +14,8 @@ class Ref extends AbstractModel public static function fromArray(array $data) { + $ref = Ref::factory(); + if (isset($data['repo'])) { $data['repo'] = Repo::fromArray($data['repo']); } @@ -22,8 +24,6 @@ public static function fromArray(array $data) $data['user'] = User::fromArray($data['user']); } - $ref = new Ref(); - return $ref->hydrate($data); } } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 9572ea5d433..5d3345b7108 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -47,11 +47,13 @@ class Repo extends AbstractModel public static function fromArray(array $data) { if (!isset($data['owner'])) { - throw new Exception\MissingArgumentException(array('owner')); + throw new Exception\MissingArgumentException('owner'); } $data['owner'] = Owner::fromArray($data['owner']); + $repo = Repo::factory($data['owner'], $data['name']); + if (isset($data['organization'])) { $data['organization'] = Org::fromArray($data['organization']); } @@ -64,8 +66,6 @@ public static function fromArray(array $data) $data['source'] = Repo::fromArray($data['source']); } - $repo = new Repo($data['owner'], $data['name']); - return $repo->hydrate($data); } @@ -116,7 +116,7 @@ public function issues(array $params = array()) public function issue($number) { - return new Issue($this, $number); + Issue::factory($this, $number)->show(); } public function labels() @@ -162,16 +162,12 @@ public function addLabel($name, $color) public function updateLabel($name, $color) { - $label = new Label($this, $name); - - return $label->update($name, $color); + return Label::factory($this, $name)->update($name, $color); } public function removeLabel($name) { - $label = new Label($this, $name); - - return $label->remove($name); + return Label::factory($this, $name)->remove($name); } public function keys() @@ -216,16 +212,12 @@ public function addKey($title, $key) public function updateKey($id, $title, $key) { - $deployKey = new DeployKey($this, $id); - - return $deployKey->update($title, $key); + return DeployKey::factory($this, $id)->update($title, $key); } public function removeKey($id) { - $deployKey = new DeployKey($this, $id); - - return $deployKey->remove(); + return DeployKey::factory($this, $id)->remove(); } public function events() @@ -283,15 +275,11 @@ public function pullRequests($state = null) public function pullRequest($number) { - $pull_request = new PullRequest($this, $number); - - return $pull_request->show(); + return PullRequest::factory($this, $number)->show(); } public function updatePullRequest($number, array $params) { - $pull_request = new PullRequest($this, $number); - - return $pull_request->update($params); + return PullRequest::factory($this, $number)->update($params); } } diff --git a/lib/Github/Model/Team.php b/lib/Github/Model/Team.php index 96bea8de136..5c04dd21692 100644 --- a/lib/Github/Model/Team.php +++ b/lib/Github/Model/Team.php @@ -16,7 +16,7 @@ class Team extends AbstractModel public static function fromArray(Org $org, array $data) { - $team = new Team($org, $data['id']); + $team = Team::factory($org, $data['id']); return $team->hydrate($data); } From ab86cae7d576c65f8816819e17b6658fac7b9f6c Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 11:24:06 +0000 Subject: [PATCH 14/20] Review comments. --- lib/Github/Model/Comment.php | 13 ++++- lib/Github/Model/CommentInterface.php | 8 +++ lib/Github/Model/Issue.php | 2 +- lib/Github/Model/PullRequest.php | 30 +++++++++++ lib/Github/Model/Repo.php | 2 +- lib/Github/Model/ReviewComment.php | 71 +++++++++++++++++++++++++++ 6 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 lib/Github/Model/CommentInterface.php create mode 100644 lib/Github/Model/ReviewComment.php diff --git a/lib/Github/Model/Comment.php b/lib/Github/Model/Comment.php index ae6f30c7911..5ff261bc9a2 100644 --- a/lib/Github/Model/Comment.php +++ b/lib/Github/Model/Comment.php @@ -2,7 +2,7 @@ namespace Github\Model; -class Comment extends AbstractModel +class Comment extends AbstractModel implements CommentInterface { protected static $_properties = array( 'issue', @@ -31,6 +31,17 @@ public function __construct(Issue $issue, $id) $this->issue = $issue; } + public function show() + { + $data = $this->api('issue')->comments()->show( + $this->issue->repo->owner->login, + $this->issue->repo->name, + $this->id + ); + + return Comment::fromArray($this->issue, $data); + } + public function update($body) { $data = $this->api('issue')->comments()->update( diff --git a/lib/Github/Model/CommentInterface.php b/lib/Github/Model/CommentInterface.php new file mode 100644 index 00000000000..d3cbf3e9499 --- /dev/null +++ b/lib/Github/Model/CommentInterface.php @@ -0,0 +1,8 @@ +api('issue')->comments()->create( $this->repo->owner->login, diff --git a/lib/Github/Model/PullRequest.php b/lib/Github/Model/PullRequest.php index a25f9c5c6b0..0e154c75da7 100644 --- a/lib/Github/Model/PullRequest.php +++ b/lib/Github/Model/PullRequest.php @@ -152,4 +152,34 @@ public function merge($message = null) return (object) $data; } + + public function reviewComments() + { + $data = $this->api('pull_request')->comments()->all( + $this->repo->owner->login, + $this->repo->name, + $this->number + ); + + $comments = array(); + foreach ($data as $comment) { + $comments[] = ReviewComment::fromArray($this, $comment); + } + + return $comments; + } + + public function addReviewComment($body, array $params = array()) + { + $params['body'] = $body; + + $data = $this->api('pull_request')->comments()->create( + $this->repo->owner->login, + $this->repo->name, + $this->number, + $params + ); + + return ReviewComment::fromArray($this, $data); + } } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 5d3345b7108..6dc51bd175c 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -116,7 +116,7 @@ public function issues(array $params = array()) public function issue($number) { - Issue::factory($this, $number)->show(); + return Issue::factory($this, $number)->show(); } public function labels() diff --git a/lib/Github/Model/ReviewComment.php b/lib/Github/Model/ReviewComment.php new file mode 100644 index 00000000000..6c4d442ec87 --- /dev/null +++ b/lib/Github/Model/ReviewComment.php @@ -0,0 +1,71 @@ +hydrate($data); + } + + public function __construct(PullRequest $pull_request, $id) + { + $this->id = $id; + $this->pull_request = $pull_request; + } + + public function show() + { + $data = $this->api('pull_request')->comments()->show( + $this->pull_request->repo->owner->login, + $this->pull_request->repo->name, + $this->id + ); + + return ReviewComment::fromArray($this->pull_request, $data); + } + + public function update($body) + { + $data = $this->api('pull_request')->comments()->update( + $this->pull_request->repo->owner->login, + $this->pull_request->repo->name, + $this->id, + array('body' => $body) + ); + + return ReviewComment::fromArray($this->pull_request, $data); + } + + public function remove() + { + $this->api('pull_request')->comments()->remove( + $this->pull_request->repo->owner->login, + $this->pull_request->repo->name, + $this->id + ); + + return true; + } +} From 692019907ada6566736931e6fc5b37dc5f02f799 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 11:45:58 +0000 Subject: [PATCH 15/20] Add repo remove() and update(), and language model --- lib/Github/Model/Language.php | 21 ++++++++++++++ lib/Github/Model/Owner.php | 12 +++++++- lib/Github/Model/Repo.php | 52 +++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 lib/Github/Model/Language.php diff --git a/lib/Github/Model/Language.php b/lib/Github/Model/Language.php new file mode 100644 index 00000000000..48f384b0685 --- /dev/null +++ b/lib/Github/Model/Language.php @@ -0,0 +1,21 @@ +hydrate($data); + } +} diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index c4ac319bb76..61cacec0f23 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -68,6 +68,16 @@ public function createRepo($name, array $params = array()) $params ); - return Repo::factory($this, $name)->hydrate($data); + return Repo::fromArray($data); + } + + public function updateRepo($name, array $params) + { + return Repo::factory($this, $name)->update($params); + } + + public function removeRepo($name) + { + return Repo::factory($this, $name)->remove(); } } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 6dc51bd175c..33549a5faa3 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -85,6 +85,25 @@ public function show() return Repo::fromArray($data); } + public function update(array $params) + { + $data = $this->api('repo')->update( + $this->owner->login, + $this->name, + $params + ); + + return Repo::fromArray($data); + } + + public function remove() + { + return $this->api('repo')->remove( + $this->owner->login, + $this->name + ); + } + public function createIssue($title, array $params) { $params['title'] = $title; @@ -282,4 +301,37 @@ public function updatePullRequest($number, array $params) { return PullRequest::factory($this, $number)->update($params); } + + public function languages() + { + $data = $this->api('repos')->languages( + $this->owner->login, + $this->name + ); + + $languages = array(); + foreach ($data as $language => $id) { + $languages[] = Language::fromArray(array( + 'name' => $language, + 'size' => $id + )); + } + + return $languages; + } + + public function teams() + { + $data = $this->api('repos')->teams()->all( + $this->owner->login, + $this->name + ); + + $teams = array(); + foreach ($data as $team) { + $teams[] = Team::fromArray($this->owner, $team); + } + + return $teams; + } } From df4d267c95c30901dc52af47983f2102442da2ac Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 13:59:46 +0000 Subject: [PATCH 16/20] Repo forks and hooks --- lib/Github/Model/Hook.php | 73 +++++++++++++++++++++++++++++++++++++ lib/Github/Model/Repo.php | 77 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 lib/Github/Model/Hook.php diff --git a/lib/Github/Model/Hook.php b/lib/Github/Model/Hook.php new file mode 100644 index 00000000000..6c24ea4fe8f --- /dev/null +++ b/lib/Github/Model/Hook.php @@ -0,0 +1,73 @@ +hydrate($data); + } + + public function __construct(Repo $repo, $id) + { + $this->id = $id; + $this->repo = $repo; + } + + public function show() + { + $data = $this->api('repo')->hooks()->show( + $this->repo->owner->login, + $this->repo->name, + $this->id + ); + + return Hook::fromArray($this, $data); + } + + public function update(array $params) + { + $data = $this->api('repo')->hooks()->update( + $this->repo->owner->login, + $this->repo->name, + $this->id, + $params + ); + + return Hook::fromArray($this, $data); + } + + public function remove() + { + return $this->api('repo')->hooks()->remove( + $this->repo->owner->login, + $this->repo->name, + $this->id + ); + } + + public function test() + { + return $this->api('repo')->hooks()->test( + $this->repo->owner->login, + $this->repo->name, + $this->id + ); + } + +} diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 33549a5faa3..d7275a842d0 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -334,4 +334,81 @@ public function teams() return $teams; } + + public function addHook($name, array $config, array $events = null, $active = true) + { + $data = $this->api('repo')->hooks()->create( + $this->owner->login, + $this->name, + array( + 'name' => $name, + 'config' => $config, + 'events' => $events, + 'active' => $active + ) + ); + + return Hook::fromArray($this, $data); + } + + public function hook($id) + { + return Hook::factory($this, $id)->show(); + } + + public function hooks() + { + $data = $this->api('repo')->hooks()->all( + $this->owner->login, + $this->name + ); + + $hooks = array(); + foreach ($data as $hook) { + $hooks[] = Hook::fromArray($this, $hook); + } + + return $hooks; + } + + public function updateHook($id, array $params) + { + return Hook::factory($this, $id)->update($params); + } + + public function removeHook($id) + { + return Hook::factory($this, $id)->remove(); + } + + public function fork($org = null) + { + $data = $this->api('repo')->forks()->create( + $this->owner->login, + $this->name, + array( + 'org' => $org + ) + ); + + return Repo::fromArray($data); + } + + public function forks($sort = null) + { + $data = $this->api('repo')->forks()->all( + $this->owner->login, + $this->name, + array( + 'sort' => $sort + ) + ); + + $forks = array(); + foreach ($data as $fork) { + $forks[] = Repo::fromArray($fork); + } + + return $forks; + } } From f7f647316dbfb21fa514819cab80394e53aa7b30 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 14:48:15 +0000 Subject: [PATCH 17/20] Tidy up Owner::createRepo() to use \Api\Repo->create() properly Standardized responses (addresses #1) --- lib/Github/Model/DeployKey.php | 8 +++-- lib/Github/Model/Hook.php | 19 ++++++++++-- lib/Github/Model/Issue.php | 2 +- lib/Github/Model/Label.php | 4 +-- lib/Github/Model/Org.php | 6 ++-- lib/Github/Model/Owner.php | 17 +++++----- lib/Github/Model/OwnerInterface.php | 1 - lib/Github/Model/Repo.php | 13 ++++++-- lib/Github/Model/Team.php | 48 +++++++++++++++++++++-------- lib/Github/Model/User.php | 4 --- 10 files changed, 82 insertions(+), 40 deletions(-) diff --git a/lib/Github/Model/DeployKey.php b/lib/Github/Model/DeployKey.php index 1a7d0c74ed5..2a55a5ea3c7 100644 --- a/lib/Github/Model/DeployKey.php +++ b/lib/Github/Model/DeployKey.php @@ -26,7 +26,7 @@ public function __construct(Repo $repo, $id) public function update($title, $key) { - return $this->api('repo')->keys()->update( + $data = $this->api('repo')->keys()->update( $this->repo->owner->name, $this->repo->name, $this->id, @@ -35,14 +35,18 @@ public function update($title, $key) 'key' => $key ) ); + + return DeployKey::fromArray($this->repo, $data); } public function remove() { - return $this->api('repo')->keys()->remove( + $this->api('repo')->keys()->remove( $this->repo->owner->name, $this->repo->name, $this->id ); + + return true; } } diff --git a/lib/Github/Model/Hook.php b/lib/Github/Model/Hook.php index 6c24ea4fe8f..471ace13c66 100644 --- a/lib/Github/Model/Hook.php +++ b/lib/Github/Model/Hook.php @@ -13,13 +13,22 @@ class Hook extends AbstractModel 'events', 'active', 'config', - 'id' + 'id', + 'last_response' ); public static function fromArray(Repo $repo, array $data) { $hook = Hook::factory($repo, $data['id']); + if (isset($data['last_response'])) { + $data['last_response'] = (object) $data['last_response']; + } + + if (isset($data['config'])) { + $data['config'] = (object) $data['config']; + } + return $hook->hydrate($data); } @@ -54,20 +63,24 @@ public function update(array $params) public function remove() { - return $this->api('repo')->hooks()->remove( + $this->api('repo')->hooks()->remove( $this->repo->owner->login, $this->repo->name, $this->id ); + + return true; } public function test() { - return $this->api('repo')->hooks()->test( + $this->api('repo')->hooks()->test( $this->repo->owner->login, $this->repo->name, $this->id ); + + return true; } } diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 51b5936f18f..ab8bf71eaec 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -201,7 +201,7 @@ public function updateMilestone($number, array $data) public function removeMilestone($number) { - return Milestone::factory($this, $number)->remove(); + return Milestone::factory($this, $number)->remove(); } public function comments($page = 1) diff --git a/lib/Github/Model/Label.php b/lib/Github/Model/Label.php index 6b82a8ef9a3..7b982de90e3 100644 --- a/lib/Github/Model/Label.php +++ b/lib/Github/Model/Label.php @@ -27,7 +27,7 @@ public function __construct(Repo $repo, $name) public function update($name, $color) { $data = $this->api('repo')->labels()->update( - $this->owner->name, + $this->repo->owner->login, $this->repo->name, $this->name, array( @@ -42,7 +42,7 @@ public function update($name, $color) public function remove() { $this->api('repo')->labels()->remove( - $this->owner->name, + $this->repo->owner->login, $this->repo->name, $this->name ); diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php index 15a6d62e401..c76cd84a5e3 100644 --- a/lib/Github/Model/Org.php +++ b/lib/Github/Model/Org.php @@ -4,9 +4,11 @@ class Org extends Owner implements OwnerInterface { - public function getCreateRepoPath() + public function createRepo($name, array $params = array()) { - return 'orgs/'.$this->name.'/repos'; + $params['organization'] = $this->login; + + return parent::createRepo($name, $params); } public function show() diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index 61cacec0f23..ba6b0f38e2e 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -51,21 +51,18 @@ public function repo($name) public function createRepo($name, array $params = array()) { $params = array_merge(array( - 'name' => $name, 'description' => null, 'homepage' => null, 'public' => true, - 'has_issues' => true, - 'has_wiki' => true, - 'has_downloads' => true, - 'team_id' => null, - 'auto_init' => false, - 'gitignore_template' => null + 'organization' => null ), $params); - $data = $this->api('repo')->post( - $this->getCreateRepoPath(), - $params + $data = $this->api('repo')->create( + $name, + $params['description'], + $params['homepage'], + $params['public'], + $params['organization'] ); return Repo::fromArray($data); diff --git a/lib/Github/Model/OwnerInterface.php b/lib/Github/Model/OwnerInterface.php index 8721cd1d3ae..f93a68d81db 100644 --- a/lib/Github/Model/OwnerInterface.php +++ b/lib/Github/Model/OwnerInterface.php @@ -4,5 +4,4 @@ interface OwnerInterface { - public function getCreateRepoPath(); } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index d7275a842d0..08f39b3cd05 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -135,7 +135,7 @@ public function issues(array $params = array()) public function issue($number) { - return Issue::factory($this, $number)->show(); + return Issue::factory($this, $number)->show(); } public function labels() @@ -181,7 +181,7 @@ public function addLabel($name, $color) public function updateLabel($name, $color) { - return Label::factory($this, $name)->update($name, $color); + return Label::factory($this, $name)->update($name, $color); } public function removeLabel($name) @@ -189,6 +189,15 @@ public function removeLabel($name) return Label::factory($this, $name)->remove($name); } + public function removeLabels() + { + foreach ($this->labels() as $label) { + $label->remove(); + } + + return true; + } + public function keys() { $data = $this->api('repo')->keys()->all( diff --git a/lib/Github/Model/Team.php b/lib/Github/Model/Team.php index 5c04dd21692..e43d10a118f 100644 --- a/lib/Github/Model/Team.php +++ b/lib/Github/Model/Team.php @@ -48,9 +48,11 @@ public function update(array $params) public function remove() { - return $this->api('organization')->teams()->remove( + $this->api('organization')->teams()->remove( $this->id ); + + return true; } public function members() @@ -69,26 +71,32 @@ public function members() public function check($login) { - return $this->api('organization')->teams()->check( + $this->api('organization')->teams()->check( $this->id, $login ); + + return true; } public function addMember($login) { - return $this->api('organization')->teams()->addMember( + $this->api('organization')->teams()->addMember( $this->id, $login ); + + return true; } public function removeMember($login) { - return $this->api('organization')->teams()->removeMember( + $this->api('organization')->teams()->removeMember( $this->id, $login ); + + return true; } public function repositories() @@ -105,35 +113,49 @@ public function repositories() return $repos; } - public function repository($login, $repository) + public function repository($repository) { + if ($repository instanceof Repo) { + $repository = $repository->name; + } + $data = $this->api('organization')->teams()->repository( $this->id, - $login, + $this->org->login, $repository ); return Repo::fromArray($data); } - public function addRepository($login, $repository) + public function addRepository($repository) { - $data = $this->api('organization')->teams()->addRepository( + if ($repository instanceof Repo) { + $repository = $repository->name; + } + + $this->api('organization')->teams()->addRepository( $this->id, - $login, + $this->org->login, $repository ); - return Repo::fromArray($data); + return true; } - public function removeRepository($login, $repository) + public function removeRepository($repository) { - return $this->api('organization')->teams()->removeRepository( + if ($repository instanceof Repo) { + $repository = $repository->name; + } + + $this->api('organization')->teams()->removeRepository( $this->id, - $login, + $this->org->login, $repository ); + + return true; } } diff --git a/lib/Github/Model/User.php b/lib/Github/Model/User.php index 3578f886380..a4bb3694797 100644 --- a/lib/Github/Model/User.php +++ b/lib/Github/Model/User.php @@ -4,8 +4,4 @@ class User extends Owner implements OwnerInterface { - public function getCreateRepoPath() - { - return '/user/repos'; - } } From 96ad6cb6dbb8242fd5572cd49728ef452949a44a Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 16 Nov 2012 15:49:43 +0000 Subject: [PATCH 18/20] Add real factory() methods --- lib/Github/Model/AbstractModel.php | 19 ++------ lib/Github/Model/Comment.php | 5 +++ lib/Github/Model/Commit.php | 5 +++ lib/Github/Model/DeployKey.php | 5 +++ lib/Github/Model/Event.php | 5 +++ lib/Github/Model/Hook.php | 5 +++ lib/Github/Model/Issue.php | 5 +++ lib/Github/Model/Label.php | 5 +++ lib/Github/Model/Language.php | 5 +++ lib/Github/Model/Milestone.php | 5 +++ lib/Github/Model/Org.php | 28 ++++++++++-- lib/Github/Model/Owner.php | 17 ++++++++ lib/Github/Model/PullRequest.php | 5 +++ lib/Github/Model/Repo.php | 69 +++++++++++++++++++++++++++++- lib/Github/Model/ReviewComment.php | 5 +++ lib/Github/Model/Team.php | 5 +++ lib/Github/Model/User.php | 4 ++ 17 files changed, 176 insertions(+), 21 deletions(-) diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php index c06a8763b43..d71f1dde92e 100644 --- a/lib/Github/Model/AbstractModel.php +++ b/lib/Github/Model/AbstractModel.php @@ -19,21 +19,6 @@ public static function client(Client $client = null) return static::$_client; } - public static function factory() - { - $args = func_get_args(); - $last = end(array_values($args)); - - $obj = new \ReflectionClass(get_called_class()); - - if ($last instanceof Client) { - $last = array_pop($args); - static::client($last); - } - - return $obj->newInstanceArgs($args); - } - protected $_data = array(); public function api($api) @@ -41,6 +26,10 @@ public function api($api) return static::client()->api($api); } + /** + * @param array $data + * @return AbstractModel + */ public function hydrate(array $data = array()) { if (!empty($data)) { diff --git a/lib/Github/Model/Comment.php b/lib/Github/Model/Comment.php index 5ff261bc9a2..c9efd107e05 100644 --- a/lib/Github/Model/Comment.php +++ b/lib/Github/Model/Comment.php @@ -14,6 +14,11 @@ class Comment extends AbstractModel implements CommentInterface 'updated_at' ); + public static function factory(Issue $issue, $id) + { + return new Comment($issue, $id); + } + public static function fromArray(Issue $issue, array $data) { $comment = Comment::factory($issue, $data['id']); diff --git a/lib/Github/Model/Commit.php b/lib/Github/Model/Commit.php index e0b61da33d6..8cf052d06cd 100644 --- a/lib/Github/Model/Commit.php +++ b/lib/Github/Model/Commit.php @@ -16,6 +16,11 @@ class Commit extends AbstractModel 'parents' ); + public static function factory(Repo $repo, $sha) + { + return new Commit($repo, $sha); + } + public static function fromArray(Repo $repo, array $data) { $commit = Commit::factory($repo, $data['sha']); diff --git a/lib/Github/Model/DeployKey.php b/lib/Github/Model/DeployKey.php index 2a55a5ea3c7..a6a929928ab 100644 --- a/lib/Github/Model/DeployKey.php +++ b/lib/Github/Model/DeployKey.php @@ -11,6 +11,11 @@ class DeployKey extends Key implements KeyInterface 'key' ); + public static function factory(Repo $repo, $id) + { + return new DeployKey($repo, $id); + } + public static function fromArray(Repo $repo, array $data) { $key = DeployKey::factory($repo, $data['id']); diff --git a/lib/Github/Model/Event.php b/lib/Github/Model/Event.php index 85bba4ac7e5..c474e52736b 100644 --- a/lib/Github/Model/Event.php +++ b/lib/Github/Model/Event.php @@ -17,6 +17,11 @@ class Event extends AbstractModel 'issue' ); + public static function factory(Repo $repo, $event, $issue = null) + { + return new Event($repo, $event, $issue); + } + public static function fromArray(Repo $repo, array $data, Issue $issue = null) { $event = Event::factory($repo, $data['event'], $issue); diff --git a/lib/Github/Model/Hook.php b/lib/Github/Model/Hook.php index 471ace13c66..2b9d164d5d9 100644 --- a/lib/Github/Model/Hook.php +++ b/lib/Github/Model/Hook.php @@ -17,6 +17,11 @@ class Hook extends AbstractModel 'last_response' ); + public static function factory(Repo $repo, $id) + { + return new Hook($repo, $id); + } + public static function fromArray(Repo $repo, array $data) { $hook = Hook::factory($repo, $data['id']); diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index ab8bf71eaec..2517cdc8798 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -25,6 +25,11 @@ class Issue extends AbstractModel 'updated_at' ); + public static function factory(Repo $repo, $number) + { + return new Commit($repo, $number); + } + public static function fromArray(Repo $repo, array $data) { $issue = Issue::factory($repo, $data['number']); diff --git a/lib/Github/Model/Label.php b/lib/Github/Model/Label.php index 7b982de90e3..bd902bd6f2d 100644 --- a/lib/Github/Model/Label.php +++ b/lib/Github/Model/Label.php @@ -11,6 +11,11 @@ class Label extends AbstractModel 'color' ); + public static function factory(Repo $repo, $name) + { + return new Label($repo, $name); + } + public static function fromArray(Repo $repo, array $data) { $label = Label::factory($repo, $data['name']); diff --git a/lib/Github/Model/Language.php b/lib/Github/Model/Language.php index 48f384b0685..c9f04aa1df3 100644 --- a/lib/Github/Model/Language.php +++ b/lib/Github/Model/Language.php @@ -9,6 +9,11 @@ class Language extends AbstractModel 'size' ); + public static function factory(array $data) + { + return new Language($data); + } + public static function fromArray(array $data) { return Language::factory($data); diff --git a/lib/Github/Model/Milestone.php b/lib/Github/Model/Milestone.php index 99bbd5508f1..849a645c137 100644 --- a/lib/Github/Model/Milestone.php +++ b/lib/Github/Model/Milestone.php @@ -19,6 +19,11 @@ class Milestone extends AbstractModel 'due_on' ); + public static function factory(Issue $issue, $number) + { + return new Milestone($issue, $number); + } + public static function fromArray($issue, array $data) { $milestone = Milestone::factory($issue, $data['number']); diff --git a/lib/Github/Model/Org.php b/lib/Github/Model/Org.php index c76cd84a5e3..d5278549e3f 100644 --- a/lib/Github/Model/Org.php +++ b/lib/Github/Model/Org.php @@ -4,13 +4,14 @@ class Org extends Owner implements OwnerInterface { - public function createRepo($name, array $params = array()) + public static function factory($login) { - $params['organization'] = $this->login; - - return parent::createRepo($name, $params); + return new Org($login); } + /** + * @return Org + */ public function show() { $data = $this->api('organization')->show( @@ -20,6 +21,9 @@ public function show() return Org::fromArray($data); } + /** + * @return Org + */ public function update(array $params) { $data = $this->api('organization')->update( @@ -30,6 +34,16 @@ public function update(array $params) return Org::fromArray($data); } + /** + * @return Repo + */ + public function createRepo($name, array $params = array()) + { + $params['organization'] = $this->login; + + return parent::createRepo($name, $params); + } + public function members($type = null) { $data = $this->api('organization')->members()->all( @@ -90,6 +104,9 @@ public function removeMember($login) ); } + /** + * @return Team + */ public function createTeam($name, array $params = array()) { $params['name'] = $name; @@ -116,6 +133,9 @@ public function teams() return $teams; } + /** + * @return Team + */ public function team($id) { return Team::factory($this, $id)->show(); diff --git a/lib/Github/Model/Owner.php b/lib/Github/Model/Owner.php index ba6b0f38e2e..c1f70c7dc19 100644 --- a/lib/Github/Model/Owner.php +++ b/lib/Github/Model/Owner.php @@ -33,6 +33,11 @@ class Owner extends AbstractModel 'type' ); + public static function factory($login) + { + return new Owner($login); + } + public static function fromArray(array $data) { return static::factory($data['login'])->hydrate($data); @@ -43,11 +48,17 @@ public function __construct($login) $this->login = $login; } + /** + * @return Repo + */ public function repo($name) { return Repo::factory($this, $name)->show(); } + /** + * @return Repo + */ public function createRepo($name, array $params = array()) { $params = array_merge(array( @@ -68,11 +79,17 @@ public function createRepo($name, array $params = array()) return Repo::fromArray($data); } + /** + * @return Repo + */ public function updateRepo($name, array $params) { return Repo::factory($this, $name)->update($params); } + /** + * @return Repo + */ public function removeRepo($name) { return Repo::factory($this, $name)->remove(); diff --git a/lib/Github/Model/PullRequest.php b/lib/Github/Model/PullRequest.php index 0e154c75da7..a8b8c46e268 100644 --- a/lib/Github/Model/PullRequest.php +++ b/lib/Github/Model/PullRequest.php @@ -39,6 +39,11 @@ class PullRequest extends Issue 'user' ); + public static function factory(Repo $repo, $number) + { + return new PullRequest($repo, $number); + } + public static function fromArray(Repo $repo, array $data) { $pull_request = PullRequest::factory($repo, $data['number']); diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index 08f39b3cd05..a94dd33412e 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -44,6 +44,14 @@ class Repo extends AbstractModel 'permissions' ); + public static function factory(Owner $owner, $name) + { + return new Repo($owner, $name); + } + + /** + * @return Repo + */ public static function fromArray(array $data) { if (!isset($data['owner'])) { @@ -75,6 +83,9 @@ public function __construct(Owner $owner, $name) $this->name = $name; } + /** + * @return Repo + */ public function show() { $data = $this->api('repo')->show( @@ -85,6 +96,10 @@ public function show() return Repo::fromArray($data); } + + /** + * @return Repo + */ public function update(array $params) { $data = $this->api('repo')->update( @@ -98,12 +113,17 @@ public function update(array $params) public function remove() { - return $this->api('repo')->remove( + $this->api('repo')->remove( $this->owner->login, $this->name ); + + return true; } + /** + * @return Issue + */ public function createIssue($title, array $params) { $params['title'] = $title; @@ -133,6 +153,9 @@ public function issues(array $params = array()) return $issues; } + /** + * @return Issue + */ public function issue($number) { return Issue::factory($this, $number)->show(); @@ -153,6 +176,9 @@ public function labels() return $labels; } + /** + * @return Issue + */ public function label($name) { $data = $this->api('repo')->labels()->show( @@ -164,7 +190,10 @@ public function label($name) return Label::fromArray($this, $data); } - // Todo: check label doesnt exist + /** + * @todo: Check label doesn't exist + * @return Label + */ public function addLabel($name, $color) { $data = $this->api('repo')->labels()->create( @@ -179,6 +208,9 @@ public function addLabel($name, $color) return Label::fromArray($this, $data); } + /** + * @return Label + */ public function updateLabel($name, $color) { return Label::factory($this, $name)->update($name, $color); @@ -213,6 +245,9 @@ public function keys() return $keys; } + /** + * @return DeployKey + */ public function key($id) { $data = $this->api('repo')->keys()->show( @@ -224,6 +259,9 @@ public function key($id) return DeployKey::fromArray($this, $data); } + /** + * @return DeployKey + */ public function addKey($title, $key) { $data = $this->api('repo')->keys()->create( @@ -238,6 +276,9 @@ public function addKey($title, $key) return DeployKey::fromArray($this, $data); } + /** + * @return DeployKey + */ public function updateKey($id, $title, $key) { return DeployKey::factory($this, $id)->update($title, $key); @@ -263,6 +304,9 @@ public function events() return $events; } + /** + * @return Event + */ public function event($event) { $data = $this->api('issue')->events()->show( @@ -274,6 +318,9 @@ public function event($event) return Event::fromArray($this, $data); } + /** + * @return PullRequest + */ public function createPullRequest(array $params) { $data = $this->api('pull_request')->create( @@ -301,11 +348,17 @@ public function pullRequests($state = null) return $pull_requests; } + /** + * @return PullRequest + */ public function pullRequest($number) { return PullRequest::factory($this, $number)->show(); } + /** + * @return PullRequest + */ public function updatePullRequest($number, array $params) { return PullRequest::factory($this, $number)->update($params); @@ -344,6 +397,9 @@ public function teams() return $teams; } + /** + * @return Hook + */ public function addHook($name, array $config, array $events = null, $active = true) { $data = $this->api('repo')->hooks()->create( @@ -360,6 +416,9 @@ public function addHook($name, array $config, array $events = null, $active = tr return Hook::fromArray($this, $data); } + /** + * @return Hook + */ public function hook($id) { return Hook::factory($this, $id)->show(); @@ -380,6 +439,9 @@ public function hooks() return $hooks; } + /** + * @return Hook + */ public function updateHook($id, array $params) { return Hook::factory($this, $id)->update($params); @@ -390,6 +452,9 @@ public function removeHook($id) return Hook::factory($this, $id)->remove(); } + /** + * @return Repo + */ public function fork($org = null) { $data = $this->api('repo')->forks()->create( diff --git a/lib/Github/Model/ReviewComment.php b/lib/Github/Model/ReviewComment.php index 6c4d442ec87..05a67255a5d 100644 --- a/lib/Github/Model/ReviewComment.php +++ b/lib/Github/Model/ReviewComment.php @@ -18,6 +18,11 @@ class ReviewComment extends AbstractModel implements CommentInterface '_links' ); + public static function factory(PullRequest $pull_request, $id) + { + return new PullRequest($pull_request, $id); + } + public static function fromArray(PullRequest $pull_request, array $data) { $comment = ReviewComment::factory($pull_request, $data['id']); diff --git a/lib/Github/Model/Team.php b/lib/Github/Model/Team.php index e43d10a118f..6759b631230 100644 --- a/lib/Github/Model/Team.php +++ b/lib/Github/Model/Team.php @@ -14,6 +14,11 @@ class Team extends AbstractModel 'repos_count' ); + public static function factory(Org $org, $id) + { + return new Team($org, $id); + } + public static function fromArray(Org $org, array $data) { $team = Team::factory($org, $data['id']); diff --git a/lib/Github/Model/User.php b/lib/Github/Model/User.php index a4bb3694797..fbe113824a2 100644 --- a/lib/Github/Model/User.php +++ b/lib/Github/Model/User.php @@ -4,4 +4,8 @@ class User extends Owner implements OwnerInterface { + public static function factory($login) + { + return new User($login); + } } From e75c689fe647f9f403c948797a3e2242c8122096 Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Wed, 21 Nov 2012 16:56:33 +0000 Subject: [PATCH 19/20] Add missing properties Add hasLabel() method Add ref() and removeRef() methods to Repo --- lib/Github/Model/Commit.php | 5 ++- lib/Github/Model/Hook.php | 7 ++- lib/Github/Model/Issue.php | 42 +++++++++++++++--- lib/Github/Model/IssueInterface.php | 7 +++ lib/Github/Model/Owner.php | 10 +++++ lib/Github/Model/PullRequest.php | 6 ++- lib/Github/Model/Ref.php | 13 +++++- lib/Github/Model/Repo.php | 68 +++++++++++++++++++++++++++-- lib/Github/Model/Team.php | 26 ++++++----- 9 files changed, 160 insertions(+), 24 deletions(-) create mode 100644 lib/Github/Model/IssueInterface.php diff --git a/lib/Github/Model/Commit.php b/lib/Github/Model/Commit.php index 8cf052d06cd..9041c14d3ac 100644 --- a/lib/Github/Model/Commit.php +++ b/lib/Github/Model/Commit.php @@ -13,7 +13,10 @@ class Commit extends AbstractModel 'committer', 'message', 'tree', - 'parents' + 'parents', + 'files', + 'stats', + 'comments_url' ); public static function factory(Repo $repo, $sha) diff --git a/lib/Github/Model/Hook.php b/lib/Github/Model/Hook.php index 2b9d164d5d9..33737e5aa56 100644 --- a/lib/Github/Model/Hook.php +++ b/lib/Github/Model/Hook.php @@ -14,7 +14,8 @@ class Hook extends AbstractModel 'active', 'config', 'id', - 'last_response' + 'last_response', + 'test_url' ); public static function factory(Repo $repo, $id) @@ -34,6 +35,10 @@ public static function fromArray(Repo $repo, array $data) $data['config'] = (object) $data['config']; } + if (isset($data['last_response'])) { + $data['last_response'] = (object) $data['last_response']; + } + return $hook->hydrate($data); } diff --git a/lib/Github/Model/Issue.php b/lib/Github/Model/Issue.php index 2517cdc8798..408a2d2a4a5 100644 --- a/lib/Github/Model/Issue.php +++ b/lib/Github/Model/Issue.php @@ -2,13 +2,12 @@ namespace Github\Model; -class Issue extends AbstractModel +class Issue extends AbstractModel implements IssueInterface { protected static $_properties = array( 'id', 'repo', 'url', - 'html_url', 'number', 'state', 'title', @@ -22,12 +21,16 @@ class Issue extends AbstractModel 'closed_at', 'closed_by', 'created_at', - 'updated_at' + 'updated_at', + 'comments_url', + 'events_url', + 'html_url', + 'labels_url' ); public static function factory(Repo $repo, $number) { - return new Commit($repo, $number); + return new Issue($repo, $number); } public static function fromArray(Repo $repo, array $data) @@ -56,7 +59,8 @@ public static function fromArray(Repo $repo, array $data) } if (isset($data['pull_request'])) { - + $data['pull_request']['number'] = $data['number']; + $data['pull_request'] = PullRequest::fromArray($issue->repo, $data['pull_request']); } return $issue->hydrate($data); @@ -127,19 +131,27 @@ public function addLabels(array $labels) public function addLabel($name) { + if ($this->hasLabel($name)) { + return $this->labels(); + } + return $this->addLabels(array($name)); } public function removeLabel($name) { - $data = $this->api('issue')->labels()->remove( + if (!$this->hasLabel($name)) { + return true; + } + + $this->api('issue')->labels()->remove( $this->repo->owner->login, $this->repo->name, $this->number, $name ); - return Label::fromArray($this->repo, $data); + return true; } public function replaceLabels(array $labels) @@ -170,6 +182,17 @@ public function clearLabels() return true; } + public function hasLabel($label_name) + { + foreach ($this->labels() as $label) { + if ($label_name == $label->name) { + return true; + } + } + + return false; + } + public function milestones(array $params = array()) { $data = $this->api('issue')->milestones()->all( @@ -263,4 +286,9 @@ public function events() return $events; } + + public function close() + { + return $this->update(array('state' => 'closed')); + } } diff --git a/lib/Github/Model/IssueInterface.php b/lib/Github/Model/IssueInterface.php new file mode 100644 index 00000000000..cc7fc47529a --- /dev/null +++ b/lib/Github/Model/IssueInterface.php @@ -0,0 +1,7 @@ +hydrate($data); } } diff --git a/lib/Github/Model/Repo.php b/lib/Github/Model/Repo.php index a94dd33412e..a6c4ccaf225 100644 --- a/lib/Github/Model/Repo.php +++ b/lib/Github/Model/Repo.php @@ -41,6 +41,40 @@ class Repo extends AbstractModel 'organization', 'parent', 'source', + 'teams_url', + 'keys_url', + 'contributors_url', + 'merges_url', + 'statuses_url', + 'hooks_url', + 'comments_url', + 'subscription_url', + 'labels_url', + 'branches_url', + 'assignees_url', + 'collaborators_url', + 'forks_url', + 'contents_url', + 'milestones_url', + 'downloads_url', + 'git_commits_url', + 'trees_url', + 'archive_url', + 'git_refs_url', + 'compare_url', + 'subscribers_url', + 'blobs_url', + 'issue_events_url', + 'pulls_url', + 'commits_url', + 'events_url', + 'stargazers_url', + 'languages_url', + 'issue_comment_url', + 'issues_url', + 'git_tags_url', + 'notifications_url', + 'tags_url', 'permissions' ); @@ -124,7 +158,7 @@ public function remove() /** * @return Issue */ - public function createIssue($title, array $params) + public function createIssue($title, array $params = array()) { $params['title'] = $title; @@ -134,7 +168,7 @@ public function createIssue($title, array $params) $params ); - return $this->getIssue($issue['number']); + return $this->issue($issue['number']); } public function issues(array $params = array()) @@ -158,7 +192,13 @@ public function issues(array $params = array()) */ public function issue($number) { - return Issue::factory($this, $number)->show(); + try { + $issue = Issue::factory($this, $number)->show(); + } catch (\Exception $e) { + $issue = PullRequest::factory($this, $number)->show(); + } + + return $issue; } public function labels() @@ -485,4 +525,26 @@ public function forks($sort = null) return $forks; } + + public function ref($ref) + { + $data = $this->api('git_data')->references()->show( + $this->owner->login, + $this->name, + $ref + ); + + return Ref::fromArray($data); + } + + public function removeRef($ref) + { + $this->api('git_data')->references()->remove( + $this->owner->login, + $this->name, + $ref + ); + + return true; + } } diff --git a/lib/Github/Model/Team.php b/lib/Github/Model/Team.php index 6759b631230..f4599dd1793 100644 --- a/lib/Github/Model/Team.php +++ b/lib/Github/Model/Team.php @@ -11,22 +11,24 @@ class Team extends AbstractModel 'name', 'permission', 'members_count', - 'repos_count' + 'repos_count', + 'repositories_url', + 'members_url' ); - public static function factory(Org $org, $id) + public static function factory(Owner $org, $id) { return new Team($org, $id); } - public static function fromArray(Org $org, array $data) + public static function fromArray(Owner $org, array $data) { $team = Team::factory($org, $data['id']); return $team->hydrate($data); } - public function __construct(Org $org, $id) + public function __construct(Owner $org, $id) { $this->org = $org; $this->id = $id; @@ -76,12 +78,16 @@ public function members() public function check($login) { - $this->api('organization')->teams()->check( - $this->id, - $login - ); - - return true; + try { + $this->api('organization')->teams()->check( + $this->id, + $login + ); + + return true; + } catch (\Exception $e) { + return false; + } } public function addMember($login) From 4a889a0ab90e363ad5a513bf54e668a456999a0e Mon Sep 17 00:00:00 2001 From: Matt Humphrey Date: Fri, 23 Nov 2012 10:44:31 +0000 Subject: [PATCH 20/20] Add setClient() --- lib/Github/Model/AbstractModel.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/Github/Model/AbstractModel.php b/lib/Github/Model/AbstractModel.php index d71f1dde92e..90a5f63ab9f 100644 --- a/lib/Github/Model/AbstractModel.php +++ b/lib/Github/Model/AbstractModel.php @@ -21,6 +21,11 @@ public static function client(Client $client = null) protected $_data = array(); + public function setClient(Client $client) + { + return static::client($client); + } + public function api($api) { return static::client()->api($api);