Skip to content

Enhance Organization Mirroring with Improved Reliability and New Options #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

arunavo4
Copy link
Contributor

@arunavo4 arunavo4 commented Apr 15, 2025

Closes:

  1. Organisation mirror not working #31
  2. How to mirror into an organization instead of an user #20

This PR enhances the organization mirroring functionality in mirror-to-gitea with improved reliability and new options. It addresses issues with fetching organizations and repositories, particularly when dealing with organizations that have specific token policies.

Key Changes

  1. Added New Option:

    • ONLY_MIRROR_ORGS: When set to true, only repositories from organizations will be mirrored, skipping personal repositories. This provides better control over what gets mirrored.
  2. Improved Organization Fetching:

    • Added multiple approaches to fetch organizations, with fallbacks if one method fails
    • Added support for handling organizations with token lifetime restrictions
    • Added detailed error messages to help users troubleshoot issues
  3. Enhanced Repository Fetching:

    • Added multiple approaches to fetch repositories from organizations
    • Added fallbacks to different API endpoints if one fails
    • Added checks to verify organization membership and repository access
  4. Better Error Handling:

    • Added specific error messages for common issues, such as token lifetime restrictions
    • Added detailed logging to help diagnose issues
    • Added helpful suggestions for resolving issues
  5. Updated Documentation:

    • Added documentation for the new ONLY_MIRROR_ORGS option
    • Added a section on testing organization mirroring
    • Added information about GitHub token requirements for organization access
  6. Added Testing Script:

    • Added test-org-mirror.sh script for testing organization mirroring specifically

Testing

The changes have been tested with various GitHub organizations, including those with token lifetime restrictions. The code now properly handles these cases and provides helpful error messages to guide users in resolving issues.

To test these changes:

  1. Update your GitHub token if needed (some organizations require tokens with lifetimes less than 366 days)
  2. Run the test-org-mirror.sh script to test organization mirroring specifically
  3. Check that organizations are properly created in Gitea and repositories are mirrored correctly

@arunavo4
Copy link
Contributor Author

arunavo4 commented Apr 15, 2025

@jaedle
This PR Fixes issue #31 you can quickly test it

#!/bin/bash
# Docker test commands for mirror-to-gitea

# Replace these variables with your actual values
GITHUB_USERNAME="your-github-username"
GITEA_URL="http://your-gitea-instance:3000/"
GITEA_TOKEN="your-gitea-token"
GITHUB_TOKEN="your-github-token"

# Basic test with dry run mode
echo "Running basic test with dry run..."
docker run -it --rm \
  -e GITHUB_USERNAME="$GITHUB_USERNAME" \
  -e GITEA_URL="$GITEA_URL" \
  -e GITEA_TOKEN="$GITEA_TOKEN" \
  -e GITHUB_TOKEN="$GITHUB_TOKEN" \
  -e MIRROR_ORGANIZATIONS=true \
  -e DRY_RUN=true \
  arunavo4/mirror-to-gitea:latest

# Test organization mirroring only (no personal repos)
echo "Running organization mirroring test..."
docker run -it --rm \
  -e GITHUB_USERNAME="$GITHUB_USERNAME" \
  -e GITEA_URL="$GITEA_URL" \
  -e GITEA_TOKEN="$GITEA_TOKEN" \
  -e GITHUB_TOKEN="$GITHUB_TOKEN" \
  -e MIRROR_ORGANIZATIONS=true \
  -e ONLY_MIRROR_ORGS=true \
  -e PRESERVE_ORG_STRUCTURE=true \
  -e DRY_RUN=true \
  arunavo4/mirror-to-gitea:latest

# Test with specific organizations included
echo "Running test with specific organizations..."
docker run -it --rm \
  -e GITHUB_USERNAME="$GITHUB_USERNAME" \
  -e GITEA_URL="$GITEA_URL" \
  -e GITEA_TOKEN="$GITEA_TOKEN" \
  -e GITHUB_TOKEN="$GITHUB_TOKEN" \
  -e MIRROR_ORGANIZATIONS=true \
  -e INCLUDE_ORGS="org1,org2" \
  -e PRESERVE_ORG_STRUCTURE=true \
  -e DRY_RUN=true \
  arunavo4/mirror-to-gitea:latest

# Uncomment to run with actual mirroring (no dry run)
# echo "Running actual mirroring..."
# docker run -it --rm \
#   -e GITHUB_USERNAME="$GITHUB_USERNAME" \
#   -e GITEA_URL="$GITEA_URL" \
#   -e GITEA_TOKEN="$GITEA_TOKEN" \
#   -e GITHUB_TOKEN="$GITHUB_TOKEN" \
#   -e MIRROR_ORGANIZATIONS=true \
#   -e ONLY_MIRROR_ORGS=true \
#   -e PRESERVE_ORG_STRUCTURE=true \
#   -e DRY_RUN=false \
#   arunavo4/mirror-to-gitea:latest

# Uncomment to run as a background service
# echo "Running as background service..."
# docker run -d --name mirror-to-gitea \
#   -e GITHUB_USERNAME="$GITHUB_USERNAME" \
#   -e GITEA_URL="$GITEA_URL" \
#   -e GITEA_TOKEN="$GITEA_TOKEN" \
#   -e GITHUB_TOKEN="$GITHUB_TOKEN" \
#   -e MIRROR_ORGANIZATIONS=true \
#   -e ONLY_MIRROR_ORGS=true \
#   -e PRESERVE_ORG_STRUCTURE=true \
#   -e DRY_RUN=false \
#   arunavo4/mirror-to-gitea:latest

@CrazyWolf13
Copy link

@arunavo4
I though true/false normally has no quotes in docker?

@arunavo4
Copy link
Contributor Author

arunavo4 commented Apr 15, 2025

@arunavo4 I though true/false normally has no quotes in docker?

@CrazyWolf13 You are right, I have updated and fixed it.

arunavo4 added 11 commits April 15, 2025 13:08
This commit adds two major improvements:

1. Add public organization mirroring feature:
   - New MIRROR_PUBLIC_ORGS option to enable mirroring of public organizations
   - New PUBLIC_ORGS option to specify which public organizations to mirror
   - Separate handling for public organizations vs. member organizations
   - Updated documentation with examples

2. Remove hardcoded organization names:
   - Removed hardcoded organization names from the codebase
   - Added proper handling for empty organization lists
   - Made organization name matching case-insensitive
   - Updated debug script to use environment variables

These changes allow users to mirror public repositories from any GitHub
organization (even if they're not a member) and fix issues with case
sensitivity in organization names.
This commit adds tests for the new configuration options:
- Add tests for MIRROR_PUBLIC_ORGS flag
- Add tests for PUBLIC_ORGS list parsing
- Add test for GitHub token requirement with public organization mirroring
- Add skipped test placeholder for public organization repository fetching

The tests ensure that the new configuration options are properly parsed
and validated, maintaining code quality and test coverage for the new
feature.
This commit fixes an issue where the early return in the organization handling
code was preventing the rest of the function from executing, which could
cause single repository migrations to be skipped.

The fix:
1. Removes the early return when no organizations are specified in INCLUDE_ORGS
2. Properly handles the empty organization list case
3. Fixes indentation and code structure for better readability

This ensures that single repository migrations and other functionality
continue to work even when no organizations are specified.
This commit adds several improvements to help diagnose and fix issues with
organization repository fetching:

1. Add multiple fallback mechanisms for fetching organization repositories:
   - Try additional API endpoints when standard methods fail
   - Add more robust error handling and retry logic

2. Add detailed logging throughout the process:
   - Log repository counts and details
   - Log organization access information
   - Show detailed information about repositories being mirrored

3. Improve user feedback:
   - Provide clearer error messages
   - Show more information about what's happening during the process

These changes should help diagnose and fix issues where organizations are
detected but their repositories aren't being mirrored properly.
This commit fixes a syntax error in the src/index.mjs file that was causing
the Docker build to fail. The issue was related to mismatched try-catch blocks
that resulted in an "Unexpected catch" error during the build process.

The fix:
1. Restructured the try-catch blocks to ensure proper nesting
2. Fixed indentation issues for better code readability
3. Ensured all try blocks have matching catch blocks

This ensures the Docker build process completes successfully for both
amd64 and arm64 architectures.
This commit fixes an issue where the tool would detect organizations but not
fetch or mirror their repositories. The problem was that the code would return
early when no organizations were specified in INCLUDE_ORGS, even if organizations
were found through the public endpoint.

The fix:
1. Refactored the organization repository fetching logic into a separate function
2. Removed the early return when no organizations are specified in INCLUDE_ORGS
3. Added logic to use organizations found through the public endpoint

This ensures that repositories from detected organizations (like community-scripts)
are properly fetched and mirrored, even when not explicitly specified in INCLUDE_ORGS.
This commit fixes an issue where the tool would detect organizations through
the public endpoint but not fetch or mirror their repositories. The problem
was that the detected organizations weren't being added to the includeOrgs
list for repository fetching.

The fix:
1. Add code to add detected organizations to the includeOrgs list
2. Log the updated includeOrgs list for debugging
3. Ensure repositories from detected organizations are fetched and mirrored

This ensures that repositories from detected organizations (like community-scripts)
are properly fetched and mirrored, even when not explicitly specified in INCLUDE_ORGS.
This commit adds a fallback mechanism to try some default organizations
(community-scripts, Proxmox) when no organizations are specified in INCLUDE_ORGS
and none are found through the standard endpoints.

The fix:
1. Add a list of default organizations to try when no organizations are found
2. Try to fetch each default organization and add it to the list if found
3. Process the default organizations if any are found

This restores the previous behavior of trying some common organizations while
still allowing the user to override it by specifying organizations in INCLUDE_ORGS.
…ild, and security scanning"

This reverts commit da65c95.
#!/bin/bash
# Script to build and push multi-architecture Docker images for mirror-to-gitea

DOCKER_USERNAME="arunavo4"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably needs changing for this repo :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea missed it thanks, will update it with other stuff

@arunavo4
Copy link
Contributor Author

gitea-mirror

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants