Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/data/contributors-log.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"{init}": {
"contributor-name": ["iamwatchdogs"],
"pull-request-number": ["1"],
"demo-path": "https://github.com/Grow-with-Open-Source/Javascript-Projects"
}
}
119 changes: 119 additions & 0 deletions .github/scripts/convert_to_html_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python

import os
import json

'''
This script requires following environment variables:

- REPO_NAME:
> example: 'iamwatchdogs/test'
> GitHub action variable: ${{ github.repository }}
'''

def find_table_points(lines):

# Setting default return values
table_start = None
table_end = None

# Setting the markers
table_start_marker = '<!-- TABLE BEGINS -->'
table_end_marker = '<!-- TABLE ENDS -->'

# Iterating over lines to find the markers
for index, line in enumerate(lines):
if table_start is None and table_start_marker in line:
table_start = index
elif table_end is None and table_end_marker in line:
table_end = index
if table_start is not None and table_end is not None:
break

# Checking for possible errors
if table_start is None or table_end is None:
print('Table not found in the file.')
exit(1)
elif table_start >= table_end:
print('Invaild use of table markers.')
exit(2)

return (table_start, table_end)


def main():

# Retrieving Environmental variables
REPO_NAME = os.environ.get('REPO_NAME')

# Setting path for the log JSON file
TARGET_FILE = 'index.md'
CONTRIBUTORS_LOG = '.github/data/contributors-log.json'

# Retrieving data from log file
with open(CONTRIBUTORS_LOG, 'r') as json_file:
data = json.load(json_file)

# Reading lines from the file
with open(TARGET_FILE, 'r') as file:
lines = file.readlines()

# Calculating Stating and ending points of the targeted table
table_start, table_end = find_table_points(lines)

# Creating HTML table header to replace md table
table_header = list()
table_header.append('<table>\n')
table_header.append('\t<tr align="center">\n')
table_header.append('\t\t<th>Project Title</th>\n')
table_header.append('\t\t<th>Contributor Names</th>\n')
table_header.append('\t\t<th>Pull Requests</th>\n')
table_header.append('\t\t<th>Demo</th>\n')
table_header.append('\t</tr>\n')

# Initializing empty list for lines
updated_lines = list()

# Iterating over log to update target file
for title, details in data.items():

# Processing contributors-names
contributors_names = details['contributor-name']
contributors_names_list = [f'<a href="https://github.com/{name}" title="goto {name} profile">{name}</a>' for name in contributors_names]
contributors_names_output = ', '.join(contributors_names_list)

# Processing pull-requests
pull_requests = details['pull-request-number']
pull_requests_list = [f'<a href="https://github.com/{REPO_NAME}/pull/{pr}" title="visit pr \#{pr}">{pr}</a>' for pr in pull_requests]
pull_requests_output = ', '.join(pull_requests_list)

# Processing demo-path
demo_path = details['demo-path']
if ' ' in demo_path:
demo_path = '%20'.join(demo_path.split())
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/{title}/</a>'

# Appending all data together
updated_lines.append('\t<tr align="center">\n')
updated_lines.append(f'\t\t<td>{title}</td>\n')
updated_lines.append(f'\t\t<td>{contributors_names_output}</td>\n')
updated_lines.append(f'\t\t<td>{pull_requests_output}</td>\n')
updated_lines.append(f'\t\t<td>{demo_path_output}</td>\n')
updated_lines.append(f'\t</tr>\n')

# Table footer
table_footer = ['</table>\n']

# Updating the lines with updated data
lines[table_start+1:table_end] = table_header+updated_lines+table_footer

# Updating the target file
with open(TARGET_FILE, 'w') as file:
file.writelines(lines)

# Printing Success Message
print(f"Updated '{TARGET_FILE}' Successfully")


if __name__ == '__main__':
main()
129 changes: 129 additions & 0 deletions .github/scripts/update_contributors_log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env python

import os
import json

'''
This script requires following environment variables:

- REPO_NAME:
> example: 'iamwatchdogs/test'
> GitHub action variable: ${{ github.repository }}

- PR_NUMBER:
> example: '5'
> GitHub action variable: ${{ github.event.pull_request.number }}
'''

def get_project_title(pr_data):

# Setting default value
project_title = 'root'

# Iterating through the "files" list
for i in pr_data["files"]:
if '/' in i["path"]:
project_title = i["path"]
break

# If we find a directory
if project_title != 'root':
project_title = project_title.split('/')[0]

return project_title

def get_contributor_name(pr_data):
return pr_data["author"]["login"]

def get_demo_path(pr_data):

# Getting required values
REPO_NAME = os.environ.get('REPO_NAME')
PROJECT_NAME = get_project_title(pr_data)

# Handling a base case
if PROJECT_NAME == 'root':
return f'https://github.com/{REPO_NAME}/'

# Setting default value
demo_path = f'https://github.com/{REPO_NAME}/tree/main/{PROJECT_NAME}'
found_required_path = False

# Iterating through the "files" list
for file_data in pr_data["files"]:
path = file_data["path"]
if "index.html" in path:
demo_path = path
found_required_path = True
break
elif path.lower().endswith('index.md') or path.lower().endswith('readme.md'):
demo_path = path
found_required_path = True

# Modifying demo path as a route
if found_required_path:
demo_path = '/'.join(demo_path.split('/')[:-1])

# Checking out for spaces:
if ' ' in demo_path:
demo_path = '%20'.join(demo_path.split())

return demo_path

def main():

# Setting required file paths
CURRENT_PR_DETAILS_PATH = 'pr.json'
CONTRIBUTORS_LOG_PATH = '.github/data/contributors-log.json'

# Reading contents from the current pr
with open(CURRENT_PR_DETAILS_PATH, 'r') as json_file:
current_pr = json.load(json_file)

# Getting required value for update
PROJECT_TITLE = get_project_title(current_pr)
CONTRIBUTOR_NAME = get_contributor_name(current_pr)
PR_NUMBER = os.environ.get('PR_NUMBER')
DEMO_PATH = get_demo_path(current_pr)

# Creating a new dict objects for JSON conversion
existing_data = None
new_data = {
PROJECT_TITLE: {
"contributor-name": [CONTRIBUTOR_NAME],
"pull-request-number": [PR_NUMBER],
"demo-path": DEMO_PATH
}
}

# Processing the data dumps
operation_name = None
if os.path.exists(CONTRIBUTORS_LOG_PATH):

# Reading existing Log file
with open(CONTRIBUTORS_LOG_PATH, 'r') as json_file:
existing_data = json.load(json_file)

# performing updation or addition based on `PROJECT_TITLE`
if PROJECT_TITLE in existing_data:
if CONTRIBUTOR_NAME not in existing_data[PROJECT_TITLE]["contributor-name"]:
existing_data[PROJECT_TITLE]["contributor-name"].append(CONTRIBUTOR_NAME)
if PR_NUMBER not in existing_data[PROJECT_TITLE]["pull-request-number"]:
existing_data[PROJECT_TITLE]["pull-request-number"].append(PR_NUMBER)
operation_name = 'Updated'
else:
existing_data.update(new_data)
operation_name = 'Appended data to'
else:
existing_data = new_data
operation_name = 'Created'

# Dumping the data into log file
with open(CONTRIBUTORS_LOG_PATH, 'w') as json_file:
json.dump(existing_data, json_file, indent=2)

# Output message
print(f'Successfully {operation_name} the log file')

if __name__ == '__main__':
main()
108 changes: 108 additions & 0 deletions .github/scripts/update_index_md.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python

import os
import json

'''
This script requires following environment variables:

- REPO_NAME:
> example: 'iamwatchdogs/test'
> GitHub action variable: ${{ github.repository }}
'''

def find_table_points(lines):

# Setting default return values
table_start = None
table_end = None

# Setting the markers
table_start_marker = '<!-- TABLE BEGINS -->'
table_end_marker = '<!-- TABLE ENDS -->'

# Iterating over lines to find the markers
for index, line in enumerate(lines):
if table_start is None and table_start_marker in line:
table_start = index
elif table_end is None and table_end_marker in line:
table_end = index
if table_start is not None and table_end is not None:
break

# Checking for possible errors
if table_start is None or table_end is None:
print('Table not found in the file.')
exit(1)
elif table_start >= table_end:
print('Invaild use of table markers.')
exit(2)

return (table_start, table_end)


def main():

# Retrieving Environmental variables
REPO_NAME = os.environ.get('REPO_NAME')

# Setting path for the log JSON file
TARGET_FILE = 'index.md'
CONTRIBUTORS_LOG = '.github/data/contributors-log.json'

# Retrieving data from log file
with open(CONTRIBUTORS_LOG, 'r') as json_file:
data = json.load(json_file)

# Reading lines from the file
with open(TARGET_FILE, 'r') as file:
lines = file.readlines()

# Calculating Stating and ending points of the targeted table
table_start, table_end = find_table_points(lines)

# Creating table header if doesn't exist
if table_end - table_start == 1:
table_header = list()
table_header.append('| Project Title | Contributor Names | Pull Requests | Demo |\n')
table_header.append('| --- | --- | --- | --- |\n')
lines[table_start+1:table_end] = table_header

# Initializing empty list for lines
updated_lines = list()

# Iterating over log to update target file
for title, details in data.items():

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

# Processing pull-requests
pull_requests = details['pull-request-number']
pull_requests_list = [f'[#{pr}](https://github.com/{REPO_NAME}/pull/{pr} "visit pr \#{pr}")' for pr in pull_requests]
pull_requests_output = ', '.join(pull_requests_list)

# Processing demo-path
demo_path = details['demo-path']
if ' ' in demo_path:
demo_path = '%20'.join(demo_path.split())
demo_path_output = f'[/{REPO_NAME}/{title}/]({demo_path} "view the result of {title}")'

# Appending all data together
updated_lines.append(f'| {title} | {contributors_names_output} | {pull_requests_output} | {demo_path_output} |\n')

# Updating the lines with updated data
lines[table_start+3:table_end] = updated_lines

# Updating the target file
with open(TARGET_FILE, 'w') as file:
file.writelines(lines)

# Printing Success Message
print(f"Updated '{TARGET_FILE}' Successfully")


if __name__ == '__main__':
main()
Loading