Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Commit

Permalink
Prepare v0.0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
floric committed Jul 31, 2020
1 parent dc81562 commit 7127382
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/update-monitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
UpdateMonitor:
runs-on: ubuntu-latest
steps:
- uses: floric/repo-monitor-action@v0.0.12
- uses: floric/repo-monitor-action@v0.0.13
with:
key: bundle-size
value: 1
Expand Down
31 changes: 20 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ async function run() {
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 res = await getContent(path, token, owner, repo);
let data = null;
const isUpdate = res.status == 200;
if (res.status == 404) {
core.info(`Found new key "${key}", will create new file.`);
if (!res) {
core.info(`Called with new key "${key}", will create new file.`);
data = { key, type: "scalar", values: [] };
}
else if (isUpdate) {
else if ((res === null || res === void 0 ? void 0 : res.status) == 200) {
core.info(`Extending existing metrics for "${key}"`);
data = JSON.parse(fromBase64(res.data.content));
}
Expand All @@ -33,12 +28,13 @@ async function run() {
isoDate: new Date().toISOString(),
});
const content = toBase64(JSON.stringify(data));
const updateRes = await octokit.repos.createOrUpdateFileContents({
const isUpdate = (res === null || res === void 0 ? void 0 : res.status) == 200;
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path,
content,
sha: isUpdate ? res.data.sha : undefined,
sha: isUpdate ? res === null || res === void 0 ? void 0 : res.data.sha : undefined,
message: isUpdate ? "Updated metrics" : "Created metrics",
});
core.info("Finished processing new metrics");
Expand All @@ -47,6 +43,19 @@ async function run() {
core.setFailed(error.message);
}
}
async function getContent(path, token, owner, repo) {
const octokit = github.getOctokit(token);
try {
return await octokit.repos.getContent({
owner,
repo,
path,
});
}
catch (err) {
return null;
}
}
function fromBase64(content) {
return Buffer.from(content, "base64").toString("ascii");
}
Expand Down
37 changes: 26 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ async function run() {
const { owner, repo } = github.context.repo;
const path = `docs/data/${key}.json`;

let res = await octokit.repos.getContent({
owner,
repo,
path,
});
let res = await getContent(path, token, owner, repo);
let data: MetricsData | null = null;
const isUpdate = res.status == 200;
if (res.status == 404) {
core.info(`Found new key "${key}", will create new file.`);
if (!res) {
core.info(`Called with new key "${key}", will create new file.`);
data = { key, type: "scalar", values: [] };
} else if (isUpdate) {
} else if (res?.status == 200) {
core.info(`Extending existing metrics for "${key}"`);
data = JSON.parse(fromBase64(res.data.content));
}
Expand All @@ -47,12 +42,13 @@ async function run() {

const content = toBase64(JSON.stringify(data));

const updateRes = await octokit.repos.createOrUpdateFileContents({
const isUpdate = res?.status == 200;
await octokit.repos.createOrUpdateFileContents({
owner,
repo,
path,
content,
sha: isUpdate ? res.data.sha : undefined,
sha: isUpdate ? res?.data.sha : undefined,
message: isUpdate ? "Updated metrics" : "Created metrics",
});
core.info("Finished processing new metrics");
Expand All @@ -61,11 +57,30 @@ async function run() {
}
}

async function getContent(
path: string,
token: string,
owner: string,
repo: string
) {
const octokit = github.getOctokit(token);
try {
return await octokit.repos.getContent({
owner,
repo,
path,
});
} catch (err) {
return null;
}
}

function fromBase64(content: string) {
return Buffer.from(content, "base64").toString("ascii");
}

function toBase64(content: string) {
return Buffer.from(content).toString("base64");
}

run();

0 comments on commit 7127382

Please sign in to comment.