Support fetching details for gitea repos

This commit is contained in:
Prathamesh Musale 2024-06-12 11:42:14 +05:30
parent b88ca4db83
commit 532d8483e2
2 changed files with 98 additions and 4 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@ dist/*
out
config.yml
.env
*~
.idea

View File

@ -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<RepoRecord> {
return repoRecord;
}
async function createGiteaRepoRecord (repoUrl: string): Promise<RepoRecord> {
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<RestEndpointMethodTypes["repos"]["get"]["response"]> {
async function getRepoDetails (octokit: Octokit, owner: string, repo: string): Promise<any> {
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<any> {
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<any> {
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<any> {
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}`);
}