add support for starring repositories during mirroring process in Gitea

This commit is contained in:
Arunavo Ray
2025-04-02 16:41:14 +05:30
parent 688211311a
commit a2f371c339
2 changed files with 51 additions and 10 deletions

View File

@@ -111,13 +111,13 @@ async function fetchStarredRepositories(octokit, options = {}) {
'X-GitHub-Api-Version': '2022-11-28'
}
})
.then(toRepositoryList);
.then(repos => toRepositoryList(repos.map(repo => ({...repo, starred: true}))));
}
// Default: Get starred repos for the authenticated user (what was previously used)
return octokit
.paginate("GET /user/starred")
.then(toRepositoryList);
.then(repos => toRepositoryList(repos.map(repo => ({...repo, starred: true}))));
}
async function fetchOrganizationRepositories(octokit, includeOrgs = [], excludeOrgs = [], preserveOrgStructure = false, options = {}) {
@@ -219,6 +219,11 @@ function toRepositoryList(repositories, preserveOrgStructure = false) {
repoInfo.organization = repository.organization;
}
// Preserve starred status if present
if (repository.starred) {
repoInfo.starred = true;
}
return repoInfo;
});
}

View File

@@ -365,22 +365,35 @@ async function mirrorIssues(repository, gitea, giteaTarget, githubToken, dryRun)
// Mirror a repository
async function mirror(repository, gitea, giteaTarget, githubToken, mirrorIssuesFlag, dryRun) {
if (await isAlreadyMirroredOnGitea(repository, gitea, giteaTarget)) {
console.log(
`Repository ${repository.name} is already mirrored in ${giteaTarget.type} ${giteaTarget.name}; doing nothing.`
);
return;
}
const isAlreadyMirrored = await isAlreadyMirroredOnGitea(repository, gitea, giteaTarget);
if (dryRun) {
// Special handling for starred repositories
if (repository.starred) {
if (isAlreadyMirrored) {
console.log(`Repository ${repository.name} is already mirrored in ${giteaTarget.type} ${giteaTarget.name}; checking if it needs to be starred.`);
await starRepositoryInGitea(repository, gitea, giteaTarget, dryRun);
return;
} else if (dryRun) {
console.log(`DRY RUN: Would mirror and star repository to ${giteaTarget.type} ${giteaTarget.name}: ${repository.name} (starred)`);
return;
}
} else if (isAlreadyMirrored) {
console.log(`Repository ${repository.name} is already mirrored in ${giteaTarget.type} ${giteaTarget.name}; doing nothing.`);
return;
} else if (dryRun) {
console.log(`DRY RUN: Would mirror repository to ${giteaTarget.type} ${giteaTarget.name}: ${repository.name}`);
return;
}
console.log(`Mirroring repository to ${giteaTarget.type} ${giteaTarget.name}: ${repository.name}`);
console.log(`Mirroring repository to ${giteaTarget.type} ${giteaTarget.name}: ${repository.name}${repository.starred ? ' (will be starred)' : ''}`);
try {
await mirrorOnGitea(repository, gitea, giteaTarget, githubToken);
// Star the repository if it's marked as starred
if (repository.starred) {
await starRepositoryInGitea(repository, gitea, giteaTarget, dryRun);
}
// Mirror issues if requested and not in dry run mode
if (mirrorIssuesFlag && !dryRun) {
await mirrorIssues(repository, gitea, giteaTarget, githubToken, dryRun);
@@ -390,6 +403,29 @@ async function mirror(repository, gitea, giteaTarget, githubToken, mirrorIssuesF
}
}
// Star a repository in Gitea
async function starRepositoryInGitea(repository, gitea, giteaTarget, dryRun) {
const ownerName = giteaTarget.name;
const repoName = repository.name;
if (dryRun) {
console.log(`DRY RUN: Would star repository in Gitea: ${ownerName}/${repoName}`);
return true;
}
try {
await request
.put(`${gitea.url}/api/v1/user/starred/${ownerName}/${repoName}`)
.set("Authorization", `token ${gitea.token}`);
console.log(`Successfully starred repository in Gitea: ${ownerName}/${repoName}`);
return true;
} catch (error) {
console.error(`Error starring repository ${ownerName}/${repoName}:`, error.message);
return false;
}
}
main().catch(error => {
console.error("Application error:", error);
process.exit(1);