Skip to content

Commit

Permalink
Merge pull request #106 from netlify/add-use-asset-mgmt
Browse files Browse the repository at this point in the history
Add feature to create file.Sum with original sha included for asset mgmt (again)
  • Loading branch information
bcomnes authored Sep 11, 2018
2 parents c5a5c79 + 62a4b1d commit 056879d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
31 changes: 31 additions & 0 deletions go/models/deploy.go

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

43 changes: 43 additions & 0 deletions go/models/deploy_site_capabilities.go

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

28 changes: 26 additions & 2 deletions go/porcelain/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -161,7 +162,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
}
}

files, err := walk(options.Dir, options.Observer)
files, err := walk(options.Dir, options.Observer, deploy.SiteCapabilities.AssetManagement)
if err != nil {
if options.Observer != nil {
options.Observer.OnFailedWalk()
Expand Down Expand Up @@ -451,7 +452,7 @@ func (n *Netlify) uploadFile(ctx context.Context, d *models.Deploy, f *FileBundl
}
}

func walk(dir string, observer DeployObserver) (*deployFiles, error) {
func walk(dir string, observer DeployObserver, useAssetManagement bool) (*deployFiles, error) {
files := newDeployFiles()

err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
Expand Down Expand Up @@ -487,6 +488,13 @@ func walk(dir string, observer DeployObserver) (*deployFiles, error) {
}
file.Sum = hex.EncodeToString(s.Sum(nil))

if useAssetManagement {
originalSha := getAssetManagementSha(o)
if originalSha != "" {
file.Sum += ":" + string(originalSha)
}
}

files.Add(rel, file)

if observer != nil {
Expand Down Expand Up @@ -634,3 +642,19 @@ func createHeader(archive *zip.Writer, i os.FileInfo, runtime string) (io.Writer
}
return archive.Create(i.Name())
}

func getAssetManagementSha(file io.Reader) string {
// currently this only supports certain type of git lfs pointer files
// version [version]\noid sha256:[oid]\nsize [size]
data := make([]byte, 150)
if count, err := file.Read(data); err == nil {
r, _ := regexp.Compile(`^version \S+\noid sha256:(\S+)\n`)
res := r.FindSubmatch(data[:count])
if len(res) == 2 {
if originalSha := res[1]; len(originalSha) == 64 {
return string(originalSha)
}
}
}
return ""
}
26 changes: 26 additions & 0 deletions go/porcelain/deploy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package porcelain

import (
"strings"
"testing"
)

func TestGetAssetManagementSha(t *testing.T) {
tests := []struct {
contents string
length int
}{
{"Not a pointer file", 0},
{"version https://git-lfs.github.com/spec/v1\n" +
"oid sha256:7e56e498ccb4cbb9c672e1aed6710fb91b2fd314394a666c11c33b2059ea3d71\n" +
"size 1743570", 64},
}

for _, test := range tests {
file := strings.NewReader(test.contents)
out := getAssetManagementSha(file)
if len(out) != test.length {
t.Fatalf("expected `%d`, got `%d`", test.length, len(out))
}
}
}
5 changes: 5 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,11 @@ definitions:
type: boolean
review_url:
type: string
site_capabilities:
type: object
properties:
asset_management:
type: boolean
deployFiles:
type: object
properties:
Expand Down

0 comments on commit 056879d

Please sign in to comment.