Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ improve icon review workflow #2689

Closed
wants to merge 9 commits into from
141 changes: 81 additions & 60 deletions .github/workflows/icon-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,107 +3,128 @@ name: 🏞️ Icon Review
on:
pull_request_target:
paths:
# Only trigger the workflow if SVG files in the icons directory are changed
- "icons/*.svg"

# Permissions for the workflow
permissions:
contents: read
pull-requests: write
issues: write

jobs:
icon-review:
name: Icon Review
runs-on: ubuntu-latest
env:
TARGET_BRANCH: ${{ github.event.pull_request.base.ref }}

permissions:
pull-requests: write

steps:
- name: 📥 Checkout Fork
- name: 📥 Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
ref: ${{ github.event.pull_request.head.ref }}
ref: ${{ github.event.pull_request.base.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
sparse-checkout: |
icons/
materal-colors.yml
fetch-depth: 0
path: fork
persist-credentials: false

- name: 📥 Checkout Original
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 0
path: main
persist-credentials: false

- name: 🔧 Setup Bun
uses: oven-sh/setup-bun@4bc047ad259df6fc24a6c9b0f9a0cb08cf17fbe5 # v2

with:
bun-version-file: ".bun-version"
okineadev marked this conversation as resolved.
Show resolved Hide resolved

- name: 🔍 Get changed SVG files
id: get_changed_files
uses: actions/github-script@v7.0.1
with:
bun-version-file: "main/.bun-version"
script: |
const { data: files } = await github.rest.pulls.listFiles({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
});

const svgFiles = files
.filter(file => file.filename.startsWith('icons/') && file.filename.endsWith('.svg'))
.map(file => file.filename);

const changedSvgFilesCount = svgFiles.length;

core.setOutput('changed_svg_files', svgFiles.join(' '));
core.setOutput('changed_svg_files_count', changedSvgFilesCount);

- name: 🔍 Review SVG files
working-directory: main
run: |
files=$(git diff --no-index ../main ../fork --diff-filter=ACMRTUX --name-only | grep '^\.\./fork/icons/.*\.svg$')
filesCount=$(echo "$files" | wc -l)
bunx --bun svg-icon-review@2.1.0 --bigIcon ${files}
echo svg_files_count=$filesCount >> $GITHUB_ENV
bunx svg-icon-review@2.1.0 --bigIcon ${{ steps.get_changed_files.outputs.changed_svg_files }}

- name: ⬆️ Upload PNG
- name: ⬆️ Upload preview image
env:
IMAGE_UPLOAD: ${{ secrets.IMAGE_UPLOAD }}
id: upload
run: |
IMAGE_URL=$(curl --location 'https://freeimage.host/json' --form 'source=@"./main/preview.png"' --form 'type="file"' --form 'action="upload"' --form 'auth_token="${{env.IMAGE_UPLOAD}}"' | jq -r '.image.url')
echo image_url=$IMAGE_URL >> $GITHUB_ENV
IMAGE_URL=$(curl --location 'https://freeimage.host/json' \
--form 'source=@"./preview.png"' \
--form 'type="file"' \
--form 'action="upload"' \
--form "auth_token=${{ env.IMAGE_UPLOAD }}" | jq -r '.image.url')

echo "image_url=$IMAGE_URL" >> $GITHUB_ENV

- name: 📃 Generate text
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
- name: ✍️ Post or update comment
uses: actions/github-script@v7.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
changed_svg_files_count: ${{ steps.get_changed_files.outputs.changed_svg_files_count }}
image_url: ${{ env.image_url }}
with:
script: |
const iconsCount = parseInt("${{ env.svg_files_count }}");
const imageURL = "${{ env.image_url }}";
const iconsCount = parseInt(process.env.changed_svg_files_count);
const imageURL = process.env.image_url;
const pluralSuffix = iconsCount > 1 ? 's' : '';
const image = `<img src="${imageURL}" alt="Generated preview" />`;
const pluralS = iconsCount > 1 ? "s" : "";

let preview = "";

if (iconsCount > 10) {
preview = `<details><summary>🏞️ <b>Generated preview</b></summary><br/>${image}</details>`;
} else {
preview = image;
}
let preview = iconsCount > 10
? `<details><summary>🏞️ <b>Generated preview</b></summary><br/>${image}</details>`
: image;

const comment = `
const commentBody = `
<!-- Preview generated by GitHub Actions -->
## Preview

Thank you for creating a pull request. This preview shows you how your icon${pluralS} will look on the different themes:
Thank you for creating a pull request. This preview shows you how your icon${pluralSuffix} will look on the different themes:

${preview}

Check how your icon${pluralS} fit${!pluralS ? 's' : ''} in a 16x16 grid with our **Pixel Perfect Checker** by following [this link](https://pixp.pages.dev/material-extensions/vscode-material-icon-theme/pull/${{ github.event.pull_request.number }}).
Check how your icon${pluralSuffix} fit${pluralSuffix ? '' : 's'} into a 16x16 grid with our **Pixel Perfect Checker** by following [this link](https://pixp.pages.dev/material-extensions/vscode-material-icon-theme/pull/${{ github.event.pull_request.number }}).

You can find more information on how to contribute in the [contribution guidelines](https://github.com/material-extensions/vscode-material-icon-theme/blob/main/CONTRIBUTING.md).
`;

// Write comment to environment variable
core.exportVariable('comment', comment);

- name: 🕵️ Find comment if exists
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: <!-- Preview generated by GitHub Actions -->

- name: ✍️ Post or update comment in PR
uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 # v4.0.0
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.pull_request.number }}
body: |
${{ env.comment }}
edit-mode: replace
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});

const existingComment = comments.find(comment =>
comment.user.login === 'github-actions[bot]' &&
comment.body.includes('<!-- Preview generated by GitHub Actions -->')
);

const comment = {
owner: context.repo.owner,
repo: context.repo.repo,
body: commentBody,
};

if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
...comment
});
} else {
await github.rest.issues.createComment({
issue_number: context.issue.number,
...comment
});
}
3 changes: 2 additions & 1 deletion .github/workflows/label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
pull-requests: write

steps:
- uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5.0.0
- name: 🔖 Add label
uses: actions/labeler@8558fd74291d67161a8a78ce36a881fa63b766a9 # v5.0.0
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"