From 6c8e139c6138f24afa674e882422a4f07f6fbc13 Mon Sep 17 00:00:00 2001 From: Akib Mohaimenur Rahman Date: Wed, 24 Feb 2021 00:03:25 -0500 Subject: [PATCH] Add new github wins action --- .github/workflows/_wins-data.yml | 36 ++++++ .../wins-data/clean-and-save-wins-data.js | 107 ++++++++++++++++++ github-actions/wins-data/package-lock.json | 26 +++++ github-actions/wins-data/package.json | 15 +++ 4 files changed, 184 insertions(+) create mode 100644 .github/workflows/_wins-data.yml create mode 100644 github-actions/wins-data/clean-and-save-wins-data.js create mode 100644 github-actions/wins-data/package-lock.json create mode 100644 github-actions/wins-data/package.json diff --git a/.github/workflows/_wins-data.yml b/.github/workflows/_wins-data.yml new file mode 100644 index 0000000000..7e40e1b060 --- /dev/null +++ b/.github/workflows/_wins-data.yml @@ -0,0 +1,36 @@ +name: Update wins data JS +# Run this action every day at 3am Pacific (11 am UTC) +on: + schedule: + - cron: '30 11 * * *' + +jobs: + github_data: + runs-on: ubuntu-latest + if: github.repository == 'hackforla/website' + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: GitHub Action for install npm dependencies + uses: bahmutov/npm-install@v1 + with: + working-directory: ./github-actions/wins-data + + - name: Generate github-data.json + env: + token: ${{ secrets.GITHUB_TOKEN }} + run: node github-actions/wins-data/clean-and-save-wins-data.js + + + - name: Auto Commit + uses: stefanzweifel/git-auto-commit-action@v3.0.0 + with: + # Optional glob pattern of files which should be added to the commit + file_pattern: _data/_wins-data.json + + commit_message: Update contributor and language data + + # Commit author settings + commit_author: GitHub Actions Bot diff --git a/github-actions/wins-data/clean-and-save-wins-data.js b/github-actions/wins-data/clean-and-save-wins-data.js new file mode 100644 index 0000000000..8693fa5e68 --- /dev/null +++ b/github-actions/wins-data/clean-and-save-wins-data.js @@ -0,0 +1,107 @@ +const axios = require("axios"); +const fs = require("fs"); +const core = require("@actions/core"); + +const PATH_TO_WRITE_FILE = "_data/_wins-data.json"; + +const BASE_API_URL = "https://sheets.googleapis.com/v4/spreadsheets"; +const SPREAD_SHEET_ID = "1zkr_dEyiT-WTksUkVyL4jYQuYC5_YvJCLrUIfBf1CeE"; +const SHEETS_A1_RANGE = "'Sheet1'!A1:O"; +const WINS_API_KEY = + process.env.WINS_API_KEY || "AIzaSyDsY0jyeGRnVYmQco1Pt0fW3iDyfHiyYZE"; //Do not remove hard coded key on pull request +const QUERY_PARAMETERS = `key=${WINS_API_KEY}`; + +const REQUEST_URL = `${BASE_API_URL}/${SPREAD_SHEET_ID}/values/${SHEETS_A1_RANGE}?${QUERY_PARAMETERS}`; + +/** + * Retrieves data from the wins excel sheet provided a valid excel url + * @param {URL} url + * @return {Object} Returns and array of arrays object, where the array at index 0 contains the data from the header row in the excel sheet and all subsequent arrays contain personal informations. + */ +async function sendGetRequest(url) { + try { + const resp = await axios.get(url); + return resp.data.values; + } catch (error) { + core.setFailed(error.message); + } +} + +/** + * @param {Array} keys Where keys are the keys that are going to be assigned to each item in the values array + * @param {Object} persons Where values is an array of array + * @param {Array} unwanted_keys An array of keys that you do not want your object to contain + * @return {Object} Returns and array of objects + */ +function create_container_object(keys, persons, unwanted_keys) { + try { + const container = []; + const persons_container = []; + + //get rid of item if display column does not exist + persons.forEach(function(person){ + let display_column_number = 14 + if (person.length > display_column_number -1) { + persons_container.push(person) + } + }) + + persons_container.map((person) => { + //If the last element in the person array is not false + if (person[person.length - 1] != "FALSE") { + const person_obj = {}; + for (const [index, key] of keys.entries()) { + person_obj[key] = person[index].replace('""', ""); + } + unwanted_keys.length > 0 && + unwanted_keys.forEach((key) => { + delete person_obj[key]; + }); + container.push(person_obj); + } + }); + + return container; + } catch (error) { + core.setFailed(error.message); + } +} + +/** + * + * @param {Object} container Writes the container to a file specified by path + * @param {String} path Path to the file destination + */ +function writeData(container, path) { + try { + fs.writeFileSync(path, JSON.stringify(container, null, 2)); + } catch (error) { + core.setFailed(error.message); + } +} + +(async function main() { + const time = new Date().toTimeString(); + core.setOutput("Running at: ", time); + + //data array from excel sheet + const excel_data_array = await sendGetRequest(REQUEST_URL); + + //An array of all the headers that are going to be used to create keys for each person object + const keys = excel_data_array[0]; + + //An array of person data + const persons = excel_data_array.slice(1); + + const unwanted_keys = ["display", "email"]; + + //an array of key,value persons object + const write_ready_container = create_container_object( + keys, + persons, + unwanted_keys + ); + + //write data to file + writeData(write_ready_container, PATH_TO_WRITE_FILE); +})(); diff --git a/github-actions/wins-data/package-lock.json b/github-actions/wins-data/package-lock.json new file mode 100644 index 0000000000..2d2937622d --- /dev/null +++ b/github-actions/wins-data/package-lock.json @@ -0,0 +1,26 @@ +{ + "name": "wins-cleaner", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@actions/core": { + "version": "1.2.6", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.2.6.tgz", + "integrity": "sha512-ZQYitnqiyBc3D+k7LsgSBmMDVkOVidaagDG7j3fOym77jNunWRuYx7VSHa9GNfFZh+zh61xsCjRj4JxMZlDqTA==" + }, + "axios": { + "version": "0.21.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", + "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "requires": { + "follow-redirects": "^1.10.0" + } + }, + "follow-redirects": { + "version": "1.13.2", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.2.tgz", + "integrity": "sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA==" + } + } +} diff --git a/github-actions/wins-data/package.json b/github-actions/wins-data/package.json new file mode 100644 index 0000000000..b3dbb559cb --- /dev/null +++ b/github-actions/wins-data/package.json @@ -0,0 +1,15 @@ +{ + "name": "wins-cleaner", + "version": "1.0.0", + "description": "", + "main": "wins-cleaner.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@actions/core": "^1.2.6", + "axios": "^0.21.1" + } +}