From d0fadc316653ba86b476eef185430470faba9d03 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 11:40:33 -0800 Subject: [PATCH 1/8] adding HideAuthorName --- wordpress-to-github/common/index.js | 29 ++++++++++-------- wordpress-to-github/index.js | 30 ++++++++++++------- .../wordpress-to-github.config.schema.json | 9 ++++-- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/wordpress-to-github/common/index.js b/wordpress-to-github/common/index.js index 55d2c30..d9f568c 100644 --- a/wordpress-to-github/common/index.js +++ b/wordpress-to-github/common/index.js @@ -1,5 +1,5 @@ // @ts-check -const crypto = require('crypto'); +const crypto = require("crypto"); const apiPath = "/wp-json/wp/v2/"; const { gitHubBlobPredictShaFromBuffer, @@ -30,13 +30,14 @@ const fetchRetry = require("fetch-retry")(require("node-fetch/lib"), { * @property {string} [PagePath] * @property {string} [MediaPath] * @property {string} [GeneralFilePath] + * @property {boolean} [HideAuthorName] True to hide author information. * @property {EndpointRequestsConfigData[]} [ApiRequests] */ /** * @typedef {object} EndpointRequestsConfigData * @property {string} Destination - * @property {string} Source + * @property {string} Source * @property {string[]} [ExcludeProperties] */ @@ -155,10 +156,10 @@ const fetchRetry = require("fetch-retry")(require("node-fetch/lib"), { * @property {string} Destination * @property {string} Source * @property {string} Hash - * + * * @typedef {object} WithData * @property {string} Data - * + * * @typedef {WordpressApiHashCacheItem & WithData} WordpressApiHashDataItem */ @@ -264,8 +265,8 @@ const WpApi_getSomething = async fetchquery => await fetchRetry(fetchquery, { method: "Get" }); /** - * Fetch API request data from the WordPress API. - * + * Fetch API request data from the WordPress API. + * * @param {string} wordPressApiUrl Full URL to the WordPress Menu API. * @param {EndpointRequestsConfigData[]} requests Array of Wordpress API requests. * @returns {Promise} @@ -282,7 +283,9 @@ const WpApi_GetApiRequestsData = (wordPressApiUrl, requests) => { if (response.ok) { return response.json(); } else { - throw new Error(`${response.status} - ${response.statusText} - ${response.url}`); + throw new Error( + `${response.status} - ${response.statusText} - ${response.url}` + ); } }) .then(json => removeExcludedProperties(json, request.ExcludeProperties)) @@ -301,12 +304,14 @@ const WpApi_GetApiRequestsData = (wordPressApiUrl, requests) => { /** * Compares a cached object to a current object to see if the cache is out of date. - * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} cacheItem - * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} currentItem + * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} cacheItem + * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} currentItem * @returns {boolean} */ const jsonCacheDiscrepancy = (cacheItem, currentItem) => { - return !cacheItem || JSON.stringify(cacheItem) !== JSON.stringify(currentItem); + return ( + !cacheItem || JSON.stringify(cacheItem) !== JSON.stringify(currentItem) + ); }; /** @@ -454,7 +459,7 @@ const syncBinaryFile = async (wordpress_url, gitRepo, mediaTree, endpoint) => { * * @param {object} json * @param {string[]} [excludeList] - * @returns {object} + * @returns {object} */ const removeExcludedProperties = (json, excludeList) => { if (excludeList) { @@ -528,4 +533,4 @@ module.exports = { WpApi_getSomething, pathFromMediaSourceUrl, addMediaSection -}; \ No newline at end of file +}; diff --git a/wordpress-to-github/index.js b/wordpress-to-github/index.js index 0674c60..c255e02 100644 --- a/wordpress-to-github/index.js +++ b/wordpress-to-github/index.js @@ -128,9 +128,13 @@ const SyncEndpoint = async ( const endpointConfig = await getRemoteConfig(gitHubTarget, gitHubCredentials); const wordPressApiUrl = sourceEndpointConfig.WordPressSource.url + apiPath; - const allApiRequests = endpointConfig.ApiRequests && endpointConfig.ApiRequests.length - ? await WpApi_GetApiRequestsData(sourceEndpointConfig.WordPressSource.url, endpointConfig.ApiRequests) - : null; + const allApiRequests = + endpointConfig.ApiRequests && endpointConfig.ApiRequests.length + ? await WpApi_GetApiRequestsData( + sourceEndpointConfig.WordPressSource.url, + endpointConfig.ApiRequests + ) + : null; //Check cache (and set cache for next time) let cacheMatch = true; @@ -180,7 +184,9 @@ const SyncEndpoint = async ( //List of WP categories const categorylist = await fetchDictionary(wordPressApiUrl, "categories"); const taglist = await fetchDictionary(wordPressApiUrl, "tags"); - const userlist = await fetchDictionary(wordPressApiUrl, "users"); + const userlist = endpointConfig.HideAuthorName + ? null + : await fetchDictionary(wordPressApiUrl, "users"); /** @type {WordpressMediaRow[] | null} */ const allMedia = endpointConfig.MediaPath @@ -257,7 +263,7 @@ const SyncEndpoint = async ( /** @type {GithubOutputJson} */ const jsonData = { ...x, - author: userlist[x.author.toString()], + author: userlist ? userlist[x.author] : x.author, wordpress_url: ensureStringStartsWith( sourceEndpointConfig.WordPressSource.url, x.source_url @@ -376,7 +382,7 @@ const SyncEndpoint = async ( const wordPressRowToGitHubOutput = wpRow => { const jsonData = { ...wpRow, - author: userlist[wpRow.author], + author: userlist ? userlist[wpRow.author] : wpRow.author, wordpress_url: wpRow.link, categories: mapLookup(wpRow, "categories", categorylist), tags: mapLookup(wpRow, "tags", taglist) @@ -504,7 +510,7 @@ const SyncEndpoint = async ( bucket[folderName] = new Map(); } - bucket[folderName].set(fileName, JSON.stringify(request.Data, null, 2)) + bucket[folderName].set(fileName, JSON.stringify(request.Data, null, 2)); return bucket; }, {}); @@ -519,9 +525,11 @@ const SyncEndpoint = async ( false ); - const reportLabel = folderName.split("/").slice(-1).join("/") || 'root'; - const updateCount = `${requestsTree.length} ${requestsTree.length === 1 ? "update" : "updates"}`; - + const reportLabel = folderName.split("/").slice(-1).join("/") || "root"; + const updateCount = `${requestsTree.length} ${ + requestsTree.length === 1 ? "update" : "updates" + }`; + addToReport( report, await CommitIfChanged( @@ -540,4 +548,4 @@ const SyncEndpoint = async ( module.exports = { SyncEndpoint -}; \ No newline at end of file +}; diff --git a/wordpress-to-github/schemas/wordpress-to-github.config.schema.json b/wordpress-to-github/schemas/wordpress-to-github.config.schema.json index e16b524..c69fb25 100644 --- a/wordpress-to-github/schemas/wordpress-to-github.config.schema.json +++ b/wordpress-to-github/schemas/wordpress-to-github.config.schema.json @@ -2,8 +2,8 @@ "$schema": "http://json-schema.org/draft-07/schema#", "title": "Schema for a single wordpress-to-github endpoint", "description": "Use this schema when building a wordpress-to-github.config for your Github branch.", - "$comment": "Version 2.0", - "$id": "wordpress-to-github_config_v2_0", + "$comment": "Version 2.1", + "$id": "wordpress-to-github_config_v2_1", "type": "object", "required": ["data"], "additionalProperties": false, @@ -109,6 +109,11 @@ "default": [], "description": "Which properties should we suppress in output?", "examples": [["content", "_links"]] + }, + "HideAuthorName": { + "type": "boolean", + "description": "True to only display the numeric id for author.", + "default": false } } } From 98bf2bd3cf26966ca6426a58c5e32dd6c141a549 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 11:56:36 -0800 Subject: [PATCH 2/8] delta reduce --- wordpress-to-github/common/index.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/wordpress-to-github/common/index.js b/wordpress-to-github/common/index.js index d9f568c..0fa70f3 100644 --- a/wordpress-to-github/common/index.js +++ b/wordpress-to-github/common/index.js @@ -1,5 +1,5 @@ // @ts-check -const crypto = require("crypto"); +const crypto = require('crypto'); const apiPath = "/wp-json/wp/v2/"; const { gitHubBlobPredictShaFromBuffer, @@ -37,7 +37,7 @@ const fetchRetry = require("fetch-retry")(require("node-fetch/lib"), { /** * @typedef {object} EndpointRequestsConfigData * @property {string} Destination - * @property {string} Source + * @property {string} Source * @property {string[]} [ExcludeProperties] */ @@ -156,10 +156,10 @@ const fetchRetry = require("fetch-retry")(require("node-fetch/lib"), { * @property {string} Destination * @property {string} Source * @property {string} Hash - * + * * @typedef {object} WithData * @property {string} Data - * + * * @typedef {WordpressApiHashCacheItem & WithData} WordpressApiHashDataItem */ @@ -266,7 +266,7 @@ const WpApi_getSomething = async fetchquery => /** * Fetch API request data from the WordPress API. - * + * * @param {string} wordPressApiUrl Full URL to the WordPress Menu API. * @param {EndpointRequestsConfigData[]} requests Array of Wordpress API requests. * @returns {Promise} @@ -283,9 +283,7 @@ const WpApi_GetApiRequestsData = (wordPressApiUrl, requests) => { if (response.ok) { return response.json(); } else { - throw new Error( - `${response.status} - ${response.statusText} - ${response.url}` - ); + throw new Error(`${response.status} - ${response.statusText} - ${response.url}`); } }) .then(json => removeExcludedProperties(json, request.ExcludeProperties)) @@ -304,14 +302,12 @@ const WpApi_GetApiRequestsData = (wordPressApiUrl, requests) => { /** * Compares a cached object to a current object to see if the cache is out of date. - * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} cacheItem - * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} currentItem + * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} cacheItem + * @param {WordpressApiDateCacheItem|WordpressApiHashCacheItem} currentItem * @returns {boolean} */ const jsonCacheDiscrepancy = (cacheItem, currentItem) => { - return ( - !cacheItem || JSON.stringify(cacheItem) !== JSON.stringify(currentItem) - ); + return !cacheItem || JSON.stringify(cacheItem) !== JSON.stringify(currentItem); }; /** From 1fd441dbd369bea43a2c0735a449460395e5f609 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 11:57:47 -0800 Subject: [PATCH 3/8] delta reduce --- wordpress-to-github/common/index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/wordpress-to-github/common/index.js b/wordpress-to-github/common/index.js index 0fa70f3..460b00d 100644 --- a/wordpress-to-github/common/index.js +++ b/wordpress-to-github/common/index.js @@ -265,7 +265,7 @@ const WpApi_getSomething = async fetchquery => await fetchRetry(fetchquery, { method: "Get" }); /** - * Fetch API request data from the WordPress API. + * Fetch API request data from the WordPress API. * * @param {string} wordPressApiUrl Full URL to the WordPress Menu API. * @param {EndpointRequestsConfigData[]} requests Array of Wordpress API requests. @@ -455,7 +455,7 @@ const syncBinaryFile = async (wordpress_url, gitRepo, mediaTree, endpoint) => { * * @param {object} json * @param {string[]} [excludeList] - * @returns {object} + * @returns {object} */ const removeExcludedProperties = (json, excludeList) => { if (excludeList) { @@ -530,3 +530,4 @@ module.exports = { pathFromMediaSourceUrl, addMediaSection }; + From 25f9fc87662c364b064bbde38880d2292d7b3724 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 11:58:10 -0800 Subject: [PATCH 4/8] delta reduce --- wordpress-to-github/common/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/wordpress-to-github/common/index.js b/wordpress-to-github/common/index.js index 460b00d..b91f6d3 100644 --- a/wordpress-to-github/common/index.js +++ b/wordpress-to-github/common/index.js @@ -529,5 +529,4 @@ module.exports = { WpApi_getSomething, pathFromMediaSourceUrl, addMediaSection -}; - +}; \ No newline at end of file From 099b5724afdb7d8700a0bc248f2f39ff0b2a7fc5 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 11:59:13 -0800 Subject: [PATCH 5/8] delta reduce --- wordpress-to-github/index.js | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/wordpress-to-github/index.js b/wordpress-to-github/index.js index c255e02..6863749 100644 --- a/wordpress-to-github/index.js +++ b/wordpress-to-github/index.js @@ -128,13 +128,9 @@ const SyncEndpoint = async ( const endpointConfig = await getRemoteConfig(gitHubTarget, gitHubCredentials); const wordPressApiUrl = sourceEndpointConfig.WordPressSource.url + apiPath; - const allApiRequests = - endpointConfig.ApiRequests && endpointConfig.ApiRequests.length - ? await WpApi_GetApiRequestsData( - sourceEndpointConfig.WordPressSource.url, - endpointConfig.ApiRequests - ) - : null; + const allApiRequests = endpointConfig.ApiRequests && endpointConfig.ApiRequests.length + ? await WpApi_GetApiRequestsData(sourceEndpointConfig.WordPressSource.url, endpointConfig.ApiRequests) + : null; //Check cache (and set cache for next time) let cacheMatch = true; @@ -184,9 +180,7 @@ const SyncEndpoint = async ( //List of WP categories const categorylist = await fetchDictionary(wordPressApiUrl, "categories"); const taglist = await fetchDictionary(wordPressApiUrl, "tags"); - const userlist = endpointConfig.HideAuthorName - ? null - : await fetchDictionary(wordPressApiUrl, "users"); + const userlist = endpointConfig.HideAuthorName ? null : await fetchDictionary(wordPressApiUrl, "users"); /** @type {WordpressMediaRow[] | null} */ const allMedia = endpointConfig.MediaPath From dbfe76fcdd877de5250febdaf1ea60d72d9e7af2 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 12:00:09 -0800 Subject: [PATCH 6/8] delta reduce --- wordpress-to-github/index.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/wordpress-to-github/index.js b/wordpress-to-github/index.js index 6863749..ac23eac 100644 --- a/wordpress-to-github/index.js +++ b/wordpress-to-github/index.js @@ -504,7 +504,7 @@ const SyncEndpoint = async ( bucket[folderName] = new Map(); } - bucket[folderName].set(fileName, JSON.stringify(request.Data, null, 2)); + bucket[folderName].set(fileName, JSON.stringify(request.Data, null, 2)) return bucket; }, {}); @@ -519,10 +519,8 @@ const SyncEndpoint = async ( false ); - const reportLabel = folderName.split("/").slice(-1).join("/") || "root"; - const updateCount = `${requestsTree.length} ${ - requestsTree.length === 1 ? "update" : "updates" - }`; + const reportLabel = folderName.split("/").slice(-1).join("/") || 'root'; + const updateCount = `${requestsTree.length} ${requestsTree.length === 1 ? "update" : "updates"}`; addToReport( report, @@ -542,4 +540,4 @@ const SyncEndpoint = async ( module.exports = { SyncEndpoint -}; +}; \ No newline at end of file From 1447d776000023120c1c672fb5dad6c0e0eedb38 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Tue, 30 Nov 2021 12:00:45 -0800 Subject: [PATCH 7/8] delta reduce --- wordpress-to-github/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wordpress-to-github/index.js b/wordpress-to-github/index.js index ac23eac..7654a74 100644 --- a/wordpress-to-github/index.js +++ b/wordpress-to-github/index.js @@ -521,7 +521,7 @@ const SyncEndpoint = async ( const reportLabel = folderName.split("/").slice(-1).join("/") || 'root'; const updateCount = `${requestsTree.length} ${requestsTree.length === 1 ? "update" : "updates"}`; - + addToReport( report, await CommitIfChanged( From 4c0974f96eba5dc054c041689ea40c5c06561800 Mon Sep 17 00:00:00 2001 From: Carter Medlin Date: Wed, 1 Dec 2021 09:55:34 -0800 Subject: [PATCH 8/8] adding HideAuthorName to readme --- wordpress-to-github/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/wordpress-to-github/README.md b/wordpress-to-github/README.md index 94562d8..81a18cc 100644 --- a/wordpress-to-github/README.md +++ b/wordpress-to-github/README.md @@ -38,7 +38,8 @@ Controls how the service will place content in GitHub. This file belongs in your } ], "GeneralFilePath": "wordpress/general/general.json", - "ExcludeProperties": ["content", "_links"] + "ExcludeProperties": ["content", "_links"], + "HideAuthorName": false } } ``` @@ -55,6 +56,7 @@ Controls how the service will place content in GitHub. This file belongs in your | **`ApiRequests.ExcludeProperties`** | A collection of property keys to remove from the output. | | **`GeneralFilePath`** | The full path and filename for a `general.json` file that contains information about the whole site. | | **`ExcludeProperties`** | Which WordPress properties should we suppress in output? | +| **`HideAuthorName`** | `true` to show user number for Author output instead of Name | ## Sample output