diff --git a/.github/workflows/update-monitor.yml b/.github/workflows/update-monitor.yml index 57940d4..d0c29e0 100644 --- a/.github/workflows/update-monitor.yml +++ b/.github/workflows/update-monitor.yml @@ -9,7 +9,7 @@ jobs: UpdateMonitor: runs-on: ubuntu-latest steps: - - uses: floric/repo-monitor-action@v0.0.10 + - uses: floric/repo-monitor-action@v0.0.11 with: key: bundle-size value: 1 diff --git a/action.yml b/action.yml index 51de9d4..5479d70 100644 --- a/action.yml +++ b/action.yml @@ -10,4 +10,4 @@ inputs: required: true runs: using: "node12" - main: "index.js" + main: "dist/index.js" diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..fbecd22 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,56 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = require("@actions/core"); +const github = require("@actions/github"); +async function run() { + try { + const key = core.getInput("key"); + const value = core.getInput("value"); + const token = core.getInput("token"); + const octokit = github.getOctokit(token); + const { owner, repo } = github.context.repo; + const path = `docs/data/${key}.json`; + let res = await octokit.repos.getContent({ + owner, + repo, + path, + }); + let data = null; + const isUpdate = res.status == 200; + if (res.status == 404) { + core.info(`Found new key "${key}", will create new file.`); + data = { key, type: "scalar", values: [] }; + } + else if (isUpdate) { + core.info(`Extending existing metrics for "${key}"`); + data = JSON.parse(fromBase64(res.data.content)); + } + if (data == null) { + throw new Error(`Loading or updating data for "${key}" has failed`); + } + data.values.push({ + value: Number.parseFloat(value), + isoDate: new Date().toISOString(), + }); + const content = toBase64(JSON.stringify(data)); + const updateRes = await octokit.repos.createOrUpdateFileContents({ + owner, + repo, + path, + content, + sha: isUpdate ? res.data.sha : undefined, + message: isUpdate ? "Updated metrics" : "Created metrics", + }); + core.info("Finished processing new metrics"); + } + catch (error) { + core.setFailed(error.message); + } +} +function fromBase64(content) { + return Buffer.from(content, "base64").toString("ascii"); +} +function toBase64(content) { + return Buffer.from(content).toString("base64"); +} +run(); diff --git a/index.ts b/index.ts deleted file mode 100644 index 5c35f38..0000000 --- a/index.ts +++ /dev/null @@ -1,40 +0,0 @@ -import * as core from "@actions/core"; -import * as github from "@actions/github"; - -async function run() { - try { - const token = core.getInput("token"); - const octokit = github.getOctokit(token); - const { owner, repo } = github.context.repo; - const path = "docs/index.html"; - - let res = await octokit.repos.getContent({ - owner, - repo, - path, - }); - - const decodedContent = fromBase64(res.data.content); - const content = toBase64(`${decodedContent} `); - - await octokit.repos.createOrUpdateFileContents({ - owner, - repo, - path, - content, - sha: res.data.sha, - message: "Updated metrics", - }); - } catch (error) { - core.setFailed(error.message); - } -} - -function fromBase64(content: string) { - return Buffer.from(content, "base64").toString("ascii"); -} - -function toBase64(content: string) { - return Buffer.from(content).toString("base64"); -} -run(); diff --git a/package.json b/package.json index f3a9bbf..fae64d1 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "author": "Florian Richter ", "license": "MIT", "scripts": { - "build": "tsc ./index.ts" + "build": "tsc" }, "dependencies": { "@actions/core": "^1.2.4", diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..38b3534 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,71 @@ +import * as core from "@actions/core"; +import * as github from "@actions/github"; + +type MetricsValue = { + value: T; + isoDate: string; +}; + +type MetricsData = { + key: string; + type: "scalar"; + values: Array>; +}; + +async function run() { + try { + const key = core.getInput("key"); + const value = core.getInput("value"); + const token = core.getInput("token"); + const octokit = github.getOctokit(token); + const { owner, repo } = github.context.repo; + const path = `docs/data/${key}.json`; + + let res = await octokit.repos.getContent({ + owner, + repo, + path, + }); + let data: MetricsData | null = null; + const isUpdate = res.status == 200; + if (res.status == 404) { + core.info(`Found new key "${key}", will create new file.`); + data = { key, type: "scalar", values: [] }; + } else if (isUpdate) { + core.info(`Extending existing metrics for "${key}"`); + data = JSON.parse(fromBase64(res.data.content)); + } + + if (data == null) { + throw new Error(`Loading or updating data for "${key}" has failed`); + } + + data.values.push({ + value: Number.parseFloat(value), + isoDate: new Date().toISOString(), + }); + + const content = toBase64(JSON.stringify(data)); + + const updateRes = await octokit.repos.createOrUpdateFileContents({ + owner, + repo, + path, + content, + sha: isUpdate ? res.data.sha : undefined, + message: isUpdate ? "Updated metrics" : "Created metrics", + }); + core.info("Finished processing new metrics"); + } catch (error) { + core.setFailed(error.message); + } +} + +function fromBase64(content: string) { + return Buffer.from(content, "base64").toString("ascii"); +} + +function toBase64(content: string) { + return Buffer.from(content).toString("base64"); +} +run(); diff --git a/tsconfig.json b/tsconfig.json index 84ae618..eecd4b8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "outDir": "./dist", "lib": ["es2020"], "module": "commonjs", "target": "es2019"