-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from github/json
- Loading branch information
Showing
4 changed files
with
158 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
# Output files | ||
contributors.md | ||
contributors.json | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |