From f2feb5d89762d2abf603a0a2e9bb3974eb7971a7 Mon Sep 17 00:00:00 2001 From: Nathan Smith Date: Wed, 3 Jan 2024 22:42:51 +0000 Subject: [PATCH] add some more endpoints --- src/github.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++------- src/plugin.ts | 13 ++++++++++++ 2 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/github.ts b/src/github.ts index d633a12..2970978 100644 --- a/src/github.ts +++ b/src/github.ts @@ -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 diff --git a/src/plugin.ts b/src/plugin.ts index c220a09..cfcac2d 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -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); + }, + }); } }