Compare commits

..

19 Commits

Author SHA1 Message Date
Shawn Hartsell 37f8b6bee9 use old runner 2024-04-22 19:24:48 -05:00
Shawn Hartsell d3bf9767f7 removing win 2024-04-22 19:20:41 -05:00
Shawn Hartsell 8fb10cfa94 more typos 2024-04-22 19:14:25 -05:00
Shawn Hartsell ab710e48bd exclude win amd64 2024-04-22 19:13:35 -05:00
Shawn Hartsell b70a65d18b another typo 2024-04-22 18:47:31 -05:00
Shawn Hartsell 8659e181c1 fixing typo 2024-04-22 18:46:51 -05:00
Shawn Hartsell fc2c609f9c fixing arch 2024-04-22 18:46:21 -05:00
Shawn Hartsell 7c183f9b51 ok let's try a qemu based matrix build 2024-04-22 18:42:58 -05:00
Shawn Hartsell 4ab3e731a1 testing new ci script for matrix build and test 2024-04-22 18:00:37 -05:00
Shawn Hartsell b47191d0e7 testing new matrix build in ci workflow 2024-04-22 17:35:16 -05:00
Shawn Hartsell cbbe0d523c fixing refactor again 2024-04-22 17:18:38 -05:00
Shawn Hartsell 5996880e1f fixing test 2024-04-22 17:01:29 -05:00
Shawn Hartsell b7399d7b57 fixing path in ci workflow 2024-04-22 16:58:46 -05:00
Shawn Hartsell c8816bc3c0 refactoring lint errors 2024-04-22 16:55:53 -05:00
Shawn Hartsell 6030864125 fixing ci workflow 2024-04-22 16:38:36 -05:00
Shawn Hartsell a0aeedb796 removing excluded dirs from lint 2024-04-22 16:32:31 -05:00
Shawn Hartsell 5f4de58149 downgrade golangci-lint version for go 1.21 2024-04-22 16:29:08 -05:00
Shawn Hartsell 1412d00696 updating golangci-lint from upgrade 2024-04-22 16:27:25 -05:00
Shawn Hartsell ed25954c6c matrix builds for CI. simplifying release config 2024-04-22 16:20:14 -05:00
20 changed files with 582 additions and 682 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
* @actions/actions-oss-maintainers @actions/actions-sync-maintainers
* @actions/actions-delivery-nexus @actions/actions-oss-maintainers
@@ -8,7 +8,13 @@ jobs:
strategy:
fail-fast: false
matrix:
runson: [ubuntu-latest, macos-latest, windows-latest]
runson: [
ubuntu-latest,
ubuntu-20.04,
macos-latest,
windows-latest,
windows-2019
]
uses: ./.github/workflows/actions-sync-e2e-test-called.yml
with:
runson: ${{ matrix.runson }}
+59 -9
View File
@@ -3,14 +3,64 @@ on:
pull_request:
workflow_dispatch:
jobs:
CI:
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Lint
run: docker compose run --rm lint
- name: Test
run: docker compose run --rm test
- name: E2E
run: docker compose run --rm test-build
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
- name: Lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.54.2
unit-test:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
- name: Unit Tests
run: ./script/test
e2e-test:
needs: unit-test
runs-on: ubuntu-20.04
strategy:
matrix:
platform: [linux]
arch: [amd64, arm64]
exclude:
# Exclude Windows AMD64 because it's not supported
- platform: windows
arch: amd64
steps:
- name: Setup QEMU
uses: docker/setup-qemu-action@v3
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: 1.21
- name: Checkout
uses: actions/checkout@v4
- name: Build Binary for ${{ matrix.platform }}-${{ matrix.arch }}
run: |
if [[ "${{ matrix.platform }}" == "linux" ]]; then
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
fi
GOOS=${{ matrix.platform }} GOARCH=${{ matrix.arch }} go build -o bin/actions-sync-${{ matrix.platform }}-${{ matrix.arch }} main.go
- name: Run End-to-End Tests for ${{ matrix.platform }}-${{ matrix.arch }}
run: ./script/test-ci bin/actions-sync-${{ matrix.platform }}-${{ matrix.arch }}
+1 -1
View File
@@ -10,7 +10,7 @@ jobs:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v9
- uses: actions/stale@v5
with:
days-before-issue-stale: 30
days-before-issue-close: 14
+1 -1
View File
@@ -35,7 +35,7 @@ jobs:
- run: go mod vendor
# Ruby is required for licensed
- uses: ruby/setup-ruby@90be1154f987f4dc0fe0dd0feedac9e473aa4ba8 # v1
- uses: ruby/setup-ruby@5f19ec79cedfadb78ab837f95b87734d0003c899
with:
ruby-version: "3.2"
+1 -1
View File
@@ -18,7 +18,7 @@ jobs:
- name: Set CURRENT_TAG
run: echo "GORELEASER_CURRENT_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@f09f1a7bee7ae6b5153daf4e8c63cc8ce8c7e7d5
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
+2 -4
View File
@@ -15,10 +15,8 @@ linters:
- govet
- unparam
run:
tests: true
skip-files:
- ".*_test\\.go$"
modules-download-mode: vendor
tests: true
modules-download-mode: vendor
issues:
exclude-rules:
- path: test/github.go
+1 -3
View File
@@ -1,5 +1,4 @@
project_name: actions/actions-sync
builds:
- id: build
goos:
@@ -20,7 +19,6 @@ release:
owner: actions
name: actions-sync
# Create the release as a draft so it can be tested before being published
# To test, go to the Actions tab and run the "Actions Sync E2E Sanity Test" workflow
# To test, go to the Actions tab and run the "Actions Sync E2E Sanity Test" workflow
draft: true
+4 -22
View File
@@ -14,24 +14,6 @@ It is designed to work when:
* The GitHub Enterprise instance is separate from the rest of the internet.
* The GitHub Enterprise instance is connected to the rest of the internet.
### Note
Thank you for your interest in this GitHub action, however, right now we are not taking contributions.
We continue to focus our resources on strategic areas that help our customers be successful while making developers' lives easier. While GitHub Actions remains a key part of this vision, we are allocating resources towards other areas of Actions and are not taking contributions to this repository at this time. The GitHub public roadmap is the best place to follow along for any updates on features were working on and what stage theyre in.
We are taking the following steps to better direct requests related to GitHub Actions, including:
1. We will be directing questions and support requests to our [Community Discussions area](https://github.com/orgs/community/discussions/categories/actions)
2. High Priority bugs can be reported through Community Discussions or you can report these to our support team https://support.github.com/contact/bug-report.
3. Security Issues should be handled as per our [security.md](security.md)
We will still provide security updates for this project and fix major breaking changes during this time.
You are welcome to still raise bugs in this repo.
## Connected instances
When there are machines which have access to both the public internet and the GHES instance run `actions-sync sync`.
@@ -56,8 +38,6 @@ When there are machines which have access to both the public internet and the GH
A path to a file containing a newline separated list of repositories to be synced. Each entry follows the format of `repo-name`.
- `actions-admin-user` _(optional)_
The name of the Actions admin user, which will be used for updating the chosen action. To use the default user, pass `actions-admin`. If not set, the impersonation is disabled. Note that `site_admin` scope is required in the token for the impersonation to work.
- `batch-size` _(optional)_
Number of refs to push in each batch. Default is 0 (no batching). Use a value like 100 if pushing fails for large repositories with many branches and tags.
**Example Usage:**
@@ -116,8 +96,6 @@ When no machine has access to both the public internet and the GHES instance:
Limit push to specific repositories in the cache directory.
- `actions-admin-user` _(optional)_
The name of the Actions admin user, which will be used for updating the chosen action. To use the default user, pass `actions-admin`. If not set, the impersonation is disabled. Note that `site_admin` scope is required in the token for the impersonation to work.
- `batch-size` _(optional)_
Number of refs to push in each batch. Default is 0 (no batching). Use a value like 100 if pushing fails for large repositories with many branches and tags.
**Example Usage:**
@@ -132,3 +110,7 @@ When no machine has access to both the public internet and the GHES instance:
When creating a personal access token include the `repo` and `workflow` scopes. Include the `site_admin` scope (optional) if you want organizations to be created as necessary or you want to use the impersonation logic for the `push` or `sync` commands.
## Contributing
If you would like to contribute your work back to the project, please see
[`CONTRIBUTING.md`](CONTRIBUTING.md).
+23 -23
View File
@@ -1,23 +1,23 @@
version: "3.5"
services:
lint:
image: lint
build:
context: .
dockerfile: Dockerfile.cibuild
entrypoint:
- script/lint
test:
image: test
build:
context: .
dockerfile: Dockerfile.cibuild
entrypoint:
- script/test
test-build:
image: test-build
build:
context: .
dockerfile: Dockerfile.cibuild
entrypoint:
- script/test-build
# version: "3.5"
# services:
# lint:
# image: lint
# build:
# context: .
# dockerfile: Dockerfile.cibuild
# entrypoint:
# - script/lint
# test:
# image: test
# build:
# context: .
# dockerfile: Dockerfile.cibuild
# entrypoint:
# - script/test
# test-build:
# image: test-build
# build:
# context: .
# dockerfile: Dockerfile.cibuild
# entrypoint:
# - script/test-build
+4 -4
View File
@@ -18,12 +18,12 @@ if [ ! -f go.mod ]; then
go mod init tools
fi
go get golang.org/x/tools/go/packages@v0.16.0
go get golang.org/x/tools/go/packages@master
if [ ! -f "${GOBIN}/mockgen" ]; then
echo "mockgen was not found, installing..."
go get github.com/golang/mock/gomock@v1.6.0
go get github.com/golang/mock/mockgen@v1.6.0
go get github.com/golang/mock/gomock@master
go get github.com/golang/mock/mockgen@master
fi
if [ ! -f "${GOBIN}/golangci-lint" ]; then
@@ -33,5 +33,5 @@ fi
if [ ! -f "${GOBIN}/goimports" ]; then
echo "goimports was not found, installing..."
go get golang.org/x/tools/cmd/goimports@v0.16.0
go get golang.org/x/tools/cmd/goimports@master
fi
+1 -1
View File
@@ -4,7 +4,7 @@ if($IsLinux) {
} elseif ($IsWindows) {
$file_postfix = "windows_amd64"
} elseif ($IsMacOS) {
$file_postfix = "darwin_amd64"
$file_postfix = "darwin_arm64"
}
# Download release to test
+1 -1
View File
@@ -7,4 +7,4 @@ test -z "${DEBUG:-}" || {
set -x
}
CGO_ENABLED=0 go build -o bin/actions-sync main.go
go build -o bin/actions-sync main.go
+1 -1
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
script/build
#script/build
OUTPUT=$(mktemp)
RESULT=-1
Executable
+426
View File
@@ -0,0 +1,426 @@
#!/usr/bin/env bash
#script/build
# if the first argument is missing then print usage and exit
if [ -z "$1" ]; then
echo "Usage: $1 <path to actions-sync binary>"
exit 1
fi
ACTIONS_SYNC_BINARY=$1
OUTPUT=$(mktemp)
RESULT=-1
SRC_GIT_DAEMON_PORT=9419
SRC_GIT_DAEMON_OUTPUT=$(mktemp)
SRC_GIT_DAEMON_PID=-1
DEST_GIT_DAEMON_PORT=9420
DEST_GIT_DAEMON_OUTPUT=$(mktemp)
DEST_GIT_DAEMON_PID=-1
DEST_API_PORT=8081
DEST_API_OUTPUT=$(mktemp)
DEST_API_PID=-1
trap "after_suite" EXIT
trap "after_suite" SIGINT
function test_version() {
version "version shouldn't require any flags"
echo "all version tests passed"
}
function test_pull() {
# Pull new repo
setup_src "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_cache
pull --repo-name "org/repo" "pulling new repo"
assert_cache_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "pulling new repo org/repo"
echo "all pull tests passed successfully"
}
function test_push() {
# Push with a new change to main
setup_cache "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing new commit to main"
assert_dest_sha "org/repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo:heads/main to new commit"
# Push a non-linear change
setup_cache "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_dest "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
push "pushing to an old commit to main"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "updating heads/main to an old commit"
# Push with no changes to main
setup_cache "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing no changes to main"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "leaving org/repo:heads/main at existing commit"
# Push multiple branches
setup_cache "org/repo:heads/change:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org/repo:heads/nochange:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_dest "org/repo:heads/change:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org/repo:heads/nochange:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing multiple branches"
assert_dest_sha "org/repo" "heads/change" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo:heads/change to new commit"
assert_dest_sha "org/repo" "heads/nochange" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "leaving org/repo:heads/nochange at existing commit"
# Pushing multiple branches and tags
setup_cache "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org/repo:tags/v1:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org/repo:tags/v1:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing multiple branches and tags"
assert_dest_sha "org/repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo:heads/main to new commit"
assert_dest_sha "org/repo" "tags/v1" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo:tags/v1 to new commit"
# Pushing multiple repositories
setup_cache "org1/repo1:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org1/repo2:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org1/repo2:tags/v1:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org2/repo1:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org1/repo1:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org1/repo2:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org1/repo2:tags/v1:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org2/repo1:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing multiple repositories"
assert_dest_sha "org1/repo1" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org1/repo1:heads/main to new commit"
assert_dest_sha "org1/repo2" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org1/repo2:heads/main to new commit"
assert_dest_sha "org1/repo2" "tags/v1" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org1/repo2:tags/v1 to new commit"
assert_dest_sha "org2/repo1" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org2/repo1:tags/v1 to new commit"
# Honor --repo-name flag, ignore other cache entries
setup_cache "org/repo1:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org/repo2:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org/repo1:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org/repo2:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push2args --repo-name "org/repo1" "pushing only one of the repos in the cache"
assert_dest_sha "org/repo1" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo1 passed in repo flag"
assert_dest_sha "org/repo2" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "org/repo2 not updated despite cache"
# Push to pre-existing org
setup_cache "org-already-exists/new-repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org-already-exists/new-repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing to existing org"
assert_dest_sha "org-already-exists/new-repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org-already-exists/new-repo"
# Push to pre-existing repo
setup_cache "org-already-exists/repo-already-exists:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org-already-exists/repo-already-exists:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing to existing repo"
assert_dest_sha "org-already-exists/repo-already-exists" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org-already-exists/repo-already-exists"
# Push to repo in user's account
setup_cache "monalisa/new-repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "monalisa/new-repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push "pushing to authenticated user's account"
assert_dest_sha "monalisa/new-repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating monalisa/new-repo"
# Push to GHAE with impersonation
setup_cache "org-already-exists/ghae-repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org-already-exists/ghae-repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push_impersonation "ghae-admin" "pushing to GHAE repo"
# Push to GHES with impersonation
setup_cache "org-already-exists/ghes-repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org-already-exists/ghes-repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
push_impersonation "ghes-admin" "pushing to GHES repo"
echo "all push tests passed successfully"
}
function test_sync() {
# Sync no change without cached
setup_src "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_cache
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo" "syncing a new commit in an uncached repo"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "syncing no changes without cache"
# Sync a new commit without cache
setup_src "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_cache
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo" "syncing a new commit in an uncached repo"
assert_dest_sha "org/repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "syncing a new commit without cache"
# Sync no change with uncached
setup_src "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_cache "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo" "syncing a new commit in an uncached repo"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "syncing no changes with cache"
# Sync a new commit with cache
setup_src "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_cache "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_dest "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo" "syncing a new commit in an uncached repo"
assert_dest_sha "org/repo" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "syncing a new commit with cache"
# Sync a non-linear change without cache
setup_src "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_cache
setup_dest "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
sync --repo-name "org/repo" "syncing an old commit in an uncached repo"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "syncing heads/main to an old commit without cache"
# Sync a non-linear change with cache
setup_src "org/repo:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
setup_cache "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
sync --repo-name "org/repo" "syncing an old commit in an cached repo"
assert_dest_sha "org/repo" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "syncing heads/main to an old commit with cache"
# Sync to a different repo
setup_src "org/repo:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_cache
setup_dest "org2/repo2:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo:org2/repo2" "syncing org/repo aliased to org2/repo2"
assert_dest_sha "org2/repo2" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "syncing org/repo aliased to org2/repo2"
# Honor --repo-name flag, ignore other cache entries
setup_cache "org/repo1:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52" \
"org/repo2:heads/main:e9009d51dd6da2c363d1d14779c53dd27fcb0c52"
setup_dest "org/repo1:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142" \
"org/repo2:heads/main:a5984bb887dd2fcdc2892cd906d6f004844d1142"
sync --repo-name "org/repo1" "syncing only one of the repos in the cache"
assert_dest_sha "org/repo1" "heads/main" "e9009d51dd6da2c363d1d14779c53dd27fcb0c52" "updating org/repo1 passed in repo flag"
assert_dest_sha "org/repo2" "heads/main" "a5984bb887dd2fcdc2892cd906d6f004844d1142" "org/repo2 not updated despite cache"
echo "all sync tests passed successfully"
}
function before_suite() {
set -e
$(
rm -rf test/tmp
mkdir -p test/tmp/dest
mkdir -p test/tmp/src
) >/dev/null
git daemon \
--reuseaddr \
--verbose \
--export-all \
--enable=receive-pack \
--port="$SRC_GIT_DAEMON_PORT" \
--base-path=test/tmp/src &>$SRC_GIT_DAEMON_OUTPUT &
SRC_GIT_DAEMON_PID=$!
git daemon \
--reuseaddr \
--verbose \
--export-all \
--enable=receive-pack \
--port="$DEST_GIT_DAEMON_PORT" \
--base-path=test/tmp/dest &>$DEST_GIT_DAEMON_OUTPUT &
DEST_GIT_DAEMON_PID=$!
# wait for the git daemons to start before continuing
if ! check_git_daemon $SRC_GIT_DAEMON_PORT; then
echo "timeout waiting for src git daemon to start"
exit 1
fi
if ! check_git_daemon $DEST_GIT_DAEMON_PORT; then
echo "timeout waiting for dest git daemon to start"
exit 1
fi
go run test/github.go \
-p "$DEST_API_PORT" \
-git-daemon-url "git://localhost:$DEST_GIT_DAEMON_PORT/" &>$DEST_API_OUTPUT &
DEST_API_PID=$!
script/build
until $(curl --output /dev/null --silent --head --fail "http://localhost:$DEST_API_PORT/ping"); do
sleep 0.1
done
set +e
}
function after_suite() {
kill "$SRC_GIT_DAEMON_PID" "$DEST_GIT_DAEMON_PID" "$DEST_API_PID"
}
function setup_src() {
rm -rf test/tmp/src
mkdir -p test/tmp/src
for r in "$@"; do
local nwo=$(echo $r | cut -d':' -f1)
local refname=$(echo $r | cut -d':' -f2)
local sha=$(echo $r | cut -d':' -f3)
local org=$(echo $nwo | cut -d'/' -f1)
mkdir -p test/tmp/src/$org && cp -R test/fixtures/repo.git test/tmp/src/$nwo
echo "$sha" >"test/tmp/src/$nwo/refs/$refname"
done
}
function setup_cache() {
rm -rf test/tmp/cache
mkdir -p test/tmp/cache
for r in "$@"; do
local nwo=$(echo $r | cut -d':' -f1)
local refname=$(echo $r | cut -d':' -f2)
local sha=$(echo $r | cut -d':' -f3)
mkdir -p test/tmp/cache/$nwo && cp -R test/fixtures/repo.git test/tmp/cache/$nwo/.git
echo "$sha" >"test/tmp/cache/$nwo/.git/refs/$refname"
done
}
function setup_dest() {
rm -rf test/tmp/dest
mkdir -p test/tmp/dest
for r in "$@"; do
local nwo=$(echo $r | cut -d':' -f1)
local refname=$(echo $r | cut -d':' -f2)
local sha=$(echo $r | cut -d':' -f3)
mkdir -p test/tmp/dest/$nwo && cp -R test/fixtures/repo.git test/tmp/dest/$nwo/.git
echo "$sha" >"test/tmp/dest/$nwo/.git/refs/$refname"
done
}
function version() {
$ACTIONS_SYNC_BINARY version \
&>$OUTPUT ||
fail $1
}
function pull() {
$ACTIONS_SYNC_BINARY pull \
--cache-dir "test/tmp/cache" \
--source-url "git://localhost:$SRC_GIT_DAEMON_PORT" \
"$1" "$2" \
&>$OUTPUT ||
fail $3
}
function push() {
$ACTIONS_SYNC_BINARY push \
--cache-dir "test/tmp/cache" \
--disable-push-git-auth \
--destination-token "token" \
--destination-url "http://localhost:$DEST_API_PORT" \
&>$OUTPUT ||
fail "$1"
}
function push2args() {
$ACTIONS_SYNC_BINARY push \
--cache-dir "test/tmp/cache" \
--disable-push-git-auth \
--destination-token "token" \
--destination-url "http://localhost:$DEST_API_PORT" \
"$1" "$2" \
&>$OUTPUT ||
fail $3
}
function push_impersonation() {
$ACTIONS_SYNC_BINARY push \
--cache-dir "test/tmp/cache" \
--disable-push-git-auth \
--destination-token "token" \
--destination-url "http://localhost:$DEST_API_PORT" \
--actions-admin-user $1 \
&>$OUTPUT ||
fail "$2"
}
function sync() {
$ACTIONS_SYNC_BINARY sync \
--cache-dir "test/tmp/cache" \
--source-url "git://localhost:$SRC_GIT_DAEMON_PORT" \
--disable-push-git-auth \
--destination-token "token" \
--destination-url "http://localhost:$DEST_API_PORT" \
"$1" "$2" \
&>$OUTPUT ||
fail $3
}
function assert_cache_sha() {
nwo=$1
ref=$2
expected=$3
actual=$(cat "test/tmp/cache/$nwo/.git/refs/$ref")
[ "$actual" == "$expected" ] || fail "unexpected cache sha \`$expected != $actual\` - \`$nwo\` \`$ref\` - \`$4\`"
}
function assert_dest_sha() {
nwo=$1
ref=$2
expected=$3
actual=$(cat "test/tmp/dest/$nwo/.git/refs/$ref")
[ "$actual" == "$expected" ] || fail "unexpected dest sha \`$expected != $actual\` - \`$nwo\` \`$ref\` - \`$4\`"
}
function fail() {
MSG=$1
echo "FAIL: Failed $MSG"
echo -----output-----
cat $OUTPUT
echo -----/output-----
echo -----github git daemon output-----
cat $SRC_GIT_DAEMON_OUTPUT
echo -----/github git daemon output-----
echo -----ghes git daemon output-----
cat $DEST_GIT_DAEMON_OUTPUT
echo -----/ghes git daemon output-----
echo -----ghes api output-----
cat $DEST_API_OUTPUT
echo -----/ghes api output-----
exit 1
}
function check_git_daemon {
port=$1
for i in {1..10}; do
process=$(ps aux | grep "git daemon" | grep $port)
if [ -z "$process" ]; then
echo "Waiting for git daemon to start on port $port..."
sleep 1
else
echo "Git daemon is running on port $port"
return 0
fi
done
echo "Git daemon did not start within the expected time."
return 1
}
test_version
before_suite
test_pull
test_push
test_sync
-6
View File
@@ -5,7 +5,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/storer"
)
// A really thin Git wrapper so we can stub it out in our tests
@@ -20,7 +19,6 @@ type GitRepository interface {
DeleteRemote(string) error
CreateRemote(*config.RemoteConfig) (GitRemote, error)
FetchContext(context.Context, *git.FetchOptions) error
References() (storer.ReferenceIter, error)
}
type GitRemote interface {
@@ -67,7 +65,3 @@ func (r *gitRepository) CreateRemote(c *config.RemoteConfig) (GitRemote, error)
func (r *gitRepository) FetchContext(ctx context.Context, o *git.FetchOptions) error {
return r.inner.FetchContext(ctx, o)
}
func (r *gitRepository) References() (storer.ReferenceIter, error) {
return r.inner.References()
}
-75
View File
@@ -1,75 +0,0 @@
package src
import (
"context"
"testing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/stretchr/testify/assert"
)
// Tests for GitRepository interface and implementations
func TestGitRepositoryInterface(t *testing.T) {
// This test verifies that our mock implements the GitRepository interface
var _ GitRepository = &mockGitRepository{}
}
func TestGitRemoteInterface(t *testing.T) {
// This test verifies that our mock implements the GitRemote interface
var _ GitRemote = &mockGitRemote{}
}
// Ensure the mockGitRepository implements all methods of GitRepository
func TestMockGitRepository_DeleteRemote(t *testing.T) {
repo := &mockGitRepository{}
err := repo.DeleteRemote("origin")
assert.NoError(t, err)
}
func TestMockGitRepository_CreateRemote(t *testing.T) {
repo := &mockGitRepository{}
remote, err := repo.CreateRemote(&config.RemoteConfig{Name: "test"})
assert.NoError(t, err)
assert.Nil(t, remote)
}
func TestMockGitRepository_FetchContext(t *testing.T) {
repo := &mockGitRepository{}
err := repo.FetchContext(context.Background(), &git.FetchOptions{})
assert.NoError(t, err)
}
func TestMockGitRepository_References(t *testing.T) {
repo := &mockGitRepository{}
refs, err := repo.References()
assert.NoError(t, err)
assert.NotNil(t, refs)
// Verify it returns a valid iterator
_, ok := refs.(storer.ReferenceIter)
assert.True(t, ok)
}
// Ensure the mockGitRemote implements all methods of GitRemote
func TestMockGitRemote_PushContext(t *testing.T) {
remote := &mockGitRemote{}
err := remote.PushContext(context.Background(), &git.PushOptions{})
assert.NoError(t, err)
}
func TestMockGitRemote_Config(t *testing.T) {
remote := &mockGitRemote{}
cfg := remote.Config()
assert.NotNil(t, cfg)
assert.Equal(t, "test-remote", cfg.Name)
// Test with custom config
customRemote := &mockGitRemote{
remoteConfig: &config.RemoteConfig{Name: "custom-remote"},
}
cfg = customRemote.Config()
assert.Equal(t, "custom-remote", cfg.Name)
}
+42 -121
View File
@@ -9,7 +9,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v43/github"
@@ -23,16 +22,9 @@ const enterpriseAPIPath = "/api/v3"
const enterpriseVersionHeaderKey = "X-GitHub-Enterprise-Version"
const xOAuthScopesHeader = "X-OAuth-Scopes"
// DefaultBatchSize of 0 means no batching (push all refs at once, original behavior)
const DefaultBatchSize = 0
// MinBatchSize is the minimum allowed batch size when batching is enabled
const MinBatchSize = 10
type PushOnlyFlags struct {
BaseURL, Token, ActionsAdminUser string
DisableGitAuth bool
BatchSize int
}
type PushFlags struct {
@@ -50,7 +42,6 @@ func (f *PushOnlyFlags) Init(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.ActionsAdminUser, "actions-admin-user", "", "A user to impersonate for the push requests. To use the default name, pass 'actions-admin'. Note that the site_admin scope in the token is required for the impersonation to work.")
cmd.Flags().StringVar(&f.Token, "destination-token", "", "Token to access API on GHES instance")
cmd.Flags().BoolVar(&f.DisableGitAuth, "disable-push-git-auth", false, "Disables git authentication whilst pushing")
cmd.Flags().IntVar(&f.BatchSize, "batch-size", DefaultBatchSize, "Number of refs to push in each batch (0 = no batching). Use a value like 100 if pushing fails for large repositories.")
}
func (f *PushFlags) Validate() Validations {
@@ -65,9 +56,6 @@ func (f *PushOnlyFlags) Validate() Validations {
if f.Token == "" {
validations = append(validations, "--destination-token must be set")
}
if f.BatchSize != 0 && f.BatchSize < MinBatchSize {
validations = append(validations, fmt.Sprintf("--batch-size must be 0 (no batching) or at least %d", MinBatchSize))
}
return validations
}
@@ -83,11 +71,11 @@ func GetImpersonationToken(ctx context.Context, flags *PushFlags) (string, error
rootRequest, err := ghClient.NewRequest("GET", enterpriseAPIPath, nil)
if err != nil {
return "", errors.Wrap(err, "error constructing request for GitHub Enterprise client.")
return "", errors.Wrap(err, "error constructing request for GitHub Enterprise client")
}
rootResponse, err := ghClient.Do(ctx, rootRequest, nil)
if err != nil {
return "", errors.Wrap(err, "error checking connectivity for GitHub Enterprise client.")
return "", errors.Wrap(err, "error checking connectivity for GitHub Enterprise client")
}
scopesHeader := rootResponse.Header.Get(xOAuthScopesHeader)
@@ -108,7 +96,7 @@ func GetImpersonationToken(ctx context.Context, flags *PushFlags) (string, error
impersonationToken, _, err := ghClient.Admin.CreateUserImpersonation(ctx, flags.ActionsAdminUser, &github.ImpersonateUserOptions{Scopes: []string{minimumRepositoryScope, "workflow"}})
if err != nil {
return "", errors.Wrap(err, "failed to impersonate Actions admin user.")
return "", errors.Wrap(err, "failed to impersonate Actions admin user")
}
fmt.Printf("got the impersonation token for `%s` ...\n", flags.ActionsAdminUser)
@@ -217,38 +205,41 @@ func getOrCreateGitHubRepo(ctx context.Context, client *github.Client, repoName,
}
// check if repository already exists
ghRepo, resp, err := client.Repositories.Get(ctx, ownerName, repoName)
if err == nil {
fmt.Printf("Existing repo `%s/%s`\n", ownerName, repoName)
} else if resp != nil && resp.StatusCode == 404 {
var ghRepo *github.Repository
var resp *github.Response
ghRepo, resp, err = client.Repositories.Get(ctx, ownerName, repoName)
if err != nil {
// repo not existing yet - try to create
visibility := github.String("public")
if isAE {
visibility = github.String("internal")
}
repo := &github.Repository{
Name: github.String(repoName),
HasIssues: github.Bool(false),
HasWiki: github.Bool(false),
HasPages: github.Bool(false),
HasProjects: github.Bool(false),
Visibility: visibility,
if resp != nil && resp.StatusCode == 404 {
fmt.Printf("repo `%s/%s` doesn't exist, attempting to create\n", ownerName, repoName)
visibility := github.String("public")
if isAE {
visibility = github.String("internal")
}
repo := &github.Repository{
Name: github.String(repoName),
HasIssues: github.Bool(false),
HasWiki: github.Bool(false),
HasPages: github.Bool(false),
HasProjects: github.Bool(false),
Visibility: visibility,
}
ghRepo, _, err = client.Repositories.Create(ctx, createRepoOrgName, repo)
if err != nil {
return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName)
}
fmt.Printf("created repo `%s/%s`\n", ownerName, repoName)
return ghRepo, nil
}
ghRepo, _, err = client.Repositories.Create(ctx, createRepoOrgName, repo)
if err == nil {
fmt.Printf("Created repo `%s/%s`\n", ownerName, repoName)
} else {
return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName)
}
} else if err != nil {
return nil, errors.Wrapf(err, "error creating repository %s/%s", ownerName, repoName)
}
if ghRepo == nil {
return nil, errors.New("error repository is nil")
}
fmt.Printf("found existing repo `%s/%s`\n", ownerName, repoName)
return ghRepo, nil
}
@@ -294,86 +285,16 @@ func syncWithCachedRepository(ctx context.Context, flags *PushFlags, ghRepo *git
Password: flags.Token,
}
}
// If batch size is 0 or negative, use original wildcard approach (no batching)
if flags.BatchSize <= 0 {
err = remote.PushContext(ctx, &git.PushOptions{
RemoteName: remote.Config().Name,
RefSpecs: []config.RefSpec{
"+refs/heads/*:refs/heads/*",
"+refs/tags/*:refs/tags/*",
},
Auth: auth,
})
if errors.Cause(err) == git.NoErrAlreadyUpToDate {
return nil
}
return errors.Wrapf(err, "failed to push to repo: %s", ghRepo.GetCloneURL())
}
// Batching requested - collect all refs and push in batches
refs, err := collectRefs(gitRepo)
if err != nil {
return errors.Wrap(err, "error collecting refs")
}
return pushRefsInBatches(ctx, remote, refs, flags.BatchSize, auth, ghRepo.GetCloneURL())
}
// collectRefs gathers all branch and tag refs from the repository
func collectRefs(gitRepo GitRepository) ([]plumbing.ReferenceName, error) {
refIter, err := gitRepo.References()
if err != nil {
return nil, err
}
var refs []plumbing.ReferenceName
err = refIter.ForEach(func(ref *plumbing.Reference) error {
name := ref.Name()
// Only include branches and tags
if name.IsBranch() || name.IsTag() {
refs = append(refs, name)
}
return nil
err = remote.PushContext(ctx, &git.PushOptions{
RemoteName: remote.Config().Name,
RefSpecs: []config.RefSpec{
"+refs/heads/*:refs/heads/*",
"+refs/tags/*:refs/tags/*",
},
Auth: auth,
})
if err != nil {
return nil, err
if errors.Cause(err) == git.NoErrAlreadyUpToDate {
return nil
}
return refs, nil
}
// pushRefsInBatches pushes refs in smaller batches to avoid server-side limits
func pushRefsInBatches(ctx context.Context, remote GitRemote, refs []plumbing.ReferenceName, batchSize int, auth transport.AuthMethod, cloneURL string) error {
totalRefs := len(refs)
for i := 0; i < totalRefs; i += batchSize {
end := i + batchSize
if end > totalRefs {
end = totalRefs
}
batch := refs[i:end]
refSpecs := make([]config.RefSpec, len(batch))
for j, ref := range batch {
// Create a refspec like "+refs/heads/main:refs/heads/main"
refSpecs[j] = config.RefSpec("+" + ref.String() + ":" + ref.String())
}
err := remote.PushContext(ctx, &git.PushOptions{
RemoteName: remote.Config().Name,
RefSpecs: refSpecs,
Auth: auth,
})
if err != nil {
if errors.Cause(err) == git.NoErrAlreadyUpToDate {
// This batch was already up to date, continue to next batch
continue
}
return errors.Wrapf(err, "failed to push batch %d-%d of %d refs to repo: %s", i+1, end, totalRefs, cloneURL)
}
}
return nil
return errors.Wrapf(err, "failed to push to repo: %s", ghRepo.GetCloneURL())
}
-400
View File
@@ -1,400 +0,0 @@
package src
import (
"context"
"fmt"
"testing"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Mock implementations for testing
type mockReferenceIter struct {
refs []*plumbing.Reference
index int
}
func (m *mockReferenceIter) Next() (*plumbing.Reference, error) {
if m.index >= len(m.refs) {
return nil, storer.ErrStop
}
ref := m.refs[m.index]
m.index++
return ref, nil
}
func (m *mockReferenceIter) ForEach(fn func(*plumbing.Reference) error) error {
for _, ref := range m.refs {
if err := fn(ref); err != nil {
if err == storer.ErrStop {
return nil
}
return err
}
}
return nil
}
func (m *mockReferenceIter) Close() {}
type mockGitRepository struct {
refs []*plumbing.Reference
err error
}
func (m *mockGitRepository) DeleteRemote(name string) error {
return nil
}
func (m *mockGitRepository) CreateRemote(c *config.RemoteConfig) (GitRemote, error) {
return nil, nil
}
func (m *mockGitRepository) FetchContext(ctx context.Context, o *git.FetchOptions) error {
return nil
}
func (m *mockGitRepository) References() (storer.ReferenceIter, error) {
if m.err != nil {
return nil, m.err
}
return &mockReferenceIter{refs: m.refs, index: 0}, nil
}
type mockGitRemote struct {
pushCalls [][]config.RefSpec
pushError error
alreadyUpToDate bool
remoteConfig *config.RemoteConfig
}
func (m *mockGitRemote) PushContext(ctx context.Context, o *git.PushOptions) error {
m.pushCalls = append(m.pushCalls, o.RefSpecs)
if m.alreadyUpToDate {
return git.NoErrAlreadyUpToDate
}
return m.pushError
}
func (m *mockGitRemote) Config() *config.RemoteConfig {
if m.remoteConfig != nil {
return m.remoteConfig
}
return &config.RemoteConfig{Name: "test-remote"}
}
// Tests for PushOnlyFlags.Validate batch size validation
func TestPushOnlyFlags_Validate_BatchSize(t *testing.T) {
tests := []struct {
name string
batchSize int
expectErr bool
errMessage string
}{
{
name: "batch size 0 (no batching) is valid",
batchSize: 0,
expectErr: false,
},
{
name: "batch size at minimum (10) is valid",
batchSize: MinBatchSize,
expectErr: false,
},
{
name: "batch size above minimum is valid",
batchSize: 100,
expectErr: false,
},
{
name: "batch size below minimum is invalid",
batchSize: 5,
expectErr: true,
errMessage: fmt.Sprintf("--batch-size must be 0 (no batching) or at least %d", MinBatchSize),
},
{
name: "batch size of 1 is invalid",
batchSize: 1,
expectErr: true,
errMessage: fmt.Sprintf("--batch-size must be 0 (no batching) or at least %d", MinBatchSize),
},
{
name: "batch size of 9 is invalid",
batchSize: 9,
expectErr: true,
errMessage: fmt.Sprintf("--batch-size must be 0 (no batching) or at least %d", MinBatchSize),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flags := PushOnlyFlags{
BaseURL: "https://example.com",
Token: "test-token",
BatchSize: tt.batchSize,
}
validations := flags.Validate()
if tt.expectErr {
require.NotEmpty(t, validations, "expected validation error")
found := false
for _, v := range validations {
if v == tt.errMessage {
found = true
break
}
}
assert.True(t, found, "expected error message not found: %s", tt.errMessage)
} else {
// Check that batch size validation didn't add an error
for _, v := range validations {
assert.NotContains(t, v, "batch-size", "unexpected batch-size validation error")
}
}
})
}
}
// Tests for collectRefs function
func TestCollectRefs(t *testing.T) {
tests := []struct {
name string
refs []*plumbing.Reference
expectedLen int
expectedRefs []plumbing.ReferenceName
expectErr bool
}{
{
name: "empty repository",
refs: []*plumbing.Reference{},
expectedLen: 0,
},
{
name: "branches only",
refs: []*plumbing.Reference{
plumbing.NewHashReference(plumbing.NewBranchReferenceName("main"), plumbing.NewHash("abc123")),
plumbing.NewHashReference(plumbing.NewBranchReferenceName("feature"), plumbing.NewHash("def456")),
},
expectedLen: 2,
expectedRefs: []plumbing.ReferenceName{
plumbing.NewBranchReferenceName("main"),
plumbing.NewBranchReferenceName("feature"),
},
},
{
name: "tags only",
refs: []*plumbing.Reference{
plumbing.NewHashReference(plumbing.NewTagReferenceName("v1.0.0"), plumbing.NewHash("abc123")),
plumbing.NewHashReference(plumbing.NewTagReferenceName("v2.0.0"), plumbing.NewHash("def456")),
},
expectedLen: 2,
expectedRefs: []plumbing.ReferenceName{
plumbing.NewTagReferenceName("v1.0.0"),
plumbing.NewTagReferenceName("v2.0.0"),
},
},
{
name: "mixed branches and tags",
refs: []*plumbing.Reference{
plumbing.NewHashReference(plumbing.NewBranchReferenceName("main"), plumbing.NewHash("abc123")),
plumbing.NewHashReference(plumbing.NewTagReferenceName("v1.0.0"), plumbing.NewHash("def456")),
plumbing.NewHashReference(plumbing.NewBranchReferenceName("develop"), plumbing.NewHash("ghi789")),
},
expectedLen: 3,
},
{
name: "filters out HEAD and other refs",
refs: []*plumbing.Reference{
plumbing.NewHashReference(plumbing.HEAD, plumbing.NewHash("abc123")),
plumbing.NewHashReference(plumbing.NewBranchReferenceName("main"), plumbing.NewHash("def456")),
plumbing.NewHashReference(plumbing.NewRemoteReferenceName("origin", "main"), plumbing.NewHash("ghi789")),
plumbing.NewHashReference(plumbing.NewTagReferenceName("v1.0.0"), plumbing.NewHash("jkl012")),
},
expectedLen: 2, // Only main branch and v1.0.0 tag
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
repo := &mockGitRepository{refs: tt.refs}
refs, err := collectRefs(repo)
if tt.expectErr {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.Len(t, refs, tt.expectedLen)
if tt.expectedRefs != nil {
for i, expected := range tt.expectedRefs {
assert.Equal(t, expected, refs[i])
}
}
})
}
}
func TestCollectRefs_Error(t *testing.T) {
repo := &mockGitRepository{err: fmt.Errorf("failed to get references")}
refs, err := collectRefs(repo)
require.Error(t, err)
assert.Nil(t, refs)
assert.Contains(t, err.Error(), "failed to get references")
}
// Tests for pushRefsInBatches function
func TestPushRefsInBatches(t *testing.T) {
tests := []struct {
name string
refs []plumbing.ReferenceName
batchSize int
expectedBatches int
alreadyUpToDate bool
pushError error
expectErr bool
expectedErrSubstr string
}{
{
name: "single batch - fewer refs than batch size",
refs: []plumbing.ReferenceName{
plumbing.NewBranchReferenceName("main"),
plumbing.NewBranchReferenceName("feature"),
},
batchSize: 10,
expectedBatches: 1,
},
{
name: "single batch - exact batch size",
refs: createNRefs(10),
batchSize: 10,
expectedBatches: 1,
},
{
name: "multiple batches - exactly divisible",
refs: createNRefs(30),
batchSize: 10,
expectedBatches: 3,
},
{
name: "multiple batches - not exactly divisible",
refs: createNRefs(25),
batchSize: 10,
expectedBatches: 3, // 10 + 10 + 5
},
{
name: "empty refs",
refs: []plumbing.ReferenceName{},
batchSize: 10,
expectedBatches: 0,
},
{
name: "all batches already up to date",
refs: []plumbing.ReferenceName{
plumbing.NewBranchReferenceName("main"),
},
batchSize: 10,
expectedBatches: 1,
alreadyUpToDate: true,
},
{
name: "push error",
refs: []plumbing.ReferenceName{
plumbing.NewBranchReferenceName("main"),
},
batchSize: 10,
pushError: fmt.Errorf("network error"),
expectErr: true,
expectedErrSubstr: "failed to push batch",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
remote := &mockGitRemote{
alreadyUpToDate: tt.alreadyUpToDate,
pushError: tt.pushError,
}
err := pushRefsInBatches(context.Background(), remote, tt.refs, tt.batchSize, nil, "https://example.com/repo.git")
if tt.expectErr {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedErrSubstr)
return
}
require.NoError(t, err)
assert.Len(t, remote.pushCalls, tt.expectedBatches)
})
}
}
func TestPushRefsInBatches_RefSpecFormat(t *testing.T) {
refs := []plumbing.ReferenceName{
plumbing.NewBranchReferenceName("main"),
plumbing.NewTagReferenceName("v1.0.0"),
}
remote := &mockGitRemote{}
err := pushRefsInBatches(context.Background(), remote, refs, 10, nil, "https://example.com/repo.git")
require.NoError(t, err)
require.Len(t, remote.pushCalls, 1)
require.Len(t, remote.pushCalls[0], 2)
// Check refspec format: should be "+refs/heads/main:refs/heads/main"
assert.Equal(t, config.RefSpec("+refs/heads/main:refs/heads/main"), remote.pushCalls[0][0])
assert.Equal(t, config.RefSpec("+refs/tags/v1.0.0:refs/tags/v1.0.0"), remote.pushCalls[0][1])
}
func TestPushRefsInBatches_BatchSizes(t *testing.T) {
// Create 25 refs
refs := createNRefs(25)
batchSize := 10
remote := &mockGitRemote{}
err := pushRefsInBatches(context.Background(), remote, refs, batchSize, nil, "https://example.com/repo.git")
require.NoError(t, err)
require.Len(t, remote.pushCalls, 3)
// First batch should have 10 refs
assert.Len(t, remote.pushCalls[0], 10)
// Second batch should have 10 refs
assert.Len(t, remote.pushCalls[1], 10)
// Third batch should have 5 refs (remainder)
assert.Len(t, remote.pushCalls[2], 5)
}
// Tests for constants
func TestConstants(t *testing.T) {
assert.Equal(t, 0, DefaultBatchSize, "DefaultBatchSize should be 0 for backward compatibility")
assert.Equal(t, 10, MinBatchSize, "MinBatchSize should be 10")
}
// Helper function to create N test refs
func createNRefs(n int) []plumbing.ReferenceName {
refs := make([]plumbing.ReferenceName, n)
for i := 0; i < n; i++ {
refs[i] = plumbing.NewBranchReferenceName(fmt.Sprintf("branch-%d", i))
}
return refs
}
+7 -7
View File
@@ -18,7 +18,7 @@ func Test_extractSourceDest(t *testing.T) {
assert.Equal(t, "src_owner/src_repo", src)
assert.Equal(t, "dst_owner/dst_repo", dst)
src, dst, err = extractSourceDest("src_owner/src_repo:dst_owner/dst_repo:bogus/bogus")
_, _, err = extractSourceDest("src_owner/src_repo:dst_owner/dst_repo:bogus/bogus")
require.Error(t, err)
}
@@ -37,24 +37,24 @@ func Test_validateNwo(t *testing.T) {
assert.Equal(t, "a/b", nwo)
// no slash separator
nwo, err = validateNwo("bogus")
_, err = validateNwo("bogus")
require.Error(t, err)
// no owner
nwo, err = validateNwo("/bogus")
_, err = validateNwo("/bogus")
require.Error(t, err)
// no repo name
nwo, err = validateNwo("bogus/")
_, err = validateNwo("bogus/")
require.Error(t, err)
nwo, err = validateNwo("bogus whitespace/bogus")
_, err = validateNwo("bogus whitespace/bogus")
require.Error(t, err)
nwo, err = validateNwo("bogus/bogus/bogus")
_, err = validateNwo("bogus/bogus/bogus")
require.Error(t, err)
// A separate destination is only permitted for "repo names", not NWOs.
nwo, err = validateNwo("owner/repo:bogus/bogus")
_, err = validateNwo("owner/repo:bogus/bogus")
require.Error(t, err)
}