Skip to content

Commit

Permalink
Add markdown out and format commits as url
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Koppert <[email protected]>
  • Loading branch information
zkoppert committed Oct 9, 2023
1 parent 1abd215 commit 948aae7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 73 deletions.
16 changes: 0 additions & 16 deletions commits.py

This file was deleted.

5 changes: 1 addition & 4 deletions contributor_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
# "username" : "zkoppert",
# "avatar_url" : "https://avatars.githubusercontent.com/u/29484535?v=4",
# "contribution_count" : "1261",
# "commits" : {
# "744d20e": "2023-06-29 09:43:24 -0700",
# "5c622f9" : "2023-06-29 15:55:38 -0700"
# },
# "commits" : "https://github.com/github/contributors/commits?author=zkoppert&since=2023-10-01&until=2023-10-05"
# }
# ]

Expand Down
24 changes: 11 additions & 13 deletions contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import env
import auth
import contributor_stats
import commits
import markdown


def main():
Expand All @@ -26,8 +26,7 @@ def main():

# Output the contributors information
print(contributors)
# print_contributors(contributors)
# write_to_markdown(contributors)
markdown.write_to_markdown(contributors, "contributors.md")
# write_to_json(contributors)


Expand All @@ -49,20 +48,15 @@ def get_all_contributors(
all_contributors = []
if repos:
for repo in repos:
all_contributors.append(
get_contributors(repo, github_connection, start_date, end_date)
)
all_contributors.append(get_contributors(repo, start_date, end_date))
else:
all_contributors.append(
get_contributors(repository_obj, github_connection, start_date, end_date)
)
all_contributors.append(get_contributors(repository_obj, start_date, end_date))

return all_contributors


def get_contributors(
repo: object,
github_connection: object,
start_date: str,
end_date: str,
):
Expand All @@ -75,13 +69,17 @@ def get_contributors(
continue

# Store the contributor information in a ContributorStats object
if start_date and end_date:
commit_url = f"https://github.com/{repo.full_name}/commits?author={user.login}&since={start_date}&until={end_date}"
else:
commit_url = (
f"https://github.com/{repo.full_name}/commits?author={user.login}"
)
contributor = contributor_stats.ContributorStats(
user.login,
user.avatar_url,
user.contributions_count,
commits.get_commits(
user.login, repo, github_connection, start_date, end_date
),
commit_url,
)
contributors.append(contributor)

Expand Down
32 changes: 32 additions & 0 deletions markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""This module contains the functions needed to write the output to markdown files."""


def write_to_markdown(collaborators, filename):
"""
This function writes a list of collaborators to a markdown file in table format.
Each collaborator is represented as a dictionary with keys 'username', 'contribution_count', and 'commits'.
Args:
collaborators (list): A list of dictionaries, where each dictionary represents a collaborator.
Each dictionary should have the keys 'username', 'contribution_count', and 'commits'.
filename (str): The path of the markdown file to which the table will be written.
"""
headers = "| Username | Contribution Count | Commits |\n| --- | --- | --- |\n"
table = headers

for repo in collaborators:
for collaborator in repo:
username = collaborator.username
contribution_count = collaborator.contribution_count
commits = collaborator.commits

row = f"| {username} | {contribution_count} | {commits} |\n"
table += row

with open(filename, "w", encoding="utf-8") as markdown_file:
markdown_file.write("# Contributors\n\n")
markdown_file.write(table)
markdown_file.write(
"\n _this file was generated by the [Contributors GitHub Action](https://github.com/github/contributors)\n"
)
35 changes: 0 additions & 35 deletions test_commits.py

This file was deleted.

9 changes: 4 additions & 5 deletions test_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class TestContributors(unittest.TestCase):
"""

@patch("contributors.contributor_stats.ContributorStats")
@patch("contributors.commits.get_commits")
def test_get_contributors(self, mock_get_commits, mock_contributor_stats):
def test_get_contributors(self, mock_contributor_stats):
"""
Test the get_contributors function.
"""
Expand All @@ -22,15 +21,15 @@ def test_get_contributors(self, mock_get_commits, mock_contributor_stats):
mock_user.avatar_url = "https://avatars.githubusercontent.com/u/12345678?v=4"
mock_user.contributions_count = "100"
mock_repo.contributors.return_value = [mock_user]
mock_github_connection = MagicMock()
mock_repo.full_name = "owner/repo"

get_contributors(mock_repo, mock_github_connection, "2022-01-01", "2022-12-31")
get_contributors(mock_repo, "2022-01-01", "2022-12-31")

mock_contributor_stats.assert_called_once_with(
"user",
"https://avatars.githubusercontent.com/u/12345678?v=4",
"100",
mock_get_commits.return_value,
"https://github.com/owner/repo/commits?author=user&since=2022-01-01&until=2022-12-31",
)


Expand Down
47 changes: 47 additions & 0 deletions test_markdown.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""This is the test module for the markdown module"""
import unittest
from unittest.mock import patch, mock_open
from markdown import write_to_markdown
import contributor_stats


class TestMarkdown(unittest.TestCase):
"""
Test case for the markdown module.
"""

@patch("builtins.open", new_callable=mock_open)
def test_write_to_markdown(self, mock_file):
"""
Test the write_to_markdown function.
"""
person1 = contributor_stats.ContributorStats(
"user1",
"url",
100,
"commit url",
)
person2 = contributor_stats.ContributorStats(
"user2",
"url2",
200,
"commit url2",
)
collaborators = [
[
person1,
person2,
]
]

write_to_markdown(collaborators, "filename")

mock_file.assert_called_once_with("filename", "w", encoding="utf-8")
mock_file().write.assert_any_call("# Contributors\n\n")
mock_file().write.assert_any_call(
"| Username | Contribution Count | Commits |\n| --- | --- | --- |\n| user1 | 100 | commit url |\n| user2 | 200 | commit url2 |\n"
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 948aae7

Please sign in to comment.