11# pylint: disable=too-many-locals
22"""This module contains the functions needed to write the output to markdown files."""
33
4+ import os
5+
46
57def write_to_markdown (
68 collaborators ,
@@ -14,19 +16,30 @@ def write_to_markdown(
1416 ghe ,
1517):
1618 """
17- This function writes a list of collaborators to a markdown file in table format.
18- Each collaborator is represented as a dictionary with keys 'username', 'contribution_count', 'new_contributor', and 'commits'.
19+ This function writes a list of collaborators to a markdown file in table format
20+ and optionally to GitHub Actions Job Summary if running in a GitHub Actions environment.
21+ Each collaborator is represented as a dictionary with keys 'username',
22+ 'contribution_count', 'new_contributor', and 'commits'.
1923
2024 Args:
21- collaborators (list): A list of dictionaries, where each dictionary represents a collaborator.
22- Each dictionary should have the keys 'username', 'contribution_count', and 'commits'.
23- filename (str): The path of the markdown file to which the table will be written.
24- start_date (str): The start date of the date range for the contributor list.
25+ collaborators (list): A list of dictionaries, where each dictionary
26+ represents a collaborator. Each dictionary should
27+ have the keys 'username', 'contribution_count',
28+ and 'commits'.
29+ filename (str): The path of the markdown file to which the table will
30+ be written.
31+ start_date (str): The start date of the date range for the contributor
32+ list.
2533 end_date (str): The end date of the date range for the contributor list.
26- organization (str): The organization for which the contributors are being listed.
27- repository (str): The repository for which the contributors are being listed.
28- sponsor_info (str): True if the user wants the sponsor_url shown in the report
29- link_to_profile (str): True if the user wants the username linked to Github profile in the report
34+ organization (str): The organization for which the contributors are
35+ being listed.
36+ repository (str): The repository for which the contributors are being
37+ listed.
38+ sponsor_info (str): True if the user wants the sponsor_url shown in
39+ the report
40+ link_to_profile (str): True if the user wants the username linked to
41+ Github profile in the report
42+ ghe (str): The GitHub Enterprise instance URL, if applicable.
3043
3144 Returns:
3245 None
@@ -44,52 +57,98 @@ def write_to_markdown(
4457 ghe ,
4558 )
4659
47- # Put together the summary table including # of new contributions, # of new contributors, % new contributors, % returning contributors
60+ # Put together the summary table including # of new contributions,
61+ # # of new contributors, % new contributors, % returning contributors
4862 summary_table = get_summary_table (
4963 collaborators , start_date , end_date , total_contributions
5064 )
5165
52- # Write the markdown file
53- write_markdown_file (
54- filename , start_date , end_date , organization , repository , table , summary_table
66+ # Generate the markdown content once
67+ content = generate_markdown_content (
68+ start_date , end_date , organization , repository , table , summary_table
5569 )
5670
71+ # Write the markdown file
72+ write_markdown_file (filename , content )
73+
74+ # Also write to GitHub Actions Step Summary if available
75+ write_to_github_summary (content )
76+
77+
78+ def write_to_github_summary (content ):
79+ """
80+ Write markdown content to GitHub Actions Step Summary.
81+
82+ Args:
83+ content (str): The pre-generated markdown content to write.
84+
85+ Returns:
86+ None
87+ """
88+ # Only write to GitHub Step Summary if we're running in a GitHub Actions
89+ # environment
90+ github_step_summary = os .environ .get ("GITHUB_STEP_SUMMARY" )
91+ if github_step_summary :
92+ with open (github_step_summary , "a" , encoding = "utf-8" ) as summary_file :
93+ summary_file .write (content )
94+
5795
58- def write_markdown_file (
59- filename , start_date , end_date , organization , repository , table , summary_table
96+ def generate_markdown_content (
97+ start_date , end_date , organization , repository , table , summary_table
6098):
6199 """
62- This function writes all the tables and data to a markdown file with tables to organizae the information .
100+ This function generates markdown content as a string .
63101
64102 Args:
65- filename (str): The path of the markdown file to which the table will be written.
66- start_date (str): The start date of the date range for the contributor list.
103+ start_date (str): The start date of the date range for the contributor
104+ list.
67105 end_date (str): The end date of the date range for the contributor list.
68- organization (str): The organization for which the contributors are being listed.
69- repository (str): The repository for which the contributors are being listed.
70- table (str): A string containing a markdown table of the contributors and the total contribution count.
71- summary_table (str): A string containing a markdown table of the summary statistics.
106+ organization (str): The organization for which the contributors are
107+ being listed.
108+ repository (str): The repository for which the contributors are being
109+ listed.
110+ table (str): A string containing a markdown table of the contributors
111+ and the total contribution count.
112+ summary_table (str): A string containing a markdown table of the
113+ summary statistics.
114+
115+ Returns:
116+ str: The complete markdown content as a string.
117+
118+ """
119+ content = "# Contributors\n \n "
120+ if start_date and end_date :
121+ content += f"- Date range for contributor list: { start_date } to { end_date } \n "
122+ if organization :
123+ content += f"- Organization: { organization } \n "
124+ if repository :
125+ content += f"- Repository: { repository } \n "
126+ content += "\n "
127+ content += summary_table
128+ content += table
129+ content += (
130+ "\n _this file was generated by the "
131+ "[Contributors GitHub Action]"
132+ "(https://github.com/github/contributors)_\n "
133+ )
134+ return content
135+
136+
137+ def write_markdown_file (filename , content ):
138+ """
139+ This function writes the pre-generated markdown content to a file.
140+
141+ Args:
142+ filename (str): The path of the markdown file to which the content will
143+ be written.
144+ content (str): The pre-generated markdown content to write.
72145
73146 Returns:
74147 None
75148
76149 """
77150 with open (filename , "w" , encoding = "utf-8" ) as markdown_file :
78- markdown_file .write ("# Contributors\n \n " )
79- if start_date and end_date :
80- markdown_file .write (
81- f"- Date range for contributor list: { start_date } to { end_date } \n "
82- )
83- if organization :
84- markdown_file .write (f"- Organization: { organization } \n " )
85- if repository :
86- markdown_file .write (f"- Repository: { repository } \n " )
87- markdown_file .write ("\n " )
88- markdown_file .write (summary_table )
89- markdown_file .write (table )
90- markdown_file .write (
91- "\n _this file was generated by the [Contributors GitHub Action](https://github.com/github/contributors)_\n "
92- )
151+ markdown_file .write (content )
93152
94153
95154def get_summary_table (collaborators , start_date , end_date , total_contributions ):
0 commit comments