Skip to content

Commit

Permalink
Update get_committer_data for latest PyGithub
Browse files Browse the repository at this point in the history
v1.55 of PyGithub changed `get_emails` to return a named tuple (#1890)
rather than a dict directly from the decoded API response.

Update the code to access the fields as a property rather than by index
& bump the pin so 1.55 is the minimum version.

Also update the existing tests for the now required `headers` parameter
on exception classes.
  • Loading branch information
DamianZaremba committed Jun 24, 2021
1 parent 17c2384 commit f5ed496
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions pyup/providers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ def get_committer_data(self, committer):
email = committer.email
else:
for item in committer.get_emails():
if item["primary"]:
email = item["email"]
if item.primary:
email = item.email
if email is None:
msg = "Unable to get {login}'s email adress. " \
"You may have to add the scope user:email".format(login=committer.login)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

requirements = [
"requests",
"pygithub>=1.43.3",
"pygithub>=1.55",
"click",
"tqdm",
"pyyaml>=4.2b4",
Expand Down
32 changes: 17 additions & 15 deletions tests/test_github.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals

from collections import namedtuple
from unittest import TestCase
from pyup.providers.github import Provider
from pyup.requirements import RequirementsBundle
Expand Down Expand Up @@ -70,7 +72,7 @@ def test_get_default_branch(self):
"foo"
)

p = PropertyMock(side_effect=UnknownObjectException(data="", status=1))
p = PropertyMock(side_effect=UnknownObjectException(data="", status=1, headers={}))
type(self.repo).default_branch = p
with self.assertRaises(errors.RepoDoesNotExistError):
self.provider.get_default_branch(self.repo)
Expand All @@ -81,7 +83,7 @@ def test_get_pull_request_permissions(self):
self.provider.get_pull_request_permissions(user, self.repo)
self.repo.add_to_collaborators.assert_called_once_with("some-dude")

self.repo.add_to_collaborators.side_effect = GithubException(data="", status=1)
self.repo.add_to_collaborators.side_effect = GithubException(data="", status=1, headers={})
with self.assertRaises(errors.NoPermissionError):
self.provider.get_pull_request_permissions(user, self.repo)

Expand All @@ -91,7 +93,7 @@ def test_iter_git_tree(self):
items = list(self.provider.iter_git_tree(self.repo, "some branch"))
self.assertEqual(items, [("type", "path")])

self.repo.get_git_tree.side_effect = GithubException(data="", status=999)
self.repo.get_git_tree.side_effect = GithubException(data="", status=999, headers={})
with self.assertRaises(GithubException):
list(self.provider.iter_git_tree(self.repo, "some branch"))

Expand All @@ -101,7 +103,7 @@ def test_get_file(self):
self.assertIsNotNone(obj)
self.repo.get_contents.assert_called_with("path", ref="branch")

self.repo.get_contents.side_effect = GithubException(data="", status=1)
self.repo.get_contents.side_effect = GithubException(data="", status=1, headers={})
content, obj = self.provider.get_file(self.repo, "path", "branch")
self.assertIsNone(content)
self.assertIsNone(obj)
Expand All @@ -127,7 +129,7 @@ def test_create_branch(self):
self.provider.create_branch(self.repo, "base branch", "new branch")
self.repo.get_git_ref.assert_called_once_with("heads/base branch")

self.repo.get_git_ref.side_effect = GithubException(data="", status=1)
self.repo.get_git_ref.side_effect = GithubException(data="", status=1, headers={})
with self.assertRaises(errors.BranchExistsError):
self.provider.create_branch(self.repo, "base branch", "new branch")

Expand Down Expand Up @@ -167,7 +169,7 @@ def test_create_commit(self, time):
self.provider.create_commit("path", "branch", "commit", "content", "sha", self.repo, "com")
self.assertEqual(self.repo.update_file.call_count, 1)

self.repo.update_file.side_effect = GithubException(data="", status=1)
self.repo.update_file.side_effect = GithubException(data="", status=1, headers={})
with self.assertRaises(GithubException):
self.provider.create_commit("path", "branch", "commit", "content", "sha", self.repo,
"com")
Expand Down Expand Up @@ -211,7 +213,7 @@ def test_get_committer_data(self):
committer = Mock()
committer.email = None
committer.login = "foo"
committer.get_emails.return_value = [{"primary": True, "email": "[email protected]"},]
committer.get_emails.return_value = [namedtuple("EmailData", ["primary", "email"])(True, "[email protected]")]
data = self.provider.get_committer_data(committer)._identity
self.assertEqual(data["name"], "foo")
self.assertEqual(data["email"], "[email protected]")
Expand All @@ -231,7 +233,7 @@ def test_get_pull_request_committer(self):
data = self.provider.get_pull_request_committer(self.repo, pr)
self.assertEqual(data, ["foo"])

self.repo.get_pull.side_effect = UnknownObjectException(data="", status=1)
self.repo.get_pull.side_effect = UnknownObjectException(data="", status=1, headers={})
data = self.provider.get_pull_request_committer(self.repo, pr)
self.assertEqual(data, [])

Expand All @@ -247,7 +249,7 @@ def test_close_pull_request(self):
self.provider.close_pull_request(self.repo, self.repo, pr, "comment", prefix="pyup-")
self.assertEqual(self.repo.get_git_ref().delete.call_count, 1)

self.repo.get_pull.side_effect = UnknownObjectException(data="", status=1)
self.repo.get_pull.side_effect = UnknownObjectException(data="", status=1, headers={})
data = self.provider.close_pull_request(self.repo, self.repo, Mock(), "comment",
prefix="pyup-")
self.assertEqual(data, False)
Expand All @@ -263,7 +265,7 @@ def test_create_pull_request(self):
self.assertEqual(self.provider.bundle.get_pull_request_class.call_count, 1)
self.assertEqual(self.provider.bundle.get_pull_request_class().call_count, 1)

self.repo.create_pull.side_effect = GithubException(data="", status=1)
self.repo.create_pull.side_effect = GithubException(data="", status=1, headers={})
with self.assertRaises(errors.NoPermissionError):
self.provider.create_pull_request(self.repo, "title", "body", "master", "new", False, [])

Expand All @@ -283,10 +285,10 @@ def test_create_pull_request_with_assignees(self):
def test_create_issue(self):
self.assertIsNot(self.provider.create_issue(self.repo, "title", "body"), False)

self.repo.create_issue.side_effect = GithubException(data="", status=404)
self.repo.create_issue.side_effect = GithubException(data="", status=404, headers={})
self.assertEqual(self.provider.create_issue(self.repo, "title", "body"), False)

self.repo.create_issue.side_effect = GithubException(data="", status=999)
self.repo.create_issue.side_effect = GithubException(data="", status=999, headers={})
with self.assertRaises(GithubException):
self.assertEqual(self.provider.create_issue(self.repo, "title", "body"), False)

Expand All @@ -302,15 +304,15 @@ def test_get_or_create_label(self):

def test_create_label(self):
# label does not exist, need to create it
self.repo.get_label.side_effect = UnknownObjectException(None, None)
self.repo.get_label.side_effect = UnknownObjectException(None, None, headers={})
self.provider.get_or_create_label(self.repo, "another-label")
self.repo.get_label.assert_called_once_with(name="another-label")
self.repo.create_label.assert_called_once_with(name="another-label", color="1BB0CE")

def test_create_label_fails(self):
# label does not exist, need to create it
self.repo.get_label.side_effect = UnknownObjectException(None, None)
self.repo.create_label.side_effect = GithubException(None, None)
self.repo.get_label.side_effect = UnknownObjectException(None, None, headers={})
self.repo.create_label.side_effect = GithubException(None, None, headers={})
label = self.provider.get_or_create_label(self.repo, "another-label")
self.assertIsNone(label)
self.repo.get_label.assert_called_once_with(name="another-label")
Expand Down

0 comments on commit f5ed496

Please sign in to comment.