Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support yank a single version on client side #605

Merged
merged 5 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func newMirrorModifyCmd() *cobra.Command {
yanked := false

cmd := &cobra.Command{
Use: "modify <comp-name> [flags]",
Use: "modify <component>[:version] [flags]",
Long: "modify component attributes (hidden, standalone, yanked)",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
Expand All @@ -253,15 +253,16 @@ func newMirrorModifyCmd() *cobra.Command {
return err
}

m, err := env.V1Repository().FetchComponentManifest(args[0])
comp, ver := environment.ParseCompVersion(args[0])
m, err := env.V1Repository().FetchComponentManifest(comp)
if err != nil {
return err
}

if endpoint == "" {
endpoint = environment.Mirror()
}
e := remote.NewEditor(endpoint, args[0]).WithDesc(desc)
e := remote.NewEditor(endpoint, comp).WithDesc(desc).WithVersion(ver.String())
flagSet := set.NewStringSet()
cmd.Flags().Visit(func(f *pflag.Flag) {
flagSet.Insert(f.Name)
Expand Down
25 changes: 25 additions & 0 deletions pkg/repository/remote/modify.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import (
"time"

"github.com/google/uuid"
"github.com/juju/errors"
"github.com/pingcap/tiup/pkg/repository/v0manifest"
"github.com/pingcap/tiup/pkg/repository/v1manifest"
)

// Editor defines the methods to modify a component attrs
type Editor interface {
WithVersion(version string) Editor
WithDesc(desc string) Editor
Standalone(bool) Editor
Hide(bool) Editor
Expand All @@ -33,6 +36,7 @@ type Editor interface {
type editor struct {
endpoint string
component string
version string
description string
options map[string]bool
}
Expand All @@ -46,6 +50,12 @@ func NewEditor(endpoint, component string) Editor {
}
}

// WithVersion set version field
func (e *editor) WithVersion(version string) Editor {
e.version = version
return e
}

// WithDesc set description field
func (e *editor) WithDesc(desc string) Editor {
e.description = desc
Expand Down Expand Up @@ -81,5 +91,20 @@ func (e *editor) Sign(key *v1manifest.KeyInfo, m *v1manifest.Component) error {
sid := uuid.New().String()
url := fmt.Sprintf("%s/api/v1/component/%s/%s", e.endpoint, sid, e.component)

if e.version != "" {
// Only support modify yanked field for specified versiion
for p := range m.Platforms {
if v0manifest.Version(e.version).IsNightly() {
return errors.New("nightly version can't be yanked")
}
vi, ok := m.Platforms[p][e.version]
if !ok {
continue
}
vi.Yanked = e.options["yanked"]
m.Platforms[p][e.version] = vi
}
return signAndSend(url, m, key, nil)
}
return signAndSend(url, m, key, e.options)
}
2 changes: 1 addition & 1 deletion pkg/repository/v1manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ var ManifestsConfig = map[string]ty{
ManifestTypeComponent: {
Filename: "",
Versioned: true,
Expire: time.Hour * 24 * 365, // 1y
Expire: time.Hour * 24 * 365 * 5, // 5y
Threshold: 1,
},
ManifestTypeSnapshot: {
Expand Down
85 changes: 85 additions & 0 deletions pkg/repository/v1manifest/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2020 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package v1manifest

import (
"testing"

"github.com/alecthomas/assert"
)

func TestComponentList(t *testing.T) {
manifest := &Index{
Components: map[string]ComponentItem{
"comp1": ComponentItem{},
"comp2": ComponentItem{Yanked: true},
},
}

list := manifest.ComponentList()
assert.Equal(t, len(list), 1)
_, ok := list["comp1"]
assert.True(t, ok)

list = manifest.ComponentListWithYanked()
assert.Equal(t, len(list), 2)
_, ok = list["comp1"]
assert.True(t, ok)
_, ok = list["comp2"]
assert.True(t, ok)
}

func TestVersionList(t *testing.T) {
manifest := &Component{
Platforms: map[string]map[string]VersionItem{
"linux/amd64": {
"v1.0.0": {Entry: "test"},
"v1.1.1": {Entry: "test", Yanked: true},
},
"any/any": {
"v1.0.0": {Entry: "test"},
"v1.1.1": {Entry: "test", Yanked: true},
},
},
}

versions := manifest.VersionList("linux/amd64")
assert.Equal(t, len(versions), 1)
_, ok := versions["v1.0.0"]
assert.True(t, ok)

versions = manifest.VersionListWithYanked("linux/amd64")
assert.Equal(t, len(versions), 2)
_, ok = versions["v1.0.0"]
assert.True(t, ok)
_, ok = versions["v1.1.1"]
assert.True(t, ok)

versions = manifest.VersionList("windows/amd64")
assert.Equal(t, len(versions), 1)
_, ok = versions["v1.0.0"]
assert.True(t, ok)

manifest = &Component{
Platforms: map[string]map[string]VersionItem{
"linux/amd64": {
"v1.0.0": {Entry: "test"},
"v1.1.1": {Entry: "test", Yanked: true},
},
},
}

versions = manifest.VersionList("windows/amd64")
assert.Equal(t, len(versions), 0)
}
1 change: 1 addition & 0 deletions tests/tiup/tiup.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mirror = "https://tiup-mirrors.pingcap.com"