gogs-api.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env tsx
  2. /**
  3. * Simple script to interact with Gogs API for issue management
  4. */
  5. import axios from 'axios';
  6. import { config } from 'dotenv';
  7. // Load environment variables
  8. config();
  9. const GOGS_SERVER_URL = process.env.GOGS_SERVER_URL?.replace(/\/$/, '');
  10. const GOGS_ACCESS_TOKEN = process.env.GOGS_ACCESS_TOKEN;
  11. if (!GOGS_SERVER_URL || !GOGS_ACCESS_TOKEN) {
  12. console.error('Error: GOGS_SERVER_URL and GOGS_ACCESS_TOKEN must be set');
  13. process.exit(1);
  14. }
  15. const client = axios.create({
  16. baseURL: `${GOGS_SERVER_URL}/api/v1`,
  17. headers: {
  18. 'Content-Type': 'application/json',
  19. Authorization: `token ${GOGS_ACCESS_TOKEN}`,
  20. },
  21. });
  22. async function getIssue(owner: string, repo: string, number: number) {
  23. const response = await client.get(`/repos/${owner}/${repo}/issues/${number}`);
  24. return response.data;
  25. }
  26. async function listIssueComments(owner: string, repo: string, number: number) {
  27. const response = await client.get(`/repos/${owner}/${repo}/issues/${number}/comments`);
  28. return response.data;
  29. }
  30. async function createIssueComment(owner: string, repo: string, number: number, body: string) {
  31. const response = await client.post(`/repos/${owner}/${repo}/issues/${number}/comments`, { body });
  32. return response.data;
  33. }
  34. // Main execution
  35. const args = process.argv.slice(2);
  36. const command = args[0];
  37. (async () => {
  38. try {
  39. if (command === 'get-issue') {
  40. const [owner, repo, number] = args.slice(1);
  41. const issue = await getIssue(owner, repo, parseInt(number));
  42. console.log(JSON.stringify(issue, null, 2));
  43. } else if (command === 'list-comments') {
  44. const [owner, repo, number] = args.slice(1);
  45. const comments = await listIssueComments(owner, repo, parseInt(number));
  46. console.log(JSON.stringify(comments, null, 2));
  47. } else if (command === 'create-comment') {
  48. const [owner, repo, number, ...bodyParts] = args.slice(1);
  49. const body = bodyParts.join(' ');
  50. const comment = await createIssueComment(owner, repo, parseInt(number), body);
  51. console.log(JSON.stringify(comment, null, 2));
  52. } else {
  53. console.error('Unknown command. Use: get-issue, list-comments, or create-comment');
  54. process.exit(1);
  55. }
  56. } catch (error: any) {
  57. console.error('Error:', error.response?.data || error.message);
  58. process.exit(1);
  59. }
  60. })();