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

feat: filter PUT by ID #1016

Merged
merged 1 commit into from May 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/http/http.go
Expand Up @@ -153,7 +153,12 @@ func Upload(ctx *context.Context, puts []config.Put, kind string, check Response
}).Error(err.Error())
return err
}
if err := uploadWithFilter(ctx, &put, artifact.Or(filters...), kind, check); err != nil {

var filter = artifact.Or(filters...)
if len(put.IDs) > 0 {
filter = artifact.And(filter, artifact.ByIDs(put.IDs...))
}
if err := uploadWithFilter(ctx, &put, filter, kind, check); err != nil {
return err
}
}
Expand Down Expand Up @@ -327,6 +332,7 @@ type targetData struct {

// resolveTargetTemplate returns the resolved target template with replaced variables
// Those variables can be replaced by the given context, goos, goarch, goarm and more
// TODO: replace this with our internal template pkg
func resolveTargetTemplate(ctx *context.Context, put *config.Put, artifact artifact.Artifact) (string, error) {
data := targetData{
Version: ctx.Version,
Expand Down
46 changes: 41 additions & 5 deletions internal/http/http_test.go
Expand Up @@ -122,7 +122,7 @@ type check struct {
func checks(checks ...check) func(rs []*h.Request) error {
return func(rs []*h.Request) error {
if len(rs) != len(checks) {
return errors.New("expectations mismatch requests")
return fmt.Errorf("expected %d requests, got %d", len(checks), len(rs))
}
for _, r := range rs {
found := false
Expand Down Expand Up @@ -226,7 +226,14 @@ func TestUpload(t *testing.T) {
} {
var file = filepath.Join(folder, "a."+a.ext)
require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644))
ctx.Artifacts.Add(artifact.Artifact{Name: "a." + a.ext, Path: file, Type: a.typ})
ctx.Artifacts.Add(artifact.Artifact{
Name: "a." + a.ext,
Path: file,
Type: a.typ,
Extra: map[string]interface{}{
"ID": "foo",
},
})
}

tests := []struct {
Expand Down Expand Up @@ -279,6 +286,22 @@ func TestUpload(t *testing.T) {
check{"/blah/2.1.0/a.tar", "u1", "x", content, map[string]string{}},
),
},
{"archive_with_ids", true, true, false, false,
func(s *httptest.Server) (*context.Context, config.Put) {
return ctx, config.Put{
Mode: ModeArchive,
Name: "a",
Target: s.URL + "/{{.ProjectName}}/{{.Version}}/",
Username: "u1",
TrustedCerts: cert(s),
IDs: []string{"foo"},
}
},
checks(
check{"/blah/2.1.0/a.deb", "u1", "x", content, map[string]string{}},
check{"/blah/2.1.0/a.tar", "u1", "x", content, map[string]string{}},
),
},
{"binary", true, true, false, false,
func(s *httptest.Server) (*context.Context, config.Put) {
return ctx, config.Put{
Expand All @@ -291,6 +314,19 @@ func TestUpload(t *testing.T) {
},
checks(check{"/blah/2.1.0/a.ubi", "u2", "x", content, map[string]string{}}),
},
{"binary_with_ids", true, true, false, false,
func(s *httptest.Server) (*context.Context, config.Put) {
return ctx, config.Put{
Mode: ModeBinary,
Name: "a",
Target: s.URL + "/{{.ProjectName}}/{{.Version}}/",
Username: "u2",
TrustedCerts: cert(s),
IDs: []string{"foo"},
}
},
checks(check{"/blah/2.1.0/a.ubi", "u2", "x", content, map[string]string{}}),
},
{"binary-add-ending-bar", true, true, false, false,
func(s *httptest.Server) (*context.Context, config.Put) {
return ctx, config.Put{
Expand Down Expand Up @@ -387,7 +423,7 @@ func TestUpload(t *testing.T) {
},
}

uploadAndCheck := func(setup func(*httptest.Server) (*context.Context, config.Put), wantErrPlain, wantErrTLS bool, check func(r []*h.Request) error, srv *httptest.Server) {
uploadAndCheck := func(t *testing.T, setup func(*httptest.Server) (*context.Context, config.Put), wantErrPlain, wantErrTLS bool, check func(r []*h.Request) error, srv *httptest.Server) {
requests = nil
ctx, put := setup(srv)
wantErr := wantErrPlain
Expand All @@ -407,15 +443,15 @@ func TestUpload(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
srv := httptest.NewServer(mux)
defer srv.Close()
uploadAndCheck(tt.setup, tt.wantErrPlain, tt.wantErrTLS, tt.check, srv)
uploadAndCheck(t, tt.setup, tt.wantErrPlain, tt.wantErrTLS, tt.check, srv)
})
}
if tt.tryTLS {
t.Run(tt.name+"-tls", func(t *testing.T) {
srv := httptest.NewUnstartedServer(mux)
srv.StartTLS()
defer srv.Close()
uploadAndCheck(tt.setup, tt.wantErrPlain, tt.wantErrTLS, tt.check, srv)
uploadAndCheck(t, tt.setup, tt.wantErrPlain, tt.wantErrTLS, tt.check, srv)
})
}
}
Expand Down
14 changes: 13 additions & 1 deletion www/content/put.md
Expand Up @@ -123,24 +123,36 @@ puts:
-
# Unique name of your Put instance. Used to identify the instance.
name: production

# IDs of the artifacts you want to PUT.
ids:
- foo
- bar

# Upload mode. Valid options are `binary` and `archive`.
# If mode is `archive`, variables _Os_, _Arch_ and _Arm_ for target name are not supported.
# In that case these variables are empty.
# Default is `archive`.
mode: archive

# URL to be used as target of the HTTP PUT request
target: https://some.server/some/path/example-repo-local/{{ .ProjectName }}/{{ .Version }}/

# User that will be used for the deployment
username: deployuser

# An optional header you can use to tell GoReleaser to pass the artifact's
# SHA256 checksum withing the upload request.
# Default is empty.
checksum_header: -X-SHA256-Sum

# Upload checksums (defaults to false)
checksum: true

# Upload signatures (defaults to false)
signature: true
# Certificate chain used to validate server certificates

# Certificate chain used to validate server certificates
trusted_certificates: |
-----BEGIN CERTIFICATE-----
MIIDrjCCApagAwIBAgIIShr2zchZo+8wDQYJKoZIhvcNAQENBQAwNTEXMBUGA1UE
Expand Down