github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/publish/almanac_publish.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package publish 16 17 import ( 18 "encoding/json" 19 "fmt" 20 "io" 21 "net/http" 22 23 "github.com/palantir/godel/apps/distgo/params" 24 ) 25 26 func almanacPublish(artifactURL string, almanacInfo AlmanacInfo, buildSpec params.ProductBuildSpec, distCfg params.Dist, stdout io.Writer) error { 27 client := http.DefaultClient 28 if err := almanacInfo.CheckProduct(client, buildSpec.ProductName); err != nil { 29 if err := almanacInfo.CreateProduct(client, buildSpec.ProductName); err != nil { 30 return fmt.Errorf("failed to create product %s: %v", buildSpec.ProductName, err) 31 } 32 } 33 34 if err := almanacInfo.CheckProductBranch(client, buildSpec.ProductName, buildSpec.VersionInfo.Branch); err != nil { 35 if err := almanacInfo.CreateProductBranch(client, buildSpec.ProductName, buildSpec.VersionInfo.Branch); err != nil { 36 return fmt.Errorf("failed to create branch %s for product %s: %v", buildSpec.VersionInfo.Branch, buildSpec.ProductName, err) 37 } 38 } 39 40 if bytes, err := almanacInfo.GetUnit(client, buildSpec.ProductName, buildSpec.VersionInfo.Branch, buildSpec.VersionInfo.Revision); err == nil { 41 // unit exists -- check if URL matches artifact URL 42 if almanacURLMatches(artifactURL, bytes) { 43 fmt.Fprintf(stdout, "Unit for product %s branch %s revision %s with URL %s already exists; skipping publish.\n", buildSpec.ProductName, buildSpec.VersionInfo.Branch, buildSpec.VersionInfo.Revision, artifactURL) 44 return nil 45 } 46 return fmt.Errorf("unit for product %s branch %s revision %s already exists; not overwriting it", buildSpec.ProductName, buildSpec.VersionInfo.Branch, buildSpec.VersionInfo.Revision) 47 } 48 49 if err := almanacInfo.CreateUnit(client, AlmanacUnit{ 50 Product: buildSpec.ProductName, 51 Branch: buildSpec.VersionInfo.Branch, 52 Revision: buildSpec.VersionInfo.Revision, 53 Metadata: distCfg.Publish.Almanac.Metadata, 54 Tags: distCfg.Publish.Almanac.Tags, 55 URL: artifactURL, 56 }, buildSpec.ProductVersion); err != nil { 57 return fmt.Errorf("failed to publish unit: %v", err) 58 } 59 60 if almanacInfo.Release { 61 if err := almanacInfo.ReleaseProduct(client, buildSpec.ProductName, buildSpec.VersionInfo.Branch, buildSpec.VersionInfo.Revision); err != nil { 62 return fmt.Errorf("failed to release unit: %v", err) 63 } 64 } 65 66 return nil 67 } 68 69 // Returns true if the provided almanacResponse is a JSON object that contains a key named "url" that has a string value 70 // that matches the provided url, false otherwise. 71 func almanacURLMatches(url string, almanacResponse []byte) bool { 72 var jsonMap map[string]*json.RawMessage 73 if err := json.Unmarshal(almanacResponse, &jsonMap); err == nil { 74 if urlJSON, ok := jsonMap["url"]; ok && urlJSON != nil { 75 var dstURL string 76 if err := json.Unmarshal(*urlJSON, &dstURL); err == nil { 77 return url == dstURL 78 } 79 } 80 } 81 return false 82 }