Skip to content

Commit

Permalink
cmd/cue: include metadata when publishing modules
Browse files Browse the repository at this point in the history
This change wires up the new metadata support
in the modregistry package to the actual `cue mod publish`
command.

Fixes #3034

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I62804cc56326f3b2b20d977901146f7548492f96
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193706
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Apr 24, 2024
1 parent cf2551f commit b7c5800
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
11 changes: 7 additions & 4 deletions cmd/cue/cmd/modpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func runModUpload(cmd *Command, args []string) error {

// TODO verify that all dependencies exist in the registry.

var vcsStatus vcs.Status
var meta *modregistry.Metadata

switch mf.Source.Kind {
case "self":
Expand Down Expand Up @@ -121,16 +121,19 @@ func runModUpload(cmd *Command, args []string) error {
}); err != nil {
return err
}
vcsStatus = status
meta = &modregistry.Metadata{
VCSType: mf.Source.Kind,
VCSCommit: status.Revision,
VCSCommitTime: status.CommitTime,
}
}
info, err := zf.Stat()
if err != nil {
return err
}

rclient := modregistry.NewClientWithResolver(resolver)
_ = vcsStatus // TODO attach vcsStatus to PutModule metadata
if err := rclient.PutModule(backgroundContext(), mv, zf, info.Size()); err != nil {
if err := rclient.PutModuleWithMetadata(backgroundContext(), mv, zf, info.Size(), meta); err != nil {
return fmt.Errorf("cannot put module: %v", err)
}
fmt.Printf("published %s\n", mv)
Expand Down
33 changes: 33 additions & 0 deletions cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"net/http/httptest"
Expand All @@ -32,7 +33,9 @@ import (
"testing"
"time"

"cuelabs.dev/go/oci/ociregistry/ociclient"
"cuelabs.dev/go/oci/ociregistry/ocimem"
"cuelabs.dev/go/oci/ociregistry/ociref"
"github.com/google/shlex"
"github.com/rogpeppe/go-internal/goproxytest"
"github.com/rogpeppe/go-internal/gotooltest"
Expand Down Expand Up @@ -112,6 +115,36 @@ func TestScript(t *testing.T) {
ts.Check(os.WriteFile(path, []byte(data), 0o666))
}
},
// get-manifest writes the manifest for a given reference within an OCI
// registry to a file in JSON format.
"get-manifest": func(ts *testscript.TestScript, neg bool, args []string) {
usage := func() {
ts.Fatalf("usage: get-metadata OCI-ref@tag dest-file")
}
if neg {
usage()
}
if len(args) != 2 {
usage()
}
ref, err := ociref.Parse(args[0])
if err != nil {
ts.Fatalf("invalid OCI reference %q: %v", args[0], err)
}
if ref.Tag == "" {
ts.Fatalf("no tag in OCI reference %q", args[0])
}
client, err := ociclient.New(ref.Host, &ociclient.Options{
Insecure: true,
})
ts.Check(err)
r, err := client.GetTag(context.Background(), ref.Repository, ref.Tag)
ts.Check(err)
data, err := io.ReadAll(r)
ts.Check(err)
err = os.WriteFile(ts.MkAbs(args[1]), data, 0o666)
ts.Check(err)
},
// memregistry starts an in-memory OCI server and sets the argument
// environment variable name to its hostname.
"memregistry": func(ts *testscript.TestScript, neg bool, args []string) {
Expand Down
15 changes: 15 additions & 0 deletions cmd/cue/cmd/testdata/script/registry_publish_with_git.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ cd $WORK/main
exec cue eval .
cmp stdout ../expect-eval-stdout

# Check that the manifest contains the expected git metadata
# Note: we use cue vet rather than cmp because the
# manifest contains information that's tricky to control/predict
# in a test, such as git commit times and commit hashes.
get-manifest $MEMREGISTRY/x.example/e:v0.0.1 $WORK/manifest0.json
exec cue vet $WORK/manifest-schema.cue $WORK/manifest0.json

# If the git directory is not clean, the publish should fail. We can
# conveniently combine that check with the .gitignore removal.
cd $WORK/example
Expand All @@ -38,6 +45,14 @@ exec cue mod get x.example/e@v0.0.2
exec cue eval .
cmp stdout $WORK/expect-eval-stdout2

-- manifest-schema.cue --
import "time"

annotations!: {
"org.cuelang.vcs-type"!: "git"
"org.cuelang.vcs-commit-time"!: time.Time
"org.cuelang.vcs-commit"!: =~"^[a-f0-9]+$"
}
-- expect-publish-stdout --
published x.example/e@v0.0.1
-- expect-eval-stdout --
Expand Down

0 comments on commit b7c5800

Please sign in to comment.