Clean
parent
9ef30d1f49
commit
a4e127a6dd
|
@ -6,3 +6,5 @@ node_modules
|
||||||
dist
|
dist
|
||||||
generated
|
generated
|
||||||
.yarnrc.yml
|
.yarnrc.yml
|
||||||
|
*.test.js
|
||||||
|
*.js.map
|
|
@ -11,9 +11,6 @@
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
|
||||||
"handlebars": "^4.7.7"
|
|
||||||
},
|
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@parcel/core": "2.3.2",
|
"@parcel/core": "2.3.2",
|
||||||
"@parcel/packager-raw-url": "2.3.2",
|
"@parcel/packager-raw-url": "2.3.2",
|
||||||
|
@ -9930,6 +9927,7 @@
|
||||||
"@mattermost/client": "^6.7.0-0",
|
"@mattermost/client": "^6.7.0-0",
|
||||||
"@mattermost/types": "^6.7.0-0",
|
"@mattermost/types": "^6.7.0-0",
|
||||||
"@types/yaml": "^1.9.7",
|
"@types/yaml": "^1.9.7",
|
||||||
|
"handlebars": "^4.7.7",
|
||||||
"markdown-it": "^12.3.2",
|
"markdown-it": "^12.3.2",
|
||||||
"markdown-it-task-lists": "^2.1.1",
|
"markdown-it-task-lists": "^2.1.1",
|
||||||
"yaml": "^2.0.0"
|
"yaml": "^2.0.0"
|
||||||
|
@ -11526,6 +11524,7 @@
|
||||||
"@mattermost/types": "^6.7.0-0",
|
"@mattermost/types": "^6.7.0-0",
|
||||||
"@types/markdown-it": "^12.2.3",
|
"@types/markdown-it": "^12.2.3",
|
||||||
"@types/yaml": "^1.9.7",
|
"@types/yaml": "^1.9.7",
|
||||||
|
"handlebars": "^4.7.7",
|
||||||
"markdown-it": "^12.3.2",
|
"markdown-it": "^12.3.2",
|
||||||
"markdown-it-task-lists": "^2.1.1",
|
"markdown-it-task-lists": "^2.1.1",
|
||||||
"yaml": "^2.0.0"
|
"yaml": "^2.0.0"
|
||||||
|
|
|
@ -30,8 +30,5 @@
|
||||||
},
|
},
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"packages/*"
|
"packages/*"
|
||||||
],
|
]
|
||||||
"dependencies": {
|
|
||||||
"handlebars": "^4.7.7"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
name: github
|
name: github
|
||||||
functions:
|
functions:
|
||||||
test:
|
queryEvents:
|
||||||
path: ./github.ts:queryEvents
|
path: ./github.ts:queryEvents
|
||||||
events:
|
events:
|
||||||
- query:gh-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 { 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 = {
|
type GithubEvent = {
|
||||||
id: string;
|
id: string;
|
||||||
|
@ -37,16 +39,53 @@ type ExposedEvent = {
|
||||||
repo: string;
|
repo: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
async function listEvents(username: string): Promise<GithubEvent[]> {
|
class GithubApi {
|
||||||
let events = await fetch(`https://api.github.com/users/${username}/events`);
|
constructor(private token?: string) {}
|
||||||
return await events.json();
|
|
||||||
|
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[]> {
|
type GithubConfig = {
|
||||||
let issues = await fetch(
|
token?: string;
|
||||||
`https://api.github.com/issues?q=${encodeURIComponent(filter)}`
|
};
|
||||||
);
|
|
||||||
return await issues.json();
|
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 {
|
function mapEvent(event: GithubEvent): any {
|
||||||
|
@ -64,21 +103,50 @@ function mapEvent(event: GithubEvent): any {
|
||||||
export async function queryEvents({
|
export async function queryEvents({
|
||||||
query,
|
query,
|
||||||
}: QueryProviderEvent): Promise<any[]> {
|
}: QueryProviderEvent): Promise<any[]> {
|
||||||
|
let api = await GithubApi.fromConfig();
|
||||||
let usernameFilter = query.filter.find((f) => f.prop === "username");
|
let usernameFilter = query.filter.find((f) => f.prop === "username");
|
||||||
if (!usernameFilter) {
|
if (!usernameFilter) {
|
||||||
throw Error("No 'username' filter specified, this is mandatory");
|
throw Error("No 'username' filter specified, this is mandatory");
|
||||||
}
|
}
|
||||||
let username = usernameFilter.value;
|
let usernames: string[] = [];
|
||||||
let allEvents = (await listEvents(username)).map(mapEvent);
|
if (usernameFilter.op === "=") {
|
||||||
return applyQuery(query, allEvents);
|
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({
|
export async function queryIssues({
|
||||||
// query,
|
query,
|
||||||
// }: QueryProviderEvent): Promise<string> {
|
}: QueryProviderEvent): Promise<any[]> {
|
||||||
// let filter = query.filter.find((f) => f.prop === "filter");
|
let api = await GithubApi.fromConfig();
|
||||||
// if (!filter) {
|
let filter = query.filter.find((f) => f.prop === "filter");
|
||||||
// throw Error("No 'filter' specified, this is mandatory");
|
if (!filter) {
|
||||||
// }
|
throw Error("No 'filter' specified, this is mandatory");
|
||||||
// let username = filter.value;
|
}
|
||||||
// }
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -39,7 +39,8 @@
|
||||||
"@types/yaml": "^1.9.7",
|
"@types/yaml": "^1.9.7",
|
||||||
"markdown-it": "^12.3.2",
|
"markdown-it": "^12.3.2",
|
||||||
"markdown-it-task-lists": "^2.1.1",
|
"markdown-it-task-lists": "^2.1.1",
|
||||||
"yaml": "^2.0.0"
|
"yaml": "^2.0.0",
|
||||||
|
"handlebars": "^4.7.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/markdown-it": "^12.2.3"
|
"@types/markdown-it": "^12.2.3"
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
import { addParentPointers, collectNodesMatching, ParseTree, renderToText } from "@silverbulletmd/common/tree";
|
import {
|
||||||
|
addParentPointers,
|
||||||
|
collectNodesMatching,
|
||||||
|
ParseTree,
|
||||||
|
renderToText,
|
||||||
|
} from "@silverbulletmd/common/tree";
|
||||||
|
|
||||||
export const queryRegex =
|
export const queryRegex =
|
||||||
/(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*#end\s*-->)/gs;
|
/(<!--\s*#query\s+(.+?)-->)(.+?)(<!--\s*#end\s*-->)/gs;
|
||||||
|
@ -44,27 +49,69 @@ export function removeQueries(pt: ParseTree) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxWidth = 70;
|
||||||
// Nicely format an array of JSON objects as a Markdown table
|
// Nicely format an array of JSON objects as a Markdown table
|
||||||
export function jsonToMDTable(
|
export function jsonToMDTable(
|
||||||
jsonArray: any[],
|
jsonArray: any[],
|
||||||
valueTransformer?: (k: string, v: any) => string | undefined
|
valueTransformer: (k: string, v: any) => string = (k, v) => "" + v
|
||||||
): string {
|
): string {
|
||||||
let headers = new Set<string>();
|
let fieldWidths = new Map<string, number>();
|
||||||
for (let entry of jsonArray) {
|
for (let entry of jsonArray) {
|
||||||
for (let k of Object.keys(entry)) {
|
for (let k of Object.keys(entry)) {
|
||||||
headers.add(k);
|
let fieldWidth = fieldWidths.get(k);
|
||||||
|
if (!fieldWidth) {
|
||||||
|
fieldWidth = valueTransformer(k, entry[k]).length;
|
||||||
|
} else {
|
||||||
|
fieldWidth = Math.max(valueTransformer(k, entry[k]).length, fieldWidth);
|
||||||
|
}
|
||||||
|
fieldWidths.set(k, fieldWidth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let headerList = [...headers];
|
|
||||||
|
let fullWidth = 0;
|
||||||
|
for (let v of fieldWidths.values()) {
|
||||||
|
fullWidth += v + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let headerList = [...fieldWidths.keys()];
|
||||||
let lines = [];
|
let lines = [];
|
||||||
lines.push("|" + headerList.join("|") + "|");
|
lines.push(
|
||||||
lines.push("|" + headerList.map((title) => "----").join("|") + "|");
|
"|" +
|
||||||
|
headerList
|
||||||
|
.map(
|
||||||
|
(headerName) =>
|
||||||
|
headerName +
|
||||||
|
charPad(" ", fieldWidths.get(headerName)! - headerName.length)
|
||||||
|
)
|
||||||
|
.join("|") +
|
||||||
|
"|"
|
||||||
|
);
|
||||||
|
lines.push(
|
||||||
|
"|" +
|
||||||
|
headerList
|
||||||
|
.map((title) => charPad("-", fieldWidths.get(title)!))
|
||||||
|
.join("|") +
|
||||||
|
"|"
|
||||||
|
);
|
||||||
for (const val of jsonArray) {
|
for (const val of jsonArray) {
|
||||||
let el = [];
|
let el = [];
|
||||||
for (let prop of headerList) {
|
for (let prop of headerList) {
|
||||||
el.push(valueTransformer ? valueTransformer(prop, val[prop]) : val[prop]);
|
let s = valueTransformer(prop, val[prop]);
|
||||||
|
el.push(s + charPad(" ", fieldWidths.get(prop)! - s.length));
|
||||||
}
|
}
|
||||||
lines.push("|" + el.join("|") + "|");
|
lines.push("|" + el.join("|") + "|");
|
||||||
}
|
}
|
||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
|
|
||||||
|
function charPad(ch: string, length: number) {
|
||||||
|
if (fullWidth > maxWidth && ch === "") {
|
||||||
|
return "";
|
||||||
|
} else if (fullWidth > maxWidth && ch === "-") {
|
||||||
|
return "--";
|
||||||
|
}
|
||||||
|
if (length < 1) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return new Array(length + 1).join(ch);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue