diff --git a/google-apps-scripts/gh-requests/Code.js b/google-apps-scripts/gh-requests/Code.js index 2c14f52af2..2207666f78 100644 --- a/google-apps-scripts/gh-requests/Code.js +++ b/google-apps-scripts/gh-requests/Code.js @@ -12,19 +12,27 @@ const ACCEPT_HEADER = { /************************************************** FUNCTIONS USING THE FETCH REQUESTS ********************************************************************/ -// Retrieves a GitHub file's SHA -function getSHA(fileName) { +// Retrieves a GitHub file's SHA and associated wins (in JSON format) +function getWins(fileName) { const branch = "update-wins-data"; const url = `https://api.github.com/repos/elizabethhonest/website/contents/_data/external/${fileName}?ref=${branch}`; const response = getRequest_(url, ACCEPT_HEADER.Repository); if (response === false ) { console.log('SHA retrieval failed.') - return false; + return [null, null]; } + let decodedContent = JSON.parse(decode(response.body.content)); + console.log('SHA retrieved.') - return response.body.sha; + return [response.body.sha, decodedContent]; +} + +function testGetWins() { + const fileName = "_wins-data.json"; + const [sha, content] = getWins(fileName); + console.log(content.length); } // Updates Github File given a payload @@ -119,7 +127,8 @@ function setToken_() { } const doc = DocumentApp.openById(id); - documentProperties.setProperty('TOKEN', doc.getBody().getText()) + documentProperties.setProperty('TOKEN', doc.getBody().getText()); + console.log(documentProperties.getProperty(`TOKEN`)) } // Uses base64 to decode an input diff --git a/google-apps-scripts/wins-form-responses/Code.js b/google-apps-scripts/wins-form-responses/Code.js index e676a55b83..f34092f56e 100644 --- a/google-apps-scripts/wins-form-responses/Code.js +++ b/google-apps-scripts/wins-form-responses/Code.js @@ -68,20 +68,33 @@ function main() { const cleanedAndFormattedKeyValueData = JSON.stringify(sortedKeyValueData, null, 5); const encodedKeyValueData = Utilities.base64Encode(`${cleanedAndFormattedKeyValueData}`, Utilities.Charset.UTF_8); - // Retrieves latest sha of the _wins data file, which is needed for edits later + // Retrieves latest sha and content of the _wins data file, which is needed for edits later const keyValueFile = "_wins-data.json" - const keyValueSha = ghrequests.getSHA(keyValueFile); + const [keyValueSha, keyValueContent] = ghrequests.getWins(keyValueFile); if (keyValueSha === false) { - console.log('Ending script...') + console.log('Ending script due to lack of returned SHA from getWins().'); return 1; } - const writeResponse = ghrequests.updateWinsFile(keyValueFile, encodedKeyValueData, keyValueSha); - if (writeResponse === false) { + // detect any changes to Wins-form (Responses) by comparing performing a string comparison + if (!arrEq(sortedKeyValueData, keyValueContent)) { + console.log("Entry difference detected. Updating wins file..."); + const writeResponse = ghrequests.updateWinsFile(keyValueFile, encodedKeyValueData, keyValueSha); + if (writeResponse === false) { + console.log('Ending script...') + return 1; + } + } + else { + console.log("No new entry difference detected."); console.log('Ending script...') return 1; } + function arrEq(arr1, arr2) { + return JSON.stringify(arr1) === JSON.stringify(arr2); + } + /* ** This section deals with the array of data for wins-data.json ** Eventually, lines 91-107 below should be deleted (EXCLUDING createPrResponse) when the team fully migrates from using wins-data.json to _wins-data.json @@ -95,7 +108,7 @@ function main() { // Retrieves latest sha of the wins data file const arrayFile = "wins-data.json" - const arrayDataSha = ghrequests.getSHA(arrayFile); + const [arrayDataSha, arrayDataContent] = ghrequests.getWins(arrayFile); if (arrayDataSha === false) { console.log('Ending script...') return 1;