Skip to content
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

feat: json output #115

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
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__/
Expand Down
12 changes: 11 additions & 1 deletion contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import auth
import contributor_stats
import env
import json_writer
import markdown


Expand Down Expand Up @@ -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,
jmeridth marked this conversation as resolved.
Show resolved Hide resolved
)


def get_all_contributors(
Expand Down
78 changes: 78 additions & 0 deletions json_writer.py
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)
68 changes: 68 additions & 0 deletions test_json_writer.py
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()
Loading