Skip to content

Commit

Permalink
Added svg check status code
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas-Boi committed Feb 22, 2021
1 parent f246192 commit 2249c46
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 71 deletions.
57 changes: 0 additions & 57 deletions .github/scripts/build_assets/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
from typing import List
import xml.etree.ElementTree as et
from pathlib import Path
import os
import json
import platform
import sys
import traceback
Expand All @@ -17,59 +13,6 @@ def exit_with_err(err: Exception):
sys.exit(1)


def check_svgs(svg_file_paths: List[Path]):
"""
Check the width, height, viewBox and style of each svgs passed in.
The viewBox must be '0 0 128 128'.
If the svg has a width and height attr, ensure it's '128px'.
The style must not contain any 'fill' declarations.
If any error is found, they will be thrown.
:param: svg_file_paths, the file paths to the svg to check for.
:return: None if there no errors. If there is, return a JSON.stringified
list with the error messages in it.
"""
# batch err messages together so user can fix everything at once
err_msgs = []
for svg_path in svg_file_paths:
tree = et.parse(svg_path)
root = tree.getroot()
namespace = "{http://www.w3.org/2000/svg}"
err_msg = [f"{svg_path}:"]

if root.tag != f"{namespace}svg":
err_msg.append(f"-root is '{root.tag}'. Root must be an 'svg' element")

if root.get("viewBox") != "0 0 128 128":
err_msg.append("-'viewBox' is not '0 0 128 128' -> Set it or scale the file using https://www.iloveimg.com/resize-image/resize-svg")

acceptable_size = [None, "128px", "128"]
if root.get("height") not in acceptable_size:
err_msg.append("-'height' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")

if root.get("width") not in acceptable_size:
err_msg.append("-'width' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")

if root.get("style") is not None and "enable-background" in root.get("style"):
err_msg.append("-deprecated 'enable-background' in style attribute -> Remove it")

if root.get("x") is not None:
err_msg.append("-unneccessary 'x' attribute in svg element -> Remove it")

if root.get("y") is not None:
err_msg.append("-unneccessary 'y' attribute in svg element -> Remove it")

style = root.findtext(f".//{namespace}style")
if style != None and "fill" in style:
err_msg.append("-contains style declaration using 'fill' -> Replace classes with the 'fill' attribute instead")

if len(err_msg) > 1:
err_msgs.append("\n".join(err_msg))

if len(err_msgs) > 0:
return "\n\n".join(err_msgs)
return 'None'


def set_env_var(key: str, value: str, delimiter: str='~'):
"""
Set the GitHub env variable of 'key' to 'value' using
Expand Down
74 changes: 69 additions & 5 deletions .github/scripts/check_svgs_on_pr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sys
import time
from enum import Enum
from typing import List
import xml.etree.ElementTree as et
from pathlib import Path


# pycharm complains that build_assets is an unresolved ref
Expand All @@ -8,6 +10,14 @@
from build_assets import util


class SVG_STATUS_CODE(Enum):
"""
The status codes to check for in post_check_svgs_comment.yml
"""
NO_SVG = 0 # action: do nothing
SVG_OK = 1 # action: let user know their svgs are fine


def main():
"""
Check the quality of the svgs.
Expand All @@ -23,14 +33,68 @@ def main():

if len(svgs) == 0:
print("No SVGs to check, ending script.")
return
err_messages = SVG_STATUS_CODE.NO_SVG.value
else:
err_messages = check_svgs(svgs)

err_messages = util.check_svgs(svgs)
filehandler.write_to_file("./svg_err_messages.txt", err_messages)
filehandler.write_to_file("./svg_err_messages.txt", str(err_messages))
print("Task completed.")
except Exception as e:
util.exit_with_err(e)


def check_svgs(svg_file_paths: List[Path]):
"""
Check the width, height, viewBox and style of each svgs passed in.
The viewBox must be '0 0 128 128'.
If the svg has a width and height attr, ensure it's '128px'.
The style must not contain any 'fill' declarations.
If any error is found, they will be thrown.
:param: svg_file_paths, the file paths to the svg to check for.
:return: None if there no errors. If there is, return a JSON.stringified
list with the error messages in it.
"""
# batch err messages together so user can fix everything at once
err_msgs = []
for svg_path in svg_file_paths:
tree = et.parse(svg_path)
root = tree.getroot()
namespace = "{http://www.w3.org/2000/svg}"
err_msg = [f"{svg_path}:"]

if root.tag != f"{namespace}svg":
err_msg.append(f"-root is '{root.tag}'. Root must be an 'svg' element")

if root.get("viewBox") != "0 0 128 128":
err_msg.append("-'viewBox' is not '0 0 128 128' -> Set it or scale the file using https://www.iloveimg.com/resize-image/resize-svg")

acceptable_size = [None, "128px", "128"]
if root.get("height") not in acceptable_size:
err_msg.append("-'height' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")

if root.get("width") not in acceptable_size:
err_msg.append("-'width' is present in svg element but is not '128' or '128px' -> Remove it or set it to '128' or '128px'")

if root.get("style") is not None and "enable-background" in root.get("style"):
err_msg.append("-deprecated 'enable-background' in style attribute -> Remove it")

if root.get("x") is not None:
err_msg.append("-unneccessary 'x' attribute in svg element -> Remove it")

if root.get("y") is not None:
err_msg.append("-unneccessary 'y' attribute in svg element -> Remove it")

style = root.findtext(f".//{namespace}style")
if style != None and "fill" in style:
err_msg.append("-contains style declaration using 'fill' -> Replace classes with the 'fill' attribute instead")

if len(err_msg) > 1:
err_msgs.append("\n".join(err_msg))

if len(err_msgs) > 0:
return "\n\n".join(err_msgs)
return SVG_STATUS_CODE.SVG_OK.value


if __name__ == "__main__":
main()
17 changes: 8 additions & 9 deletions .github/workflows/post_check_svgs_comment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ on:
types:
- completed
jobs:
post_screenshots_in_comment:
name: Post the screenshot
post_result_of_svg_check:
name: Post the result of the Check SVG Action
runs-on: ubuntu-18.04
steps:
- name: Check if the trigger run worked. If it failed, fail the current run.
if: success() && github.event.workflow_run.conclusion != 'success'
if: github.event.workflow_run.conclusion != 'success'
uses: cutenode/action-always-fail@v1.0.1

- name: Download workflow artifact
uses: dawidd6/action-download-artifact@v2.11.0
if: success()
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow: peek_icons.yml
Expand All @@ -36,13 +37,11 @@ jobs:

- name: Comment on the PR about the result - Success
uses: jungwinter/comment@v1 # let us comment on a specific PR
if: success() && steps.err_message_reader.outputs.content == 'None'
if: success() && steps.err_message_reader.outputs.content == '1'
env:
MESSAGE: |
Hi!
I'm Devicons' SVG-Checker Bot and I just checked all the SVGs in this branch.
Everything looks great. Good job!
I'm Devicons' SVG-Checker Bot and everything looks great. Good job!
Have a nice day,
SVG-Checker Bot :grin:
Expand All @@ -54,7 +53,7 @@ jobs:

- name: Comment on the PR about the result - SVG Error
uses: jungwinter/comment@v1 # let us comment on a specific PR
if: success() && steps.err_message_reader.outputs.content != 'None'
if: success() && (steps.err_message_reader.outputs.content != '0' || steps.err_message_reader.outputs.content != '1')
env:
MESSAGE: |
Hi!
Expand Down Expand Up @@ -82,7 +81,7 @@ jobs:

- name: Comment on the PR about the result - Failure
uses: jungwinter/comment@v1 # let us comment on a specific PR
if: failure() || cancelled()
if: failure()
env:
MESSAGE: |
Hi!
Expand Down

0 comments on commit 2249c46

Please sign in to comment.