mirror of
https://github.com/aljazceru/hypergolic.git
synced 2025-12-17 21:44:21 +01:00
Merge pull request #109 from bob2402/dev-mode-show-commit
problem: nostrocket version information does not show current commit hash and number of commits
This commit is contained in:
@@ -4,16 +4,8 @@
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import Separator from '@/components/ui/separator/separator.svelte';
|
||||
import { currentUser, devmode } from '@/stores/session';
|
||||
import {
|
||||
Code,
|
||||
GitBranch,
|
||||
HelpCircle,
|
||||
Mail,
|
||||
Package,
|
||||
Pyramid,
|
||||
Rocket,
|
||||
Users
|
||||
} from 'lucide-svelte';
|
||||
import { commitInfo } from '@/stores/github';
|
||||
import { Code, HelpCircle, Mail, Package, Pyramid, Rocket } from 'lucide-svelte';
|
||||
import { GitAltBrand, TelegramBrand } from 'svelte-awesome-icons';
|
||||
import NotifyMe from './NotifyMe.svelte';
|
||||
|
||||
@@ -85,6 +77,8 @@
|
||||
{#if $devmode}
|
||||
<span class="m-2 flex flex-col border border-orange-500 p-1 font-mono text-sm">
|
||||
<span class="text-center">RELEASE NAME:</span>
|
||||
<span class=" text-nowrap text-center italic">shippable intermediary</span></span
|
||||
>
|
||||
<span class=" text-nowrap text-center italic">shippable intermediary</span>
|
||||
<span class=" max-w-[200px] truncate text-nowrap">Commit: {$commitInfo.hash}</span>
|
||||
<span class=" text-nowrap">Height: {$commitInfo.count}</span>
|
||||
</span>
|
||||
{/if}
|
||||
|
||||
@@ -102,11 +102,6 @@ export async function getCuckPrice(): Promise<number> {
|
||||
});
|
||||
}
|
||||
|
||||
interface CommitInfo {
|
||||
count: number;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
interface GitHubUrlParts {
|
||||
owner: string;
|
||||
repo: string;
|
||||
@@ -114,7 +109,7 @@ interface GitHubUrlParts {
|
||||
number?: string;
|
||||
}
|
||||
|
||||
class GitHubApiError extends Error {
|
||||
export class GitHubApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public status?: number
|
||||
@@ -124,7 +119,7 @@ class GitHubApiError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
function parseGitHubUrl(url: URL): GitHubUrlParts {
|
||||
export function parseGitHubUrl(url: URL): GitHubUrlParts {
|
||||
const parts = url.pathname.split('/').filter(Boolean);
|
||||
if (parts.length < 2) {
|
||||
throw new Error('Invalid GitHub URL');
|
||||
@@ -137,38 +132,6 @@ function parseGitHubUrl(url: URL): GitHubUrlParts {
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchGitHubApi(apiUrl: URL): Promise<any> {
|
||||
const response = await fetch(apiUrl);
|
||||
if (!response.ok) {
|
||||
throw new GitHubApiError(`HTTP error! status: ${response.status}`, response.status);
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function getCommit(url: URL): Promise<CommitInfo> {
|
||||
try {
|
||||
const { owner, repo } = parseGitHubUrl(url);
|
||||
const apiURL = new URL(`https://api.github.com/repos/${owner}/${repo}/commits`);
|
||||
const json = await fetchGitHubApi(apiURL);
|
||||
|
||||
if (!json[0]?.sha) {
|
||||
throw new GitHubApiError('Failed to fetch commit info: API returned unexpected data');
|
||||
}
|
||||
|
||||
return {
|
||||
count: json.length,
|
||||
hash: json[0].sha
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof GitHubApiError) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(
|
||||
`Failed to fetch commit info: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function parseProblem(problem: string): Promise<string | undefined> {
|
||||
if (!isGitHubIssuesOrPullUrl(problem)) {
|
||||
return undefined;
|
||||
@@ -176,9 +139,15 @@ export async function parseProblem(problem: string): Promise<string | undefined>
|
||||
|
||||
try {
|
||||
const { owner, repo, number } = parseGitHubUrl(new URL(problem));
|
||||
|
||||
const apiURL = new URL(`https://api.github.com/repos/${owner}/${repo}/issues/${number}`);
|
||||
const { title } = await fetchGitHubApi(apiURL);
|
||||
return title;
|
||||
const response = await fetch(apiURL);
|
||||
if (!response.ok) {
|
||||
throw new GitHubApiError(`HTTP error! status: ${response.status}`, response.status);
|
||||
}
|
||||
|
||||
const json = await response.json();
|
||||
return json.title;
|
||||
} catch (error) {
|
||||
console.error('Failed to parse problem:', error);
|
||||
return undefined;
|
||||
|
||||
48
src/lib/stores/github.ts
Normal file
48
src/lib/stores/github.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { GitHubApiError, parseGitHubUrl } from '@/helpers';
|
||||
import { writable } from 'svelte/store';
|
||||
|
||||
interface CommitInfo {
|
||||
count: number;
|
||||
hash: string;
|
||||
}
|
||||
|
||||
let _c: CommitInfo = { hash: '', count: 0 };
|
||||
export const commitInfo = writable(_c);
|
||||
|
||||
export async function getCommitInfo(url: URL): Promise<CommitInfo> {
|
||||
try {
|
||||
const { owner, repo } = parseGitHubUrl(url);
|
||||
const apiURL = new URL(`https://api.github.com/repos/${owner}/${repo}/commits?per_page=1`);
|
||||
|
||||
const response = await fetch(apiURL);
|
||||
if (!response.ok) {
|
||||
throw new GitHubApiError(`HTTP error! status: ${response.status}`, response.status);
|
||||
}
|
||||
const json = await response.json();
|
||||
|
||||
if (!json[0]?.sha) {
|
||||
throw new GitHubApiError('Failed to fetch commit info: API returned unexpected data');
|
||||
}
|
||||
const totalCommits = parseLinkHeader(response.headers.get('Link'));
|
||||
|
||||
let r: CommitInfo = {
|
||||
count: totalCommits,
|
||||
hash: json[0].sha
|
||||
};
|
||||
commitInfo.set(r);
|
||||
return r;
|
||||
} catch (error) {
|
||||
if (error instanceof GitHubApiError) {
|
||||
throw error;
|
||||
}
|
||||
throw new Error(
|
||||
`Failed to fetch commit info: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function parseLinkHeader(header: string | null): number {
|
||||
if (!header) return 0;
|
||||
const matches = header.match(/page=(\d+)>; rel="last"/);
|
||||
return matches ? parseInt(matches[1], 10) : 0;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
import { onMount } from 'svelte';
|
||||
import '../app.css';
|
||||
import SidePanelLayout from '../layouts/SidePanelLayout.svelte';
|
||||
import { getCommitInfo } from '@/stores/github';
|
||||
|
||||
let sessionStarted = false;
|
||||
let connected = false;
|
||||
@@ -29,6 +30,7 @@
|
||||
|
||||
onMount(() => {
|
||||
getBitcoinTip();
|
||||
getCommitInfo(new URL('https://github.com/nostrocket/hypergolic/'));
|
||||
});
|
||||
|
||||
setInterval(
|
||||
|
||||
Reference in New Issue
Block a user