Skip to content

Commit b4fb751

Browse files
authored
Merge pull request devicons#481 from devicons/TB_checkSvg
Added working workflows that has commenting on forked repo's PR.
2 parents e4237a4 + 4ab2703 commit b4fb751

18 files changed

+487
-185
lines changed

.github/scripts/build_assets/drafts/check_devicon_object.py renamed to .github/drafts/check_devicon_object.py

File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import traceback
2+
import sys
3+
4+
# pycharm complains that build_assets is an unresolved ref
5+
# don't worry about it, the script still runs
6+
from build_assets import filehandler, arg_getters
7+
from build_assets import util
8+
9+
10+
def main():
11+
"""
12+
Check the quality of the svgs of the whole icons folder.
13+
"""
14+
args = arg_getters.get_check_svgs_monthly_args()
15+
16+
try:
17+
devicon_json = filehandler.get_json_file_content(args.devicon_json_path)
18+
svgs = filehandler.get_svgs_paths(devicon_json, args.icons_folder_path)
19+
util.check_svgs(svgs)
20+
print("All SVGs found were good. Task completed.")
21+
except Exception as e:
22+
util.exit_with_err(e)
23+
24+
25+
if __name__ == "__main__":
26+
main()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Check SVGs Monthly
2+
on: workflow_dispatch
3+
# schedule:
4+
# - cron: '0 0 1 * *'
5+
jobs:
6+
check_develop:
7+
name: Check the SVGs' quality in the `develop` branch
8+
runs-on: ubuntu-18.04
9+
steps:
10+
11+
- uses: actions/checkout@v2
12+
with:
13+
ref: develop
14+
15+
- uses: actions/setup-python@v2
16+
with:
17+
python-version: 3.8
18+
19+
- name: Install dependencies
20+
run: python -m pip install --upgrade pip
21+
22+
- name: Run the check_svg script
23+
run: >
24+
python ./.github/scripts/check_svgs_monthly.py ./devicon.json ./icons
25+
26+
check_master:
27+
name: Check the SVGs' quality in the `master` branch
28+
runs-on: ubuntu-18.04
29+
steps:
30+
31+
- uses: actions/checkout@v2 # check out default branch, which is master
32+
33+
- uses: actions/setup-python@v2
34+
with:
35+
python-version: 3.8
36+
37+
- name: Install dependencies
38+
run: python -m pip install --upgrade pip
39+
40+
- name: Run the check_svg script
41+
run: >
42+
python ./.github/scripts/check_svgs_monthly.py ./icomoon.json ./devicon.json ./icons

.github/scripts/build_assets/drafts/peek_icons imgur.yml renamed to .github/drafts/peek_icons imgur.yml

File renamed without changes.

.github/scripts/build_assets/arg_getters.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,26 @@ def get_selenium_runner_args(peek_mode=False):
4040
return parser.parse_args()
4141

4242

43-
def get_check_svgs_args():
43+
def get_check_svgs_on_pr_args():
4444
"""
45-
Get the commandline arguments for the chec_svgs.py.
45+
Get the commandline arguments for the check_svgs_on_pr.py.
4646
"""
47-
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct.")
48-
parser.add_argument("icomoon_json_path",
49-
help="The path to the icomoon.json aka the selection.json created by Icomoon",
47+
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run whenever a PR is opened")
48+
parser.add_argument("files_added_json_path",
49+
help="The path to the files_added.json created by the gh-action-get-changed-files@2.1.4",
50+
action=PathResolverAction)
51+
52+
parser.add_argument("files_modified_json_path",
53+
help="The path to the files_modified.json created by the gh-action-get-changed-files@2.1.4",
5054
action=PathResolverAction)
55+
return parser.parse_args()
56+
5157

58+
def get_check_svgs_monthly_args():
59+
"""
60+
Get the commandline arguments for the check_svgs_monthly.py.
61+
"""
62+
parser = ArgumentParser(description="Check the SVGs to ensure their attributes are correct. Run monthly.")
5263
parser.add_argument("devicon_json_path",
5364
help="The path to the devicon.json",
5465
action=PathResolverAction)

.github/scripts/build_assets/filehandler.py

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,16 @@
66
import re
77

88

9-
def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]:
9+
def find_new_icons(devicon_json_path: str, icomoon_json_path: str):
1010
"""
1111
Find the newly added icons by finding the difference between
1212
the devicon.json and the icomoon.json.
1313
:param devicon_json_path, the path to the devicon.json.
1414
:param icomoon_json_path: a path to the iconmoon.json.
1515
:return: a list of the new icons as JSON objects.
1616
"""
17-
with open(devicon_json_path) as json_file:
18-
devicon_json = json.load(json_file)
19-
20-
with open(icomoon_json_path) as json_file:
21-
icomoon_json = json.load(json_file)
17+
devicon_json = get_json_file_content(devicon_json_path)
18+
icomoon_json = get_json_file_content(icomoon_json_path)
2219

2320
new_icons = []
2421
for icon in devicon_json:
@@ -28,7 +25,17 @@ def find_new_icons(devicon_json_path: str, icomoon_json_path: str) -> List[dict]
2825
return new_icons
2926

3027

31-
def is_not_in_icomoon_json(icon, icomoon_json) -> bool:
28+
def get_json_file_content(json_path: str):
29+
"""
30+
Get the json content of the json_path.
31+
:param: json_path, the path to the json file.
32+
:return: a dict representing the file content.
33+
"""
34+
with open(json_path) as json_file:
35+
return json.load(json_file)
36+
37+
38+
def is_not_in_icomoon_json(icon, icomoon_json):
3239
"""
3340
Checks whether the icon's name is not in the icomoon_json.
3441
:param icon: the icon object we are searching for.
@@ -45,7 +52,7 @@ def is_not_in_icomoon_json(icon, icomoon_json) -> bool:
4552

4653

4754
def get_svgs_paths(new_icons: List[dict], icons_folder_path: str,
48-
icon_versions_only: bool=False, as_str: bool=True) -> List[str] or List[Path]:
55+
icon_versions_only: bool=False, as_str: bool=True):
4956
"""
5057
Get all the suitable svgs file path listed in the devicon.json.
5158
:param new_icons, a list containing the info on the new icons.
@@ -199,3 +206,35 @@ def create_screenshot_folder(dir, screenshot_name: str="screenshots/"):
199206
print(f"{screenshot_folder} already exist. Script will do nothing.")
200207
finally:
201208
return str(screenshot_folder)
209+
210+
def get_added_modified_svgs(files_added_json_path: str,
211+
files_modified_json_path: str):
212+
"""
213+
Get the svgs added and modified from the files_changed_json_path.
214+
:param: files_added_json_path, the path to the files_added.json created by the gh-action-get-changed-files@2.1.4
215+
:param: files_modified_json_path, the path to the files_modified.json created by the gh-action-get-changed-files@2.1.4
216+
:return: a list of the svg file paths that were added/modified in this pr as Path.
217+
"""
218+
files_added = get_json_file_content(files_added_json_path)
219+
files_modified = get_json_file_content(files_modified_json_path)
220+
221+
svgs = []
222+
for file in files_added:
223+
path = Path(file)
224+
if path.suffix.lower() == ".svg":
225+
svgs.append(path)
226+
227+
for file in files_modified:
228+
path = Path(file)
229+
if path.suffix.lower() == ".svg":
230+
svgs.append(path)
231+
232+
return svgs
233+
234+
235+
def write_to_file(path: str, value: any):
236+
"""
237+
Write the value to a JSON file.
238+
"""
239+
with open(path, "w") as file:
240+
file.write(value)

.github/scripts/build_assets/github_env.py

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
from typing import List
2-
import sys
32
import xml.etree.ElementTree as et
4-
import time
53
from pathlib import Path
4+
import os
5+
import json
6+
import platform
7+
import sys
8+
import traceback
69

710

8-
# pycharm complains that build_assets is an unresolved ref
9-
# don't worry about it, the script still runs
10-
from build_assets import filehandler, arg_getters
11-
from build_assets import github_env
12-
13-
14-
def main():
11+
def exit_with_err(err: Exception):
1512
"""
16-
Check the quality of the svgs.
17-
If any error is found, set an environmental variable called ERR_MSGS
18-
that will contains the error messages.
13+
Exit the current step and display the err.
14+
:param: err, the error/exception encountered.
1915
"""
20-
args = arg_getters.get_check_svgs_args()
21-
new_icons = filehandler.find_new_icons(args.devicon_json_path, args.icomoon_json_path)
22-
23-
if len(new_icons) == 0:
24-
sys.exit("No files need to be uploaded. Ending script...")
25-
26-
# print list of new icons
27-
print("SVGs being checked:", *new_icons, sep = "\n", end='\n\n')
28-
29-
time.sleep(1) # do this so the logs stay clean
30-
try:
31-
# check the svgs
32-
svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path, as_str=False)
33-
check_svgs(svgs)
34-
print("All SVGs found were good.\nTask completed.")
35-
except Exception as e:
36-
github_env.set_env_var("ERR_MSGS", str(e))
37-
sys.exit(str(e))
16+
traceback.print_exc()
17+
sys.exit(1)
3818

3919

4020
def check_svgs(svg_file_paths: List[Path]):
@@ -45,6 +25,8 @@ def check_svgs(svg_file_paths: List[Path]):
4525
The style must not contain any 'fill' declarations.
4626
If any error is found, they will be thrown.
4727
:param: svg_file_paths, the file paths to the svg to check for.
28+
:return: None if there no errors. If there is, return a JSON.stringified
29+
list with the error messages in it.
4830
"""
4931
# batch err messages together so user can fix everything at once
5032
err_msgs = []
@@ -84,8 +66,36 @@ def check_svgs(svg_file_paths: List[Path]):
8466
err_msgs.append("\n".join(err_msg))
8567

8668
if len(err_msgs) > 0:
87-
raise Exception("Errors found in these files:\n" + "\n\n".join(err_msgs))
69+
return "\n\n".join(err_msgs)
70+
return 'None'
8871

8972

90-
if __name__ == "__main__":
91-
main()
73+
def set_env_var(key: str, value: str, delimiter: str='~'):
74+
"""
75+
Set the GitHub env variable of 'key' to 'value' using
76+
the method specified here:
77+
https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable
78+
Support both Windows and Ubuntu machines provided by GitHub Actions.
79+
80+
:param: key, the name of the env variable.
81+
:param: value, the value of the env variable.
82+
:param: delimiter, the delimiter that you want to use
83+
to write to the file. Only applicable if the 'value' contains
84+
'\n' character aka a multiline string.
85+
"""
86+
if platform.system() == "Windows":
87+
if "\n" in value:
88+
os.system(f'echo "{key}<<{delimiter}" >> %GITHUB_ENV%')
89+
os.system(f'echo "{value}" >> %GITHUB_ENV%')
90+
os.system(f'echo "{delimiter}" >> %GITHUB_ENV%')
91+
else:
92+
os.system(f'echo "{key}={value}" >> %GITHUB_ENV%')
93+
elif platform.system() == "Linux":
94+
if "\n" in value:
95+
os.system(f'echo "{key}<<{delimiter}" >> $GITHUB_ENV')
96+
os.system(f'echo "{value}" >> $GITHUB_ENV')
97+
os.system(f'echo "{delimiter}" >> $GITHUB_ENV')
98+
else:
99+
os.system(f'echo "{key}={value}" >> $GITHUB_ENV')
100+
else:
101+
raise Exception("This function doesn't support this platform: " + platform.system())

.github/scripts/check_all_icons.py

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import sys
2+
import time
3+
4+
5+
# pycharm complains that build_assets is an unresolved ref
6+
# don't worry about it, the script still runs
7+
from build_assets import filehandler, arg_getters
8+
from build_assets import util
9+
10+
11+
def main():
12+
"""
13+
Check the quality of the svgs.
14+
If any svg error is found, create a json file called 'svg_err_messages.json'
15+
in the root folder that will contains the error messages.
16+
"""
17+
args = arg_getters.get_check_svgs_on_pr_args()
18+
try:
19+
# check the svgs
20+
svgs = filehandler.get_added_modified_svgs(args.files_added_json_path,
21+
args.files_modified_json_path)
22+
print("SVGs to check: ", *svgs, sep='\n')
23+
24+
if len(svgs) == 0:
25+
print("No SVGs to check, ending script.")
26+
return
27+
28+
err_messages = util.check_svgs(svgs)
29+
filehandler.write_to_file("./svg_err_messages.txt", err_messages)
30+
print("Task completed.")
31+
except Exception as e:
32+
util.exit_with_err(e)
33+
34+
35+
if __name__ == "__main__":
36+
main()

0 commit comments

Comments
 (0)