diff --git a/contributor_stats.py b/contributor_stats.py index 1200f65..ffd960e 100644 --- a/contributor_stats.py +++ b/contributor_stats.py @@ -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, @@ -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 diff --git a/contributors.py b/contributors.py index 42491c0..96cdd86 100644 --- a/contributors.py +++ b/contributors.py @@ -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, diff --git a/markdown.py b/markdown.py index a32328a..1acda35 100644 --- a/markdown.py +++ b/markdown.py @@ -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 diff --git a/test_contributor_stats.py b/test_contributor_stats.py index adcd0a5..69ce9ce 100644 --- a/test_contributor_stats.py +++ b/test_contributor_stats.py @@ -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): @@ -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", @@ -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): """ @@ -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()