forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from avinashkranjan:master #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8bb054e
added github action files & updated database
XZANATOL 03caee2
added python script used by github actions
XZANATOL 3853466
Added requested changes
XZANATOL 8265e23
Added requested changes v2
XZANATOL f6498f0
Changes
kaustubhgupta fe77510
Merge pull request #1025 from XZANATOL/action_db_update
avinashkranjan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
.github/scripts/Update_Database.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from github import Github | ||
import json | ||
import sys | ||
import re | ||
|
||
# Regex Patterns | ||
category = r"- \[x\] (.+)" | ||
name = r"Title: (.+)" | ||
path = r"Folder: (.+)" | ||
requirments_path = r"Requirments: (.+)" | ||
entry = r"Script: (.+)" | ||
arguments = r"Arguments: (.+)" | ||
contributor = r"Contributor: (.+)" | ||
description = r"Description: (.+)" | ||
|
||
|
||
def add_script(category, name, path, entry, arguments, requirments_path, contributor, description, pa_token): | ||
""" Add a Contributor script to database """ | ||
new_data = {category: {name: [path, entry, arguments, requirments_path, contributor, description]}} | ||
data_store = read_data() | ||
|
||
try: | ||
# If category doesn't exist try will fail and except will ask to add a new category with the project | ||
if data_store[category]: # Check for existing category or a new one | ||
data_store[category].update(new_data[category]) # Add script | ||
except: | ||
data_store.update(new_data) # Add new category | ||
|
||
# <----- This part is to avoid a single/double quotes error when trying to update the database with PyGithub -----> | ||
with open("./Master Script/datastore.json", "w") as file: | ||
json.dump(data_store, file) | ||
print("Script added to database, pushing changes to repo...") | ||
|
||
with open("./Master Script/datastore.json", "r") as file: | ||
data_store = file.readlines()[0] | ||
|
||
# <----- Github Login & Database Update -----> | ||
git = Github(pa_token) | ||
user_object = git.get_user() | ||
print("[+] PyGithub Login Success!") | ||
|
||
repo = git.get_repo("avinashkranjan/Amazing-Python-Scripts") | ||
datastore_fileMaster = repo.get_contents("./Master Script/datastore.json", ref="master") | ||
datastore_fileWebsite = repo.get_contents("./datastore.json", ref="gh-pages") | ||
|
||
repo.update_file(datastore_fileMaster.path, "Updated datastore.json", data_store, datastore_fileMaster.sha, branch="master") | ||
repo.update_file("./datastore.json", "Updated datastore.json", data_store, datastore_fileWebsite.sha, branch="gh-pages") | ||
print("[+] Database Updated") | ||
|
||
|
||
def read_data(): | ||
""" Loads datastore.json """ | ||
with open("./Master Script/datastore.json", "r") as file: | ||
data = json.load(file) | ||
return data | ||
|
||
|
||
def extract_from_pr_body(pr_body, pa_token): | ||
""" Manipulates the provided PR body and extracts the required information """ | ||
pr_body = pr_body.split("\n") | ||
for element in pr_body: | ||
pr_body[pr_body.index(element)] = element.rstrip("\r") | ||
|
||
# A special case for contributors in gh-pages branch and other dependency PRs | ||
try: | ||
pr_body = pr_body[pr_body.index("## Project Metadata"):] | ||
except: | ||
sys.exit() | ||
|
||
category_list = [] | ||
for text in pr_body: | ||
# <----- Validate Category -----> | ||
cat = re.match(category, text) | ||
if cat is not None: | ||
category_list.append(cat[1]) | ||
# <----- Validate Title -----> | ||
if re.match(name, text) is not None: | ||
title = re.match(name, text)[1] | ||
# <----- Validate Folder -----> | ||
if re.match(path, text) is not None: | ||
folder = re.match(path, text)[1] | ||
# <----- Validate requirments.txt -----> | ||
if re.match(requirments_path, text) is not None: | ||
requirements = re.match(requirments_path, text)[1] | ||
# <----- Validate Script.py -----> | ||
if re.match(entry, text) is not None: | ||
script = re.match(entry, text)[1] | ||
# <----- Validate Arguments -----> | ||
if re.match(arguments, text) is not None: | ||
argument = re.match(arguments, text)[1] | ||
# <----- Validate Contribute -----> | ||
if re.match(contributor, text) is not None: | ||
user = re.match(contributor, text)[1] | ||
# <----- Validate Description -----> | ||
if re.match(description, text) is not None: | ||
desc = re.match(description, text)[1] | ||
|
||
# For GitHub Actions logging | ||
print("<----- MetaData ----->") | ||
print("Categories:", category_list) | ||
print("Title:", title) | ||
print("Path:", folder) | ||
print("Requirements:", requirements) | ||
print("Entry:", script) | ||
print("Arguments:",argument) | ||
print("Contributer:", user) | ||
print("Description:", desc) | ||
print("<----- ----- ----->") | ||
|
||
# The loop is for scripts that will be added to multiple categories. | ||
for cat in category_list: | ||
add_script(cat, title, folder, script, argument, requirements, user, desc, pa_token) | ||
|
||
|
||
# Start Checkpoint | ||
if __name__ == "__main__": | ||
# Get PR body and pass pa_token | ||
data = sys.argv[1] | ||
extract_from_pr_body(data, sys.argv[2]) |
30 changes: 30 additions & 0 deletions
.github/workflows/database_auto_updater.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
name: Python package | ||
|
||
on: | ||
pull_request: | ||
# Will trigger on closed PRs | ||
types: [closed] | ||
|
||
jobs: | ||
extract_and_update: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python 3.6 | ||
# Checks if PR is merged (subflag) | ||
if: ${{ github.event.pull_request.merged == true }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.6' | ||
|
||
- name: Run script | ||
# Checks if PR is merged (subflag) | ||
if: ${{ github.event.pull_request.merged == true }} | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install PyGithub | ||
python ./.github/scripts/Update_Database.py "$body" "$token" | ||
env: | ||
body: ${{ github.event.pull_request.body }} | ||
token: ${{ secrets.GITHUB_TOKEN }} |
2 changes: 1 addition & 1 deletion
Master Script/datastore.json
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.