Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github Action to generate spreadsheet layers #545

Merged
merged 16 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/ISSUE_TEMPLATE/new-spreadsheet-layer.md
Original file line number Diff line number Diff line change
@@ -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
<!-- Enter the formatted data in the below block -->
```json

```
20 changes: 20 additions & 0 deletions .github/workflows/generate-layer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "New Spreadsheet Layer"
on:
issue_comment:
types: [created, edited]
issues:
types: [opened]

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: Generate New Spreadsheet Layer
run: node ./scripts/actionGenLayer
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74 changes: 74 additions & 0 deletions scripts/actionGenLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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() {
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) {
if (token.type === "code") {
if (token.lang === "json") {
const layerData = JSON.parse(token.text);
//Handle names with spaces
layerData.name = layerData.name.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}"`);
exec(`git fetch --all`);
exec(`git checkout ${baseBranchName}`);
exec(`git checkout -b ${prBranchName}`);

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}`);

exec(
`gh pr create --title "new-layer: ${layerData.name}" --body "Ref ${github.context.payload.issue.html_url}" --head "${prBranchName}" --base "${baseBranchName}"`
);
}
}
}
}

run().catch((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);
}
});