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:
gsovereignty
2024-08-19 19:27:56 +08:00
committed by GitHub
4 changed files with 66 additions and 53 deletions

View File

@@ -4,16 +4,8 @@
import { Badge } from '@/components/ui/badge'; import { Badge } from '@/components/ui/badge';
import Separator from '@/components/ui/separator/separator.svelte'; import Separator from '@/components/ui/separator/separator.svelte';
import { currentUser, devmode } from '@/stores/session'; import { currentUser, devmode } from '@/stores/session';
import { import { commitInfo } from '@/stores/github';
Code, import { Code, HelpCircle, Mail, Package, Pyramid, Rocket } from 'lucide-svelte';
GitBranch,
HelpCircle,
Mail,
Package,
Pyramid,
Rocket,
Users
} from 'lucide-svelte';
import { GitAltBrand, TelegramBrand } from 'svelte-awesome-icons'; import { GitAltBrand, TelegramBrand } from 'svelte-awesome-icons';
import NotifyMe from './NotifyMe.svelte'; import NotifyMe from './NotifyMe.svelte';
@@ -85,6 +77,8 @@
{#if $devmode} {#if $devmode}
<span class="m-2 flex flex-col border border-orange-500 p-1 font-mono text-sm"> <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-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} {/if}

View File

@@ -102,11 +102,6 @@ export async function getCuckPrice(): Promise<number> {
}); });
} }
interface CommitInfo {
count: number;
hash: string;
}
interface GitHubUrlParts { interface GitHubUrlParts {
owner: string; owner: string;
repo: string; repo: string;
@@ -114,7 +109,7 @@ interface GitHubUrlParts {
number?: string; number?: string;
} }
class GitHubApiError extends Error { export class GitHubApiError extends Error {
constructor( constructor(
message: string, message: string,
public status?: number 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); const parts = url.pathname.split('/').filter(Boolean);
if (parts.length < 2) { if (parts.length < 2) {
throw new Error('Invalid GitHub URL'); 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> { export async function parseProblem(problem: string): Promise<string | undefined> {
if (!isGitHubIssuesOrPullUrl(problem)) { if (!isGitHubIssuesOrPullUrl(problem)) {
return undefined; return undefined;
@@ -176,9 +139,15 @@ export async function parseProblem(problem: string): Promise<string | undefined>
try { try {
const { owner, repo, number } = parseGitHubUrl(new URL(problem)); const { owner, repo, number } = parseGitHubUrl(new URL(problem));
const apiURL = new URL(`https://api.github.com/repos/${owner}/${repo}/issues/${number}`); const apiURL = new URL(`https://api.github.com/repos/${owner}/${repo}/issues/${number}`);
const { title } = await fetchGitHubApi(apiURL); const response = await fetch(apiURL);
return title; if (!response.ok) {
throw new GitHubApiError(`HTTP error! status: ${response.status}`, response.status);
}
const json = await response.json();
return json.title;
} catch (error) { } catch (error) {
console.error('Failed to parse problem:', error); console.error('Failed to parse problem:', error);
return undefined; return undefined;

48
src/lib/stores/github.ts Normal file
View 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;
}

View File

@@ -7,6 +7,7 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import '../app.css'; import '../app.css';
import SidePanelLayout from '../layouts/SidePanelLayout.svelte'; import SidePanelLayout from '../layouts/SidePanelLayout.svelte';
import { getCommitInfo } from '@/stores/github';
let sessionStarted = false; let sessionStarted = false;
let connected = false; let connected = false;
@@ -29,6 +30,7 @@
onMount(() => { onMount(() => {
getBitcoinTip(); getBitcoinTip();
getCommitInfo(new URL('https://github.com/nostrocket/hypergolic/'));
}); });
setInterval( setInterval(