Skip to content

Commit

Permalink
Clean up workflows (#8113)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jun 19, 2024
1 parent 61b8386 commit f87f20a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 35 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ on:
push:
branches:
- master
paths:
- 'entries/**'
- 'img/**'

concurrency:
group: 'publish'
Expand All @@ -18,7 +21,7 @@ jobs:
environment: production
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 2
- uses: ruby/setup-ruby@v1
Expand Down
21 changes: 21 additions & 0 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,39 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get modified files
id: diff
run: |
echo "::debug:: Fetching files from ${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.number }}/files"
FILES=$(curl -s "${{ github.api_url }}/repos/${{ github.repository }}/pulls/${{ github.event.number }}/files" | jq -r '.[] | select(.status != "removed") | .filename' | tr '\n' ' ')
ENTRIES=$(echo "$FILES" | tr ' ' '\n' | grep -E '^entries/.*\.json$' | tr '\n' ' ')
if [ -n "$ENTRIES" ]; then
echo "entries=${ENTRIES}" >> $GITHUB_OUTPUT
fi
IMAGES=$(echo "$FILES" | tr ' ' '\n' | grep -E '^img/.*$' | tr '\n' ' ')
if [ -n "IMAGES" ]; then
echo "images=${IMAGES}" >> $GITHUB_OUTPUT
fi
RUBY=$(echo "$FILES" | tr ' ' '\n' | grep -E '^(script|tests)/.*rb$' | tr '\n' ' ')
if [ -n "RUBY" ]; then
echo "ruby=${RUBY}" >> $GITHUB_OUTPUT
fi
- uses: ruby/setup-ruby@v1
if: ${{ steps.diff.outputs.entries || steps.diff.outputs.images || steps.diff.outputs.ruby }}
with:
bundler-cache: true
ruby-version: '3.0'
env:
BUNDLE_WITH: 'tests'
- name: Validate SVG
if: steps.diff.outputs.images
run: bundle exec ruby ./tests/svg-lint.rb
- name: Validate URL/Domain reachability
if: steps.diff.outputs.entries
run: bundle exec ruby ./tests/validate-urls.rb
continue-on-error: true
- name: Validate Ruby scripts
if: steps.diff.outputs.ruby
run: bundle exec rubocop

node-tests:
Expand Down
72 changes: 40 additions & 32 deletions tests/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,58 @@ let seenImages = [];
let errors = false;

async function main() {
await parseEntries(await glob("entries/**/*.json"));
const [entries, images] = await Promise.all([
glob("entries/**/*.json"),
glob("img/*/*.*"),
]);

await parseImages(await glob("img/*/*.*"));
await parseEntries(entries);
await parseImages(images);

process.exit(+errors);
}

async function parseEntries(entries) {
for (const file of entries) {
const data = await fs.readFile(file, "utf8");
const json = await JSON.parse(data);
const entry = json[Object.keys(json)[0]];
const { img, domain } = entry;
const path = `img/${img ? `${img[0]}/${img}` : `${domain[0]}/${domain}.svg`}`;
await Promise.all(
entries.map(async (file) => {
const data = await fs.readFile(file, "utf8");
const json = await JSON.parse(data);
const entry = json[Object.keys(json)[0]];
const { img, domain } = entry;
const path = `img/${img ? `${img[0]}/${img}` : `${domain[0]}/${domain}.svg`}`;

try {
await fs.readFile(path);
} catch (e) {
core.error(`Image ${path} not found.`, { file });
errors = true;
}
seenImages.push(path);
}
try {
await fs.readFile(path);
} catch (e) {
core.error(`Image ${path} not found.`, { file });
errors = true;
}
seenImages.push(path);
}),
);
}

async function parseImages(images) {
for (const image of images) {
if (!seenImages.includes(image)) {
core.error(`Unused image`, { file: image });
errors = true;
}

if (image.endsWith(".png")) {
if (!dimensionsAreValid(await getPNGDimensions(image), PNG_RES)) {
core.error(
`PNGs must be one of the following dimensions: ${PNG_RES.map((a) =>
a.join("x"),
).join(", ")}`,
{ file: image },
);
await Promise.all(
images.map(async (image) => {
if (!seenImages.includes(image)) {
core.error(`Unused image`, { file: image });
errors = true;
}
}
}

if (image.endsWith(".png")) {
if (!dimensionsAreValid(await getPNGDimensions(image), PNG_RES)) {
core.error(
`PNGs must be one of the following dimensions: ${PNG_RES.map((a) =>
a.join("x"),
).join(", ")}`,
{ file: image },
);
errors = true;
}
}
}),
);
}

function dimensionsAreValid(dimensions, validSizes) {
Expand Down
9 changes: 7 additions & 2 deletions tests/validate-urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ def check_url(path, url, res = nil)
depth += 1
raise('Too many redirections') if depth > 5

url = URI.parse(url)
res = Net::HTTP.get_response(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == 'https')
http.open_timeout = 5
http.read_timeout = 5
res = http.request_get(uri.request_uri)

break unless res.is_a? Net::HTTPRedirection

url = URI(res['location']).is_a?(URI::HTTP) ? res['location'] : "#{url.scheme}:https://#{url.host}#{res['location']}"
Expand Down

0 comments on commit f87f20a

Please sign in to comment.