Skip to content

Commit 8912a99

Browse files
committed
Updated logging scripts
updated the scripts to properly log the contributions made to the files whose parent directory is .github and to make sure the same is reflected within the deployed pages, the markdown updation srcipts have been modified. Also added docstrings.
1 parent eb57cd2 commit 8912a99

File tree

3 files changed

+168
-17
lines changed

3 files changed

+168
-17
lines changed

.github/scripts/convert_to_html_tables.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@
1111
> GitHub action variable: ${{ github.repository }}
1212
'''
1313

14+
1415
def find_table_points(lines):
16+
"""
17+
Find table points within a given list of lines.
18+
19+
The table points are determined by the presence of the markers:
20+
<!-- TABLE BEGINS -->
21+
<!-- TABLE ENDS -->
22+
23+
Args:
24+
lines (list): List of lines to search in.
25+
26+
Returns:
27+
tuple: A tuple of two integers containing the start and end indices of
28+
the table points.
29+
30+
Raises:
31+
SystemExit: If the table markers are not found or if the table end
32+
marker appears before the table start marker.
33+
"""
1534

1635
# Setting default return values
1736
table_start = None
@@ -42,6 +61,18 @@ def find_table_points(lines):
4261

4362

4463
def main():
64+
"""
65+
Update the index.md file with the latest contributors data.
66+
67+
This function retrieves the REPO_NAME environment variable and the
68+
CONTRIBUTORS_LOG file path. It then reads the log file and extracts the
69+
data from it. The function then reads the index.md file and calculates
70+
the table points. If the table does not exist, it creates the table
71+
header. The function then iterates over the log data and updates the
72+
table with the latest data. Finally, it updates the index.md file with
73+
the updated data and prints a success message.
74+
75+
"""
4576

4677
# Retrieving Environmental variables
4778
REPO_NAME = os.environ.get('REPO_NAME')
@@ -79,22 +110,29 @@ def main():
79110

80111
# Processing contributors-names
81112
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]
113+
contributors_names_list = [
114+
f'<a href="https://github.com/{name}" title="goto {name} profile">{name}</a>' for name in contributors_names]
83115
contributors_names_output = ', '.join(contributors_names_list)
84116

85117
# Processing pull-requests
86118
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]
119+
pull_requests_list = [
120+
f'<a href="https://github.com/{REPO_NAME}/pull/{pr}" title="visit pr \#{pr}">{pr}</a>' for pr in pull_requests]
88121
pull_requests_output = ', '.join(pull_requests_list)
89122

90123
# Processing demo-path
91124
demo_path = details['demo-path']
92125
if ' ' in demo_path:
93126
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>'
127+
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/{title}/</a>'
95128
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-
129+
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/</a>'
130+
elif title == '{workflows}':
131+
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github/workflows</a>'
132+
elif title == '{scripts}':
133+
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github/scripts</a>'
134+
elif title == '{others}':
135+
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github</a>'
98136

99137
# Appending all data together
100138
updated_lines.append('\t<tr align="center">\n')
@@ -119,4 +157,4 @@ def main():
119157

120158

121159
if __name__ == '__main__':
122-
main()
160+
main()

.github/scripts/update_contributors_log.py

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
> GitHub action variable: ${{ github.event.pull_request.number }}
1616
'''
1717

18+
1819
def get_project_title(pr_data):
20+
"""
21+
Determines the project title based on the file paths in the pull request data.
22+
23+
Args:
24+
pr_data (dict): The pull request data containing file paths.
25+
26+
Returns:
27+
str: The project title derived from the directory name in the file path.
28+
Returns 'root' if changes are made in the root of the repository.
29+
Special cases include '{workflows}', '{scripts}', and '{others}'
30+
for certain paths within the '.github' directory.
31+
32+
"""
1933

2034
# Setting default value
2135
project_title = 'root'
@@ -26,16 +40,45 @@ def get_project_title(pr_data):
2640
project_title = i["path"]
2741
break
2842

29-
# If we find a directory
30-
if project_title != 'root':
31-
project_title = project_title.split('/')[0]
43+
# changes are made in the root of repo
44+
if project_title == 'root':
45+
return project_title
46+
47+
if '.github/workflows' in project_title:
48+
project_title = '{workflows}'
49+
elif '.github/scripts' in project_title:
50+
project_title = '{scripts}'
51+
elif '.github' in project_title:
52+
project_title = '{others}'
53+
else:
54+
project_title = project_title.split('/')[0] # directory name
3255

3356
return project_title
3457

58+
3559
def get_contributor_name(pr_data):
60+
"""
61+
Retrieves the username of the contributor who made the pull request.
62+
63+
Args:
64+
pr_data (dict): The pull request data containing the author's username.
65+
66+
Returns:
67+
str: The username of the contributor.
68+
"""
3669
return pr_data["author"]["login"]
3770

71+
3872
def get_demo_path(pr_data):
73+
"""
74+
Retrieves the demo path for the pull request.
75+
76+
Args:
77+
pr_data (dict): The pull request data containing information about the pull request.
78+
79+
Returns:
80+
str: The demo path of the pull request.
81+
"""
3982

4083
# Getting required values
4184
REPO_NAME = os.environ.get('REPO_NAME')
@@ -45,8 +88,17 @@ def get_demo_path(pr_data):
4588
if PROJECT_NAME == 'root':
4689
return f'https://github.com/{REPO_NAME}/'
4790

91+
url_path = PROJECT_NAME
92+
93+
# Setting custom path for workflow maintance
94+
SPECIAL_CASES = ['{workflows}', '{scripts}', '{others}']
95+
if PROJECT_NAME in SPECIAL_CASES:
96+
url_path = '.github'
97+
if PROJECT_NAME in SPECIAL_CASES[:2]:
98+
url_path += f'/{PROJECT_NAME[1:-1]}'
99+
48100
# Setting default value
49-
demo_path = f'https://github.com/{REPO_NAME}/tree/main/{PROJECT_NAME}'
101+
demo_path = f'https://github.com/{REPO_NAME}/tree/main/{url_path}'
50102
found_required_path = False
51103

52104
# Iterating through the "files" list
@@ -56,7 +108,7 @@ def get_demo_path(pr_data):
56108
demo_path = path
57109
found_required_path = True
58110
break
59-
elif path.lower().endswith('index.md') or path.lower().endswith('readme.md'):
111+
elif path.lower().endswith('index.md') or path.lower().endswith('readme.md'):
60112
demo_path = path
61113
found_required_path = True
62114

@@ -70,7 +122,26 @@ def get_demo_path(pr_data):
70122

71123
return demo_path
72124

125+
73126
def main():
127+
"""
128+
Updates the contributors log file after a pull request has been merged.
129+
130+
This function is to be called in a GitHub Actions workflow after a pull request has been merged.
131+
It reads the details of the current pull request from a JSON file, extracts the required information,
132+
and updates the contributors log file accordingly.
133+
134+
The contributors log file is a JSON file that contains information about each contributor, including
135+
their name, the number of the pull request they contributed to, and the path to their project.
136+
137+
The function dumps the data into the log file and outputs a success message upon completion.
138+
139+
Args:
140+
None
141+
142+
Returns:
143+
None
144+
"""
74145

75146
# Setting required file paths
76147
CURRENT_PR_DETAILS_PATH = 'pr.json'
@@ -125,5 +196,6 @@ def main():
125196
# Output message
126197
print(f'Successfully {operation_name} the log file')
127198

199+
128200
if __name__ == '__main__':
129-
main()
201+
main()

.github/scripts/update_index_md.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,26 @@
1111
> GitHub action variable: ${{ github.repository }}
1212
'''
1313

14+
1415
def find_table_points(lines):
16+
"""
17+
Find table points within a given list of lines.
18+
19+
The table points are determined by the presence of the markers:
20+
<!-- TABLE BEGINS -->
21+
<!-- TABLE ENDS -->
22+
23+
Args:
24+
lines (list): List of lines to search in.
25+
26+
Returns:
27+
tuple: A tuple of two integers containing the start and end indices of
28+
the table points.
29+
30+
Raises:
31+
SystemExit: If the table markers are not found or if the table end
32+
marker appears before the table start marker.
33+
"""
1534

1635
# Setting default return values
1736
table_start = None
@@ -42,6 +61,18 @@ def find_table_points(lines):
4261

4362

4463
def main():
64+
"""
65+
Update the index.md file with the latest contributors data.
66+
67+
This function retrieves the REPO_NAME environment variable and the
68+
CONTRIBUTORS_LOG file path. It then reads the log file and extracts the
69+
data from it. The function then reads the index.md file and calculates
70+
the table points. If the table does not exist, it creates the table
71+
header. The function then iterates over the log data and updates the
72+
table with the latest data. Finally, it updates the index.md file with
73+
the updated data and prints a success message.
74+
75+
"""
4576

4677
# Retrieving Environmental variables
4778
REPO_NAME = os.environ.get('REPO_NAME')
@@ -64,7 +95,8 @@ def main():
6495
# Creating table header if doesn't exist
6596
if table_end - table_start == 1:
6697
table_header = list()
67-
table_header.append('| Project Title | Contributor Names | Pull Requests | Demo |\n')
98+
table_header.append(
99+
'| Project Title | Contributor Names | Pull Requests | Demo |\n')
68100
table_header.append('| --- | --- | --- | --- |\n')
69101
lines[table_start+1:table_end] = table_header
70102

@@ -76,12 +108,14 @@ def main():
76108

77109
# Processing contributors-names
78110
contributors_names = details['contributor-name']
79-
contributors_names_list = [f'[{name}](https://github.com/{name} "goto {name} profile")' for name in contributors_names]
111+
contributors_names_list = [
112+
f'[{name}](https://github.com/{name} "goto {name} profile")' for name in contributors_names]
80113
contributors_names_output = ', '.join(contributors_names_list)
81114

82115
# Processing pull-requests
83116
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]
117+
pull_requests_list = [
118+
f'[#{pr}](https://github.com/{REPO_NAME}/pull/{pr} "visit pr \#{pr}")' for pr in pull_requests]
85119
pull_requests_output = ', '.join(pull_requests_list)
86120

87121
# Processing demo-path
@@ -91,9 +125,16 @@ def main():
91125
demo_path_output = f'[/{REPO_NAME}/{title}/]({demo_path} "view the result of {title}")'
92126
if title == 'root' or title == '{init}':
93127
demo_path_output = f'[/{REPO_NAME}/]({demo_path} "view the result of {title}")'
128+
elif title == '{workflows}':
129+
demo_path_output = f'[/{REPO_NAME}/.github/workflows]({demo_path} "view the result of {title}")'
130+
elif title == '{scripts}':
131+
demo_path_output = f'[/{REPO_NAME}/.github/scripts]({demo_path} "view the result of {title}")'
132+
elif title == '{others}':
133+
demo_path_output = f'[/{REPO_NAME}/.github]({demo_path} "view the result of {title}")'
94134

95135
# Appending all data together
96-
updated_lines.append(f'| {title} | {contributors_names_output} | {pull_requests_output} | {demo_path_output} |\n')
136+
updated_lines.append(
137+
f'| {title} | {contributors_names_output} | {pull_requests_output} | {demo_path_output} |\n')
97138

98139
# Updating the lines with updated data
99140
lines[table_start+3:table_end] = updated_lines
@@ -107,4 +148,4 @@ def main():
107148

108149

109150
if __name__ == '__main__':
110-
main()
151+
main()

0 commit comments

Comments
 (0)