WooCommerce Docs: Update manifest hash on content update (#39445)

This commit is contained in:
Paul Sealock 2023-08-01 13:13:42 +12:00 committed by GitHub
parent dd162f3305
commit fb26613f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 116 additions and 6 deletions

View File

@ -6,8 +6,9 @@
{
"post_title": "Local Development",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/local-development.md",
"hash": "7fa2ec20fa654573e07b0d6237a0618ab19999467472a2362fa4ec1955b229f7",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/local-development.md",
"id": "c387a046f8307a4f459091a2c45e82263b45e777"
"id": "1a3672ea88756c6d00a3b3baa3f7481b361c8a1e"
}
],
"categories": [
@ -17,8 +18,9 @@
{
"post_title": "Install the Plugin",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/installation/install-plugin.md",
"hash": "89be25e4e91f5c807c7dc9ff778b2b76c6f7061cb9aca6e0cfc7fddb0cc28b6b",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/installation/install-plugin.md",
"id": "c4ac8c48c3445186189d15f01028eacd517b9707"
"id": "6c7bb21cb195798a444844fef0ff79aacff86de1"
}
],
"categories": []
@ -29,8 +31,9 @@
{
"post_title": "What Went Wrong?",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/get-started/troubleshooting/what-went-wrong.md",
"hash": "9868a39ccc26aa79f06caed1dc3576878953e6279facf7a66ad6f45fd2cd7d0a",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/get-started/troubleshooting/what-went-wrong.md",
"id": "f87dcc9b8177ad2d8549ef7298d66fb7c8616d71"
"id": "e2f590a5994fada97bdbdcac751e8bc441530868"
}
],
"categories": []
@ -43,18 +46,20 @@
{
"post_title": "Unit Testing",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/testing/unit-tests.md",
"hash": "f4950c5597f1ef70f546fd3c4b92fd3c23518317aca2d72edb5a7da519c9946f",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/testing/unit-tests.md",
"id": "1b9de005cc5da3b49d12c24837a5a574aa58ad96"
"id": "6f1b9c74de42a10cf4c77c2843e0fbdf1ff46316"
},
{
"post_title": "Integration Testing",
"edit_url": "https://github.com/woocommerce/woocommerce/edit/trunk/plugins/woocommerce-docs/example-docs/testing/integration-tests.md",
"hash": "6929e58b2a32d027f1049eea2ff6136f47b8fb52d4d1781f257de59b999155b0",
"url": "https://raw.githubusercontent.com/woocommerce/woocommerce/trunk/plugins/woocommerce-docs/example-docs/testing/integration-tests.md",
"id": "ee3fd61c9d7b13614e4431708c336f20c9779e1f"
"id": "09c092c2b450edc45b9c247e0a983b7d176984a1"
}
],
"categories": []
}
],
"hash": "84a06ceffaced22a955a131c1272ec9a1d14ae0a7cf9bac4ed21874384f86e9a"
"hash": "80feb7e964daa576ce2c4265dc0e0e5d05a0df4fce356cdf63e245e22435e9ba"
}

View File

@ -2,6 +2,7 @@
* External dependencies
*/
import path from 'path';
import fs from 'fs';
/**
* Internal dependencies
@ -137,6 +138,104 @@ describe( 'generateManifest', () => {
'https://github.com/edit/example-docs/get-started/local-development.md'
);
} );
it( 'should create a hash for each post in a manifest', async () => {
const manifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
const topLevelCategories = manifest.categories;
const posts = [
...topLevelCategories[ 0 ].posts,
...topLevelCategories[ 0 ].categories[ 0 ].posts,
...topLevelCategories[ 0 ].categories[ 1 ].posts,
...topLevelCategories[ 1 ].posts,
];
posts.forEach( ( post ) => {
expect( post.hash ).not.toBeUndefined();
} );
} );
it( 'should update a post hash and manifest hash when content is updated', async () => {
const manifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
const post = manifest.categories[ 0 ].posts[ 0 ];
const originalPostHash = post.hash;
const originalManifestHash = manifest.hash;
// Confirm hashes are not undefined
expect( originalPostHash ).not.toBeUndefined();
expect( originalManifestHash ).not.toBeUndefined();
// Update the file content of the corresponding post
const filePath = path.join( dir, 'get-started/local-development.md' );
const fileContent = fs.readFileSync( filePath, 'utf-8' );
const updatedFileContent = fileContent + '\n\n<!-- updated -->';
fs.writeFileSync( filePath, updatedFileContent );
// Generate a new manifest
const nextManifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
const nextPost = nextManifest.categories[ 0 ].posts[ 0 ];
const nextPostHash = nextPost.hash;
const nextManifestHash = nextManifest.hash;
// Confirm hashes are newly created.
expect( nextPostHash ).not.toEqual( originalPostHash );
expect( nextManifestHash ).not.toEqual( originalManifestHash );
// Reset the file content
fs.writeFileSync( filePath, fileContent );
} );
it( 'should not update a post hash and manifest hash when content is unchanged', async () => {
const manifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
const post = manifest.categories[ 0 ].posts[ 0 ];
const originalPostHash = post.hash;
const originalManifestHash = manifest.hash;
// Confirm hashes are not undefined
expect( originalPostHash ).not.toBeUndefined();
expect( originalManifestHash ).not.toBeUndefined();
// Generate a new manifest
const nextManifest = await generateManifestFromDirectory(
rootDir,
dir,
'example-docs',
'https://example.com',
'https://example.com/edit'
);
const nextPost = nextManifest.categories[ 0 ].posts[ 0 ];
const nextPostHash = nextPost.hash;
const nextManifestHash = nextManifest.hash;
// Confirm hashes are newly created.
expect( nextPostHash ).toEqual( originalPostHash );
expect( nextManifestHash ).toEqual( originalManifestHash );
} );
} );
describe( 'generatePostId', () => {

View File

@ -77,6 +77,12 @@ async function processDirectory(
const post: Post = { ...fileFrontmatter };
// Generate hash of the post contents.
post.hash = crypto
.createHash( 'sha256' )
.update( JSON.stringify( fileContent ) )
.digest( 'hex' );
// get the folder name of rootDirectory.
const relativePath = path.relative( fullPathToDocs, filePath );