From d70012072e8bb13dd34379f311c32ae59e0a1eba Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Sun, 20 Jun 2021 17:05:09 +0530 Subject: [PATCH 01/15] initial script to generate new spreadsheet layers --- scripts/newLayer.sh | 32 ++++++++++++++++++++++++++++++++ scripts/sample.input.json | 4 ++++ 2 files changed, 36 insertions(+) create mode 100755 scripts/newLayer.sh create mode 100644 scripts/sample.input.json diff --git a/scripts/newLayer.sh b/scripts/newLayer.sh new file mode 100755 index 000000000..7a9e173aa --- /dev/null +++ b/scripts/newLayer.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +root=$(git rev-parse --show-toplevel) + +layerName=$(cat input.json | jq -r .name) +url=$(cat input.json | jq -r .url) + +temp=$' +L.layerGroup.'$layerName' = function (options) { + return new L.SpreadsheetLayer({ + url: "'$url'", // String url of data sheet + lat: "Latitude", // name of latitude column + lon: "Longitude", // name of longitude column + generatePopup: function () { + // function used to create content of popups + }, + imageOptions: { + icon: L.icon.mapKnitterIcon(), + }, // optional, defaults to blank + // sheetNum: // optional, defaults to 0 (first sheet) + }); +}; +' + +# do it +echo "$temp" >$root/src/$layerName.js +echo "require('./$layerName.js');" >>$root/src/leafletEnvironmentalLayers.js + +sed -i "0,/\(.*'\)/s//\1, '$layerName'/" $root/src/AllLayers.js +sed -i "0,/\(.*'\)/s//\1, '$layerName'/" $root/example/oneLinerCodeExample.html + +grunt build diff --git a/scripts/sample.input.json b/scripts/sample.input.json new file mode 100644 index 000000000..3d3cea7d7 --- /dev/null +++ b/scripts/sample.input.json @@ -0,0 +1,4 @@ +{ + "name": "newLayer", + "url": "https://docs.google.com/spreadsheets/d/1AR2KRuvxgruqLSCzJoIWxcyLDfPAE3tCifQthTHhpFo/" +} \ No newline at end of file From 6a8bcea2dc0bd4e2be3a42c06d4d1dfcaa13c933 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Mon, 21 Jun 2021 13:23:25 +0530 Subject: [PATCH 02/15] introduce base code for spreadsheet based layers --- src/info.json | 131 ++++++++++++++++++++++++------ src/leafletEnvironmentalLayers.js | 1 + src/spreadsheetLayers/index.js | 17 ++++ src/spreadsheetLayers/layers.json | 6 ++ 4 files changed, 129 insertions(+), 26 deletions(-) create mode 100644 src/spreadsheetLayers/index.js create mode 100644 src/spreadsheetLayers/layers.json diff --git a/src/info.json b/src/info.json index c1957adca..80392d569 100644 --- a/src/info.json +++ b/src/info.json @@ -24,7 +24,7 @@ "fractracker": { "name": "FracTracker", "url": "https://www.fractracker.org/", - "api_url": "https://spreadsheets.google.com/feeds/list/19j4AQmjWuELuzn1GIn0TFRcK42HjdHF_fsIa8jtM1yw/o4rmdye/public/values?alt=json" , + "api_url": "https://spreadsheets.google.com/feeds/list/19j4AQmjWuELuzn1GIn0TFRcK42HjdHF_fsIa8jtM1yw/o4rmdye/public/values?alt=json", "data": { "type": "", "disclaimer": "Data from agency data sets to crowd-sourced data" @@ -54,7 +54,11 @@ }, "indigenousLands": { "name": "Indigenous Lands", - "layers": ["Territories", "Languages", "Treaties"], + "layers": [ + "Territories", + "Languages", + "Treaties" + ], "url": "https://native-land.ca/", "data": { "type": "Historical data", @@ -70,7 +74,17 @@ }, "justiceMap": { "name": "Justice Map", - "layers": ["income", "americanIndian", "asian", "black", "multi", "hispanic", "nonWhite", "white", "plurality"], + "layers": [ + "income", + "americanIndian", + "asian", + "black", + "multi", + "hispanic", + "nonWhite", + "white", + "plurality" + ], "url": "http://www.justicemap.org/index.php?giAdvanced=0#instruction_div", "data": { "type": "", @@ -81,9 +95,15 @@ "icon": "#f3a482", "extents": { "bounds": [ - [16.97274101999902, -172.880859375], - [71.27259471233448, -38.05664062500001] - ], + [ + 16.97274101999902, + -172.880859375 + ], + [ + 71.27259471233448, + -38.05664062500001 + ] + ], "minZoom": 5, "maxZoom": 15 } @@ -145,7 +165,12 @@ }, "openInfraMap": { "name": "Open Infra Map", - "layers": ["Power", "Petroleum", "Telecom", "Water"], + "layers": [ + "Power", + "Petroleum", + "Telecom", + "Water" + ], "url": "https://openinframap.org/about.html", "contribute_url": "https://openinframap.org/about.html", "data": { @@ -170,7 +195,20 @@ }, "openWeatherMap": { "name": "Open Weather Map", - "layers": ["clouds", "cloudsClassic", "precipitation", "precipitationClassic", "rain", "rainClassic", "snow", "pressure", "pressureContour", "temperature", "wind", "city"], + "layers": [ + "clouds", + "cloudsClassic", + "precipitation", + "precipitationClassic", + "rain", + "rainClassic", + "snow", + "pressure", + "pressureContour", + "temperature", + "wind", + "city" + ], "url": "https://openweathermap.org/guide", "data": { "type": "RT", @@ -209,9 +247,15 @@ "icon": "#b52822", "extents": { "bounds": [ - [16.97274101999902, -172.880859375], - [71.27259471233448, -38.05664062500001] - ], + [ + 16.97274101999902, + -172.880859375 + ], + [ + 71.27259471233448, + -38.05664062500001 + ] + ], "minZoom": 5, "maxZoom": 15 } @@ -232,9 +276,9 @@ "purpleLayer": {}, "purpleairmarker": { "extents": { - "minZoom": 8, - "maxZoom": 15 - } + "minZoom": 8, + "maxZoom": 15 + } } } }, @@ -267,9 +311,15 @@ "icon": "#6ccc00", "extents": { "bounds": [ - [60.54377524118842, -21.708984375000004], - [13.66733825965496, -148.27148437500003] - ], + [ + 60.54377524118842, + -21.708984375000004 + ], + [ + 13.66733825965496, + -148.27148437500003 + ] + ], "minZoom": 5, "maxZoom": 15 } @@ -287,9 +337,15 @@ "icon": "#4f4fff", "extents": { "bounds": [ - [42.2102, -72.0204], - [41.2272, -70.9618] - ], + [ + 42.2102, + -72.0204 + ], + [ + 41.2272, + -70.9618 + ] + ], "minZoom": 6, "maxZoom": 18 } @@ -307,9 +363,15 @@ "icon": "#739ccf", "extents": { "bounds": [ - [43.689721907017194, -93.12835693359376], - [45.60250901510299, -89.54956054687501] - ], + [ + 43.689721907017194, + -93.12835693359376 + ], + [ + 45.60250901510299, + -89.54956054687501 + ] + ], "minZoom": 10, "maxZoom": 15 } @@ -326,11 +388,28 @@ "icon": "#096", "extents": { "bounds": [ - [-44.087585028245165, -148.88671875000003], - [76.63922560965888, 140.62500000000003] - ], + [ + -44.087585028245165, + -148.88671875000003 + ], + [ + 76.63922560965888, + 140.62500000000003 + ] + ], "minZoom": 3, "maxZoom": 18 } + }, + "testSpreadsheetLayer": { + "name": "testSpreadsheetLayer", + "url": "", + "data": { + "type": "", + "disclaimer": "" + }, + "description": "", + "layer_desc": "Spreadsheet data", + "icon": "#cc12cc" } } \ No newline at end of file diff --git a/src/leafletEnvironmentalLayers.js b/src/leafletEnvironmentalLayers.js index 050be2e02..28fdcac5c 100644 --- a/src/leafletEnvironmentalLayers.js +++ b/src/leafletEnvironmentalLayers.js @@ -13,3 +13,4 @@ require('./PLpeopleLayer.js'); require('./layercode.js'); require('./eonetFiresLayer'); require('./AllLayers.js'); +require('./spreadsheetLayers') diff --git a/src/spreadsheetLayers/index.js b/src/spreadsheetLayers/index.js new file mode 100644 index 000000000..27753de02 --- /dev/null +++ b/src/spreadsheetLayers/index.js @@ -0,0 +1,17 @@ +const layers = require("./layers.json"); + +for (const layer of layers) { + //Evaluate based on dynamic data + let newLayer = function (options) { + return new L.SpreadsheetLayer({ + url: layer.url, + lat: "Latitude", + lon: "Longitude", + generatePopup: function () {}, + imageOptions: { + icon: L.icon.mapKnitterIcon(), + }, + }); + }; + eval("L.layerGroup." + layer.name + "=newLayer"); +} diff --git a/src/spreadsheetLayers/layers.json b/src/spreadsheetLayers/layers.json new file mode 100644 index 000000000..dfb336931 --- /dev/null +++ b/src/spreadsheetLayers/layers.json @@ -0,0 +1,6 @@ +[ + { + "name": "testSpreadsheetLayer", + "url": "https://docs.google.com/spreadsheets/d/1AR2KRuvxgruqLSCzJoIWxcyLDfPAE3tCifQthTHhpFo/" + } +] \ No newline at end of file From 25084a262341328a2cbcb7104e94ed98c3f01ca0 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Mon, 21 Jun 2021 13:26:39 +0530 Subject: [PATCH 03/15] new js based script for generating layers --- scripts/genSpreadsheetLayer.js | 69 ++++++++++++++++++++++++++++++++++ scripts/newLayer.sh | 32 ---------------- scripts/sample.input.json | 5 ++- 3 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 scripts/genSpreadsheetLayer.js delete mode 100755 scripts/newLayer.sh diff --git a/scripts/genSpreadsheetLayer.js b/scripts/genSpreadsheetLayer.js new file mode 100644 index 000000000..d9efba15f --- /dev/null +++ b/scripts/genSpreadsheetLayer.js @@ -0,0 +1,69 @@ +const fs = require("fs"); +const path = require("path"); + +function generateSpreadsheetLayer(layerData) { + fs.readFile( + path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), + "utf8", + (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let spreadsheetLayers = JSON.parse(data); + spreadsheetLayers.push({ + name: layerData.name, + url: layerData.url, + }); + let newSpreadsheetLayers = JSON.stringify(spreadsheetLayers, null, 2); + fs.writeFile( + "../src/spreadsheetLayers/layers.json", + newSpreadsheetLayers, + "utf8", + (err) => { + if (err) { + console.log(`Error writing file: ${err}`); + } else { + console.log(`File is written successfully!`); + } + } + ); + } + } + ); + + fs.readFile( + path.resolve(__dirname, "../src/info.json"), + "utf8", + (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let layerInfo = JSON.parse(data); + layerInfo[layerData.name] = { + name: layerData.name, + url: "", + data: { + type: "", + disclaimer: "", + }, + description: "", + layer_desc: layerData.description, + icon: "#cc12cc", + }; + let newLayerInfo = JSON.stringify(layerInfo, null, 2); + fs.writeFile("../src/info.json", newLayerInfo, "utf8", (err) => { + if (err) { + console.log(`Error writing file: ${err}`); + } else { + console.log(`File is written successfully!`); + } + }); + } + } + ); +} + +let layerData = JSON.parse(fs.readFileSync("input.json", "utf8")); +generateSpreadsheetLayer(layerData); diff --git a/scripts/newLayer.sh b/scripts/newLayer.sh deleted file mode 100755 index 7a9e173aa..000000000 --- a/scripts/newLayer.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/bin/bash - -root=$(git rev-parse --show-toplevel) - -layerName=$(cat input.json | jq -r .name) -url=$(cat input.json | jq -r .url) - -temp=$' -L.layerGroup.'$layerName' = function (options) { - return new L.SpreadsheetLayer({ - url: "'$url'", // String url of data sheet - lat: "Latitude", // name of latitude column - lon: "Longitude", // name of longitude column - generatePopup: function () { - // function used to create content of popups - }, - imageOptions: { - icon: L.icon.mapKnitterIcon(), - }, // optional, defaults to blank - // sheetNum: // optional, defaults to 0 (first sheet) - }); -}; -' - -# do it -echo "$temp" >$root/src/$layerName.js -echo "require('./$layerName.js');" >>$root/src/leafletEnvironmentalLayers.js - -sed -i "0,/\(.*'\)/s//\1, '$layerName'/" $root/src/AllLayers.js -sed -i "0,/\(.*'\)/s//\1, '$layerName'/" $root/example/oneLinerCodeExample.html - -grunt build diff --git a/scripts/sample.input.json b/scripts/sample.input.json index 3d3cea7d7..14080d8e7 100644 --- a/scripts/sample.input.json +++ b/scripts/sample.input.json @@ -1,4 +1,5 @@ { - "name": "newLayer", + "name": "testSpreadsheetLayer", + "description": "Spreadsheet data", "url": "https://docs.google.com/spreadsheets/d/1AR2KRuvxgruqLSCzJoIWxcyLDfPAE3tCifQthTHhpFo/" -} \ No newline at end of file +} \ No newline at end of file From 982f0ae7a00d07c806957874f2a242eadd1c7182 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Mon, 28 Jun 2021 01:22:45 +0530 Subject: [PATCH 04/15] refactor code --- scripts/genSpreadsheetLayer.js | 102 ++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/scripts/genSpreadsheetLayer.js b/scripts/genSpreadsheetLayer.js index d9efba15f..3b4e65d38 100644 --- a/scripts/genSpreadsheetLayer.js +++ b/scripts/genSpreadsheetLayer.js @@ -1,24 +1,23 @@ const fs = require("fs"); const path = require("path"); -function generateSpreadsheetLayer(layerData) { - fs.readFile( - path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), - "utf8", - (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let spreadsheetLayers = JSON.parse(data); - spreadsheetLayers.push({ - name: layerData.name, - url: layerData.url, - }); - let newSpreadsheetLayers = JSON.stringify(spreadsheetLayers, null, 2); +function generateSpreadsheetLayer(layerData, layersPath, infoPath, confirm) { + let newSpreadsheetLayers, newLayerInfo; + fs.readFile(path.resolve(__dirname, layersPath), "utf8", (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let spreadsheetLayers = JSON.parse(data); + spreadsheetLayers.push({ + name: layerData.name, + url: layerData.url, + }); + newSpreadsheetLayers = spreadsheetLayers; + if (confirm) { fs.writeFile( "../src/spreadsheetLayers/layers.json", - newSpreadsheetLayers, + JSON.stringify(spreadsheetLayers, null, 2), "utf8", (err) => { if (err) { @@ -30,40 +29,51 @@ function generateSpreadsheetLayer(layerData) { ); } } - ); + }); - fs.readFile( - path.resolve(__dirname, "../src/info.json"), - "utf8", - (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let layerInfo = JSON.parse(data); - layerInfo[layerData.name] = { - name: layerData.name, - url: "", - data: { - type: "", - disclaimer: "", - }, - description: "", - layer_desc: layerData.description, - icon: "#cc12cc", - }; - let newLayerInfo = JSON.stringify(layerInfo, null, 2); - fs.writeFile("../src/info.json", newLayerInfo, "utf8", (err) => { - if (err) { - console.log(`Error writing file: ${err}`); - } else { - console.log(`File is written successfully!`); + fs.readFile(path.resolve(__dirname, infoPath), "utf8", (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let layerInfo = JSON.parse(data); + layerInfo[layerData.name] = { + name: layerData.name, + url: "", + data: { + type: "", + disclaimer: "", + }, + description: "", + layer_desc: layerData.description, + icon: "#cc12cc", + }; + newLayerInfo = layerInfo; + if (confirm) { + fs.writeFile( + "../src/info.json", + JSON.stringify(layerInfo, null, 2), + "utf8", + (err) => { + if (err) { + console.log(`Error writing file: ${err}`); + } else { + console.log(`File is written successfully!`); + } } - }); + ); } } - ); + }); + return { newSpreadsheetLayers, newLayerInfo }; } +exports.generateSpreadsheetLayer = (data) => generateSpreadsheetLayer(data); + let layerData = JSON.parse(fs.readFileSync("input.json", "utf8")); -generateSpreadsheetLayer(layerData); +generateSpreadsheetLayer( + layerData, + "../src/spreadsheetLayers/layers.json", + "../src/info.json", + true +); From 657b1091aec8dd46803f349e85114f16c5c29419 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Mon, 28 Jun 2021 02:05:52 +0530 Subject: [PATCH 05/15] initial gh action for generating layer --- .github/workflows/generate-layer.yml | 16 +++++++++ scripts/actionGenLayer.js | 49 ++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 .github/workflows/generate-layer.yml create mode 100644 scripts/actionGenLayer.js diff --git a/.github/workflows/generate-layer.yml b/.github/workflows/generate-layer.yml new file mode 100644 index 000000000..ccb4fe665 --- /dev/null +++ b/.github/workflows/generate-layer.yml @@ -0,0 +1,16 @@ +name: "New Spreadsheet Layer" +on: + issue_comment: + types: [created, edited] + +jobs: + main: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Checkout and install dependencies + run: npm install @actions/core @actions/github marked + + - name: Checks the PR title + run: node ./scripts/actionGenLayer diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js new file mode 100644 index 000000000..83599df59 --- /dev/null +++ b/scripts/actionGenLayer.js @@ -0,0 +1,49 @@ +const core = require("@actions/core"); +const github = require("@actions/github"); +const marked = require("marked"); +const { generateSpreadsheetLayer } = require("./genSpreadsheetLayer"); + +const commentPrefix = "@github-actions run"; + +async function run() { + const comment = github.context.payload.comment.body; + if (!comment.startsWith(commentPrefix)) { + console.log( + `HINT: Comment-run is triggered when your comment start with "${commentPrefix}"` + ); + return; + } + const tokens = marked.lexer(comment); + for (const token of tokens) { + if (token.type === "code") { + if (token.lang === "json") { + const layerData = JSON.parse(token.text); + await generateSpreadsheetLayer( + layerData, + "../src/spreadsheetLayers/layers.json", + "../src/info.json", + true + ); + } + } + } + const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; + const gitUserName = "github-actions[bot]"; + const prBranchName = `new-gen-layer/${layerData.name}`; + + const baseBranchName = github.context.payload.repository.default_branch; + exec(`git config --global user.email "${gitUserEmail}"`); + exec(`git config --global user.name "${gitUserName}"`); + exec(`git fetch --all`); + exec(`git checkout ${baseBranchName}`); + exec(`git checkout -b ${prBranchName}`); + + exec("git status"); + exec("git add ."); + exec(`git commit -m "layer: new layer ${layerData.name}"`); + exec(`git push -fu origin ${prBranchName}`); +} + +run().catch((e) => { + core.setFailed(e); +}); From 6399cea5855e19d0b4aec3a0f49a0fdb056cf114 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Thu, 1 Jul 2021 01:24:33 +0530 Subject: [PATCH 06/15] refactor actual script into a new file seperated the function in the existing file so as to make it compatible with both action and manual script --- scripts/genSpreadsheetLayer.js | 67 ++++++++++++++++------------------ scripts/manualGenLayer.js | 6 +++ 2 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 scripts/manualGenLayer.js diff --git a/scripts/genSpreadsheetLayer.js b/scripts/genSpreadsheetLayer.js index 3b4e65d38..a2e865e78 100644 --- a/scripts/genSpreadsheetLayer.js +++ b/scripts/genSpreadsheetLayer.js @@ -1,37 +1,41 @@ const fs = require("fs"); const path = require("path"); -function generateSpreadsheetLayer(layerData, layersPath, infoPath, confirm) { +function generateSpreadsheetLayer(layerData, confirm) { let newSpreadsheetLayers, newLayerInfo; - fs.readFile(path.resolve(__dirname, layersPath), "utf8", (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let spreadsheetLayers = JSON.parse(data); - spreadsheetLayers.push({ - name: layerData.name, - url: layerData.url, - }); - newSpreadsheetLayers = spreadsheetLayers; - if (confirm) { - fs.writeFile( - "../src/spreadsheetLayers/layers.json", - JSON.stringify(spreadsheetLayers, null, 2), - "utf8", - (err) => { - if (err) { - console.log(`Error writing file: ${err}`); - } else { - console.log(`File is written successfully!`); + fs.readFile( + path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), + "utf8", + (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let spreadsheetLayers = JSON.parse(data); + spreadsheetLayers.push({ + name: layerData.name, + url: layerData.url, + }); + newSpreadsheetLayers = spreadsheetLayers; + if (confirm) { + fs.writeFile( + path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), + JSON.stringify(spreadsheetLayers, null, 2), + "utf8", + (err) => { + if (err) { + console.log(`Error writing file: ${err}`); + } else { + console.log(`File is written successfully!`); + } } - } - ); + ); + } } } - }); + ); - fs.readFile(path.resolve(__dirname, infoPath), "utf8", (err, data) => { + fs.readFile("../src/info.json", "utf8", (err, data) => { if (err) { console.log(`Error reading file from disk: ${err}`); } else { @@ -68,12 +72,5 @@ function generateSpreadsheetLayer(layerData, layersPath, infoPath, confirm) { return { newSpreadsheetLayers, newLayerInfo }; } -exports.generateSpreadsheetLayer = (data) => generateSpreadsheetLayer(data); - -let layerData = JSON.parse(fs.readFileSync("input.json", "utf8")); -generateSpreadsheetLayer( - layerData, - "../src/spreadsheetLayers/layers.json", - "../src/info.json", - true -); +exports.generateSpreadsheetLayer = (...data) => + generateSpreadsheetLayer(...data); diff --git a/scripts/manualGenLayer.js b/scripts/manualGenLayer.js new file mode 100644 index 000000000..5ff17493c --- /dev/null +++ b/scripts/manualGenLayer.js @@ -0,0 +1,6 @@ +const path = require("path"); +const fs = require("fs"); +const { generateSpreadsheetLayer } = require("./genSpreadsheetLayer"); + +let layerData = JSON.parse(fs.readFileSync("input.json", "utf8")); +generateSpreadsheetLayer(layerData, true); From e46dc2d9ac18925bf49fbc19b58a8905426cea08 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Thu, 1 Jul 2021 23:48:57 +0530 Subject: [PATCH 07/15] update action script --- scripts/actionGenLayer.js | 54 ++++++++++++++++----------- scripts/genSpreadsheetLayer.js | 67 ++++++++++++++++++---------------- 2 files changed, 69 insertions(+), 52 deletions(-) diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index 83599df59..e8121e9b5 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -1,10 +1,15 @@ const core = require("@actions/core"); const github = require("@actions/github"); const marked = require("marked"); +const { execSync } = require("child_process"); const { generateSpreadsheetLayer } = require("./genSpreadsheetLayer"); const commentPrefix = "@github-actions run"; +function exec(cmd) { + console.log(execSync(cmd).toString()); +} + async function run() { const comment = github.context.payload.comment.body; if (!comment.startsWith(commentPrefix)) { @@ -18,30 +23,37 @@ async function run() { if (token.type === "code") { if (token.lang === "json") { const layerData = JSON.parse(token.text); - await generateSpreadsheetLayer( - layerData, - "../src/spreadsheetLayers/layers.json", - "../src/info.json", - true - ); + console.log(layerData); + generateSpreadsheetLayer(layerData, true); + const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; + const gitUserName = "github-actions[bot]"; + const prBranchName = `new-gen-layer/${layerData.name}`; + + const baseBranchName = github.context.payload.repository.default_branch; + exec(`git config --global user.email "${gitUserEmail}"`); + exec(`git config --global user.name "${gitUserName}"`); + exec(`git fetch --all`); + exec(`git checkout ${baseBranchName}`); + exec(`git checkout -b ${prBranchName}`); + + exec("git status"); + exec("git add ."); + exec(`git commit -m "layer: new layer ${layerData.name}"`); + exec(`git push -fu origin ${prBranchName}`); + + //Set token secret + // const githubClient = new github(githubToken) + // await githubClient.pulls.create({ + // base: baseBranchName, + // head: prBranchName, + // owner: context.repo.owner, + // repo: context.repo.repo, + // title: `new-layer: ${layerData.name}`, + // body: `Ref ${github.context.payload.comment.url}`, + // }); } } } - const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; - const gitUserName = "github-actions[bot]"; - const prBranchName = `new-gen-layer/${layerData.name}`; - - const baseBranchName = github.context.payload.repository.default_branch; - exec(`git config --global user.email "${gitUserEmail}"`); - exec(`git config --global user.name "${gitUserName}"`); - exec(`git fetch --all`); - exec(`git checkout ${baseBranchName}`); - exec(`git checkout -b ${prBranchName}`); - - exec("git status"); - exec("git add ."); - exec(`git commit -m "layer: new layer ${layerData.name}"`); - exec(`git push -fu origin ${prBranchName}`); } run().catch((e) => { diff --git a/scripts/genSpreadsheetLayer.js b/scripts/genSpreadsheetLayer.js index a2e865e78..94ec0cdfe 100644 --- a/scripts/genSpreadsheetLayer.js +++ b/scripts/genSpreadsheetLayer.js @@ -2,6 +2,7 @@ const fs = require("fs"); const path = require("path"); function generateSpreadsheetLayer(layerData, confirm) { + console.log("layerData: inside function call",layerData) let newSpreadsheetLayers, newLayerInfo; fs.readFile( path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), @@ -35,40 +36,44 @@ function generateSpreadsheetLayer(layerData, confirm) { } ); - fs.readFile("../src/info.json", "utf8", (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let layerInfo = JSON.parse(data); - layerInfo[layerData.name] = { - name: layerData.name, - url: "", - data: { - type: "", - disclaimer: "", - }, - description: "", - layer_desc: layerData.description, - icon: "#cc12cc", - }; - newLayerInfo = layerInfo; - if (confirm) { - fs.writeFile( - "../src/info.json", - JSON.stringify(layerInfo, null, 2), - "utf8", - (err) => { - if (err) { - console.log(`Error writing file: ${err}`); - } else { - console.log(`File is written successfully!`); + fs.readFile( + path.resolve(__dirname, "../src/info.json"), + "utf8", + (err, data) => { + if (err) { + console.log(`Error reading file from disk: ${err}`); + } else { + // parse JSON string to JSON object + let layerInfo = JSON.parse(data); + layerInfo[layerData.name] = { + name: layerData.name, + url: "", + data: { + type: "", + disclaimer: "", + }, + description: "", + layer_desc: layerData.description, + icon: "#cc12cc", + }; + newLayerInfo = layerInfo; + if (confirm) { + fs.writeFile( + path.resolve(__dirname, "../src/info.json"), + JSON.stringify(layerInfo, null, 2), + "utf8", + (err) => { + if (err) { + console.log(`Error writing file: ${err}`); + } else { + console.log(`File is written successfully!`); + } } - } - ); + ); + } } } - }); + ); return { newSpreadsheetLayers, newLayerInfo }; } From 285042e75d5ceb271fabe4d14f8b64e25a7ac957 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Fri, 2 Jul 2021 00:42:42 +0530 Subject: [PATCH 08/15] promisify the generate layer function --- scripts/genSpreadsheetLayer.js | 121 ++++++++++++++------------------- scripts/manualGenLayer.js | 6 +- 2 files changed, 55 insertions(+), 72 deletions(-) diff --git a/scripts/genSpreadsheetLayer.js b/scripts/genSpreadsheetLayer.js index 94ec0cdfe..05ebdc8fd 100644 --- a/scripts/genSpreadsheetLayer.js +++ b/scripts/genSpreadsheetLayer.js @@ -1,79 +1,60 @@ -const fs = require("fs"); +const { writeFile, readFile } = require("fs").promises; const path = require("path"); -function generateSpreadsheetLayer(layerData, confirm) { - console.log("layerData: inside function call",layerData) +async function generateSpreadsheetLayer(layerData, confirm) { let newSpreadsheetLayers, newLayerInfo; - fs.readFile( - path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), - "utf8", - (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let spreadsheetLayers = JSON.parse(data); - spreadsheetLayers.push({ - name: layerData.name, - url: layerData.url, - }); - newSpreadsheetLayers = spreadsheetLayers; - if (confirm) { - fs.writeFile( - path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), - JSON.stringify(spreadsheetLayers, null, 2), - "utf8", - (err) => { - if (err) { - console.log(`Error writing file: ${err}`); - } else { - console.log(`File is written successfully!`); - } - } - ); - } - } + try { + const data = await readFile( + path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), + "utf8" + ); + let spreadsheetLayers = JSON.parse(data); + spreadsheetLayers.push({ + name: layerData.name, + url: layerData.url, + }); + newSpreadsheetLayers = spreadsheetLayers; + if (confirm) { + await writeFile( + path.resolve(__dirname, "../src/spreadsheetLayers/layers.json"), + JSON.stringify(spreadsheetLayers, null, 2), + "utf8" + ); } - ); + console.log("Successful"); + } catch (error) { + console.error(e); + } - fs.readFile( - path.resolve(__dirname, "../src/info.json"), - "utf8", - (err, data) => { - if (err) { - console.log(`Error reading file from disk: ${err}`); - } else { - // parse JSON string to JSON object - let layerInfo = JSON.parse(data); - layerInfo[layerData.name] = { - name: layerData.name, - url: "", - data: { - type: "", - disclaimer: "", - }, - description: "", - layer_desc: layerData.description, - icon: "#cc12cc", - }; - newLayerInfo = layerInfo; - if (confirm) { - fs.writeFile( - path.resolve(__dirname, "../src/info.json"), - JSON.stringify(layerInfo, null, 2), - "utf8", - (err) => { - if (err) { - console.log(`Error writing file: ${err}`); - } else { - console.log(`File is written successfully!`); - } - } - ); - } - } + try { + const data = await readFile( + path.resolve(__dirname, "../src/info.json"), + "utf8" + ); + let layerInfo = JSON.parse(data); + layerInfo[layerData.name] = { + name: layerData.name, + url: "", + data: { + type: "", + disclaimer: "", + }, + description: "", + layer_desc: layerData.description, + icon: "#cc12cc", + }; + newLayerInfo = layerInfo; + if (confirm) { + await writeFile( + path.resolve(__dirname, "../src/info.json"), + JSON.stringify(layerInfo, null, 2), + "utf8" + ); } - ); + console.log("Successful"); + } catch (error) { + console.error(e); + } return { newSpreadsheetLayers, newLayerInfo }; } diff --git a/scripts/manualGenLayer.js b/scripts/manualGenLayer.js index 5ff17493c..5a15ef1cf 100644 --- a/scripts/manualGenLayer.js +++ b/scripts/manualGenLayer.js @@ -2,5 +2,7 @@ const path = require("path"); const fs = require("fs"); const { generateSpreadsheetLayer } = require("./genSpreadsheetLayer"); -let layerData = JSON.parse(fs.readFileSync("input.json", "utf8")); -generateSpreadsheetLayer(layerData, true); +(async () => { + let layerData = await JSON.parse(fs.readFileSync("input.json", "utf8")); + await generateSpreadsheetLayer(layerData, true); +})(); From 233e552f69271d0afcdd758037e330029489f019 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Fri, 2 Jul 2021 00:57:36 +0530 Subject: [PATCH 09/15] wait for files to be generated --- scripts/actionGenLayer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index e8121e9b5..f1be8f764 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -24,7 +24,7 @@ async function run() { if (token.lang === "json") { const layerData = JSON.parse(token.text); console.log(layerData); - generateSpreadsheetLayer(layerData, true); + await generateSpreadsheetLayer(layerData, true); const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; const gitUserName = "github-actions[bot]"; const prBranchName = `new-gen-layer/${layerData.name}`; @@ -37,7 +37,8 @@ async function run() { exec(`git checkout -b ${prBranchName}`); exec("git status"); - exec("git add ."); + exec("git add **/layers.json"); + exec("git add **/info.json"); exec(`git commit -m "layer: new layer ${layerData.name}"`); exec(`git push -fu origin ${prBranchName}`); From a54908d43c2862da1e74dfca5c0a46dad5b0c8d6 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Fri, 2 Jul 2021 01:10:57 +0530 Subject: [PATCH 10/15] Generate Pull Request after changes --- .github/workflows/generate-layer.yml | 4 +++- scripts/actionGenLayer.js | 13 +++---------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/.github/workflows/generate-layer.yml b/.github/workflows/generate-layer.yml index ccb4fe665..187b72986 100644 --- a/.github/workflows/generate-layer.yml +++ b/.github/workflows/generate-layer.yml @@ -12,5 +12,7 @@ jobs: - name: Checkout and install dependencies run: npm install @actions/core @actions/github marked - - name: Checks the PR title + - name: Generate New Spreadsheet Layer run: node ./scripts/actionGenLayer + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index f1be8f764..8de8d9d44 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -42,16 +42,9 @@ async function run() { exec(`git commit -m "layer: new layer ${layerData.name}"`); exec(`git push -fu origin ${prBranchName}`); - //Set token secret - // const githubClient = new github(githubToken) - // await githubClient.pulls.create({ - // base: baseBranchName, - // head: prBranchName, - // owner: context.repo.owner, - // repo: context.repo.repo, - // title: `new-layer: ${layerData.name}`, - // body: `Ref ${github.context.payload.comment.url}`, - // }); + exec( + `gh pr create --title "new-layer: ${layerData.name}" --body "Ref ${github.context.payload.comment.url}" --head "${prBranchName}" --base "${baseBranchName}"` + ); } } } From 73a0d5fac44964f5aa1d0a188502f94d5698f387 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Tue, 6 Jul 2021 01:28:59 +0530 Subject: [PATCH 11/15] refinements --- .../ISSUE_TEMPLATE/new-spreadsheet-layer.md | 15 ++++++++++++++ .github/workflows/generate-layer.yml | 2 ++ scripts/actionGenLayer.js | 20 ++++++++++++------- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 .github/ISSUE_TEMPLATE/new-spreadsheet-layer.md diff --git a/.github/ISSUE_TEMPLATE/new-spreadsheet-layer.md b/.github/ISSUE_TEMPLATE/new-spreadsheet-layer.md new file mode 100644 index 000000000..9487c90c2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/new-spreadsheet-layer.md @@ -0,0 +1,15 @@ +--- +name: New Spreadsheet Layer +about: Use this template for proposing new spreadsheet layer +title: "[New Spreadsheet Layer]: " +labels: new layer +--- +## New Spreadsheet Layer + +Please describe the purpose of this layer + +## Layer Data + +```json + +``` \ No newline at end of file diff --git a/.github/workflows/generate-layer.yml b/.github/workflows/generate-layer.yml index 187b72986..61b7bc744 100644 --- a/.github/workflows/generate-layer.yml +++ b/.github/workflows/generate-layer.yml @@ -2,6 +2,8 @@ name: "New Spreadsheet Layer" on: issue_comment: types: [created, edited] + issues: + types: [opened] jobs: main: diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index 8de8d9d44..70ac2fb60 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -11,12 +11,18 @@ function exec(cmd) { } async function run() { - const comment = github.context.payload.comment.body; - if (!comment.startsWith(commentPrefix)) { - console.log( - `HINT: Comment-run is triggered when your comment start with "${commentPrefix}"` - ); - return; + let comment; + + if (github.context.payload.action == "opened") { + comment = github.context.payload.issue.body; + } else { + comment = github.context.payload.comment.body; + if (!comment.startsWith(commentPrefix)) { + console.log( + `HINT: Comment-run is triggered when your comment start with "${commentPrefix}"` + ); + return; + } } const tokens = marked.lexer(comment); for (const token of tokens) { @@ -43,7 +49,7 @@ async function run() { exec(`git push -fu origin ${prBranchName}`); exec( - `gh pr create --title "new-layer: ${layerData.name}" --body "Ref ${github.context.payload.comment.url}" --head "${prBranchName}" --base "${baseBranchName}"` + `gh pr create --title "new-layer: ${layerData.name}" --body "Ref ${github.context.payload.issue.html_url}" --head "${prBranchName}" --base "${baseBranchName}"` ); } } From b1645e03d94fa3c77898437f43e0aa80c7f299be Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Wed, 14 Jul 2021 12:20:52 +0530 Subject: [PATCH 12/15] Handle names with spaces and exec grunt build --- scripts/actionGenLayer.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index 70ac2fb60..1136de4a5 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -29,12 +29,14 @@ async function run() { if (token.type === "code") { if (token.lang === "json") { const layerData = JSON.parse(token.text); + //Handle names with spaces + layerData.name = layerData.split(" ").join("_"); console.log(layerData); await generateSpreadsheetLayer(layerData, true); const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; const gitUserName = "github-actions[bot]"; const prBranchName = `new-gen-layer/${layerData.name}`; - + exec("grunt build"); const baseBranchName = github.context.payload.repository.default_branch; exec(`git config --global user.email "${gitUserEmail}"`); exec(`git config --global user.name "${gitUserName}"`); From b9f580767b5b21ca60ec668ef2767e88c37f6ee2 Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Wed, 14 Jul 2021 12:28:42 +0530 Subject: [PATCH 13/15] Post comment on error --- scripts/actionGenLayer.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index 1136de4a5..0d44c18bb 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -59,5 +59,15 @@ async function run() { } run().catch((e) => { - core.setFailed(e); + try { + exec( + `git config --global user.email "github-actions[bot]@users.noreply.github.com"` + ); + exec(`git config --global user.name "github-actions[bot]"`); + exec( + `gh issue comment ${github.context.payload.issue.html_url} --body 'Action Failed to execute. \n > This is auto-generared'` + ); + } finally { + core.setFailed(e); + } }); From 112eb6b49936f414de98a857842cd5919c4d877a Mon Sep 17 00:00:00 2001 From: daemon1024 Date: Wed, 14 Jul 2021 13:02:04 +0530 Subject: [PATCH 14/15] Fix name handling --- scripts/actionGenLayer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/actionGenLayer.js b/scripts/actionGenLayer.js index 0d44c18bb..8ee3f8102 100644 --- a/scripts/actionGenLayer.js +++ b/scripts/actionGenLayer.js @@ -30,7 +30,7 @@ async function run() { if (token.lang === "json") { const layerData = JSON.parse(token.text); //Handle names with spaces - layerData.name = layerData.split(" ").join("_"); + layerData.name = layerData.name.split(" ").join("_"); console.log(layerData); await generateSpreadsheetLayer(layerData, true); const gitUserEmail = "github-actions[bot]@users.noreply.github.com"; @@ -47,6 +47,7 @@ async function run() { exec("git status"); exec("git add **/layers.json"); exec("git add **/info.json"); + exec("git add **/LeafletEnvironmentalLayers.js -f"); exec(`git commit -m "layer: new layer ${layerData.name}"`); exec(`git push -fu origin ${prBranchName}`); From 7a1b6b7bd40d274eabc6fdfe9fbe3eec2a4c30bd Mon Sep 17 00:00:00 2001 From: Barun Acharya <47106543+daemon1024@users.noreply.github.com> Date: Thu, 5 Aug 2021 11:07:04 +0530 Subject: [PATCH 15/15] Use GITHUB_TOKEN from repository --- .github/workflows/generate-layer.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/generate-layer.yml b/.github/workflows/generate-layer.yml index 61b7bc744..430a11d49 100644 --- a/.github/workflows/generate-layer.yml +++ b/.github/workflows/generate-layer.yml @@ -17,4 +17,4 @@ jobs: - name: Generate New Spreadsheet Layer run: node ./scripts/actionGenLayer env: - GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}