Skip to content

Commit

Permalink
✨ Add v1 to generate summary
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 14, 2020
1 parent edb494c commit 739fbdd
Showing 1 changed file with 44 additions and 49 deletions.
93 changes: 44 additions & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<Event> => {
const lines = (await readFile(join(".", "events", year, file), "utf-8")).split("\n");
const parseNoteFile = async (
owner: string,
repo: string,
octokit: InstanceType<typeof GitHub>,
year: string,
file: string
): Promise<Note> => {
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),
};
};

Expand All @@ -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<Event> } = {};
const allCountries = new Set<string>();
let totalEvents = 0;
let pastEvents = "";
let upcomingEvents = "";
const years = await readdir(join(".", "events"));
const allNotes: { [index: string]: Array<Note> } = {};
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<string> = [];
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("<!--events-->")[0]
}<!--events-->\n\n${content.trim()}\n<!--/events-->${readmeContents.split("<!--/events-->")[1]}`;
readmeContents.split("<!--notes-->")[0]
}<!--notes-->\n\n${content.trim()}\n<!--/notes-->${readmeContents.split("<!--/notes-->")[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()
Expand Down

0 comments on commit 739fbdd

Please sign in to comment.