Skip to content

Commit

Permalink
adds ListRepositoryRefs endpoint
Browse files Browse the repository at this point in the history
adds SyncedRefs field to SyncStatus for a record of last synced refs
  • Loading branch information
kishie committed Jan 26, 2024
1 parent f3c8668 commit 75a0744
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 16 deletions.
8 changes: 8 additions & 0 deletions internal/plugins/ostree/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ func (p *Plugin) DeleteRepository(ctx context.Context, repository string) (err e
return p.repositoryManager.Get(ctx, repository).DeleteRepository(ctx)
}

func (p *Plugin) ListRepositoryRefs(ctx context.Context, repository string) (refs []apiv1.OSTreeRef, err error) {
if err := checkRepository(repository); err != nil {
return nil, err
}

return p.repositoryManager.Get(ctx, repository).ListRepositoryRefs(ctx, repository)
}

func (p *Plugin) AddRemote(ctx context.Context, repository string, properties *apiv1.OSTreeRemoteProperties) (err error) {
if err := checkRepository(repository); err != nil {
return err
Expand Down
5 changes: 1 addition & 4 deletions internal/plugins/ostree/pkg/libostree/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,8 @@ func (r *Repo) ListRefsExt(flags ListRefsExtFlags, prefix ...string) ([]Ref, err
break
}

// GHashTable
//ref := (*C.OstreeCollectionRef)(unsafe.Pointer(&cRef))

ret = append(ret, Ref{
Name: C.GoString((*C.char)(cRef)), //C.GoString(ref.ref_name),
Name: C.GoString((*C.char)(cRef)),
Checksum: C.GoString((*C.char)(cChecksum)),
})
}
Expand Down
59 changes: 55 additions & 4 deletions internal/plugins/ostree/pkg/ostreerepository/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,38 @@ func (h *Handler) DeleteRepository(ctx context.Context) (err error) {
return nil
}

func (h *Handler) ListRepositoryRefs(ctx context.Context, repository string) (refs []apiv1.OSTreeRef, err error) {

Check failure on line 138 in internal/plugins/ostree/pkg/ostreerepository/api.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'repository' seems to be unused, consider removing or renaming it as _ (revive)
// Transition to provisioning state
if err := h.setState(StateProvisioning); err != nil {
return nil, err
}
defer h.clearState()

if !h.checkRepoExists(ctx) {
return nil, ctl.Errf("repository does not exist")
}

err = h.BeginLocalRepoTransaction(ctx, func(ctx context.Context, repo *libostree.Repo) (bool, error) {
// Get the refs from the local repo
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
}

// Convert the refs to the API type
for _, loRef := range loRefs {
refs = append(refs, apiv1.OSTreeRef{
Name: loRef.Name,
Commit: loRef.Checksum,
})
}

return false, nil
})

return
}

func (h *Handler) AddRemote(ctx context.Context, remote *apiv1.OSTreeRemoteProperties) (err error) {
// Transition to provisioning state
if err := h.setState(StateProvisioning); err != nil {
Expand Down Expand Up @@ -285,6 +317,15 @@ func (h *Handler) SyncRepository(_ context.Context, properties *apiv1.OSTreeRepo
return false, ctl.Errf("regenerating summary for ostree repository %s: %s", h.repoDir, err)
}

// List the refs in the repository and store in the repoSync
loRefs, err := repo.ListRefsExt(libostree.ListRefsExtFlagsNone)
if err != nil {
return false, ctl.Errf("listing refs from ostree repository: %s", err)
}
repoSync := *h.repoSync.Load()
repoSync.SyncedRefs = loRefs
h.setRepoSync(&repoSync)

return true, nil
})
}()
Expand All @@ -297,10 +338,20 @@ func (h *Handler) GetRepositorySyncStatus(_ context.Context) (syncStatus *apiv1.
if repoSync == nil {
return nil, ctl.Errf("repository sync status not available")
}

var refs []apiv1.OSTreeRef
for _, loRef := range repoSync.SyncedRefs {
refs = append(refs, apiv1.OSTreeRef{
Name: loRef.Name,
Commit: loRef.Checksum,
})
}

return &apiv1.SyncStatus{
Syncing: repoSync.Syncing,
StartTime: utils.TimeToString(repoSync.StartTime),
EndTime: utils.TimeToString(repoSync.EndTime),
SyncError: repoSync.SyncError,
Syncing: repoSync.Syncing,
StartTime: utils.TimeToString(repoSync.StartTime),
EndTime: utils.TimeToString(repoSync.EndTime),
SyncError: repoSync.SyncError,
SyncedRefs: refs,
}, nil
}
11 changes: 7 additions & 4 deletions internal/plugins/ostree/pkg/ostreerepository/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ package ostreerepository

import (
"time"

"go.ciq.dev/beskar/internal/plugins/ostree/pkg/libostree"
)

type RepoSync struct {
Syncing bool
StartTime int64
EndTime int64
SyncError string
Syncing bool
StartTime int64
EndTime int64
SyncError string
SyncedRefs []libostree.Ref
}

func (h *Handler) setRepoSync(repoSync *RepoSync) {
Expand Down
22 changes: 18 additions & 4 deletions pkg/plugins/ostree/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type OSTreeRepositoryProperties struct {
Remotes []OSTreeRemoteProperties `json:"remotes"`
}

type OSTreeRef struct {
// Name - The name of the ref.
Name string `json:"name"`

// Commit - The commit hash of the ref.
Commit string `json:"commit"`
}

type OSTreeRemoteProperties struct {
// Name - The name of the remote repository.
Name string `json:"name"`
Expand Down Expand Up @@ -58,10 +66,11 @@ type OSTreeRepositorySyncRequest struct {

// Mirror sync status.
type SyncStatus struct {
Syncing bool `json:"syncing"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
SyncError string `json:"sync_error"`
Syncing bool `json:"syncing"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
SyncError string `json:"sync_error"`
SyncedRefs []OSTreeRef `json:"synced_refs"`

// TODO: Implement these
// The data for these is present when performing a pull via the ostree cli, so it is in the libostree code base.
Expand All @@ -88,6 +97,11 @@ type OSTree interface {
//kun:success statusCode=202
DeleteRepository(ctx context.Context, repository string) (err error)

// List OSTree repository refs (A.K.A. Branches).
//kun:op GET /repository/refs
//kun:success statusCode=200
ListRepositoryRefs(ctx context.Context, repository string) (refs []OSTreeRef, err error)

// Add a new remote to the OSTree repository.
//kun:op POST /repository/remote
//kun:success statusCode=200
Expand Down
37 changes: 37 additions & 0 deletions pkg/plugins/ostree/api/v1/endpoint.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions pkg/plugins/ostree/api/v1/http.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions pkg/plugins/ostree/api/v1/http_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions pkg/plugins/ostree/api/v1/oas2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 75a0744

Please sign in to comment.