Skip to content

Commit

Permalink
fix: shorten commit_urls by using links in markdown
Browse files Browse the repository at this point in the history
Signed-off-by: Zack Koppert <[email protected]>
  • Loading branch information
zkoppert committed Oct 12, 2023
1 parent 01fc0c6 commit c02d993
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 35 deletions.
148 changes: 115 additions & 33 deletions markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,73 @@ def write_to_markdown(
"""
# Put together the contributor table
if start_date and end_date:
headers = "| Username | Contribution Count | New Contributor | Commits |\n| --- | --- | --- | --- |\n"
else:
headers = "| Username | Contribution Count | Commits |\n| --- | --- | --- |\n"
table, total_contributions = get_contributor_table(
collaborators, start_date, end_date, organization, repository
)

table = headers
total_contributions = 0
# Put together the summary table including # of new contributions, # of new contributors, % new contributors, % returning contributors
summary_table = get_summary_table(
collaborators, start_date, end_date, total_contributions
)

for collaborator in collaborators:
total_contributions += collaborator.contribution_count
username = collaborator.username
contribution_count = collaborator.contribution_count
commit_url = collaborator.commit_url
new_contributor = collaborator.new_contributor
# Write the markdown file
write_markdown_file(
filename, start_date, end_date, organization, repository, table, summary_table
)


def write_markdown_file(
filename, start_date, end_date, organization, repository, table, summary_table
):
"""
This function writes all the tables and data to a markdown file with tables to organizae the information.
Args:
filename (str): The path of the markdown file to which the table will be written.
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 (str): The repository for which the contributors are being listed.
table (str): A string containing a markdown table of the contributors and the total contribution count.
summary_table (str): A string containing a markdown table of the summary statistics.
Returns:
None
"""
with open(filename, "w", encoding="utf-8") as markdown_file:
markdown_file.write("# Contributors\n\n")
if start_date and end_date:
row = f"| {username} | {contribution_count} | {new_contributor} | {commit_url} |\n"
else:
row = f"| {username} | {contribution_count} | {commit_url} |\n"
markdown_file.write(
f"- Date range for contributor list: {start_date} to {end_date}\n"
)
if organization:
markdown_file.write(f"- Organization: {organization}\n")
if repository:
markdown_file.write(f"- Repository: {repository}\n")
markdown_file.write("\n")
markdown_file.write(summary_table)
markdown_file.write(table)
markdown_file.write(
"\n _this file was generated by the [Contributors GitHub Action](https://github.com/github/contributors)_\n"
)

table += row

# Put together the summary table including # of new contributions, # of new contributors, % new contributors, % returning contributors
def get_summary_table(collaborators, start_date, end_date, total_contributions):
"""
This function returns a string containing a markdown table of the summary statistics.
Args:
collaborators (list): A list of dictionaries, where each dictionary represents a collaborator.
Each dictionary should have the keys 'username', 'contribution_count', and 'commits'.
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.
total_contributions (int): The total number of contributions made by all of the contributors.
Returns:
summary_table (str): A string containing a markdown table of the summary statistics.
"""
if start_date and end_date:
summary_table = "| Total Contributors | Total Contributions | % new contributors |\n| --- | --- | --- |\n"
summary_table += (
Expand All @@ -70,20 +114,58 @@ def write_to_markdown(
"| " + str(len(collaborators)) + " | " + str(total_contributions) + " |\n\n"
)

# Write the markdown file
with open(filename, "w", encoding="utf-8") as markdown_file:
markdown_file.write("# Contributors\n\n")
if start_date and end_date:
markdown_file.write(
f"Date range for contributor list: {start_date} to {end_date}\n"
)
if organization:
markdown_file.write(f"- Organization: {organization}\n")
return summary_table


def get_contributor_table(
collaborators, start_date, end_date, organization, repository
):
"""
This function returns a string containing a markdown table of the contributors and the total contribution count.
Args:
collaborators (list): A list of dictionaries, where each dictionary represents a collaborator.
Each dictionary should have the keys 'username', 'contribution_count', and 'commits'.
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 (str): The repository for which the contributors are being listed.
Returns:
table (str): A string containing a markdown table of the contributors and the total contribution count.
total_contributions (int): The total number of contributions made by all of the contributors.
"""
if start_date and end_date:
headers = "| Username | Contribution Count | New Contributor | Commits |\n| --- | --- | --- | --- |\n"
else:
headers = "| Username | Contribution Count | Commits |\n| --- | --- | --- |\n"

table = headers
total_contributions = 0

for collaborator in collaborators:
total_contributions += collaborator.contribution_count
username = collaborator.username
contribution_count = collaborator.contribution_count
if repository:
markdown_file.write(f"- Repository: {repository}\n")
markdown_file.write("\n")
markdown_file.write(summary_table)
markdown_file.write(table)
markdown_file.write(
"\n _this file was generated by the [Contributors GitHub Action](https://github.com/github/contributors)_\n"
)
commit_urls = collaborator.commit_url
if organization:
# split the urls from the comma separated list and make them into markdown links
commit_url_list = collaborator.commit_url.split(",")
commit_urls = ""
for url in commit_url_list:
url = url.strip()
# get the organization and repository name from the url ie. org1/repo2 from https://github.com/org1/repo2/commits?author-zkoppert
org_repo_link_name = url.split("/commits")[0].split("github.com/")[1]
url = f"[{org_repo_link_name}]({url})"
commit_urls += url + ", "
new_contributor = collaborator.new_contributor

if start_date and end_date:
row = f"| {username} | {contribution_count} | {new_contributor} | {commit_urls} |\n"
else:
row = f"| {username} | {contribution_count} | {commit_urls} |\n"

table += row
return table, total_contributions
4 changes: 2 additions & 2 deletions test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def test_write_to_markdown(self, mock_file):
]

write_to_markdown(
collaborators, "filename", "2023-01-01", "2023-01-02", None, None
collaborators, "filename", "2023-01-01", "2023-01-02", None, "org/repo"
)

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(
"Date range for contributor list: 2023-01-01 to 2023-01-02\n"
"- Date range for contributor list: 2023-01-01 to 2023-01-02\n"
)
mock_file().write.assert_any_call(
"| Total Contributors | Total Contributions | % new contributors |\n| --- | --- | --- |\n| 2 | 300 | 50.0% | \n\n"
Expand Down

0 comments on commit c02d993

Please sign in to comment.