Compare commits

...

2 Commits

4 changed files with 390 additions and 2 deletions

3
.gitignore vendored
View File

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

View File

@ -0,0 +1,276 @@
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
schema: string
name: string
url: string
clone_url: string
owner: {
name: string
type: 'Organization' | 'User'
url: string
}
is_private: boolean
is_archived: boolean
default_branch: string
latest_tag: {
tag_name: string
url: string
} | null
is_fork: boolean
fork_source: {
name: string
url: string
} | null
license: {
name: string
spdx_id: string
} | null
}
async function main () {
const argv = getArgs();
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);
const recordsDir = path.resolve(argv.recordsDir);
const recordFile = `${recordsDir}/${repoRecord.name}.json`;
fs.writeFileSync(recordFile, jsonData, 'utf8');
console.log(`Repo record written to ${recordFile}`);
}
async function createRepoRecord (repoUrl: string): Promise<RepoRecord> {
const githubPAT = process.env.GITHUB_TOKEN;
assert(githubPAT, 'GITHUB_TOKEN not provided');
const octokit = new Octokit({
auth: githubPAT
});
const { owner, repo } = extractOwnerAndRepo(repoUrl);
// fetch repo details
const data = await getRepoDetails(octokit, 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,
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.source.name,
url: data.source.html_url
};
}
// populate license
if (data.license) {
repoRecord.license = {
name: data.license.name,
spdx_id: data.license.spdx_id
};
}
// fetch and populate latest_tag
const latestTag = await getRepoLatestTag(octokit, owner, repo);
if (latestTag) {
repoRecord.latest_tag = {
tag_name: latestTag.tag_name,
url: latestTag.html_url
};
}
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 {
const response = await octokit.repos.get({
owner: owner,
repo: repo
});
return response.data;
} catch (error) {
console.error('Error fetching repository:', error);
}
}
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({
owner: owner,
repo: repo
});
return response.data;
} catch (error: any) {
if ((error as Error).message.includes('Not Found')) {
return null;
}
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|git\.vdb\.to)\/([^\/]+)\/([^\/]+)(\/|$)/);
if (!match) {
throw new Error(`Unable to extract repo owner and name from the given URL: ${url}`);
}
return { owner: match[1], repo: match[2] };
}
function getArgs (): any {
return yargs(hideBin(process.argv)).parserConfiguration({
'parse-numbers': false
}).usage('Usage: $0 [options]')
.option('config', {
alias: 'c',
describe: 'Config',
type: 'string'
})
.option('repoUrl', {
alias: 'r',
describe: 'Repository URL',
type: 'string',
demandOption: true
})
.option('recordsDir', {
alias: 'd',
describe: 'Directory to export the repo record to',
type: 'string',
demandOption: true
})
.help().argv;
}
main()
.catch(err => {
console.error(err);
});

View File

@ -6,6 +6,7 @@
"author": "",
"license": "UNLICENSED",
"devDependencies": {
"@octokit/rest": "^20.1.1",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^27.4.1",
"@types/js-yaml": "^4.0.5",

112
yarn.lock
View File

@ -1276,6 +1276,101 @@
resolved "https://registry.yarnpkg.com/@octetstream/promisify/-/promisify-2.0.2.tgz#29ac3bd7aefba646db670227f895d812c1a19615"
integrity sha512-7XHoRB61hxsz8lBQrjC1tq/3OEIgpvGWg6DKAdwi7WRzruwkmsdwmOoUXbU4Dtd4RSOMDwed0SkP3y8UlMt1Bg==
"@octokit/auth-token@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-4.0.0.tgz#40d203ea827b9f17f42a29c6afb93b7745ef80c7"
integrity sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA==
"@octokit/core@^5.0.2":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-5.2.0.tgz#ddbeaefc6b44a39834e1bb2e58a49a117672a7ea"
integrity sha512-1LFfa/qnMQvEOAdzlQymH0ulepxbxnCYAKJZfMci/5XJyIHWgEYnDmgnKakbTh7CH2tFQ5O60oYDvns4i9RAIg==
dependencies:
"@octokit/auth-token" "^4.0.0"
"@octokit/graphql" "^7.1.0"
"@octokit/request" "^8.3.1"
"@octokit/request-error" "^5.1.0"
"@octokit/types" "^13.0.0"
before-after-hook "^2.2.0"
universal-user-agent "^6.0.0"
"@octokit/endpoint@^9.0.1":
version "9.0.5"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-9.0.5.tgz#e6c0ee684e307614c02fc6ac12274c50da465c44"
integrity sha512-ekqR4/+PCLkEBF6qgj8WqJfvDq65RH85OAgrtnVp1mSxaXF03u2xW/hUdweGS5654IlC0wkNYC18Z50tSYTAFw==
dependencies:
"@octokit/types" "^13.1.0"
universal-user-agent "^6.0.0"
"@octokit/graphql@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-7.1.0.tgz#9bc1c5de92f026648131f04101cab949eeffe4e0"
integrity sha512-r+oZUH7aMFui1ypZnAvZmn0KSqAUgE1/tUXIWaqUCa1758ts/Jio84GZuzsvUkme98kv0WFY8//n0J1Z+vsIsQ==
dependencies:
"@octokit/request" "^8.3.0"
"@octokit/types" "^13.0.0"
universal-user-agent "^6.0.0"
"@octokit/openapi-types@^22.2.0":
version "22.2.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-22.2.0.tgz#75aa7dcd440821d99def6a60b5f014207ae4968e"
integrity sha512-QBhVjcUa9W7Wwhm6DBFu6ZZ+1/t/oYxqc2tp81Pi41YNuJinbFRx8B133qVOrAaBbF7D/m0Et6f9/pZt9Rc+tg==
"@octokit/plugin-paginate-rest@11.3.1":
version "11.3.1"
resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-11.3.1.tgz#fe92d04b49f134165d6fbb716e765c2f313ad364"
integrity sha512-ryqobs26cLtM1kQxqeZui4v8FeznirUsksiA+RYemMPJ7Micju0WSkv50dBksTuZks9O5cg4wp+t8fZ/cLY56g==
dependencies:
"@octokit/types" "^13.5.0"
"@octokit/plugin-request-log@^4.0.0":
version "4.0.1"
resolved "https://registry.yarnpkg.com/@octokit/plugin-request-log/-/plugin-request-log-4.0.1.tgz#98a3ca96e0b107380664708111864cb96551f958"
integrity sha512-GihNqNpGHorUrO7Qa9JbAl0dbLnqJVrV8OXe2Zm5/Y4wFkZQDfTreBzVmiRfJVfE4mClXdihHnbpyyO9FSX4HA==
"@octokit/plugin-rest-endpoint-methods@13.2.2":
version "13.2.2"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-13.2.2.tgz#af8e5dd2cddfea576f92ffaf9cb84659f302a638"
integrity sha512-EI7kXWidkt3Xlok5uN43suK99VWqc8OaIMktY9d9+RNKl69juoTyxmLoWPIZgJYzi41qj/9zU7G/ljnNOJ5AFA==
dependencies:
"@octokit/types" "^13.5.0"
"@octokit/request-error@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-5.1.0.tgz#ee4138538d08c81a60be3f320cd71063064a3b30"
integrity sha512-GETXfE05J0+7H2STzekpKObFe765O5dlAKUTLNGeH+x47z7JjXHfsHKo5z21D/o/IOZTUEI6nyWyR+bZVP/n5Q==
dependencies:
"@octokit/types" "^13.1.0"
deprecation "^2.0.0"
once "^1.4.0"
"@octokit/request@^8.3.0", "@octokit/request@^8.3.1":
version "8.4.0"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-8.4.0.tgz#7f4b7b1daa3d1f48c0977ad8fffa2c18adef8974"
integrity sha512-9Bb014e+m2TgBeEJGEbdplMVWwPmL1FPtggHQRkV+WVsMggPtEkLKPlcVYm/o8xKLkpJ7B+6N8WfQMtDLX2Dpw==
dependencies:
"@octokit/endpoint" "^9.0.1"
"@octokit/request-error" "^5.1.0"
"@octokit/types" "^13.1.0"
universal-user-agent "^6.0.0"
"@octokit/rest@^20.1.1":
version "20.1.1"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-20.1.1.tgz#ec775864f53fb42037a954b9a40d4f5275b3dc95"
integrity sha512-MB4AYDsM5jhIHro/dq4ix1iWTLGToIGk6cWF5L6vanFaMble5jTX/UBQyiv05HsWnwUtY8JrfHy2LWfKwihqMw==
dependencies:
"@octokit/core" "^5.0.2"
"@octokit/plugin-paginate-rest" "11.3.1"
"@octokit/plugin-request-log" "^4.0.0"
"@octokit/plugin-rest-endpoint-methods" "13.2.2"
"@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.5.0":
version "13.5.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.5.0.tgz#4796e56b7b267ebc7c921dcec262b3d5bfb18883"
integrity sha512-HdqWTf5Z3qwDVlzCrP8UJquMwunpDiMPt5er+QjGzL4hqr/vBVY/MauQgS1xWxCDT1oMx1EULyqxncdCY/NVSQ==
dependencies:
"@octokit/openapi-types" "^22.2.0"
"@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf"
@ -1957,6 +2052,11 @@ bech32@^2.0.0:
resolved "https://registry.yarnpkg.com/bech32/-/bech32-2.0.0.tgz#078d3686535075c8c79709f054b1b226a133b355"
integrity sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==
before-after-hook@^2.2.0:
version "2.2.3"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==
big-integer@1.6.36:
version "1.6.36"
resolved "https://registry.yarnpkg.com/big-integer/-/big-integer-1.6.36.tgz#78631076265d4ae3555c04f85e7d9d2f3a071a36"
@ -2371,6 +2471,11 @@ delayed-stream@~1.0.0:
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==
deprecation@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==
detect-newline@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/detect-newline/-/detect-newline-3.1.0.tgz#576f5dfc63ae1a192ff192d8ad3af6308991b651"
@ -4334,7 +4439,7 @@ object.values@^1.1.7:
define-properties "^1.2.0"
es-abstract "^1.22.1"
once@^1.3.0:
once@^1.3.0, once@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
@ -5165,6 +5270,11 @@ undici-types@~5.26.4:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==
universal-user-agent@^6.0.0:
version "6.0.1"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.1.tgz#15f20f55da3c930c57bddbf1734c6654d5fd35aa"
integrity sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==
universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"