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
@@ -200,6 +200,20 @@ func (r *githubRepository) History(ctx context.Context, path, ref string) ([]pro
return ret, nil
}
// ListRefs list refs from the git repository and add the ref URL to the ref item
func (r *githubRepository) ListRefs(ctx context.Context) ([]provisioning.RefItem, error) {
refs, err := r.gitRepo.ListRefs(ctx)
if err != nil {
return nil, fmt.Errorf("list refs: %w", err)
}
for i := range refs {
refs[i].RefURL = fmt.Sprintf("%s/tree/%s", r.config.Spec.GitHub.URL, refs[i].Name)
}
return refs, nil
}
func (r *githubRepository) LatestRef(ctx context.Context) (string, error) {
return r.gitRepo.LatestRef(ctx)
}
@@ -921,6 +921,40 @@ func TestGitHubRepositoryDelegation(t *testing.T) {
mockGitRepo.AssertExpectations(t)
})
t.Run("ListRefs delegates to git repo but adds ref URL", func(t *testing.T) {
mockGitRepo := git.NewMockGitRepository(t)
// The git repo returns refs without RefURL
gitRepoRefs := []provisioning.RefItem{
{Name: "main", Hash: "abc123def456"},
{Name: "feature", Hash: "def456ghi789"},
}
mockGitRepo.On("ListRefs", ctx).Return(gitRepoRefs, nil)
repo := &githubRepository{
config: config,
gitRepo: mockGitRepo,
}
result, err := repo.ListRefs(ctx)
require.NoError(t, err)
// The returned refs should have RefURL set
expectedRefs := []provisioning.RefItem{
{
Name: "main",
Hash: "abc123def456",
RefURL: "https://github.com/grafana/grafana/tree/main",
},
{
Name: "feature",
Hash: "def456ghi789",
RefURL: "https://github.com/grafana/grafana/tree/feature",
},
}
assert.Equal(t, expectedRefs, result)
mockGitRepo.AssertExpectations(t)
})
t.Run("CompareFiles delegates to git repo", func(t *testing.T) {
mockGitRepo := git.NewMockGitRepository(t)
expectedChanges := []repository.VersionedFileChange{