-
Notifications
You must be signed in to change notification settings - Fork 15
/
markdown.py
205 lines (177 loc) · 8.47 KB
/
markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# pylint: disable=too-many-locals
"""This module contains the functions needed to write the output to markdown files."""
def write_to_markdown(
collaborators,
filename,
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
ghe,
):
"""
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', 'new_contributor', 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.
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.
sponsor_info (str): True if the user wants the sponsor_url shown in the report
link_to_profile (str): True if the user wants the username linked to Github profile in the report
Returns:
None
"""
# Put together the contributor table
table, total_contributions = get_contributor_table(
collaborators,
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
ghe,
)
# 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
)
# 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:
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"
)
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"
if len(collaborators) > 0:
new_contributors_percentage = round(
(len([x for x in collaborators if x.new_contributor is True]))
/ len(collaborators)
* 100,
2,
)
else:
new_contributors_percentage = 0
summary_table += f"| {str(len(collaborators))} | {str(total_contributions)} | {str(new_contributors_percentage)}% |\n\n"
else:
summary_table = "| Total Contributors | Total Contributions |\n| --- | --- |\n"
summary_table += (
f"| {str(len(collaborators))} | {str(total_contributions)} |\n\n"
)
return summary_table
def get_contributor_table(
collaborators,
start_date,
end_date,
organization,
repository,
sponsor_info,
link_to_profile,
ghe,
):
"""
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.
sponsor_info (str): True if the user wants the sponsor_url shown in the report
link_to_profile (str): True if the user wants the username linked to Github profile in the report
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.
"""
columns = ["Username", "All Time Contribution Count"]
if start_date and end_date:
columns += ["New Contributor"]
if sponsor_info == "true":
columns += ["Sponsor URL"]
if start_date and end_date:
columns += [f"Commits between {start_date} and {end_date}"]
else:
columns += ["All Commits"]
headers = "| " + " | ".join(columns) + " |\n"
headers += "| " + " | ".join(["---"] * len(columns)) + " |\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:
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
endpoint = ghe.removeprefix("https://") if ghe else "github.com"
org_repo_link_name = url.split("/commits")[0].split(f"{endpoint}/")[1]
url = f"[{org_repo_link_name}]({url})"
commit_urls += f"{url}, "
new_contributor = collaborator.new_contributor
row = f"| {'' if link_to_profile == 'false' else '@'}{username} | {contribution_count} |"
if "New Contributor" in columns:
row += f" {new_contributor} |"
if "Sponsor URL" in columns:
if collaborator.sponsor_info == "":
row += " not sponsorable |"
else:
row += f" [Sponsor Link]({collaborator.sponsor_info}) |"
row += f" {commit_urls} |\n"
table += row
return table, total_contributions