Skip to content

Commit

Permalink
fix: suggestions + add test to sponsorinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardojdsilva87 committed Oct 29, 2024
1 parent 4234b6a commit 7f806e2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
11 changes: 4 additions & 7 deletions contributor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,10 @@ def get_sponsor_information(contributors: list, token: str, ghe: str) -> list:
variables = {"username": contributor.username}

# Send the GraphQL request
api_endpoint = (
f"{ghe}/api/v3".removeprefix("https://") if ghe else "api.github.com"
)
api_endpoint = f"{ghe}/api/v3" if ghe else "https://api.github.com"
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(
f"https://{api_endpoint}/graphql",
f"{api_endpoint}/graphql",
json={"query": query, "variables": variables},
headers=headers,
timeout=60,
Expand All @@ -170,10 +168,9 @@ def get_sponsor_information(contributors: list, token: str, ghe: str) -> list:

data = response.json()["data"]

endpoint = ghe if ghe else "https://github.com"
# if the user has a sponsor page, add it to the contributor object
if data["repositoryOwner"]["hasSponsorsListing"]:
contributor.sponsor_info = (
f"https://{api_endpoint}/sponsors/{contributor.username}"
)
contributor.sponsor_info = f"{endpoint}/sponsors/{contributor.username}"

return contributors
6 changes: 3 additions & 3 deletions contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ def get_contributors(repo: object, start_date: str, end_date: str, ghe: str):
continue

# Store the contributor information in a ContributorStats object
api_endpoint = ghe.removeprefix("https://") if ghe else "github.com"
endpoint = ghe if ghe else "https://github.com"
if start_date and end_date:
commit_url = f"https://{api_endpoint}/{repo.full_name}/commits?author={user.login}&since={start_date}&until={end_date}"
commit_url = f"{endpoint}/{repo.full_name}/commits?author={user.login}&since={start_date}&until={end_date}"
else:
commit_url = f"https://{api_endpoint}/{repo.full_name}/commits?author={user.login}"
commit_url = f"{endpoint}/{repo.full_name}/commits?author={user.login}"
contributor = contributor_stats.ContributorStats(
user.login,
False,
Expand Down
6 changes: 2 additions & 4 deletions markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ def get_contributor_table(
for url in commit_url_list:
url = url.strip()
# get the organization and repository name from the url ie. org1/repo2 from https://github.com/org1/repo2/commits?author-zkoppert
api_endpoint = ghe.removeprefix("https://") if ghe else "github.com"
org_repo_link_name = url.split("/commits")[0].split(f"{api_endpoint}/")[
1
]
endpoint = ghe.removeprefix("https://") if ghe else "github.com"
org_repo_link_name = url.split("/commits")[0].split(f"{endpoint}/")[1]
url = f"[{org_repo_link_name}]({url})"
commit_urls += f"{url}, "
new_contributor = collaborator.new_contributor
Expand Down
66 changes: 63 additions & 3 deletions test_contributor_stats.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""This module contains the tests for the ContributorStats class."""

import unittest
from unittest.mock import MagicMock, patch

from contributor_stats import ContributorStats, is_new_contributor, merge_contributors
from contributor_stats import (
ContributorStats,
get_sponsor_information,
is_new_contributor,
merge_contributors,
)


class TestContributorStats(unittest.TestCase):
Expand All @@ -28,7 +34,7 @@ def test_init(self):
Test the __init__ method of the ContributorStats class.
"""
self.assertEqual(self.contributor.username, "zkoppert")
self.assertEqual(self.contributor.new_contributor, False)
self.assertFalse(self.contributor.new_contributor)
self.assertEqual(
self.contributor.avatar_url,
"https://avatars.githubusercontent.com/u/29484535?v=4",
Expand Down Expand Up @@ -95,7 +101,7 @@ def test_merge_contributors(self):

result = merge_contributors(all_contributors)

self.assertTrue(expected_result == result)
self.assertEqual(expected_result, result)

def test_is_new_contributor_true(self):
"""
Expand Down Expand Up @@ -154,5 +160,59 @@ def test_is_new_contributor_false(self):
self.assertFalse(result)


class TestSponsorInfo(unittest.TestCase):
@patch("requests.post")
def test_fetch_sponsor_info(self, mock_post):
# Mock response data
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"data": {"repositoryOwner": {"hasSponsorsListing": True}}
}
mock_post.return_value = mock_response

# Mock contributors
user = "user1"
returning_contributors = [
ContributorStats(
username=user,
new_contributor=False,
avatar_url="https://avatars.githubusercontent.com/u/",
contribution_count="100",
commit_url="url1",
sponsor_info="",
),
]

# Test parameters
ghe = ""
token = "token"

# Call the function
result = get_sponsor_information(returning_contributors, token, ghe)

# Assertions
self.assertEqual(result[0].sponsor_info, "https://github.com/sponsors/user1")

# Ensure the post request was called with the correct parameters
mock_post.assert_called_once_with(
"https://api.github.com/graphql",
json={
"query": """
query($username: String!){
repositoryOwner(login: $username) {
... on User {
hasSponsorsListing
}
}
}
""",
"variables": {"username": "user1"},
},
headers={"Authorization": "Bearer token"},
timeout=60,
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 7f806e2

Please sign in to comment.