Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/scripts/newliner.py
Original file line number Diff line number Diff line change
@@ -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<<EOF" >> $GITHUB_ENV',
f'echo "{mod_files}" >> $GITHUB_ENV',
'echo "EOF" >> $GITHUB_ENV'
]
]
21 changes: 0 additions & 21 deletions .github/workflows/optimize_icons.yml

This file was deleted.

48 changes: 48 additions & 0 deletions .github/workflows/optimizer.yml
Original file line number Diff line number Diff line change
@@ -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 }}
Comment on lines +38 to +44
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why we can't use the newline-action?

Suggested change
- 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 }}
- name: Add newline
uses: Logerfo/newline-action@0.0.4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

This would add newline to any file, not just SVG files.
It's also possible to ignore files in a config (see link above)

Come to think of it, maybe it would be better to add the newline in a separate script like this:

name: Add newline
on:
  pull_request:
    types: [synchronize, opened]
jobs:
  build:
    name: newline-action
    runs-on: ubuntu-16.04
    steps:
      - uses: actions/checkout@master
      - uses: Logerfo/newline-action@0.0.4
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }} # The `GITHUB_TOKEN` secret.
          config-path: .github/newline.yml # The path of the addtional configurations file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds good to me. 👍


- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: ${{ env.commit_msg }}