8000 Add project method to GitHub class · pythonthings/github3.py@2d9c888 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d9c888

Browse files
rco-abletonsigmavirus24
authored andcommitted
Add project method to GitHub class
The Projects API allows a user to fetch a project directly, without reference to an organisation or repository. This commit exposes this ability by adding a `project()` method to the `GitHub` class. Callers may obtain any project to which they have access by calling this method directly: import github3 project = github3.login(token='token').project(1234)
1 parent 9733e5d commit 2d9c888

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

github3/github.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .issues import Issue, issue_params
1919
from .models import GitHubCore
2020
from .orgs import Membership, Organization, Team
21+
from .projects import Project
2122
from .pulls import PullRequest
2223
from .repos.repo import Repository, repo_issue_params
2324
from .search import (CodeSearchResult, IssueSearchResult,
@@ -946,6 +947,20 @@ def organizations_with(self, username, number=-1, etag=None):
946947
return self._iter(int(number), url, Organization, etag=etag)
947948
return iter([])
948949

950+
def project(self, number):
951+
"""Return the Project with id ``number``.
952+
953+
:param int number: id of the project
954+
:returns: :class:`Project <github3.projects.Project>`
955+
"""
956+
number = int(number)
957+
json = None
958+
if number > 0:
959+
url = self._build_url('projects', str(number))
960+
json = self._json(self._get(
961+
url, headers=Project.CUSTOM_HEADERS), 200)
962+
return self._instance_or_null(Project, json)
963+
949964
def public_gists(self, number=-1, etag=None):
950965
"""Retrieve all public gists and iterate over them.
951966

tests/cassettes/GitHub_project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"http_interactions": [{"response": {"headers": {"X-GitHub-Media-Type": "github.inertia-preview; format=json", "X-GitHub-Request-Id": "E462:26DD:F42A5A:12F9A02:58FA1673", "X-Content-Type-Options": "nosniff", "Content-Type": "application/json; charset=utf-8", "X-RateLimit-Remaining": "4992", "Cache-Control": "private, max-age=60, s-maxage=60", "Content-Security-Policy": "default-src 'none'", "Server": "GitHub.com", "Content-Encoding": "gzip", "Access-Control-Allow-Origin": "*", "X-Served-By": "15bc4ab707db6d6b474783868c7cc828", "X-RateLimit-Reset": "1492787938", "X-OAuth-Scopes": "repo, user:email", "Vary": "Accept, Authorization, Cookie, X-GitHub-OTP", "X-Accepted-OAuth-Scopes": "public_repo, repo", "Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload", "Date": "Fri, 21 Apr 2017 14:25:56 GMT", "X-XSS-Protection": "1; mode=block", "X-Frame-Options": "deny", "X-RateLimit-Limit": "5000", "Transfer-Encoding": "chunked", "ETag": "W/\"261c15fcb665321c15adfb9cb1484d9b\"", "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", "Status": "200 OK"}, "status": {"code": 200, "message": "OK"}, "body": {"string": "", "encoding": "utf-8", "base64_string": "H4sIAPG3AVkC/52TUW/CIBCA/8rCcxVpzXRNlv2J7WUvBlusLC0QuNZ0Tf/7DuycumwGn9rAfd8dBzcQfVDCblpbk5zsAYzLKeVGzisJ+3Y7L3RDrTDaUSerhnfSti5d0uNuNjc9Sci/sLH6QxTgaPa0ztgaw/fQ1FcJz+L/SPPjYagodN02ym1iMtMJQlyWJD8uJkTxRqADhIPZRGDEVpf9tPoQ/jGwbbbCkpwlxAEHD2kjlK/GCg4atwZS60oq3Dk/xZSQsXSVsadVQnjHgf/qeVh02XSC1glbaAVCQThMS7/5l+45Q2VlJ413k1u34HWXVxh1ERi803WtD2jZRCaiJ/Jkkaq604LkQDXs/ZPFmNE3QjqILypQQ/hgB70HL9VaUUabJg7L8pM0DmFcgrDdusJKA1Kr+AIvaLRpW3ElP/l9NqS9JExyNBwopEWHrzEeP2IDzqPseNGPoY5CyA6bfafyikcj9MZP5BvG+tZLEBteNn4Wd7x2YpymFBEOGJcu2Gq2SGfp4pU95stVzrJ3P0OmvBEzfgGQvxGbMgUAAA=="}, "url": "https://api.github.com/projects/398318"}, "request": {"headers": {"User-Agent": "github3.py/1.0.0a4", "Authorization": "token <AUTH_TOKEN>", "Content-Type": "application/json", "Accept-Charset": "utf-8", "Connection": "keep-alive", "Accept-Encoding": "gzip, deflate", "Accept": "application/vnd.github.inertia-preview+json"}, "uri": "https://api.github.com/projects/398318", "method": "GET", "body": {"string": "", "encoding": "utf-8"}}, "recorded_at": "2017-04-21T14:26:02"}], "recorded_with": "betamax/0.8.0"}

tests/integration/test_github.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ def test_organization(self):
359359

360360
assert isinstance(o, github3.orgs.Organization)
361361

362+
def test_project(self):
363+
"""Test the ability to retrieve a project by its id."""
364+
self.token_login()
365+
cassette_name = self.cassette_name('project')
366+
with self.recorder.use_cassette(cassette_name):
367+
r = self.gh.project(398318)
368+
369+
assert isinstance(r, github3.projects.Project)
370+
362371
def test_pubsubhubbub(self):
363372
"""Test the ability to create a pubsubhubbub hook."""
364373
self.token_login()

tests/unit/test_github.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from github3 import GitHubEnterprise, GitHubError
44
from github3.github import GitHub, GitHubStatus
5+
from github3.projects import Project
56

67
from . import helper
78

@@ -397,6 +398,15 @@ def test_organization(self):
397398
url_for('orgs/github3py')
398399
)
399400

401+
def test_project(self):
402+
"""Test the ability to retrieve a project by its id."""
403+
self.instance.project(400543)
404+
405+
self.session.get.assert_called_once_with(
406+
url_for('projects/400543'),
407+
headers=Project.CUSTOM_HEADERS
408+
)
409+
400410
def test_pubsubhubbub(self):
401411
"""Verify the request for creating a pubsubhubbub hook."""
402412
topic = (

0 commit comments

Comments
 (0)
0