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

[Suggestion] Don't commit images with low visual change in dev docs sync #2524

Closed
wants to merge 2 commits into from
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
5 changes: 5 additions & 0 deletions scripts/js/commands/api/syncDevDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { readApiFullVersion } from "../../lib/apiVersions.js";
import {
generateVersion,
determineMinorVersion,
resetVisuallyUnchangedImages,
Arguments,
} from "./updateApiDocs.js";

Expand Down Expand Up @@ -52,9 +53,13 @@ zxMain(async () => {

if (qiskitUrl) {
await regenDocs("qiskit", qiskitVersion);
await resetVisuallyUnchangedImages(`public/images/api/qiskit/dev/`);
}
if (runtimeUrl) {
await regenDocs("qiskit-ibm-runtime", runtimeVersion);
await resetVisuallyUnchangedImages(
`public/images/api/qiskit-ibm-runtime/dev/`,
);
}
});

Expand Down
44 changes: 44 additions & 0 deletions scripts/js/commands/api/updateApiDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import yargs from "yargs/yargs";
import { hideBin } from "yargs/helpers";
import { $ } from "zx";

import { Pkg } from "../../lib/api/Pkg.js";
import { zxMain } from "../../lib/zx.js";
Expand Down Expand Up @@ -157,3 +158,46 @@ if (import.meta.url === `file://${process.argv[1]}`) {
await generateVersion(pkg, args);
});
}

/**
* Create a backup of the new image, restore the original image using Git, then compare the two.
* If they're within a similarity threshold, delete the backup and keep the original, otherwise keep the new file.
*/
export async function resetVisuallyUnchangedImages(dir: string): Promise<void> {
const changedImages = (await $`git diff --name-only ${dir}`).stdout
.trim()
.split("\n")
.filter((file) => file.endsWith(".png"));

if (!changedImages.length) return;

const backupExtension = "-backup.png";
await Promise.all(
changedImages.map(
(filePath) => $`cp ${filePath} ${filePath}${backupExtension}`,
),
);

await $`git restore ${changedImages}`;

async function resetImageIfUnchanged(filePath: string): Promise<void> {
const backupFile = filePath + backupExtension;

// See https://imagemagick.org/script/compare.php
// Command is a bit strange: Returns exit code 1 if files are different and writes difference metric to stderr
const difference = (
await $`magick compare -metric AE -channel all ${filePath} ${backupFile} /dev/null 2>&1`.nothrow()
).stdout;

if (Number(difference) < 500) {
console.log(`Resetting image due to low visual change: ${filePath}`);
await $`rm ${backupFile}`;
} else {
await $`mv ${backupFile} ${filePath}`;
}
}

await Promise.all(
changedImages.map((filePath) => resetImageIfUnchanged(filePath)),
);
}
Loading