Skip to content

Commit

Permalink
Try to add writing contents to ephemeral repo
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSGK committed Sep 28, 2024
1 parent d4da020 commit 7cac66a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
37 changes: 37 additions & 0 deletions pkg/repo/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,43 @@ import (
"google.golang.org/protobuf/proto"
)

// TODO: Diffing method

// Take the namespace contents of `other` into `dst`.
// Namespaces and features in `other` that are not in `dst` are added.
// Conflicting features are overwritten with ones from `other`.
// Namespaces in `dst` that are not in `other` are NOT removed.
// Returns the modified `dst`. `other` should not be used after being passed.
func TakeNamespaceContents(dst *featurev1beta1.RepositoryContents, other *featurev1beta1.RepositoryContents) *featurev1beta1.RepositoryContents {
for _, otherNs := range other.Namespaces {
replaced := false
for i, dstNs := range dst.Namespaces {
if dstNs.Name == otherNs.Name {
dst.Namespaces[i] = otherNs
replaced = true
}
}
if !replaced {
dst.Namespaces = append(dst.Namespaces, otherNs)
}
}
// Need to also overwrite appropriate file descriptors
// Assumes that file descriptors corresponding to namespaces have deterministic names
for _, otherFD := range other.FileDescriptorSet.File {
replaced := false
for i, dstFD := range dst.FileDescriptorSet.File {
if dstFD.Name == otherFD.Name {
dst.FileDescriptorSet.File[i] = otherFD
replaced = true
}
}
if !replaced {
dst.FileDescriptorSet.File = append(dst.FileDescriptorSet.File, otherFD)
}
}
return dst
}

// Expects base64 encoded serialized RepositoryContents message
func DecodeRepositoryContents(encoded []byte) (*featurev1beta1.RepositoryContents, error) {
// Because Protobuf is not self-describing, we have to jump through some hoops here for deserialization.
Expand Down
7 changes: 4 additions & 3 deletions pkg/repo/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,9 +681,10 @@ func (r *repository) BuildDynamicTypeRegistry(ctx context.Context, protoDirPath
// because we need to first ensure that buf cmd line can be executed in the
// ephemeral env.
func (r *repository) ReBuildDynamicTypeRegistry(ctx context.Context, protoDirPath string, useExternalTypes bool) (*protoregistry.Types, error) {
if !r.bufEnabled {
return nil, errors.New("buf cmd line not enabled")
}
// TODO: We do need a way of regenerating the buf image in ephemeral repos. Disabling this check for now.
// if !r.bufEnabled {
// return nil, errors.New("buf cmd line not enabled")
// }
sTypes, err := prototypes.ReBuildDynamicTypeRegistry(ctx, protoDirPath, useExternalTypes, r)
if err != nil {
return nil, err
Expand Down
13 changes: 10 additions & 3 deletions pkg/sync/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ func WriteContentsToLocalRepo(ctx context.Context, contents *featurev1beta1.Repo
if err != nil {
return errors.Wrap(err, "prepare repo")
}
return WriteContentsToRepo(ctx, contents, r)
}

func WriteContentsToRepo(ctx context.Context, contents *featurev1beta1.RepositoryContents, r repo.ConfigurationRepository) error {
// Discard logs, mainly for silencing compilation later
// TODO: Maybe a verbose flag
r.ConfigureLogger(&repo.LoggingConfiguration{
Writer: io.Discard,
// TODO: Allow passing in writer
clear := r.ConfigureLogger(&repo.LoggingConfiguration{
Writer: io.Discard,
ColorsDisabled: true,
})
defer clear()
rootMD, _, err := r.ParseMetadata(ctx)
if err != nil {
return err
Expand Down Expand Up @@ -159,6 +165,7 @@ func WriteContentsToLocalRepo(ctx context.Context, contents *featurev1beta1.Repo
if err := WriteTypesToRepo(ctx, contents.FileDescriptorSet, r); err != nil {
return errors.Wrap(err, "write type files")
}
// FIXME: We need this to be runnable on ephemeral repositories
if _, err := r.ReBuildDynamicTypeRegistry(ctx, rootMD.ProtoDirectory, false); err != nil {
return errors.Wrap(err, "final rebuild type registry")
}
Expand Down

0 comments on commit 7cac66a

Please sign in to comment.