Skip to content

Commit

Permalink
Merge pull request #2 from calibreapp/overall-metrics
Browse files Browse the repository at this point in the history
Add overall metrics for processing
  • Loading branch information
benschwarz authored Aug 1, 2019
2 parents 63e7bf1 + 35c7f8c commit a52a157
Show file tree
Hide file tree
Showing 15 changed files with 4,912 additions and 55 deletions.
17 changes: 17 additions & 0 deletions .github/main.workflow
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
workflow "Test" {
on = "push"
resolves = ["npm test"]
}

action "npm ci" {
uses = "./"
runs = "npm"
args = "ci"
}

action "npm test" {
needs = "npm ci"
uses = "./"
runs = "npm"
args = "test"
}
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,6 @@ RUN npm ci
# copy in src
COPY LICENSE README.md entrypoint.js /usr/local/src/image-actions/
COPY src/ /usr/local/src/image-actions/src/
COPY __tests__/ /usr/local/src/image-actions/__tests__/

ENTRYPOINT ["/usr/local/src/image-actions/entrypoint.js"]
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Image actions

Image actions will automatically compress jpeg and png images in Github Pull Requests.
Image actions will automatically compress jpeg and png images in GitHub Pull Requests.

- **Compression is fast, efficient and lossless**
- Uses mozjpeg + libvips, the best image compression available
- Runs in Github Actions **for free**
- Runs in GitHub Actions **for free**

![Preview of image-actions pull request comment](https://user-images.githubusercontent.com/924/62024579-e1470d00-b218-11e9-8655-693ea42ba0f7.png)

Expand All @@ -29,5 +29,5 @@ This action requires a `GITHUB_TOKEN` so that it has access to commit the optimi

## Links and resources

- [View calibre/image-actions on the Github Marketplace](https://github.com/marketplace/actions/image-actions)
- [View calibre/image-actions on the GitHub Marketplace](https://github.com/marketplace/actions/image-actions)
- [Mozjpeg](https://github.com/mozilla/mozjpeg)
Binary file added __tests__/example-images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __tests__/example-images/optimised-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added __tests__/example-images/roo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 56 additions & 0 deletions __tests__/github-markdown_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const results = {
images: [
{
name: "icon.png",
path: "__tests__/test-images/icon.png",
beforeStats: 8914,
afterStats: 3361,
percentChange: -62.29526587390622,
compressionWasSignificant: true
},
{
name: "optimised-image.png",
path: "__tests__/test-images/optimised-image.png",
beforeStats: 3361,
afterStats: 3361,
percentChange: 0,
compressionWasSignificant: false
},
{
name: "roo.jpg",
path: "__tests__/test-images/roo.jpg",
beforeStats: 468895,
afterStats: 485742,
percentChange: 3.592915258213452,
compressionWasSignificant: false
}
],
metrics: {
bytesSaved: 5553,
percentChange: -62.29526587390622
}
};

const markdown = require("../src/github-markdown");

const referenceMarkdown = `
Images automagically compressed by [Calibre](https://calibreapp.com)'s [image-actions](https://github.com/marketplace/actions/image-actions) ✨
Compression reduced images by 62.3%, saving 5.42 KB
| Filename | Before | After | Improvement |
| --- | --- | --- | --- |
| \`icon.png\` | 8.71 KB | 3.28 KB | -62.3% |
<details>
<summary>Some images were already optimised</summary>
* \`optimised-image.png\`
* \`roo.jpg\`
</details>`;

test("writes the markdown", async () => {
const markdownResult = await markdown(results);
expect(markdownResult).toEqual(referenceMarkdown);
});
68 changes: 68 additions & 0 deletions __tests__/image-processing_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const path = require("path");
const fs = require("fs").promises;

const EXAMPLE_IMAGES_DIR = `${process.cwd()}/__tests__/example-images`;
const TMP_TEST_IMAGES_DIR = `${process.cwd()}/__tests__/test-images`;

const EXAMPLE_IMAGES = ["roo.jpg", "icon.png", "optimised-image.png"];

beforeEach(async () => {
await fs.mkdir(TMP_TEST_IMAGES_DIR);

// Copy in reference images for stats
for await (const image of EXAMPLE_IMAGES) {
await fs.copyFile(
path.join(EXAMPLE_IMAGES_DIR, image),
path.join(TMP_TEST_IMAGES_DIR, image)
);
}
});

afterEach(async () => {
for await (const image of EXAMPLE_IMAGES) {
await fs.unlink(path.join(TMP_TEST_IMAGES_DIR, image));
}
await fs.rmdir(TMP_TEST_IMAGES_DIR);
});

const imageProcessing = require("../src/image-processing");

test("returns metrics for images", async () => {
const results = await imageProcessing();

expect(results.metrics).toEqual({
bytesSaved: 5553,
percentChange: -62.29526587390622
});
});

test("returns images with stats", async () => {
const results = await imageProcessing();

expect(results.images).toEqual([
{
afterStats: 3361,
beforeStats: 8914,
compressionWasSignificant: true,
name: "icon.png",
path: "__tests__/test-images/icon.png",
percentChange: -62.29526587390622
},
{
afterStats: 3361,
beforeStats: 3361,
compressionWasSignificant: false,
name: "optimised-image.png",
path: "__tests__/test-images/optimised-image.png",
percentChange: 0
},
{
afterStats: 485759,
beforeStats: 468895,
compressionWasSignificant: false,
name: "roo.jpg",
path: "__tests__/test-images/roo.jpg",
percentChange: 3.596540803378147
}
]);
});
4 changes: 0 additions & 4 deletions docker-compose.yml

This file was deleted.

9 changes: 3 additions & 6 deletions entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ if (!GITHUB_TOKEN) {
process.exit(1);
}

if (!REPO_DIRECTORY) {
console.log("There is no REPO_DIRECTORY set");
process.exit(1);
}

const main = async () => {
// Bail out if the event that executed the action wasn’t a pull_request
if (GITHUB_EVENT_NAME !== "pull_request") {
Expand Down Expand Up @@ -59,7 +54,9 @@ const main = async () => {
const markdown = await generateMarkdownReport(results);

console.log("->> Committing files…");
const optimisedImages = results.filter(img => img.compressionWasSignificant);
const optimisedImages = results.images.filter(
img => img.compressionWasSignificant
);
await createCommit(optimisedImages);

console.log("->> Leaving comment on PR…");
Expand Down
Loading

0 comments on commit a52a157

Please sign in to comment.