Skip to content

Commit

Permalink
add new adapter GitHubIssue and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bridge-y committed Jun 25, 2024
1 parent 6d1a529 commit 386467a
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 18 deletions.
35 changes: 25 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Context, Hono } from "hono";
import { cors } from "hono/cors";
import { createMemoryNote, NoteArguments } from "./note/Note";
import { NoteArguments, createMemoryNote } from "./note/Note";
import { createGitHubIssueStorage } from "./note/adapters/GitHubIssue";
import { createGitHubProjectStorage } from "./note/adapters/GitHubProject";
import { createNotionStorage } from "./note/adapters/Notion";
import { createCloudflareStorage } from "./note/adapters/cloudflare";
import { HTML } from "./widget/render";
import { createNotionStorage } from "./note/adapters/Notion";

declare var MEMORY_NOTE_TOKEN: string;
declare var BACKEND_SERVICE: "github" | "cloudflare" | "notion";
declare var GITHUB_OWNER: string;
declare var GITHUB_REPO: string;
declare var GITHUB_PROJECT_ID: string;
declare let MEMORY_NOTE_TOKEN: string;
declare let BACKEND_SERVICE: "github" | "cloudflare" | "notion" | "github_issue";
declare let GITHUB_TOKEN: string;
declare let GITHUB_OWNER: string;
declare let GITHUB_REPO: string;
declare let GITHUB_PROJECT_ID: string;
declare let GITHUB_ISSUE_LABEL: string;
if (
typeof BACKEND_SERVICE === "string" &&
BACKEND_SERVICE !== "github" &&
BACKEND_SERVICE !== "cloudflare" &&
BACKEND_SERVICE !== "notion"
BACKEND_SERVICE !== "notion" &&
BACKEND_SERVICE !== "github_issue"
) {
throw new Error("BACKEND_SERVICE should github or cloudflare or notion");
}
Expand Down Expand Up @@ -55,13 +59,15 @@ const newMemoryNote = (c: Context) => {
NOTION_FILTER_OPTIONS: c.env.NOTION_FILTER_OPTIONS
})
});
} else if (backendService === "cloudflare") {
}
if (backendService === "cloudflare") {
return createMemoryNote({
storage: createCloudflareStorage({
kvStorage: c.env.MEMORY_NOTE
})
});
} else if (backendService === "github") {
}
if (backendService === "github") {
return createMemoryNote({
storage: createGitHubProjectStorage({
owner: GITHUB_OWNER,
Expand All @@ -70,6 +76,15 @@ const newMemoryNote = (c: Context) => {
})
});
}
if (backendService === "github_issue") {
return createMemoryNote({
storage: createGitHubIssueStorage({
token: c.env.GITHUB_TOKEN,
owner: c.env.GITHUB_OWNER,
repo: c.env.GITHUB_REPO
})
});
}
throw new Error("invalid backend service");
};

Expand Down
2 changes: 1 addition & 1 deletion src/note/Note.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Note, StorageAdapter } from "./StorageAdapter";
import type { Note, StorageAdapter } from "./StorageAdapter";

/**
* Note stack Architecture
Expand Down
95 changes: 95 additions & 0 deletions src/note/adapters/GitHubIssue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Octokit } from "@octokit/core";
import type { AppendNote, Note, StorageAdapter } from "../StorageAdapter";

// noteKey is a label name used for filtering issues
// example: "inbox" for filtering issues with label "inbox"
export const createGitHubIssueStorage = ({
token,
owner,
repo,
deleteMode = "CLOSE"
}: {
token: string;
owner: string;
repo: string;
deleteMode?: "DELETE" | "CLOSE";
}): StorageAdapter => {
const octokit = new Octokit({ auth: token });

return {
async getNotes(label: string): Promise<Note[]> {
let response;
if (label !== "_all") {
response = await octokit.request("GET /repos/{owner}/{repo}/issues", {
owner,
repo,
labels: label,
state: "open"
});
} else {
response = await octokit.request("GET /repos/{owner}/{repo}/issues", {
owner,
repo,
state: "open"
});
}
return response.data.map((issue: { id: number; title: string; updated_at: string }) => ({
id: String(issue.id),
message: issue.title,
timestamp: new Date(issue.updated_at).getTime()
}));
},
async appendNote(label: string, note: AppendNote): Promise<Note> {
let response;
// if _all is set to label, note will have no label
if (label !== "_all") {
response = await octokit.request("POST /repos/{owner}/{repo}/issues", {
owner,
repo,
title: note.message,
labels: [label]
});
} else {
response = await octokit.request("POST /repos/{owner}/{repo}/issues", {
owner,
repo,
title: note.message
});
}
const issue = response.data;
return {
id: String(issue.id),
message: note.message,
timestamp: new Date(issue.created_at).getTime()
};
},
async deleteNote(_: string, id: Note["id"]): Promise<Note> {
const response = await octokit.request("GET /repos/{owner}/{repo}/issues/{issue_number}", {
owner,
repo,
issue_number: Number(id)
});
const issue = response.data;

if (deleteMode === "CLOSE") {
await octokit.request("PATCH /repos/{owner}/{repo}/issues/{issue_number}", {
owner,
repo,
issue_number: Number(id),
state: "closed"
});
} else {
await octokit.request("DELETE /repos/{owner}/{repo}/issues/{issue_number}", {
owner,
repo,
issue_number: Number(id)
});
}
return {
id: String(issue.id),
message: issue.title,
timestamp: new Date(issue.updated_at).getTime()
};
}
};
};
2 changes: 1 addition & 1 deletion src/note/adapters/GitHubProject.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppendNote, Note, StorageAdapter } from "../StorageAdapter";
import { Octokit } from "@octokit/core";
import type { AppendNote, Note, StorageAdapter } from "../StorageAdapter";

// $ wrangler secret put GITHUB_TOKEN
// <INPUT_YOUR_API_TOKEN>
Expand Down
4 changes: 2 additions & 2 deletions src/note/adapters/Notion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AppendNote, Note, StorageAdapter } from "../StorageAdapter";
import { Client } from "@notionhq/client";
import { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import type { PageObjectResponse } from "@notionhq/client/build/src/api-endpoints";
import type { AppendNote, Note, StorageAdapter } from "../StorageAdapter";

// :listId is notion database id
export type createNotionDatabaseSimple = {
Expand Down
2 changes: 1 addition & 1 deletion src/note/adapters/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppendNote, Note, StorageAdapter } from "../StorageAdapter";
import { uuid } from "@cfworker/uuid";
import type { AppendNote, Note, StorageAdapter } from "../StorageAdapter";

export const createCloudflareStorage = ({ kvStorage }: { kvStorage: KVNamespace }): StorageAdapter => {
return {
Expand Down
4 changes: 2 additions & 2 deletions src/widget/render.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Widget } from "./widget";
import { html, raw } from "hono/html";
import { Note } from "../note/StorageAdapter";
import type { Note } from "../note/StorageAdapter";
import { Widget } from "./widget";

export const HTML = ({ notes }: { notes: Note[] }) => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/widget/widget.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jsx } from "hono/jsx";
import { Note } from "../note/StorageAdapter";
import type React from "react";
import type { Note } from "../note/StorageAdapter";

const List = ({ children, ...props }: React.JSX.IntrinsicElements["ul"]) => {
return (
Expand Down

0 comments on commit 386467a

Please sign in to comment.