Provisioning: Add refs endpoint (#108014)

This commit is contained in:
Roberto Jiménez Sánchez
2025-07-11 13:58:39 -07:00
committed by GitHub
parent a33b634a9f
commit 05a2d26787
17 changed files with 727 additions and 0 deletions
@@ -393,6 +393,64 @@ func (_c *MockGitRepository_LatestRef_Call) RunAndReturn(run func(context.Contex
return _c
}
// ListRefs provides a mock function with given fields: ctx
func (_m *MockGitRepository) ListRefs(ctx context.Context) ([]v0alpha1.RefItem, error) {
ret := _m.Called(ctx)
if len(ret) == 0 {
panic("no return value specified for ListRefs")
}
var r0 []v0alpha1.RefItem
var r1 error
if rf, ok := ret.Get(0).(func(context.Context) ([]v0alpha1.RefItem, error)); ok {
return rf(ctx)
}
if rf, ok := ret.Get(0).(func(context.Context) []v0alpha1.RefItem); ok {
r0 = rf(ctx)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).([]v0alpha1.RefItem)
}
}
if rf, ok := ret.Get(1).(func(context.Context) error); ok {
r1 = rf(ctx)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MockGitRepository_ListRefs_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'ListRefs'
type MockGitRepository_ListRefs_Call struct {
*mock.Call
}
// ListRefs is a helper method to define mock.On call
// - ctx context.Context
func (_e *MockGitRepository_Expecter) ListRefs(ctx interface{}) *MockGitRepository_ListRefs_Call {
return &MockGitRepository_ListRefs_Call{Call: _e.mock.On("ListRefs", ctx)}
}
func (_c *MockGitRepository_ListRefs_Call) Run(run func(ctx context.Context)) *MockGitRepository_ListRefs_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context))
})
return _c
}
func (_c *MockGitRepository_ListRefs_Call) Return(_a0 []v0alpha1.RefItem, _a1 error) *MockGitRepository_ListRefs_Call {
_c.Call.Return(_a0, _a1)
return _c
}
func (_c *MockGitRepository_ListRefs_Call) RunAndReturn(run func(context.Context) ([]v0alpha1.RefItem, error)) *MockGitRepository_ListRefs_Call {
_c.Call.Return(run)
return _c
}
// Read provides a mock function with given fields: ctx, path, ref
func (_m *MockGitRepository) Read(ctx context.Context, path string, ref string) (*repository.FileInfo, error) {
ret := _m.Called(ctx, path, ref)
@@ -476,6 +476,27 @@ func (r *gitRepository) History(_ context.Context, _ string, _ string) ([]provis
}}
}
func (r *gitRepository) ListRefs(ctx context.Context) ([]provisioning.RefItem, error) {
refs, err := r.client.ListRefs(ctx)
if err != nil {
return nil, fmt.Errorf("list refs: %w", err)
}
refItems := make([]provisioning.RefItem, 0, len(refs))
for _, ref := range refs {
// Only branches
if !strings.HasPrefix(ref.Name, "refs/heads/") {
continue
}
refItems = append(refItems, provisioning.RefItem{
Name: strings.TrimPrefix(ref.Name, "refs/heads/"),
Hash: ref.Hash.String(),
})
}
return refItems, nil
}
func (r *gitRepository) LatestRef(ctx context.Context) (string, error) {
ctx, _ = r.logger(ctx, "")
branchRef, err := r.client.GetRef(ctx, fmt.Sprintf("refs/heads/%s", r.gitConfig.Branch))
@@ -1190,6 +1190,97 @@ func TestGitRepository_Delete(t *testing.T) {
}
}
func TestGitRepository_ListRefs(t *testing.T) {
tests := []struct {
name string
setupMock func(*mocks.FakeClient)
gitConfig RepositoryConfig
wantError bool
wantRefs []provisioning.RefItem
errorType error
}{
{
name: "success - list refs",
setupMock: func(mockClient *mocks.FakeClient) {
mockClient.ListRefsReturns([]nanogit.Ref{
{
Name: "refs/heads/main",
Hash: hash.MustFromHex("abcdef1234567890abcdef1234567890abcdef12"),
},
{
Name: "refs/heads/feature",
Hash: hash.MustFromHex("1234567890abcdef1234567890abcdef12345678"),
},
{
Name: "refs/tags/v1.0.0",
Hash: hash.MustFromHex("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
},
}, nil)
},
gitConfig: RepositoryConfig{
Branch: "main",
},
wantError: false,
wantRefs: []provisioning.RefItem{
{
Name: "main",
Hash: "abcdef1234567890abcdef1234567890abcdef12",
},
{
Name: "feature",
Hash: "1234567890abcdef1234567890abcdef12345678",
},
},
},
{
name: "failure - list refs error",
setupMock: func(mockClient *mocks.FakeClient) {
mockClient.ListRefsReturns(nil, errors.New("list refs failed"))
},
gitConfig: RepositoryConfig{
Branch: "main",
},
wantError: true,
errorType: errors.New("list refs failed"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockClient := &mocks.FakeClient{}
tt.setupMock(mockClient)
gitRepo := &gitRepository{
client: mockClient,
gitConfig: tt.gitConfig,
config: &provisioning.Repository{
Spec: provisioning.RepositorySpec{
Type: provisioning.GitHubRepositoryType,
},
},
}
refs, err := gitRepo.ListRefs(context.Background())
if tt.wantError {
require.Error(t, err)
if tt.errorType != nil {
require.Contains(t, err.Error(), tt.errorType.Error())
}
require.Nil(t, refs)
} else {
require.NoError(t, err)
require.NotNil(t, refs)
require.Equal(t, len(tt.wantRefs), len(refs))
for i, wantRef := range tt.wantRefs {
require.Equal(t, wantRef.Name, refs[i].Name)
require.Equal(t, wantRef.Hash, refs[i].Hash)
}
}
})
}
}
func TestGitRepository_LatestRef(t *testing.T) {
tests := []struct {
name string