-
Notifications
You must be signed in to change notification settings - Fork 21
feat: json output #115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
feat: json output #115
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.