Clean
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
name: github
|
||||
functions:
|
||||
test:
|
||||
queryEvents:
|
||||
path: ./github.ts:queryEvents
|
||||
events:
|
||||
- query:gh-events
|
||||
queryIssues:
|
||||
path: ./github.ts:queryIssues
|
||||
events:
|
||||
- query:gh-issues
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { applyQuery, QueryProviderEvent, renderQuery } from "../query/engine";
|
||||
import { jsonToMDTable } from "../query/util";
|
||||
import { readPage } from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
||||
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
||||
import { extractMeta } from "../query/data";
|
||||
|
||||
type GithubEvent = {
|
||||
id: string;
|
||||
@@ -37,16 +39,53 @@ type ExposedEvent = {
|
||||
repo: string;
|
||||
};
|
||||
|
||||
async function listEvents(username: string): Promise<GithubEvent[]> {
|
||||
let events = await fetch(`https://api.github.com/users/${username}/events`);
|
||||
return await events.json();
|
||||
class GithubApi {
|
||||
constructor(private token?: string) {}
|
||||
|
||||
async apiCall(url: string, options: any = {}): Promise<any> {
|
||||
let res = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
Authorization: this.token ? `token ${this.token}` : undefined,
|
||||
},
|
||||
});
|
||||
if (res.status !== 200) {
|
||||
throw new Error(await res.text());
|
||||
}
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async listEvents(username: string): Promise<GithubEvent[]> {
|
||||
return this.apiCall(
|
||||
`https://api.github.com/users/${username}/events?per_page=100`
|
||||
);
|
||||
}
|
||||
|
||||
async listIssues(filter: string): Promise<any[]> {
|
||||
return this.apiCall(
|
||||
`https://api.github.com/issues?q=${encodeURIComponent(filter)}`
|
||||
);
|
||||
}
|
||||
|
||||
static async fromConfig(): Promise<GithubApi> {
|
||||
return new GithubApi((await getConfig()).token);
|
||||
}
|
||||
}
|
||||
|
||||
async function listIssues(filter: string): Promise<any[]> {
|
||||
let issues = await fetch(
|
||||
`https://api.github.com/issues?q=${encodeURIComponent(filter)}`
|
||||
);
|
||||
return await issues.json();
|
||||
type GithubConfig = {
|
||||
token?: string;
|
||||
};
|
||||
|
||||
async function getConfig(): Promise<GithubConfig> {
|
||||
try {
|
||||
let { text } = await readPage("github-config");
|
||||
let parsedContent = await parseMarkdown(text);
|
||||
let pageMeta = await extractMeta(parsedContent);
|
||||
return pageMeta as GithubConfig;
|
||||
} catch (e) {
|
||||
console.error("No github-config page found, using default config");
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function mapEvent(event: GithubEvent): any {
|
||||
@@ -64,21 +103,50 @@ function mapEvent(event: GithubEvent): any {
|
||||
export async function queryEvents({
|
||||
query,
|
||||
}: QueryProviderEvent): Promise<any[]> {
|
||||
let api = await GithubApi.fromConfig();
|
||||
let usernameFilter = query.filter.find((f) => f.prop === "username");
|
||||
if (!usernameFilter) {
|
||||
throw Error("No 'username' filter specified, this is mandatory");
|
||||
}
|
||||
let username = usernameFilter.value;
|
||||
let allEvents = (await listEvents(username)).map(mapEvent);
|
||||
return applyQuery(query, allEvents);
|
||||
let usernames: string[] = [];
|
||||
if (usernameFilter.op === "=") {
|
||||
usernames = [usernameFilter.value];
|
||||
} else if (usernameFilter.op === "in") {
|
||||
usernames = usernameFilter.value;
|
||||
} else {
|
||||
throw new Error(`Unsupported operator ${usernameFilter.op}`);
|
||||
}
|
||||
let allEvents: GithubEvent[] = [];
|
||||
for (let eventList of await Promise.all(
|
||||
usernames.map((username) => api.listEvents(username))
|
||||
)) {
|
||||
allEvents.push(...eventList);
|
||||
}
|
||||
// console.log("Usernames", usernames, "Event list lenght", allEvents[0]);
|
||||
return applyQuery(query, allEvents.map(mapEvent));
|
||||
}
|
||||
|
||||
// export async function queryIssues({
|
||||
// query,
|
||||
// }: QueryProviderEvent): Promise<string> {
|
||||
// let filter = query.filter.find((f) => f.prop === "filter");
|
||||
// if (!filter) {
|
||||
// throw Error("No 'filter' specified, this is mandatory");
|
||||
// }
|
||||
// let username = filter.value;
|
||||
// }
|
||||
export async function queryIssues({
|
||||
query,
|
||||
}: QueryProviderEvent): Promise<any[]> {
|
||||
let api = await GithubApi.fromConfig();
|
||||
let filter = query.filter.find((f) => f.prop === "filter");
|
||||
if (!filter) {
|
||||
throw Error("No 'filter' specified, this is mandatory");
|
||||
}
|
||||
let queries: string[] = [];
|
||||
if (filter.op === "=") {
|
||||
queries = [filter.value];
|
||||
} else if (filter.op === "in") {
|
||||
queries = filter.value;
|
||||
} else {
|
||||
throw new Error(`Unsupported operator ${filter.op}`);
|
||||
}
|
||||
let allIssues: any[] = [];
|
||||
for (let issuesList of await Promise.all(
|
||||
queries.map((query) => api.listIssues(query))
|
||||
)) {
|
||||
allIssues.push(...issuesList);
|
||||
}
|
||||
return allIssues;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user