1+ #!/usr/bin/env python
2+
3+ import os
4+ import json
5+
6+ '''
7+ This script requires following environment variables:
8+
9+ - REPO_NAME:
10+ > example: 'iamwatchdogs/test'
11+ > GitHub action variable: ${{ github.repository }}
12+ '''
13+
14+ def find_table_points (lines ):
15+
16+ # Setting default return values
17+ table_start = None
18+ table_end = None
19+
20+ # Setting the markers
21+ table_start_marker = '<!-- TABLE BEGINS -->'
22+ table_end_marker = '<!-- TABLE ENDS -->'
23+
24+ # Iterating over lines to find the markers
25+ for index , line in enumerate (lines ):
26+ if table_start is None and table_start_marker in line :
27+ table_start = index
28+ elif table_end is None and table_end_marker in line :
29+ table_end = index
30+ if table_start is not None and table_end is not None :
31+ break
32+
33+ # Checking for possible errors
34+ if table_start is None or table_end is None :
35+ print ('Table not found in the file.' )
36+ exit (1 )
37+ elif table_start >= table_end :
38+ print ('Invaild use of table markers.' )
39+ exit (2 )
40+
41+ return (table_start , table_end )
42+
43+
44+ def main ():
45+
46+ # Retrieving Environmental variables
47+ REPO_NAME = os .environ .get ('REPO_NAME' )
48+
49+ # Setting path for the log JSON file
50+ TARGET_FILE = 'index.md'
51+ CONTRIBUTORS_LOG = '.github/data/contributors-log.json'
52+
53+ # Retrieving data from log file
54+ with open (CONTRIBUTORS_LOG , 'r' ) as json_file :
55+ data = json .load (json_file )
56+
57+ # Reading lines from the file
58+ with open (TARGET_FILE , 'r' ) as file :
59+ lines = file .readlines ()
60+
61+ # Calculating Stating and ending points of the targeted table
62+ table_start , table_end = find_table_points (lines )
63+
64+ # Creating HTML table header to replace md table
65+ table_header = list ()
66+ table_header .append ('<table>\n ' )
67+ table_header .append ('\t <tr align="center">\n ' )
68+ table_header .append ('\t \t <th>Project Title</th>\n ' )
69+ table_header .append ('\t \t <th>Contributor Names</th>\n ' )
70+ table_header .append ('\t \t <th>Pull Requests</th>\n ' )
71+ table_header .append ('\t \t <th>Demo</th>\n ' )
72+ table_header .append ('\t </tr>\n ' )
73+
74+ # Initializing empty list for lines
75+ updated_lines = list ()
76+
77+ # Iterating over log to update target file
78+ for title , details in data .items ():
79+
80+ # Processing contributors-names
81+ contributors_names = details ['contributor-name' ]
82+ contributors_names_list = [f'<a href="https://github.com/{ name } " title="goto { name } profile">{ name } </a>' for name in contributors_names ]
83+ contributors_names_output = ', ' .join (contributors_names_list )
84+
85+ # Processing pull-requests
86+ pull_requests = details ['pull-request-number' ]
87+ pull_requests_list = [f'<a href="https://github.com/{ REPO_NAME } /pull/{ pr } " title="visit pr \#{ pr } ">{ pr } </a>' for pr in pull_requests ]
88+ pull_requests_output = ', ' .join (pull_requests_list )
89+
90+ # Processing demo-path
91+ demo_path = details ['demo-path' ]
92+ if ' ' in demo_path :
93+ demo_path = '%20' .join (demo_path .split ())
94+ demo_path_output = f'<a href="{ demo_path } " title="view the result of { title } ">/{ REPO_NAME } /{ title } /</a>'
95+ if title == 'root' or title == '{init}' :
96+ demo_path_output = f'<a href="{ demo_path } " title="view the result of { title } ">/{ REPO_NAME } /</a>'
97+
98+
99+ # Appending all data together
100+ updated_lines .append ('\t <tr align="center">\n ' )
101+ updated_lines .append (f'\t \t <td>{ title } </td>\n ' )
102+ updated_lines .append (f'\t \t <td>{ contributors_names_output } </td>\n ' )
103+ updated_lines .append (f'\t \t <td>{ pull_requests_output } </td>\n ' )
104+ updated_lines .append (f'\t \t <td>{ demo_path_output } </td>\n ' )
105+ updated_lines .append (f'\t </tr>\n ' )
106+
107+ # Table footer
108+ table_footer = ['</table>\n ' ]
109+
110+ # Updating the lines with updated data
111+ lines [table_start + 1 :table_end ] = table_header + updated_lines + table_footer
112+
113+ # Updating the target file
114+ with open (TARGET_FILE , 'w' ) as file :
115+ file .writelines (lines )
116+
117+ # Printing Success Message
118+ print (f"Updated '{ TARGET_FILE } ' Successfully" )
119+
120+
121+ if __name__ == '__main__' :
122+ main ()
0 commit comments