diff --git a/.gitignore b/.gitignore index 142fc9f..71ed06e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Output files contributors.md +contributors.json # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/contributors.py b/contributors.py index a074689..6518fbc 100644 --- a/contributors.py +++ b/contributors.py @@ -6,6 +6,7 @@ import auth import contributor_stats import env +import json_writer import markdown @@ -72,7 +73,16 @@ def main(): sponsor_info, link_to_profile, ) - # write_to_json(contributors) + json_writer.write_to_json( + filename="contributors.json", + start_date=start_date, + end_date=end_date, + organization=organization, + repository_list=repository_list, + sponsor_info=sponsor_info, + link_to_profile=link_to_profile, + contributors=contributors, + ) def get_all_contributors( diff --git a/json_writer.py b/json_writer.py new file mode 100644 index 0000000..85dc607 --- /dev/null +++ b/json_writer.py @@ -0,0 +1,78 @@ +""" This module contains a function that writes data to a JSON file. """ + +import json + + +def write_to_json( + contributors, + filename, + start_date, + end_date, + organization, + repository_list, + sponsor_info, + link_to_profile, +): + """Write data to a JSON file. + + Args: + contributors (list): A list of Contributor objects. + filename (str): The name of the JSON file. + start_date (str): The start date of the date range for the contributor list. + end_date (str): The end date of the date range for the contributor list. + organization (str): The organization for which the contributors are being listed. + repository_list (list): A list of repositories for which the contributors are being listed. + sponsor_info (str): A string indicating whether sponsor information should be included. + link_to_profile (str): A string indicating whether a link to the contributor's profile should be included. + + Returns: + None + """ + + # Prepare data for JSON such that it looks like the markdown data. ie. + # { + # "start_date": "2024-03-08", + # "end_date": "2024-03-15", + # "organization": null, + # "repository_list": [ + # "github/stale-repos", + # "github/issue-metrics", + # "github/contributors", + # "github/automatic-contrib-prs", + # "github/evergreen", + # "github/cleanowners" + # ], + # "sponsor_info": false, + # "link_to_profile": false, + # "contributors": [ + # { + # "username": "zkoppert", + # "new_contributor": false, + # "avatar_url": "https://avatars.githubusercontent.com/u/6935431?v=4", + # "contribution_count": 785, + # "commit_url": "https://github.com/github/stale-repos/commits?author=zkoppert&since=2024-03-08&until=2024-03-15, + # "sponsor_info": "" + # }, + # { + # "username": "jmeridth", + # "new_contributor": false, + # "avatar_url": "https://avatars.githubusercontent.com/u/35014?v=4", + # "contribution_count": 94, + # "commit_url": "https://github.com/github/stale-repos/commits?author=jmeridth&since=2024-03-08&until=2024-03-15, + # "sponsor_info": "" + # } + # ] + # } + data = { + "start_date": start_date, + "end_date": end_date, + "organization": organization, + "repository_list": repository_list, + "sponsor_info": sponsor_info, + "link_to_profile": link_to_profile, + "contributors": [contributor.__dict__ for contributor in contributors], + } + + # Write data to a JSON file + with open(filename, "w", encoding="utf-8") as f: + json.dump(data, f, indent=4) diff --git a/test_json_writer.py b/test_json_writer.py new file mode 100644 index 0000000..1071ea1 --- /dev/null +++ b/test_json_writer.py @@ -0,0 +1,68 @@ +""" Test the write_to_json function in json_writer.py. """ + +import json +import os +import unittest + +from contributor_stats import ContributorStats +from json_writer import write_to_json + + +class TestWriteToJson(unittest.TestCase): + """Test the write_to_json function.""" + + def setUp(self): + """Set up data for the tests.""" + self.filename = "test.json" + self.data = { + "start_date": "2022-01-01", + "end_date": "2022-01-31", + "organization": "test_org", + "repository_list": ["repo1", "repo2"], + "sponsor_info": False, + "link_to_profile": False, + "contributors": [ + { + "username": "test_user", + "new_contributor": False, + "avatar_url": "https://test_url.com", + "contribution_count": 10, + "commit_url": "https://test_commit_url.com", + "sponsor_info": "", + } + ], + } + + def test_write_to_json(self): + """Test that write_to_json writes the correct data to a JSON file.""" + contributors = ( + ContributorStats( + username="test_user", + new_contributor=False, + avatar_url="https://test_url.com", + contribution_count=10, + commit_url="https://test_commit_url.com", + sponsor_info="", + ), + ) + + write_to_json( + contributors=contributors, + filename=self.filename, + start_date=self.data["start_date"], + end_date=self.data["end_date"], + organization=self.data["organization"], + repository_list=self.data["repository_list"], + sponsor_info=self.data["sponsor_info"], + link_to_profile=self.data["link_to_profile"], + ) + with open(self.filename, "r", encoding="utf-8") as f: + result = json.load(f) + self.assertDictEqual(result, self.data) + + def tearDown(self): + os.remove(self.filename) + + +if __name__ == "__main__": + unittest.main()