Repository: sigmavirus24/github3.py

Issue:
Need a method to list all organizations on GitHub
https://developer.github.com/v3/orgs/#list-all-organizations was added recently. As a result we need to add `GitHub#all_organizations` to correspond with `GitHub#all_users`, `GitHub#all_events`, and `GitHub#all_repositories`.

Discussion:
sigmavirus24: 2 things:

1. This is failing flake8 checks
2. This needs unittests

Patch:
diff --git a/github3/github.py b/github3/github.py
index be5f5aab..9c61e4b9 100644
--- a/github3/github.py
+++ b/github3/github.py
@@ -78,6 +78,26 @@ class GitHub(GitHubCore):
         url = self._build_url('events')
         return self._iter(int(number), url, Event, etag=etag)
 
+    def all_organizations(self, number=-1, since=None, etag=None,
+                          per_page=None):
+        """Iterate over every organization in the order they were created.
+
+        :param int number: (optional), number of organizations to return.
+            Default: -1, returns all of them
+        :param int since: (optional), last organization id seen (allows
+            restarting this iteration)
+        :param str etag: (optional), ETag from a previous request to the same
+            endpoint
+        :param int per_page: (optional), number of organizations to list per
+            request
+        :returns: generator of :class:`Organization
+            <github3.orgs.Organization>`
+        """
+        url = self._build_url('organizations')
+        return self._iter(int(number), url, Organization,
+                          params={'since': since, 'per_page': per_page},
+                          etag=etag)
+
     def all_repositories(self, number=-1, since=None, etag=None,
                          per_page=None):
         """Iterate over every repository in the order they were created.


Test patch:
diff --git a/tests/cassettes/GitHub_all_organizations.json b/tests/cassettes/GitHub_all_organizations.json
new file mode 100644
index 00000000..d379a91d
--- /dev/null
+++ b/tests/cassettes/GitHub_all_organizations.json
@@ -0,0 +1,1 @@
+{"http_interactions": [{"request": {"uri": "https://api.github.com/organizations?per_page=25", "method": "GET", "headers": {"Accept": "application/vnd.github.v3.full+json", "Accept-Charset": "utf-8", "Content-Type": "application/json", "Accept-Encoding": "gzip, deflate", "Connection": "keep-alive", "User-Agent": "github3.py/1.0.0a2"}, "body": {"string": "", "encoding": "utf-8"}}, "recorded_at": "2015-07-27T16:50:16", "response": {"url": "https://api.github.com/organizations?per_page=25", "headers": {"x-github-media-type": "github.v3; param=full; format=json", "content-type": "application/json; charset=utf-8", "x-github-request-id": "3EBD09C7:4121:BE664F4:55B66147", "x-served-by": "065b43cd9674091fec48a221b420fbb3", "x-ratelimit-remaining": "58", "transfer-encoding": "chunked", "access-control-expose-headers": "ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval", "x-frame-options": "deny", "content-security-policy": "default-src 'none'", "status": "200 OK", "x-xss-protection": "1; mode=block", "date": "Mon, 27 Jul 2015 16:50:16 GMT", "x-ratelimit-limit": "60", "etag": "W/\"467f85325c09d7c93ffa03581d0b7de5\"", "cache-control": "public, max-age=60, s-maxage=60", "content-encoding": "gzip", "access-control-allow-origin": "*", "link": "<https://api.github.com/organizations?per_page=25&since=2595>; rel=\"next\", <https://api.github.com/organizations{?since}>; rel=\"first\"", "x-ratelimit-reset": "1438017277", "access-control-allow-credentials": "true", "vary": "Accept", "x-content-type-options": "nosniff", "server": "GitHub.com", "strict-transport-security": "max-age=31536000; includeSubdomains; preload"}, "status": {"message": "OK", "code": 200}, "body": {"base64_string": "H4sIAAAAAAAAA63ay27jNhSA4VcR1G0QX+JbAhRdzKqLAsV00UVRDGiZlomhRIGi7BqDefceySJFyRrMIXF2gyD/yYQfaElU/vmWSpWLMv1IudZnzXn6kopT+rHZvKSNlvD1izFV/bFYsEq85sJcmuNrpoqF0nm9GBrNK1V/QReL7vvhZ/ErL01I+AigLHhx5Dok7Ytvi8c/vsOQqjlKkX0JnzUO/ZHsygzT06Xovlj3C9jUXGeqNPCrd2vZLDab366/vsF/6MTrTIvKCAUmZSPl9xePqAQqfmf61CsdViglP0NDuSjCamhjuIaaRGwYR4x2WM2hpalPVohS1EbfM7Dmmlu41eodIzdTY/2mabji04QIy6cZFKJPQ4ldAWcOdrobMyUlz4y4cnHirN+Rq/UBA/vUYlnHYTjqpI8gnUygAJ2MpOZcH+Y40795cmyEPCW1Opsb0zwxCv4trzzRnMmk0uooeV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LyaAs0bR80G6/9zttrAIx28DbI7bPuOOqH2MiyZTcLBXBnB5VoKLDeMnOp99tS5uy3593+CzYb+GisAAA==", "string": "", "encoding": "utf-8"}}}], "recorded_with": "betamax/0.4.2"}
\ No newline at end of file
diff --git a/tests/integration/test_github.py b/tests/integration/test_github.py
index 11067a7f..d27eb229 100644
--- a/tests/integration/test_github.py
+++ b/tests/integration/test_github.py
@@ -169,6 +169,13 @@ class TestGitHub(IntegrationHelper):
 
         assert isinstance(i, github3.issues.Issue)
 
+    def test_all_organizations(self):
+        """Test the ability to iterate over all of the organizations."""
+        cassette_name = self.cassette_name('all_organizations')
+        with self.recorder.use_cassette(cassette_name):
+            for r in self.gh.all_organizations(number=25):
+                assert isinstance(r, github3.orgs.Organization)
+
     def test_all_repositories(self):
         """Test the ability to iterate over all of the repositories."""
         cassette_name = self.cassette_name('iter_all_repos')
diff --git a/tests/unit/test_github.py b/tests/unit/test_github.py
index dd98da37..8c7e10ca 100644
--- a/tests/unit/test_github.py
+++ b/tests/unit/test_github.py
@@ -317,6 +317,40 @@ class TestGitHubIterators(UnitIteratorHelper):
             headers={}
         )
 
+    def test_all_organizations(self):
+        """Show that one can iterate over all organizations."""
+        i = self.instance.all_organizations()
+        self.get_next(i)
+
+        self.session.get.assert_called_once_with(
+            url_for('organizations'),
+            params={'per_page': 100},
+            headers={}
+        )
+
+    def test_all_organizations_per_page(self):
+        """Show that one can iterate over all organizations with per_page."""
+        i = self.instance.all_organizations(per_page=25)
+        self.get_next(i)
+
+        self.session.get.assert_called_once_with(
+            url_for('organizations'),
+            params={'per_page': 25},
+            headers={}
+        )
+
+    def test_all_organizations_since(self):
+        """Show that one can limit the organizations returned."""
+        since = 100000
+        i = self.instance.all_organizations(since=since)
+        self.get_next(i)
+
+        self.session.get.assert_called_once_with(
+            url_for('organizations'),
+            params={'per_page': 100, 'since': since},
+            headers={}
+        )
+
     def test_all_repositories(self):
         """Show that one can iterate over all repositories."""
         i = self.instance.all_repositories()