#!/usr/bin/env tsx /** * Simple script to interact with Gogs API for issue management */ import axios from 'axios'; import { config } from 'dotenv'; // Load environment variables config(); const GOGS_SERVER_URL = process.env.GOGS_SERVER_URL?.replace(/\/$/, ''); const GOGS_ACCESS_TOKEN = process.env.GOGS_ACCESS_TOKEN; if (!GOGS_SERVER_URL || !GOGS_ACCESS_TOKEN) { console.error('Error: GOGS_SERVER_URL and GOGS_ACCESS_TOKEN must be set'); process.exit(1); } const client = axios.create({ baseURL: `${GOGS_SERVER_URL}/api/v1`, headers: { 'Content-Type': 'application/json', Authorization: `token ${GOGS_ACCESS_TOKEN}`, }, }); async function getIssue(owner: string, repo: string, number: number) { const response = await client.get(`/repos/${owner}/${repo}/issues/${number}`); return response.data; } async function listIssueComments(owner: string, repo: string, number: number) { const response = await client.get(`/repos/${owner}/${repo}/issues/${number}/comments`); return response.data; } async function createIssueComment(owner: string, repo: string, number: number, body: string) { const response = await client.post(`/repos/${owner}/${repo}/issues/${number}/comments`, { body }); return response.data; } // Main execution const args = process.argv.slice(2); const command = args[0]; (async () => { try { if (command === 'get-issue') { const [owner, repo, number] = args.slice(1); const issue = await getIssue(owner, repo, parseInt(number)); console.log(JSON.stringify(issue, null, 2)); } else if (command === 'list-comments') { const [owner, repo, number] = args.slice(1); const comments = await listIssueComments(owner, repo, parseInt(number)); console.log(JSON.stringify(comments, null, 2)); } else if (command === 'create-comment') { const [owner, repo, number, ...bodyParts] = args.slice(1); const body = bodyParts.join(' '); const comment = await createIssueComment(owner, repo, parseInt(number), body); console.log(JSON.stringify(comment, null, 2)); } else { console.error('Unknown command. Use: get-issue, list-comments, or create-comment'); process.exit(1); } } catch (error: any) { console.error('Error:', error.response?.data || error.message); process.exit(1); } })();