Skip to content

Commit

Permalink
Updating logging script and Adding automation workflows
Browse files Browse the repository at this point in the history
Merge pull request #28 from iamwatchdogs/updates
  • Loading branch information
iamwatchdogs authored Oct 19, 2024
2 parents d8e7a8a + f2dc7c4 commit e2fcfc8
Show file tree
Hide file tree
Showing 8 changed files with 311 additions and 11 deletions.
26 changes: 26 additions & 0 deletions .github/auto-assign-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Set to true to add reviewers to pull requests
addReviewers: true

# Set to true to add assignees to pull requests
addAssignees: author

# A list of reviewers to be added to pull requests (GitHub user name)
reviewers:
- iamwatchdogs

# A number of reviewers added to the pull request
# Set 0 to add all the reviewers (default: 0)
numberOfReviewers: 1

# A list of assignees, overrides reviewers if set
# assignees:
# - assigneeA

# A number of assignees to add to the pull request
# Set to 0 to add all of the assignees.
# Uses numberOfReviewers if unset.
# numberOfAssignees: 2

# A list of keywords to be skipped the process that add reviewers if pull requests include it
# skipKeywords:
# - wip
43 changes: 41 additions & 2 deletions .github/scripts/convert_to_html_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@
> GitHub action variable: ${{ github.repository }}
'''


def find_table_points(lines):
"""
Find table points within a given list of lines.
The table points are determined by the presence of the markers:
<!-- TABLE BEGINS -->
<!-- TABLE ENDS -->
Args:
lines (list): List of lines to search in.
Returns:
tuple: A tuple of two integers containing the start and end indices of
the table points.
Raises:
SystemExit: If the table markers are not found or if the table end
marker appears before the table start marker.
"""

# Setting default return values
table_start = None
Expand Down Expand Up @@ -42,6 +61,18 @@ def find_table_points(lines):


def main():
"""
Update the index.md file with the latest contributors data.
This function retrieves the REPO_NAME environment variable and the
CONTRIBUTORS_LOG file path. It then reads the log file and extracts the
data from it. The function then reads the index.md file and calculates
the table points. If the table does not exist, it creates the table
header. The function then iterates over the log data and updates the
table with the latest data. Finally, it updates the index.md file with
the updated data and prints a success message.
"""

# Retrieving Environmental variables
REPO_NAME = os.environ.get('REPO_NAME')
Expand Down Expand Up @@ -79,12 +110,14 @@ def main():

# 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_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_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
Expand All @@ -94,6 +127,12 @@ def main():
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/{title}/</a>'
if title == 'root' or title == '{init}':
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/</a>'
elif title == '{workflows}':
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github/workflows</a>'
elif title == '{scripts}':
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github/scripts</a>'
elif title == '{others}':
demo_path_output = f'<a href="{demo_path}" title="view the result of {title}">/{REPO_NAME}/.github</a>'

# Appending all data together
updated_lines.append('\t<tr align="center">\n')
Expand Down
82 changes: 77 additions & 5 deletions .github/scripts/update_contributors_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@
> GitHub action variable: ${{ github.event.pull_request.number }}
'''


def get_project_title(pr_data):
"""
Determines the project title based on the file paths in the pull request data.
Args:
pr_data (dict): The pull request data containing file paths.
Returns:
str: The project title derived from the directory name in the file path.
Returns 'root' if changes are made in the root of the repository.
Special cases include '{workflows}', '{scripts}', and '{others}'
for certain paths within the '.github' directory.
"""

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

# If we find a directory
if project_title != 'root':
project_title = project_title.split('/')[0]
# changes are made in the root of repo
if project_title == 'root':
return project_title

if '.github/workflows' in project_title:
project_title = '{workflows}'
elif '.github/scripts' in project_title:
project_title = '{scripts}'
elif '.github' in project_title:
project_title = '{others}'
else:
project_title = project_title.split('/')[0] # directory name

return project_title


def get_contributor_name(pr_data):
"""
Retrieves the username of the contributor who made the pull request.
Args:
pr_data (dict): The pull request data containing the author's username.
Returns:
str: The username of the contributor.
"""
return pr_data["author"]["login"]


def get_demo_path(pr_data):
"""
Retrieves the demo path for the pull request.
Args:
pr_data (dict): The pull request data containing information about the pull request.
Returns:
str: The demo path of the pull request.
"""

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

url_path = PROJECT_NAME

# Setting custom path for workflow maintance
SPECIAL_CASES = ['{workflows}', '{scripts}', '{others}']
if PROJECT_NAME in SPECIAL_CASES:
url_path = '.github'
if PROJECT_NAME in SPECIAL_CASES[:2]:
url_path += f'/{PROJECT_NAME[1:-1]}'

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

# Iterating through the "files" list
Expand All @@ -56,7 +108,7 @@ def get_demo_path(pr_data):
demo_path = path
found_required_path = True
break
elif path.lower().endswith('index.md') or path.lower().endswith('readme.md'):
elif path.lower().endswith('index.md') or path.lower().endswith('readme.md'):
demo_path = path
found_required_path = True

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

return demo_path


def main():
"""
Updates the contributors log file after a pull request has been merged.
This function is to be called in a GitHub Actions workflow after a pull request has been merged.
It reads the details of the current pull request from a JSON file, extracts the required information,
and updates the contributors log file accordingly.
The contributors log file is a JSON file that contains information about each contributor, including
their name, the number of the pull request they contributed to, and the path to their project.
The function dumps the data into the log file and outputs a success message upon completion.
Args:
None
Returns:
None
"""

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


if __name__ == '__main__':
main()
49 changes: 45 additions & 4 deletions .github/scripts/update_index_md.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,26 @@
> GitHub action variable: ${{ github.repository }}
'''


def find_table_points(lines):
"""
Find table points within a given list of lines.
The table points are determined by the presence of the markers:
<!-- TABLE BEGINS -->
<!-- TABLE ENDS -->
Args:
lines (list): List of lines to search in.
Returns:
tuple: A tuple of two integers containing the start and end indices of
the table points.
Raises:
SystemExit: If the table markers are not found or if the table end
marker appears before the table start marker.
"""

# Setting default return values
table_start = None
Expand Down Expand Up @@ -42,6 +61,18 @@ def find_table_points(lines):


def main():
"""
Update the index.md file with the latest contributors data.
This function retrieves the REPO_NAME environment variable and the
CONTRIBUTORS_LOG file path. It then reads the log file and extracts the
data from it. The function then reads the index.md file and calculates
the table points. If the table does not exist, it creates the table
header. The function then iterates over the log data and updates the
table with the latest data. Finally, it updates the index.md file with
the updated data and prints a success message.
"""

# Retrieving Environmental variables
REPO_NAME = os.environ.get('REPO_NAME')
Expand All @@ -64,7 +95,8 @@ def main():
# 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(
'| Project Title | Contributor Names | Pull Requests | Demo |\n')
table_header.append('| --- | --- | --- | --- |\n')
lines[table_start+1:table_end] = table_header

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

# 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_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_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
Expand All @@ -91,9 +125,16 @@ def main():
demo_path_output = f'[/{REPO_NAME}/{title}/]({demo_path} "view the result of {title}")'
if title == 'root' or title == '{init}':
demo_path_output = f'[/{REPO_NAME}/]({demo_path} "view the result of {title}")'
elif title == '{workflows}':
demo_path_output = f'[/{REPO_NAME}/.github/workflows]({demo_path} "view the result of {title}")'
elif title == '{scripts}':
demo_path_output = f'[/{REPO_NAME}/.github/scripts]({demo_path} "view the result of {title}")'
elif title == '{others}':
demo_path_output = f'[/{REPO_NAME}/.github]({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')
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
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/auto-assigner.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Auto Assign

on:
pull_request_target:
types: [opened, ready_for_review]
issues:
types: [opened]

permissions:
issues: write
pull-requests: write

jobs:
auto-assign:
runs-on: ubuntu-latest
steps:
- uses: kentaro-m/[email protected]
with:
configuration-path: '.github/auto-assign-config.yml'
28 changes: 28 additions & 0 deletions .github/workflows/auto-commenter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Auto-commenter

on:
pull_request_target:
types: [opened, closed]

permissions:
id-token: write
issues: write
pull-requests: write

jobs:
automated-message:
runs-on: ubuntu-latest
steps:
- uses: wow-actions/auto-comment@v1
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
pullRequestOpened: |
👋 @{{ author }}
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.
pullRequestClosed: |
👋 @{{ author }} This PR is closed. If you think there's been a mistake, please contact the maintainer @iamwatchdogs.
pullRequestMerged: |
Thank you for contributing @{{ author }}. Make sure to check your contribution on [GitHub Pages](https://grow-with-open-source.github.io/Javascript-Projects/ "view contributions").
Loading

0 comments on commit e2fcfc8

Please sign in to comment.