From b582544c059b7532d66c35ff162e6adf9685e23f Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 4 Mar 2023 01:07:00 -0300 Subject: [PATCH] Add newliner --- .github/scripts/newliner.py | 33 +++++++++++++++++++ .github/workflows/optimize_icons.yml | 21 ------------ .github/workflows/optimizer.yml | 48 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 .github/scripts/newliner.py delete mode 100644 .github/workflows/optimize_icons.yml create mode 100644 .github/workflows/optimizer.yml diff --git a/.github/scripts/newliner.py b/.github/scripts/newliner.py new file mode 100644 index 000000000..73442663d --- /dev/null +++ b/.github/scripts/newliner.py @@ -0,0 +1,33 @@ +import json, re +from os import system +from sys import argv + +PR_PATHS = 'pr-files.json' + +# Search for added and modified files without a newline at the end. +file_paths = json.load(open(f'./{PR_PATHS}')) +mod_files = [] +for file_path in file_paths: + file = open(file_path).readlines() + if len(file) > 0 and not re.match(r'.*\n', file[-1]): + file.append('\n') + open(file_path, 'w').write(''.join(file)) + mod_files.append(file_path) + +# Remove PR_PATHS +system(f'rm -rf {PR_PATHS}') + +# Create the commit message. +if len(mod_files) > 0: + mod_files = '\\n'.join([ + 'Optimized icons' if argv[1] == 'on' else 'Add newline at the end of files', + *map(lambda e: f'- {e}', mod_files) + ]) + + [ + system(e) for e in [ + 'echo "commit_msg<> $GITHUB_ENV', + f'echo "{mod_files}" >> $GITHUB_ENV', + 'echo "EOF" >> $GITHUB_ENV' + ] + ] diff --git a/.github/workflows/optimize_icons.yml b/.github/workflows/optimize_icons.yml deleted file mode 100644 index 1c2b12044..000000000 --- a/.github/workflows/optimize_icons.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Optimize icons -on: - - pull_request -permissions: write-all - -jobs: - optimize: - runs-on: ubuntu-latest - name: Optimize icons with SVGO - steps: - - uses: actions/checkout@v3 - - - uses: ericcornelissen/svgo-action@v3 - with: - repo-token: ${{ secrets.GITHUB_TOKEN }} - svgo-config: ./.github/scripts/svgo.config.js - svgo-version: 3 - - - uses: stefanzweifel/git-auto-commit-action@v4 - with: - commit_message: Icons optimized by SVGO diff --git a/.github/workflows/optimizer.yml b/.github/workflows/optimizer.yml new file mode 100644 index 000000000..6066b8086 --- /dev/null +++ b/.github/workflows/optimizer.yml @@ -0,0 +1,48 @@ +name: Optimize icons +on: + pull_request: + types: + - labeled + - synchronize +permissions: write-all +env: + SVGO: off +jobs: + optimize: + name: Optimize icons with SVGO + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: SVGO icon optimizer + if: github.event.label.name == 'feature:icon' + uses: ericcornelissen/svgo-action@v3 + with: + repo-token: ${{ secrets.GITHUB_TOKEN }} + svgo-config: ./.github/scripts/svgo.config.js + svgo-version: 3 + + - name: Check if there are some icon to be optimized + if: github.event.label.name == 'feature:icon' + run: echo "SVGO=on" >> $GITHUB_ENV + + - name: Get paths of added and modified files + uses: jitterbit/get-changed-files@v1 + id: pr-files + with: + format: json + + - name: Save paths added and modified files + run: echo '${{ steps.pr-files.outputs.added_modified }}' > pr-files.json + + - name: Setup Python v3.10 + uses: actions/setup-python@v4 + with: + python-version: '3.10' + + - name: Add newline + run: python ./.github/scripts/newliner.py ${{ env.SVGO }} + + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: ${{ env.commit_msg }}