Skip to content

Commit

Permalink
Implemented check for changes in Wins-form (Responses) spreadsheet wi…
Browse files Browse the repository at this point in the history
…thin Wins Data automation (#7409)

* Added workflow_dispatch trigger to schedule-fri-0700.yml

* Cloned gh-requests Code.js from Google

* Cloned wins-form-responses Code.js from Google

* Replaced references to `elizabethhonest ` in `gh-requests/Code.js` with `iancooperman`

* Fixed misspelling in own name, causing authentication error

* Implemented deep comparison of google sheet with existing wins form data in respository

* Rewords gh-requests code to returns full wins instead of just sha

* Reverted url changes made for testing purposes

* Removed large, commented-out section of unused code I wrote

* Removed large, commented-out section of code I wrote

* Revert "Removed large, commented-out section of code I wrote" duplicate that accidentally committed changes to appsscript.json

This reverts commit 1b474c1.

* Changed `gh-requests/Code.js line 62 back to elizabethhonest

* Made log message resulting from SHA retrieval failure more descriptive

* Revert "Cloned wins-form-responses Code.js from Google"

This reverts commit 003c436.
  • Loading branch information
iancooperman authored Oct 18, 2024
1 parent de0301e commit b3d4986
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
19 changes: 14 additions & 5 deletions google-apps-scripts/gh-requests/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions google-apps-scripts/wins-form-responses/Code.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit b3d4986

Please sign in to comment.