diff --git a/.github/scripts/build_assets/util.py b/.github/scripts/build_assets/arg_getters.py similarity index 79% rename from .github/scripts/build_assets/util.py rename to .github/scripts/build_assets/arg_getters.py index 075d80fa..b48e53e5 100644 --- a/.github/scripts/build_assets/util.py +++ b/.github/scripts/build_assets/arg_getters.py @@ -1,7 +1,7 @@ from argparse import ArgumentParser from build_assets.PathResolverAction import PathResolverAction -def get_commandline_args(peek_mode=False): +def get_selenium_runner_args(peek_mode=False): parser = ArgumentParser(description="Upload svgs to Icomoon to create icon files.") parser.add_argument("--headless", @@ -33,3 +33,12 @@ def get_commandline_args(peek_mode=False): help="The title of the PR that we are peeking at") return parser.parse_args() + + +def get_generate_markdown_args(): + parser = ArgumentParser(description="Generate markdown for the image urls passed in.") + + parser.add_argument("img_urls", + help="The urls of the images. Must be the string/JSON form of an array. Ex: '[1,2,3]'") + + return parser.parse_args() \ No newline at end of file diff --git a/.github/scripts/build_assets/filehandler.py b/.github/scripts/build_assets/filehandler.py index 8e51d9cf..65a1234c 100644 --- a/.github/scripts/build_assets/filehandler.py +++ b/.github/scripts/build_assets/filehandler.py @@ -147,7 +147,7 @@ def rename_extracted_files(extract_path: str): print("Files renamed") -def create_screenshot_folder(dir, screenshot_name: str="screenshots"): +def create_screenshot_folder(dir, screenshot_name: str="screenshots/"): """ Create a screenshots folder in the dir. :param dir, the dir where we want to create the folder. @@ -158,5 +158,11 @@ def create_screenshot_folder(dir, screenshot_name: str="screenshots"): folder = Path(dir).resolve() if not folder.is_dir(): raise Exception(f"This is not a dir: {str(folder)}. \ndir must be a valid directory") - folder.mkdir(screenshot_name, exist_ok=True) - return str(folder) + + screenshot_folder = Path(folder, screenshot_name) + try: + os.mkdir(screenshot_folder) + except FileExistsError: + print(f"{screenshot_folder} already exist. Script will do nothing.") + finally: + return str(screenshot_folder) diff --git a/.github/scripts/generate_screenshot_markdown.py b/.github/scripts/generate_screenshot_markdown.py index 2078a49d..2a4f0872 100644 --- a/.github/scripts/generate_screenshot_markdown.py +++ b/.github/scripts/generate_screenshot_markdown.py @@ -1,5 +1,7 @@ from typing import List +from build_assets import arg_getters + def generate_screenshot_markdown(img_urls: List[str]): """ @@ -12,5 +14,6 @@ def generate_screenshot_markdown(img_urls: List[str]): if __name__ == "__main__": - markdown = generate_screenshot_markdown() + args = arg_getters.get_generate_markdown_args() + markdown = generate_screenshot_markdown(args.img_urls) print("\n\n".join(markdown)) # format it before printing diff --git a/.github/scripts/icomoon_build.py b/.github/scripts/icomoon_build.py index 6b3eb352..da8f0236 100644 --- a/.github/scripts/icomoon_build.py +++ b/.github/scripts/icomoon_build.py @@ -4,11 +4,11 @@ from selenium.common.exceptions import TimeoutException # pycharm complains that build_assets is an unresolved ref # don't worry about it, the script still runs from build_assets.SeleniumRunner import SeleniumRunner -from build_assets import filehandler, util +from build_assets import filehandler, arg_getters def main(): - args = util.get_commandline_args() + args = arg_getters.get_selenium_runner_args() new_icons = filehandler.find_new_icons(args.devicon_json_path, args.icomoon_json_path) if len(new_icons) == 0: print("No files need to be uploaded. Ending script...") diff --git a/.github/scripts/icomoon_peek.py b/.github/scripts/icomoon_peek.py index df6bbba3..925c3b34 100644 --- a/.github/scripts/icomoon_peek.py +++ b/.github/scripts/icomoon_peek.py @@ -1,4 +1,3 @@ -from os import sep from typing import List import re from selenium.common.exceptions import TimeoutException @@ -6,15 +5,12 @@ from selenium.common.exceptions import TimeoutException # pycharm complains that build_assets is an unresolved ref # don't worry about it, the script still runs from build_assets.SeleniumRunner import SeleniumRunner -from build_assets import filehandler, util +from build_assets import filehandler, arg_getters def main(): - args = util.get_commandline_args(True) + args = arg_getters.get_selenium_runner_args(True) new_icons = filehandler.find_new_icons(args.devicon_json_path, args.icomoon_json_path) - if len(new_icons) == 0: - print("No files need to be uploaded. Ending script...") - return # get only the icon object that has the name matching the pr title filtered_icons = find_object_added_in_this_pr(new_icons, args.pr_title) @@ -23,11 +19,19 @@ def main(): print("List of new icons:", *new_icons, sep = "\n") print("Icons being uploaded:", *filtered_icons, sep = "\n") + if len(new_icons) == 0: + print("No files need to be uploaded. Ending script...") + return + + screenshot_folder = filehandler.create_screenshot_folder("./") + if len(filtered_icons) == 0: + print("No icons found matching the icon name in the PR's title. Fallback to uploading all new icons found.") + screenshot_folder = "" + runner = None try: runner = SeleniumRunner(args.download_path, args.geckodriver_path, args.headless) svgs = filehandler.get_svgs_paths(new_icons, args.icons_folder_path) - screenshot_folder = filehandler.create_screenshot_folder("./") runner.upload_svgs(svgs, screenshot_folder) print("Task completed.") except TimeoutException as e: diff --git a/.github/workflows/build_icons.yml b/.github/workflows/build_icons.yml index 3492f2cd..5fa09ef4 100644 --- a/.github/workflows/build_icons.yml +++ b/.github/workflows/build_icons.yml @@ -25,22 +25,22 @@ jobs: ./devicon.json ./icons ./ --headless - name: Upload geckodriver.log for debugging purposes uses: actions/upload-artifact@v2 - if: ${{failure()}} + if: failure() with: name: geckodriver-log path: ./geckodriver.log - name: Build devicon.min.css - if: ${{ success() }} + if: success() run: npm run build-css - name: Upload screenshot of the newly made icons id: imgur_step - uses: devicons/public-upload-to-imgur@v1 - if: ${{success()}} + uses: devicons/public-upload-to-imgur@v1.1.2 + if: success() with: - img_path: ./new_icons.png + path: ./new_icons.png client_id: ${{secrets.IMGUR_CLIENT_ID}} - name: Create Pull Request - if: ${{ success() }} + if: success() uses: peter-evans/create-pull-request@v3 env: MESSAGE: | diff --git a/.github/workflows/peek_icons.yml b/.github/workflows/peek_icons.yml index 882c36a6..c260d6b0 100644 --- a/.github/workflows/peek_icons.yml +++ b/.github/workflows/peek_icons.yml @@ -23,35 +23,39 @@ jobs: - name: Run icomoon_peek.py env: PR_TITLE: ${{ github.event.pull_request.title }} + shell: cmd run: > python ./.github/scripts/icomoon_peek.py ./.github/scripts/build_assets/geckodriver-v0.27.0-win64/geckodriver.exe ./icomoon.json - ./devicon.json ./icons ./ --headless --pr_title $PR_TITLE + ./devicon.json ./icons ./ --headless --pr_title "%PR_TITLE%" - name: Upload geckodriver.log for debugging purposes uses: actions/upload-artifact@v2 - if: ${{failure()}} + if: failure() with: name: geckodriver-log path: ./geckodriver.log - name: Upload screenshot of the newly made icons id: new_icons_overview_step - uses: devicons/public-upload-to-imgur@v1.1 - if: ${{success()}} + uses: devicons/public-upload-to-imgur@main + if: success() with: - img_path: ./screenshots/new_icons.png + path: ./screenshots/new_icons.png client_id: ${{secrets.IMGUR_CLIENT_ID}} - name: Upload zoomed in screenshot of the newly made icons id: new_icons_detailed_step - uses: devicons/public-upload-to-imgur@v1.1 - if: ${{success()}} + uses: devicons/public-upload-to-imgur@main + if: success() with: - img_path: ./screenshots/screenshot_*.png + path: ./screenshots/screenshot_*.png client_id: ${{secrets.IMGUR_CLIENT_ID}} - - name: Generate the markdowns for the screenshot - run: | + - name: Generate the markdowns for the screenshot and put it in the DETAILED_IMGS_MARKDOWN env var + env: + IMG_URLS: ${{ steps.new_icons_detailed_step.outputs.img_url }} + run: | echo "DETAILED_IMGS_MARKDOWN<> $GITHUB_ENV - python ./.github/scripts/generate_screenshot_markdown.py >> $GITHUB_ENV + python ./.github/scripts/generate_screenshot_markdown.py "%IMG_URLS%" >> $GITHUB_ENV echo "EOF" >> $GITHUB_ENV + shell: cmd - name: Comment on the PR about the result uses: github-actions-up-and-running/pr-comment@v1.0.1 env: diff --git a/package.json b/package.json index 01b2b98f..651a8a7a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,10 @@ "description": "Programming related icons collection", "main": "devicon.min.css", "scripts": { - "build-css": "gulp updateCss && gulp clean" + "build-css": "gulp updateCss && gulp clean", + "peek-test": "python ./.github/scripts/icomoon_peek.py ./.github/scripts/build_assets/geckodriver-v0.27.0-win64/geckodriver.exe ./icomoon.json ./devicon.json ./icons ./ --pr_title \"%PR_TITLE%\"", + "build-test": "python ./.github/scripts/icomoon_build.py ./.github/scripts/build_assets/geckodriver-v0.27.0-win64/geckodriver.exe ./icomoon.json ./devicon.json ./icons ./" + }, "repository": { "type": "git",