Skip to content

Commit fe77510

Browse files
Merge pull request avinashkranjan#1025 from XZANATOL/action_db_update
Action db update
2 parents df8bb61 + f6498f0 commit fe77510

File tree

5 files changed

+193
-9
lines changed

5 files changed

+193
-9
lines changed

.github/pull_request_template.md

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,7 @@ Please include a summary of the change and which issue is fixed. List any depend
1010

1111
## Fixes #(issue_no)
1212

13-
Replace `issue_no` with the issue number which is fixed in this PR
14-
15-
## Have you read the [Contributing Guidelines on Pull Requests](https://github.com/avinashkranjan/Amazing-Python-Scripts/blob/master/CONTRIBUTING.md)?
16-
17-
- [ ] Yes
18-
- [ ] No
13+
<!-- Replace `issue_no` with the issue number which is fixed in this PR -->
1914

2015
## Type of change
2116

@@ -34,5 +29,42 @@ _Please delete options that are not relevant._
3429
- [ ] I have commented my code, particularly in hard-to-understand areas
3530
- [ ] I have created a helpful and easy to understand `README.md`
3631
- [ ] My documentation follows [`Template for README.md`](https://github.com/avinashkranjan/Amazing-Python-Scripts/blob/master/Template%20for%20README.md)
37-
- [ ] My changes generate no new warnings
38-
- [ ] I have added tests/screenshots(if any) that prove my fix is effective or that my feature works.
32+
- [ ] I have added the project meta data in the PR template.
33+
- [ ] I have created the ``requirements.txt`` file if needed.
34+
35+
## Project Metadata
36+
37+
`` If there is no-file/nothing to fill the below fields with, then type: none ``
38+
39+
`` Example: `` If no requirements.txt needed/present, then type none in ``Requirments``.
40+
41+
Category:
42+
- [ ] Calculators
43+
- [ ] AI/ML
44+
- [ ] Scrappers
45+
- [ ] Social_Media
46+
- [ ] PDF
47+
- [ ] Image_Processing
48+
- [ ] Video_Processing
49+
- [ ] Games
50+
- [ ] Networking
51+
- [ ] OS_Utilities
52+
- [ ] Automation
53+
- [ ] Cryptography
54+
- [ ] Computer_Vision
55+
- [ ] Fun
56+
- [ ] Others
57+
58+
Title: \<write script title here\>
59+
60+
Folder: \<type the folder name that contains your script\>
61+
62+
Requirments: \<type the name of text file containing the required to install python packages, type None if no file required\>
63+
64+
Script: \<Enter the name of the ``.py`` file (The main entry point of the program)\>
65+
66+
Arguments: \<enter any arguments that the script needs but `-` separeted like: h-c-m\>
67+
68+
Contributor: \<Enter your Github handle/username without url\>
69+
70+
Description: \<Enter a one line description that describes your script. Also, explain the arguments usage here\>

.github/scripts/Update_Database.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
from github import Github
2+
import json
3+
import sys
4+
import re
5+
6+
# Regex Patterns
7+
category = r"- \[x\] (.+)"
8+
name = r"Title: (.+)"
9+
path = r"Folder: (.+)"
10+
requirments_path = r"Requirments: (.+)"
11+
entry = r"Script: (.+)"
12+
arguments = r"Arguments: (.+)"
13+
contributor = r"Contributor: (.+)"
14+
description = r"Description: (.+)"
15+
16+
17+
def add_script(category, name, path, entry, arguments, requirments_path, contributor, description, pa_token):
18+
""" Add a Contributor script to database """
19+
new_data = {category: {name: [path, entry, arguments, requirments_path, contributor, description]}}
20+
data_store = read_data()
21+
22+
try:
23+
# If category doesn't exist try will fail and except will ask to add a new category with the project
24+
if data_store[category]: # Check for existing category or a new one
25+
data_store[category].update(new_data[category]) # Add script
26+
except:
27+
data_store.update(new_data) # Add new category
28+
29+
# <----- This part is to avoid a single/double quotes error when trying to update the database with PyGithub ----->
30+
with open("./Master Script/datastore.json", "w") as file:
31+
json.dump(data_store, file)
32+
print("Script added to database, pushing changes to repo...")
33+
34+
with open("./Master Script/datastore.json", "r") as file:
35+
data_store = file.readlines()[0]
36+
37+
# <----- Github Login & Database Update ----->
38+
git = Github(pa_token)
39+
user_object = git.get_user()
40+
print("[+] PyGithub Login Success!")
41+
42+
repo = git.get_repo("avinashkranjan/Amazing-Python-Scripts")
43+
datastore_fileMaster = repo.get_contents("./Master Script/datastore.json", ref="master")
44+
datastore_fileWebsite = repo.get_contents("./datastore.json", ref="gh-pages")
45+
46+
repo.update_file(datastore_fileMaster.path, "Updated datastore.json", data_store, datastore_fileMaster.sha, branch="master")
47+
repo.update_file("./datastore.json", "Updated datastore.json", data_store, datastore_fileWebsite.sha, branch="gh-pages")
48+
print("[+] Database Updated")
49+
50+
51+
def read_data():
52+
""" Loads datastore.json """
53+
with open("./Master Script/datastore.json", "r") as file:
54+
data = json.load(file)
55+
return data
56+
57+
58+
def extract_from_pr_body(pr_body, pa_token):
59+
""" Manipulates the provided PR body and extracts the required information """
60+
pr_body = pr_body.split("\n")
61+
for element in pr_body:
62+
pr_body[pr_body.index(element)] = element.rstrip("\r")
63+
64+
# A special case for contributors in gh-pages branch and other dependency PRs
65+
try:
66+
pr_body = pr_body[pr_body.index("## Project Metadata"):]
67+
except:
68+
sys.exit()
69+
70+
category_list = []
71+
for text in pr_body:
72+
# <----- Validate Category ----->
73+
cat = re.match(category, text)
74+
if cat is not None:
75+
category_list.append(cat[1])
76+
# <----- Validate Title ----->
77+
if re.match(name, text) is not None:
78+
title = re.match(name, text)[1]
79+
# <----- Validate Folder ----->
80+
if re.match(path, text) is not None:
81+
folder = re.match(path, text)[1]
82+
# <----- Validate requirments.txt ----->
83+
if re.match(requirments_path, text) is not None:
84+
requirements = re.match(requirments_path, text)[1]
85+
# <----- Validate Script.py ----->
86+
if re.match(entry, text) is not None:
87+
script = re.match(entry, text)[1]
88+
# <----- Validate Arguments ----->
89+
if re.match(arguments, text) is not None:
90+
argument = re.match(arguments, text)[1]
91+
# <----- Validate Contribute ----->
92+
if re.match(contributor, text) is not None:
93+
user = re.match(contributor, text)[1]
94+
# <----- Validate Description ----->
95+
if re.match(description, text) is not None:
96+
desc = re.match(description, text)[1]
97+
98+
# For GitHub Actions logging
99+
print("<----- MetaData ----->")
100+
print("Categories:", category_list)
101+
print("Title:", title)
102+
print("Path:", folder)
103+
print("Requirements:", requirements)
104+
print("Entry:", script)
105+
print("Arguments:",argument)
106+
print("Contributer:", user)
107+
print("Description:", desc)
108+
print("<----- ----- ----->")
109+
110+
# The loop is for scripts that will be added to multiple categories.
111+
for cat in category_list:
112+
add_script(cat, title, folder, script, argument, requirements, user, desc, pa_token)
113+
114+
115+
# Start Checkpoint
116+
if __name__ == "__main__":
117+
# Get PR body and pass pa_token
118+
data = sys.argv[1]
119+
extract_from_pr_body(data, sys.argv[2])
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Python package
2+
3+
on:
4+
pull_request:
5+
# Will trigger on closed PRs
6+
types: [closed]
7+
8+
jobs:
9+
extract_and_update:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
14+
- name: Set up Python 3.6
15+
# Checks if PR is merged (subflag)
16+
if: ${{ github.event.pull_request.merged == true }}
17+
uses: actions/setup-python@v2
18+
with:
19+
python-version: '3.6'
20+
21+
- name: Run script
22+
# Checks if PR is merged (subflag)
23+
if: ${{ github.event.pull_request.merged == true }}
24+
run: |
25+
python -m pip install --upgrade pip
26+
pip install PyGithub
27+
python ./.github/scripts/Update_Database.py "$body" "$token"
28+
env:
29+
body: ${{ github.event.pull_request.body }}
30+
token: ${{ secrets.GITHUB_TOKEN }}

Master Script/datastore.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

Master Script/Script.py renamed to Master Script/manual db updater.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
parser = OptionParser()
1515
parser.add_option("-a", "--add", action="store_true", dest="add", help="Goes straight to the add script phase")
1616

17+
18+
# The database is automatically updated after the PR is merged.
19+
# ONLY Use this function if you were asked to, to manually add projects to the database.
1720
def add_script():
1821
""" Add a Contributor script through a series of inputs """
1922
print("Double check inputs before pressing enter. If one input is incorrect press CTRL-C and re-run the script")

0 commit comments

Comments
 (0)