mirror of
https://github.com/nathonius/obsidian-github-link.git
synced 2026-07-22 09:20:25 +00:00
Merge pull request #88 from nathonius/fix/61/sonar
resolve sonar issues
This commit is contained in:
commit
213b497aa2
7 changed files with 69 additions and 69 deletions
|
|
@ -14,7 +14,7 @@ import type { RequestUrlParam, RequestUrlResponse } from "obsidian";
|
|||
|
||||
import { RequestError, promiseWithResolvers } from "src/util";
|
||||
import { Notice, requestUrl } from "obsidian";
|
||||
import { Logger } from "src/plugin";
|
||||
import { logger } from "src/plugin";
|
||||
import Queue from "queue";
|
||||
|
||||
const baseApi = "https://api.github.com";
|
||||
|
|
@ -41,7 +41,7 @@ export async function queueRequest(config: RequestUrlParam, token?: string): Pro
|
|||
|
||||
async function githubRequest(config: RequestUrlParam, token?: string): Promise<RequestUrlResponse> {
|
||||
if (rateLimitReset !== null && rateLimitReset > new Date()) {
|
||||
Logger.warn(
|
||||
logger.warn(
|
||||
`GitHub rate limit exceeded. No more requests will be made until ${rateLimitReset.toLocaleTimeString()}`,
|
||||
);
|
||||
throw new Error("GitHub rate limit exceeded.");
|
||||
|
|
@ -59,18 +59,18 @@ async function githubRequest(config: RequestUrlParam, token?: string): Promise<R
|
|||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
try {
|
||||
Logger.debug(`Request: ${config.url}`);
|
||||
Logger.debug(config);
|
||||
logger.debug(`Request: ${config.url}`);
|
||||
logger.debug(config);
|
||||
const response = await requestUrl(config);
|
||||
Logger.debug(`Response:`);
|
||||
Logger.debug(response);
|
||||
logger.debug(`Response:`);
|
||||
logger.debug(response);
|
||||
|
||||
// Handle rate limit
|
||||
const retryAfterSeconds = parseInt(response.headers["retry-after"]);
|
||||
const rateLimitRemaining = parseInt(response.headers["x-ratelimit-remaining"]);
|
||||
const rateLimitResetSeconds = parseInt(response.headers["x-ratelimit-reset"]);
|
||||
if (!isNaN(retryAfterSeconds)) {
|
||||
Logger.warn(`Got retry-after header with value ${retryAfterSeconds}`);
|
||||
logger.warn(`Got retry-after header with value ${retryAfterSeconds}`);
|
||||
await sleep(retryAfterSeconds * 1000);
|
||||
return githubRequest(config, token);
|
||||
} else if (!isNaN(rateLimitRemaining) && rateLimitRemaining === 0 && !isNaN(rateLimitResetSeconds)) {
|
||||
|
|
@ -81,7 +81,7 @@ async function githubRequest(config: RequestUrlParam, token?: string): Promise<R
|
|||
}
|
||||
new Notice(message);
|
||||
} else if (!isNaN(rateLimitRemaining) && rateLimitRemaining <= 5) {
|
||||
Logger.warn("GitHub rate limit approaching.");
|
||||
logger.warn("GitHub rate limit approaching.");
|
||||
}
|
||||
|
||||
return response;
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ export async function getMyIssues(
|
|||
skipCache = false,
|
||||
): Promise<IssueListResponse> {
|
||||
const account = getAccount(org);
|
||||
if (!account || !account.token) {
|
||||
if (!account?.token) {
|
||||
return [];
|
||||
}
|
||||
const _params = sanitizeObject(params, {
|
||||
|
|
|
|||
|
|
@ -32,45 +32,36 @@ export function parseUrl(urlString: string): ParsedUrl {
|
|||
const parsedUrl: ParsedUrl = { url: urlString, host: url.hostname };
|
||||
|
||||
const urlParts = url.pathname.split("/");
|
||||
if (urlParts.length >= 4) {
|
||||
if (urlParts.length > 4) {
|
||||
const issueNumber = parseInt(urlParts[4], 10);
|
||||
switch (urlParts[3].toLowerCase()) {
|
||||
case "issues":
|
||||
if (urlParts[4]) {
|
||||
const issueNumber = parseInt(urlParts[4], 10);
|
||||
if (!isNaN(issueNumber)) {
|
||||
parsedUrl.issue = issueNumber;
|
||||
}
|
||||
if (!isNaN(issueNumber)) {
|
||||
parsedUrl.issue = issueNumber;
|
||||
}
|
||||
break;
|
||||
case "pull":
|
||||
if (urlParts[4]) {
|
||||
const prNumber = parseInt(urlParts[4], 10);
|
||||
if (!isNaN(prNumber)) {
|
||||
parsedUrl.pr = prNumber;
|
||||
}
|
||||
if (!isNaN(issueNumber)) {
|
||||
parsedUrl.pr = issueNumber;
|
||||
}
|
||||
break;
|
||||
case "blob":
|
||||
parsedUrl.code = {};
|
||||
if (urlParts[4]) {
|
||||
parsedUrl.code.branch = urlParts[4];
|
||||
}
|
||||
parsedUrl.code.branch = urlParts[4];
|
||||
if (urlParts[5]) {
|
||||
const pathParts = urlParts.slice(5);
|
||||
parsedUrl.code.path = pathParts.join("/");
|
||||
}
|
||||
break;
|
||||
case "commit":
|
||||
if (urlParts[4]) {
|
||||
parsedUrl.commit = urlParts.slice(4).join("/");
|
||||
}
|
||||
parsedUrl.commit = urlParts.slice(4).join("/");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (urlParts.length >= 3) {
|
||||
if (urlParts.length > 2) {
|
||||
parsedUrl.repo = urlParts[2];
|
||||
}
|
||||
if (urlParts.length >= 2) {
|
||||
if (urlParts.length > 1) {
|
||||
parsedUrl.org = urlParts[1];
|
||||
}
|
||||
|
||||
|
|
|
|||
44
src/logger.ts
Normal file
44
src/logger.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
export enum LogLevel {
|
||||
Error = 0,
|
||||
Warn = 1,
|
||||
Info = 2,
|
||||
Debug = 3,
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
public logLevel: LogLevel = LogLevel.Error;
|
||||
public log(message: unknown, level: LogLevel) {
|
||||
if (level <= this.logLevel) {
|
||||
switch (level) {
|
||||
case LogLevel.Error:
|
||||
console.error(message);
|
||||
break;
|
||||
case LogLevel.Warn:
|
||||
console.warn(message);
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
console.info(message);
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
console.debug(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public error(message: unknown) {
|
||||
this.log(message, LogLevel.Error);
|
||||
}
|
||||
|
||||
public warn(message: unknown) {
|
||||
this.log(message, LogLevel.Warn);
|
||||
}
|
||||
|
||||
public info(message: unknown) {
|
||||
this.log(message, LogLevel.Info);
|
||||
}
|
||||
|
||||
public debug(message: unknown) {
|
||||
this.log(message, LogLevel.Debug);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import { DEFAULT_SETTINGS, GithubLinkPluginSettingsTab } from "./settings";
|
||||
import { LogLevel, verboseFactory } from "./util";
|
||||
import { Logger } from "./logger";
|
||||
|
||||
import type { GithubLinkPluginSettings } from "./settings";
|
||||
import { InlineRenderer } from "./inline/inline";
|
||||
|
|
@ -7,13 +7,13 @@ import { Plugin } from "obsidian";
|
|||
import { QueryProcessor } from "./query/processor";
|
||||
import { createInlineViewPlugin } from "./inline/view-plugin";
|
||||
|
||||
export let PluginSettings: GithubLinkPluginSettings = { ...DEFAULT_SETTINGS };
|
||||
export let Logger = verboseFactory(LogLevel.Error);
|
||||
export const PluginSettings: GithubLinkPluginSettings = { ...DEFAULT_SETTINGS };
|
||||
export const logger = new Logger();
|
||||
|
||||
export class GithubLinkPlugin extends Plugin {
|
||||
async onload() {
|
||||
PluginSettings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
Logger = verboseFactory(PluginSettings.logLevel);
|
||||
Object.assign(PluginSettings, await this.loadData());
|
||||
logger.logLevel = PluginSettings.logLevel;
|
||||
|
||||
this.addSettingTab(new GithubLinkPluginSettingsTab(this.app, this));
|
||||
this.registerMarkdownPostProcessor(InlineRenderer);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ import { PluginSettingTab, Setting } from "obsidian";
|
|||
import type { App } from "obsidian";
|
||||
import { AuthModal } from "./auth-modal";
|
||||
import type { GithubLinkPlugin } from "./plugin";
|
||||
import { LogLevel } from "./util";
|
||||
import { LogLevel } from "./logger";
|
||||
import { PluginSettings } from "./plugin";
|
||||
import type { Verification } from "@octokit/auth-oauth-device/dist-types/types";
|
||||
import { auth } from "./github/auth";
|
||||
|
|
|
|||
35
src/util.ts
35
src/util.ts
|
|
@ -2,41 +2,6 @@ export type RemoveIndexSignature<T> = {
|
|||
[K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: T[K];
|
||||
};
|
||||
|
||||
export enum LogLevel {
|
||||
Error = 0,
|
||||
Warn = 1,
|
||||
Info = 2,
|
||||
Debug = 3,
|
||||
}
|
||||
|
||||
export function verboseFactory(logLevel: LogLevel) {
|
||||
const log = (message: unknown, level: LogLevel) => {
|
||||
if (level <= logLevel) {
|
||||
switch (level) {
|
||||
case LogLevel.Error:
|
||||
console.error(message);
|
||||
break;
|
||||
case LogLevel.Warn:
|
||||
console.warn(message);
|
||||
break;
|
||||
case LogLevel.Info:
|
||||
console.info(message);
|
||||
break;
|
||||
case LogLevel.Debug:
|
||||
console.debug(message);
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
return {
|
||||
log,
|
||||
error: (message: unknown) => log(message, LogLevel.Error),
|
||||
warn: (message: unknown) => log(message, LogLevel.Warn),
|
||||
info: (message: unknown) => log(message, LogLevel.Info),
|
||||
debug: (message: unknown) => log(message, LogLevel.Debug),
|
||||
};
|
||||
}
|
||||
|
||||
export function titleCase(value: string): string {
|
||||
const words = value.split(/[-_]/);
|
||||
return words.map((w) => w.charAt(0)?.toUpperCase() + w.slice(1)).join(" ");
|
||||
|
|
|
|||
Loading…
Reference in a new issue