diff --git a/.gitignore b/.gitignore index c36d7c2..d1c0f4f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ dist/* out config.yml +.env *~ -.idea \ No newline at end of file +.idea diff --git a/demo/scripts/create-repo-record.ts b/demo/scripts/create-repo-record.ts index 86b0007..1fd0c36 100644 --- a/demo/scripts/create-repo-record.ts +++ b/demo/scripts/create-repo-record.ts @@ -1,11 +1,18 @@ +import { config } from 'dotenv'; import yargs from 'yargs'; import path from 'path'; import fs from 'fs'; import assert from 'assert'; import { hideBin } from 'yargs/helpers'; +import axios from 'axios'; import { Octokit } from '@octokit/rest'; +// Load environment variables from .env file +config(); + +const GITEA_BASE_URL = 'https://git.vdb.to'; + interface RepoRecord { type: string version: string @@ -39,7 +46,12 @@ interface RepoRecord { async function main () { const argv = getArgs(); - const repoRecord = await createRepoRecord(argv.repoUrl); + let repoRecord: RepoRecord; + if (argv.repoUrl.includes('github.com')) { + repoRecord = await createRepoRecord(argv.repoUrl); + } else { + repoRecord = await createGiteaRepoRecord(argv.repoUrl); + } const jsonData = JSON.stringify(repoRecord, null, 2); @@ -112,6 +124,59 @@ async function createRepoRecord (repoUrl: string): Promise { return repoRecord; } +async function createGiteaRepoRecord (repoUrl: string): Promise { + const giteaPAT = process.env.GITEA_TOKEN; + assert(giteaPAT, 'GITEA_TOKEN not provided'); + + const { owner, repo } = extractOwnerAndRepo(repoUrl); + + // fetch repo details + const data = await getGiteaRepoDetails(giteaPAT, owner, repo); + + const repoRecord: RepoRecord = { + type: 'RepositoryRecord', + version: '0.1.0', // fetch from registry and increment if exists + schema: '', + name: data.name, + url: data.html_url, + clone_url: data.clone_url, + owner: { + name: data.owner.login, + // type: data.owner.type, + type: 'Organization', + url: data.owner.html_url + }, + is_private: data.private, + is_archived: data.archived, + default_branch: data.default_branch, + latest_tag: null, + is_fork: data.fork, + fork_source: null, + license: null + }; + + // populate fork source + if (data.fork) { + repoRecord.fork_source = { + name: data.parent.name, + url: data.parent.html_url + }; + } + + // TODO: populate license + + // fetch and populate latest_tag + const latestTag = await getGiteaRepoLatestTag(giteaPAT, owner, repo); + if (latestTag) { + repoRecord.latest_tag = { + tag_name: latestTag.tag_name, + url: latestTag.html_url + }; + } + + return repoRecord; +} + // async function getRepoDetails (octokit: Octokit, repoUrl: string): Promise { async function getRepoDetails (octokit: Octokit, owner: string, repo: string): Promise { try { @@ -126,6 +191,18 @@ async function getRepoDetails (octokit: Octokit, owner: string, repo: string): P } } +async function getGiteaRepoDetails (token: string, owner: string, repo: string): Promise { + try { + const response = await axios.get(`${GITEA_BASE_URL}/api/v1/repos/${owner}/${repo}`, { + headers: { Authorization: `token ${token}` } + }); + + return response.data; + } catch (error) { + console.error('Error fetching repository:', error); + } +} + async function getRepoLatestTag (octokit: Octokit, owner: string, repo: string): Promise { try { const response = await octokit.repos.getLatestRelease({ @@ -139,13 +216,29 @@ async function getRepoLatestTag (octokit: Octokit, owner: string, repo: string): return null; } - console.error('Error fetching repository:', error); + console.error('Error fetching latest release:', error); + } +} + +async function getGiteaRepoLatestTag (token: string, owner: string, repo: string): Promise { + try { + const response = await axios.get(`${GITEA_BASE_URL}/api/v1/repos/${owner}/${repo}/releases/latest`, { + headers: { Authorization: `token ${token}` } + }); + + return response.data; + } catch (error: any) { + if ((error as Error).message.includes('code 404')) { + return null; + } + + console.error('Error fetching latest release:', error); } } function extractOwnerAndRepo (url: string): { owner: string; repo: string } { // eslint-disable-next-line no-useless-escape - const match = url.match(/github\.com\/([^\/]+)\/([^\/]+)(\/|$)/); + const match = url.match(/(?:github\.com|git\.vdb\.to)\/([^\/]+)\/([^\/]+)(\/|$)/); if (!match) { throw new Error(`Unable to extract repo owner and name from the given URL: ${url}`); }