add some more endpoints

This commit is contained in:
Nathan Smith 2024-01-03 22:42:51 +00:00
parent 7abb5673b2
commit f2feb5d897
2 changed files with 63 additions and 7 deletions

View file

@ -1,10 +1,12 @@
import { RequestUrlParam, requestUrl } from "obsidian";
import { GithubLinkPluginSettings } from "./settings";
import { OnVerificationCallback } from "@octokit/auth-oauth-device/dist-types/types";
import { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
import { createOAuthDeviceAuth } from "@octokit/auth-oauth-device";
import { request } from "@octokit/request";
const baseApi = "https://api.github.com";
interface ParsedUrl {
url: string;
host: string;
@ -21,17 +23,14 @@ interface ParsedUrl {
// TODO: Clean this file up, lots of junk in here
export async function githubRequest(
config: RequestUrlParam,
settings: GithubLinkPluginSettings
) {
export async function githubRequest(config: RequestUrlParam, token?: string) {
if (!config.headers) {
config.headers = {};
}
config.headers.Accept = "application/vnd.github+json";
config.headers["X-GitHub-Api-Version"] = "2022-11-28";
if (settings.token) {
config.headers.Authorization = `Bearer ${settings.token}`;
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
try {
const response = await requestUrl(config);
@ -42,6 +41,50 @@ export async function githubRequest(
}
}
export async function getIssue(
org: string,
repo: string,
issue: number,
token?: string
) {
const result = await githubRequest(
{ url: `${baseApi}/repos/${org}/${repo}/issues/${issue}` },
token
);
return result.json as RestEndpointMethodTypes["issues"]["get"]["response"]["data"];
}
export async function getPullRequest(
org: string,
repo: string,
pr: number,
token?: string
) {
const result = await githubRequest(
{
url: `${baseApi}/repos/${org}/${repo}/pulls/${pr}`,
},
token
);
return result.json as RestEndpointMethodTypes["pulls"]["get"]["response"]["data"];
}
export async function getCode(
org: string,
repo: string,
path: string,
branch: string,
token?: string
) {
const result = await githubRequest(
{
url: `${baseApi}/repos/${org}/${repo}/contents/${path}?ref=${branch}`,
},
token
);
return result.json as RestEndpointMethodTypes["repos"]["getContent"]["response"]["data"];
}
export function parseUrl(urlString: string): ParsedUrl {
// Some potential URLs:
// https://github.com/nathonius/alloy-theme

View file

@ -4,11 +4,24 @@ import {
} from "./settings";
import { Plugin } from "obsidian";
import { getIssue } from "./github";
export class GithubLinkPlugin extends Plugin {
public settings: GithubLinkPluginSettings = {};
async onload() {
this.settings = Object.assign({}, await this.loadData());
this.addSettingTab(new GithubLinkPluginSettingsTab(this.app, this));
this.addCommand({
id: "get-issue",
name: "Get GitHub issue",
callback: async () => {
const result = await getIssue(
"nathonius",
"obsidian-trello",
4
);
console.log(result);
},
});
}
}