Skip to content
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

sync remote repos outside a workflow ctx #115

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

coopernetes
Copy link

  • add a bunch of new inputs into the action to allow a remote repo/host to be used as the upstream or destination of the sync.
  • the new API surface on the action isn't the best but should be compatible with the existing usage (relying on running the workflow on the source repo and read in the builtin env vars from GitHub Actions)

related: #114

I have only tested this on a local setup and it seems to work well enough both with the existing inputs and the new ones. It could use some more iteration. See logs:

Using GitHub Actions env vars (simulating a workflow run)

$ DEBUG=1 GITHUB_REPOSITORY=coopernetes/test-repo-sync GITHUB_ACTOR=coopernetes GITHUB_TOKEN="github_pat_token" SYNC_TAGS=true ../github-sync/github-sync.sh "https://github.com/coopernetes/test-repo" "main:main"
+ UPSTREAM_REPO=https://github.com/coopernetes/test-repo
+ BRANCH_MAPPING=main:main
+ '[' -z https://github.com/coopernetes/test-repo ']'
+ '[' -z main:main ']'
+ echo https://github.com/coopernetes/test-repo
+ grep -Eq ':|@|\.git/?$'
+ echo UPSTREAM_REPO=https://github.com/coopernetes/test-repo
UPSTREAM_REPO=https://github.com/coopernetes/test-repo
+ echo BRANCHES=main:main
BRANCHES=main:main
+ git config --unset-all http.https://github.com/.extraheader
+ :
+ _hostname=github.com
+ _user=coopernetes
+ _token=github_pat_token
+ _repo=coopernetes/test-repo-sync
+ '[' -z coopernetes ']'
+ '[' -z github_pat_token ']'
+ '[' -z coopernetes/test-repo-sync ']'
+ echo 'Resetting origin to: https://coopernetes:***@github.com/coopernetes/test-repo-sync'
Resetting origin to: https://coopernetes:***@github.com/coopernetes/test-repo-sync
+ git remote set-url origin https://coopernetes:github_pat_token@github.com/coopernetes/test-repo-sync
+ echo 'Adding tmp_upstream https://github.com/coopernetes/test-repo'
Adding tmp_upstream https://github.com/coopernetes/test-repo
+ '[' -n '' ']'
+ git remote add tmp_upstream https://github.com/coopernetes/test-repo
+ echo 'Fetching tmp_upstream'
Fetching tmp_upstream
+ git fetch tmp_upstream --quiet
+ git remote --verbose
origin	https://coopernetes:github_pat_token@github.com/coopernetes/test-repo-sync (fetch)
origin	https://coopernetes:github_pat_token@github.com/coopernetes/test-repo-sync (push)
tmp_upstream	https://github.com/coopernetes/test-repo (fetch)
tmp_upstream	https://github.com/coopernetes/test-repo (push)
+ echo 'Pushing changings from tmp_upstream to origin'
Pushing changings from tmp_upstream to origin
+ git push origin refs/remotes/tmp_upstream/main:refs/heads/main --force
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 907 bytes | 907.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/coopernetes/test-repo-sync
 + b6b6257...aad2d2c tmp_upstream/main -> main (forced update)
+ '[' true = true ']'
+ echo 'Force syncing all tags'
Force syncing all tags
++ git tag -l
+ git tag -d bar-tag
+ git fetch tmp_upstream --tags --quiet
+ git push origin --tags --force
Everything up-to-date
+ echo 'Removing tmp_upstream'
Removing tmp_upstream
+ git remote rm tmp_upstream
+ git remote --verbose
origin	https://coopernetes:github_pat_token@github.com/coopernetes/test-repo-sync (fetch)
origin	https://coopernetes:github_pat_token@github.com/coopernetes/test-repo-sync (push)

Sync from github.com to gitlab.com

$ DEBUG=1 DESTINATION_HOST=gitlab.com DESTINATION_REPO=tomcooperca/test-repo DESTINATION_TOKEN=$(cat ~/.gitlab-token) DESTINATION_USER=oauth2 ../github-sync/github-sync.sh "https://github.com/coopernetes/test-repo-sync" "main:main"
+ UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
+ BRANCH_MAPPING=main:main
+ '[' -z https://github.com/coopernetes/test-repo-sync ']'
+ '[' -z main:main ']'
+ echo https://github.com/coopernetes/test-repo-sync
+ grep -Eq ':|@|\.git/?$'
+ echo UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
+ echo BRANCHES=main:main
BRANCHES=main:main
+ git config --unset-all http.https://github.com/.extraheader
+ :
+ _hostname=gitlab.com
+ _user=oauth2
+ _token=glpat-token
+ _repo=tomcooperca/test-repo
+ '[' -z oauth2 ']'
+ '[' -z glpat-token ']'
+ '[' -z tomcooperca/test-repo ']'
+ echo 'Resetting origin to: https://oauth2:***@gitlab.com/tomcooperca/test-repo'
Resetting origin to: https://oauth2:***@gitlab.com/tomcooperca/test-repo
+ git remote set-url origin https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo
+ echo 'Adding tmp_upstream https://github.com/coopernetes/test-repo-sync'
Adding tmp_upstream https://github.com/coopernetes/test-repo-sync
+ '[' -n '' ']'
+ git remote add tmp_upstream https://github.com/coopernetes/test-repo-sync
+ echo 'Fetching tmp_upstream'
Fetching tmp_upstream
+ git fetch tmp_upstream --quiet
+ git remote --verbose
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (fetch)
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (push)
tmp_upstream	https://github.com/coopernetes/test-repo-sync (fetch)
tmp_upstream	https://github.com/coopernetes/test-repo-sync (push)
+ echo 'Pushing changings from tmp_upstream to origin'
Pushing changings from tmp_upstream to origin
+ git push origin refs/remotes/tmp_upstream/main:refs/heads/main --force
warning: redirecting to https://gitlab.com/tomcooperca/test-repo.git/
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 617 bytes | 617.00 KiB/s, done.
Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
To https://gitlab.com/tomcooperca/test-repo
 + 9125584...b6b6257 tmp_upstream/main -> main (forced update)
+ '[' '' = true ']'
+ echo 'Removing tmp_upstream'
Removing tmp_upstream
+ git remote rm tmp_upstream
+ git remote --verbose
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (fetch)
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (push)

Sync tags on remote

$ DEBUG=1 DESTINATION_HOST=gitlab.com DESTINATION_REPO=tomcooperca/test-repo DESTINATION_TOKEN=$(cat ~/.gitlab-token) DESTINATION_USER=oauth2 SYNC_TAGS=true ../github-sync/github-sync.sh "https://github.com/coopernetes/test-repo-sync" "main:main"
+ UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
+ BRANCH_MAPPING=main:main
+ '[' -z https://github.com/coopernetes/test-repo-sync ']'
+ '[' -z main:main ']'
+ echo https://github.com/coopernetes/test-repo-sync
+ grep -Eq ':|@|\.git/?$'
+ echo UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
UPSTREAM_REPO=https://github.com/coopernetes/test-repo-sync
+ echo BRANCHES=main:main
BRANCHES=main:main
+ git config --unset-all http.https://github.com/.extraheader
+ :
+ _hostname=gitlab.com
+ _user=oauth2
+ _token=glpat-token
+ _repo=tomcooperca/test-repo
+ '[' -z oauth2 ']'
+ '[' -z glpat-token ']'
+ '[' -z tomcooperca/test-repo ']'
+ echo 'Resetting origin to: https://oauth2:***@gitlab.com/tomcooperca/test-repo'
Resetting origin to: https://oauth2:***@gitlab.com/tomcooperca/test-repo
+ git remote set-url origin https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo
+ echo 'Adding tmp_upstream https://github.com/coopernetes/test-repo-sync'
Adding tmp_upstream https://github.com/coopernetes/test-repo-sync
+ '[' -n '' ']'
+ git remote add tmp_upstream https://github.com/coopernetes/test-repo-sync
+ echo 'Fetching tmp_upstream'
Fetching tmp_upstream
+ git fetch tmp_upstream --quiet
+ git remote --verbose
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (fetch)
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (push)
tmp_upstream	https://github.com/coopernetes/test-repo-sync (fetch)
tmp_upstream	https://github.com/coopernetes/test-repo-sync (push)
+ echo 'Pushing changings from tmp_upstream to origin'
Pushing changings from tmp_upstream to origin
+ git push origin refs/remotes/tmp_upstream/main:refs/heads/main --force
warning: redirecting to https://gitlab.com/tomcooperca/test-repo.git/
Everything up-to-date
+ '[' true = true ']'
+ echo 'Force syncing all tags'
Force syncing all tags
++ git tag -l
+ git tag -d bar-tag
+ git fetch tmp_upstream --tags --quiet
+ git push origin --tags --force
warning: redirecting to https://gitlab.com/tomcooperca/test-repo.git/
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 368 bytes | 368.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To https://gitlab.com/tomcooperca/test-repo
 * [new tag]         bar-tag -> bar-tag
+ echo 'Removing tmp_upstream'
Removing tmp_upstream
+ git remote rm tmp_upstream
+ git remote --verbose
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (fetch)
origin	https://oauth2:glpat-token@gitlab.com/tomcooperca/test-repo (push)

* add a bunch of new inputs into the action to allow a remote repo/host to be used as the upstream or destination of the sync.
* the new API surface on the action isn't the best but should be compatible with the existing usage (relying on running the workflow on the source repo and read in the builtin env vars from GitHub Actions)

related: repo-sync#114
@coopernetes coopernetes marked this pull request as draft January 4, 2024 04:56
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.

None yet

1 participant