From 739fbdd456adead0d0f50aed44827bf1f10e1ac6 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Sat, 14 Nov 2020 23:28:21 +0530 Subject: [PATCH] :sparkles: Add v1 to generate summary --- src/index.ts | 93 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/src/index.ts b/src/index.ts index 68036db..aa5437b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,31 +1,25 @@ import { getInput, setFailed, setOutput } from "@actions/core"; import { context, getOctokit } from "@actions/github"; +import { GitHub } from "@actions/github/lib/utils"; import { readdir, readFile } from "fs-extra"; import { join } from "path"; -const emojiFlags = require("emoji-flags"); -interface Event { +interface Note { slug: string; - name: string; date: Date; - venue: string; - city: string; - emoji: string; } -const parseEventFile = async (year: string, file: string): Promise => { - const lines = (await readFile(join(".", "events", year, file), "utf-8")).split("\n"); +const parseNoteFile = async ( + owner: string, + repo: string, + octokit: InstanceType, + year: string, + file: string +): Promise => { + const commits = await octokit.repos.listCommits({ owner, repo, path: `notes/${year}/${file}` }); return { slug: file, - name: - (lines.find((line) => line.startsWith("title: ")) || "").split("title: ")[1] || - (lines.find((line) => line.startsWith("# ")) || "").split("# ")[1], - date: new Date((lines.find((line) => line.startsWith("date: ")) || "").split("date: ")[1]), - emoji: emojiFlags.countryCode( - (lines.find((line) => line.startsWith("country: ")) || "").split("country: ")[1] - ).emoji, - venue: (lines.find((line) => line.startsWith("venue: ")) || "").split("venue: ")[1], - city: (lines.find((line) => line.startsWith("city: ")) || "").split("city: ")[1], + date: new Date(commits.data[0].commit.author.date), }; }; @@ -34,70 +28,71 @@ const token = getInput("token") || process.env.GH_PAT || process.env.GITHUB_TOKE export const run = async () => { if (!token) throw new Error("GitHub token not found"); const octokit = getOctokit(token); + const [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/"); - const allEvents: { [index: string]: Array } = {}; - const allCountries = new Set(); - let totalEvents = 0; - let pastEvents = ""; - let upcomingEvents = ""; - const years = await readdir(join(".", "events")); + const allNotes: { [index: string]: Array } = {}; + let totalNotes = 0; + let pastNotes = ""; + let upcomingNotes = ""; + const years = await readdir(join(".", "notes")); for await (const year of years) { - const events = await readdir(join(".", "events", year)); - for await (const event of events) { - totalEvents++; - allEvents[year] = allEvents[year] || []; - const eventFile = await parseEventFile(year, event); - allEvents[year].push(eventFile); - allCountries.add(eventFile.emoji); + const notes = await readdir(join(".", "notes", year)); + for await (const note of notes) { + totalNotes++; + allNotes[year] = allNotes[year] || []; + const noteFile = await parseNoteFile(owner, repo, octokit, year, note); + allNotes[year].push(noteFile); } } - Object.keys(allEvents) + Object.keys(allNotes) .sort((a, b) => parseInt(b) - parseInt(a)) .forEach((year) => { - allEvents[year] = allEvents[year].sort( + allNotes[year] = allNotes[year].sort( (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() ); let addedYears: Array = []; - allEvents[year].forEach((event) => { - const isPast = new Date(event.date).getTime() < new Date().getTime(); - const text = `${addedYears.includes(year) ? "" : `### ${year}\n\n`}- [**${ - event.name - }**](./events/${year}/${event.slug}), ${new Date(event.date).toLocaleDateString("en-us", { + allNotes[year].forEach((note) => { + const isPast = new Date(note.date).getTime() < new Date().getTime(); + const text = `${addedYears.includes(year) ? "" : `### ${year}\n\n`}- [\`${ + note.slug + }\`](./notes/${year}/${note.slug}), ${new Date(note.date).toLocaleDateString("en-us", { year: "numeric", month: "long", day: "numeric", - })} \n ${event.emoji} ${[event.venue, event.city].filter((i) => i).join(", ")}\n\n`; - if (isPast) pastEvents += text; - else upcomingEvents += text; + })}\n`; + if (isPast) pastNotes += text; + else upcomingNotes += text; addedYears.push(year); }); }); let content = `## 🎤 Summary -- ${totalEvents} events in ${years.length} years -- ${allCountries.size} countries — ${Array.from(allCountries).join("")} +- ${totalNotes} notes in ${years.length} years `; - if (upcomingEvents.length) content += `## 🔮 Upcoming events\n\n${upcomingEvents}`; - if (pastEvents.length) content += `## 📜 Past events\n\n${pastEvents}`; + if (upcomingNotes.length) content += `## 🔮 Upcoming notes\n\n${upcomingNotes}`; + if (pastNotes.length) content += `## 📜 Past notes\n\n${pastNotes}`; let readmeContents = await readFile(join(".", "README.md"), "utf-8"); readmeContents = `${ - readmeContents.split("")[0] - }\n\n${content.trim()}\n${readmeContents.split("")[1]}`; + readmeContents.split("")[0] + }\n\n${content.trim()}\n${readmeContents.split("")[1]}`; const currentContents = await octokit.repos.getContent({ owner: context.repo.owner, repo: context.repo.repo, path: "README.md", }); const base64Content = Buffer.from(readmeContents).toString("base64"); - if (Buffer.from(currentContents.data.content, "base64").toString("utf8").trim() !== readmeContents.trim()) + if ( + Buffer.from(currentContents.data.content, "base64").toString("utf8").trim() !== + readmeContents.trim() + ) await octokit.repos.createOrUpdateFileContents({ owner: context.repo.owner, repo: context.repo.repo, sha: currentContents.data.sha, path: "README.md", - message: ":pencil: Update event summary [skip ci]", + message: ":pencil: Update notes summary [skip ci]", content: base64Content, }); - setOutput("Events updated", totalEvents); + setOutput("Notes updated", totalNotes); }; run()