Skip to content

Commit e446842

Browse files
committed
Added update_index_md.py
1 parent e2d68be commit e446842

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

.github/scripts/update_index_md.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 table header if doesn't exist
65+
if table_end - table_start == 1:
66+
table_header = list()
67+
table_header.append('| Project Title | Contributor Names | Pull Requests | Demo |\n')
68+
table_header.append('| --- | --- | --- | --- |\n')
69+
lines[table_start+1:table_end] = table_header
70+
71+
# Initializing empty list for lines
72+
updated_lines = list()
73+
74+
# Iterating over log to update target file
75+
for title, details in data.items():
76+
77+
# Processing contributors-names
78+
contributors_names = details['contributor-name']
79+
contributors_names_list = [f'[{name}](https://github.com/{name} "goto {name} profile")' for name in contributors_names]
80+
contributors_names_output = ', '.join(contributors_names_list)
81+
82+
# Processing pull-requests
83+
pull_requests = details['pull-request-number']
84+
pull_requests_list = [f'[#{pr}](https://github.com/{REPO_NAME}/pull/{pr} "visit pr \#{pr}")' for pr in pull_requests]
85+
pull_requests_output = ', '.join(pull_requests_list)
86+
87+
# Processing demo-path
88+
demo_path = details['demo-path']
89+
if ' ' in demo_path:
90+
demo_path = '%20'.join(demo_path.split())
91+
demo_path_output = f'[/{REPO_NAME}/{title}/]({demo_path} "view the result of {title}")'
92+
if title == 'root' or title == '{init}':
93+
demo_path_output = f'[/{REPO_NAME}/]({demo_path} "view the result of {title}")'
94+
95+
# Appending all data together
96+
updated_lines.append(f'| {title} | {contributors_names_output} | {pull_requests_output} | {demo_path_output} |\n')
97+
98+
# Updating the lines with updated data
99+
lines[table_start+3:table_end] = updated_lines
100+
101+
# Updating the target file
102+
with open(TARGET_FILE, 'w') as file:
103+
file.writelines(lines)
104+
105+
# Printing Success Message
106+
print(f"Updated '{TARGET_FILE}' Successfully")
107+
108+
109+
if __name__ == '__main__':
110+
main()

0 commit comments

Comments
 (0)