From 5da9760aebb19138c69bf7316e359730543b749a Mon Sep 17 00:00:00 2001 From: Leonard Gram Date: Mon, 5 Nov 2018 09:51:54 +0100 Subject: [PATCH] build: publisher supports both local and remote. --- .../release_publisher/externalrelease.go | 5 +-- .../build/release_publisher/localrelease.go | 23 ++++++------- scripts/build/release_publisher/main.go | 33 ++++++++++++++----- scripts/build/release_publisher/publisher.go | 8 ++--- .../build/release_publisher/publisher_test.go | 25 +++++++++----- 5 files changed, 61 insertions(+), 33 deletions(-) diff --git a/scripts/build/release_publisher/externalrelease.go b/scripts/build/release_publisher/externalrelease.go index 795e3bc999b..2d69fa604f8 100644 --- a/scripts/build/release_publisher/externalrelease.go +++ b/scripts/build/release_publisher/externalrelease.go @@ -11,15 +11,16 @@ import ( type releaseFromExternalContent struct { getter urlGetter rawVersion string + artifactConfigurations []buildArtifact } -func (re releaseFromExternalContent) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string, artifactConfigurations []buildArtifact) (*release, error) { +func (re releaseFromExternalContent) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string) (*release, error) { version := re.rawVersion[1:] now := time.Now() isBeta := strings.Contains(version, "beta") builds := []build{} - for _, ba := range artifactConfigurations { + for _, ba := range re.artifactConfigurations { sha256, err := re.getter.getContents(fmt.Sprintf("%s.sha256", ba.getUrl(baseArchiveUrl, version, isBeta))) if err != nil { return nil, err diff --git a/scripts/build/release_publisher/localrelease.go b/scripts/build/release_publisher/localrelease.go index 45bebc524e4..1fb266aa041 100644 --- a/scripts/build/release_publisher/localrelease.go +++ b/scripts/build/release_publisher/localrelease.go @@ -14,14 +14,16 @@ import ( type releaseLocalSources struct { path string + artifactConfigurations []buildArtifact } -func (r releaseLocalSources) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string, artifactConfigurations []buildArtifact) (*release, error) { - buildData := r.findBuilds(artifactConfigurations, baseArchiveUrl) +func (r releaseLocalSources) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string) (*release, error) { + buildData := r.findBuilds(baseArchiveUrl) + now := time.Now() rel := release{ Version: buildData.version, - ReleaseDate: time.Time{}, + ReleaseDate: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local), Stable: false, Beta: false, Nightly: true, @@ -38,9 +40,9 @@ type buildData struct { builds []build } -func (r releaseLocalSources) findBuilds(buildArtifacts []buildArtifact, baseArchiveUrl string) buildData { +func (r releaseLocalSources) findBuilds(baseArchiveUrl string) buildData { data := buildData{} - filepath.Walk(r.path, createBuildWalker(r.path, &data, buildArtifacts, baseArchiveUrl)) + filepath.Walk(r.path, createBuildWalker(r.path, &data, r.artifactConfigurations, baseArchiveUrl)) return data } @@ -54,14 +56,13 @@ func createBuildWalker(path string, data *buildData, archiveTypes []buildArtifac return nil } - shaBytes, err := ioutil.ReadFile(path + ".sha256") - if err != nil { - log.Fatalf("Failed to read sha256 file %v", err) - } - - for _, archive := range archiveTypes { if strings.HasSuffix(f.Name(), archive.urlPostfix) { + shaBytes, err := ioutil.ReadFile(path + ".sha256") + if err != nil { + log.Fatalf("Failed to read sha256 file %v", err) + } + version, err := grabVersion(f.Name(), archive.urlPostfix) if err != nil { log.Println(err) diff --git a/scripts/build/release_publisher/main.go b/scripts/build/release_publisher/main.go index 8b3ec8da74e..66ab38ab00e 100644 --- a/scripts/build/release_publisher/main.go +++ b/scripts/build/release_publisher/main.go @@ -13,6 +13,7 @@ func main() { var releaseNotesUrl string var dryRun bool var enterprise bool + var fromLocal bool var apiKey string flag.StringVar(&version, "version", "", "Grafana version (ex: --version v5.2.0-beta1)") @@ -21,6 +22,7 @@ func main() { flag.StringVar(&apiKey, "apikey", "", "Grafana.com API key (ex: --apikey ABCDEF)") flag.BoolVar(&dryRun, "dry-run", false, "--dry-run") flag.BoolVar(&enterprise, "enterprise", false, "--enterprise") + flag.BoolVar(&fromLocal, "from-local", false, "--from-local") flag.Parse() if len(os.Args) == 1 { @@ -33,24 +35,39 @@ func main() { log.Println("Dry-run has been enabled.") } var baseUrl string + var builder releaseBuilder + var product string + + if fromLocal { + path, _ := os.Getwd() + builder = releaseLocalSources{ + path: path, + artifactConfigurations: buildArtifactConfigurations, + } + } else { + builder = releaseFromExternalContent{ + getter: getHttpContents{}, + rawVersion: version, + artifactConfigurations: buildArtifactConfigurations, + } + } if enterprise { - baseUrl = fmt.Sprintf("https://s3-us-west-2.amazonaws.com/%s", "grafana-enterprise-releases/release/grafana-enterprise") + baseUrl = "https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/release/grafana-enterprise" + product = "grafana-enterprise" } else { - baseUrl = fmt.Sprintf("https://s3-us-west-2.amazonaws.com/%s", "grafana-releases/release/grafana") + baseUrl = "https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana" + product = "grafana" } p := publisher{ apiKey: apiKey, - baseUri: "https://grafana.com/api", - product: "grafana", + apiUri: "https://grafana.com/api", + product: product, dryRun: dryRun, enterprise: enterprise, baseArchiveUrl: baseUrl, - builder: releaseFromExternalContent{ - getter: getHttpContents{}, - rawVersion: version, - }, + builder: builder, } if err := p.doRelease(whatsNewUrl, releaseNotesUrl); err != nil { log.Fatalf("error: %v", err) diff --git a/scripts/build/release_publisher/publisher.go b/scripts/build/release_publisher/publisher.go index e8c3b676e59..0874c1357b6 100644 --- a/scripts/build/release_publisher/publisher.go +++ b/scripts/build/release_publisher/publisher.go @@ -13,7 +13,7 @@ import ( type publisher struct { apiKey string - baseUri string + apiUri string product string dryRun bool enterprise bool @@ -22,11 +22,11 @@ type publisher struct { } type releaseBuilder interface { - prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string, artifactConfigurations []buildArtifact) (*release, error) + prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string) (*release, error) } func (p *publisher) doRelease(whatsNewUrl string, releaseNotesUrl string) error { - currentRelease, err := p.builder.prepareRelease(p.baseArchiveUrl, whatsNewUrl, releaseNotesUrl, buildArtifactConfigurations) + currentRelease, err := p.builder.prepareRelease(p.baseArchiveUrl, whatsNewUrl, releaseNotesUrl) if err != nil { return err } @@ -151,7 +151,7 @@ func newBuild(baseArchiveUrl string, ba buildArtifact, version string, isBeta bo } func (p *publisher) apiUrl(url string) string { - return fmt.Sprintf("%s/%s%s", p.baseUri, p.product, url) + return fmt.Sprintf("%s/%s%s", p.apiUri, p.product, url) } func (p *publisher) postRequest(url string, obj interface{}, desc string) error { diff --git a/scripts/build/release_publisher/publisher_test.go b/scripts/build/release_publisher/publisher_test.go index 14e92f01bc0..fed17007bb6 100644 --- a/scripts/build/release_publisher/publisher_test.go +++ b/scripts/build/release_publisher/publisher_test.go @@ -16,9 +16,10 @@ func TestPreparingReleaseFromRemote(t *testing.T) { builder = releaseFromExternalContent{ getter: mockHttpGetter{}, rawVersion: versionIn, + artifactConfigurations: buildArtifactConfigurations, } - rel, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana", whatsNewUrl, relNotesUrl, buildArtifacts) + rel, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana", whatsNewUrl, relNotesUrl) if !rel.Beta || rel.Stable { t.Errorf("%s should have been tagged as beta (not stable), but wasn't .", versionIn) @@ -57,11 +58,13 @@ func TestPreparingReleaseFromLocal(t *testing.T) { expectedBuilds := 4 var builder releaseBuilder + testDataPath := "local_test_data" builder = releaseLocalSources{ - path: "local_test_data", + path: testDataPath, + artifactConfigurations: buildArtifactConfigurations, } - relAll, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/master/grafana-enterprise", whatsNewUrl, relNotesUrl, buildArtifactConfigurations) + relAll, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/master/grafana-enterprise", whatsNewUrl, relNotesUrl) if relAll.Stable || !relAll.Nightly { t.Error("Expected a nightly release but wasn't.") @@ -88,11 +91,17 @@ func TestPreparingReleaseFromLocal(t *testing.T) { expectedArch := "amd64" expectedOs := "win" - relOne, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/master/grafana-enterprise", whatsNewUrl, relNotesUrl, []buildArtifact{{ - os: expectedOs, - arch: expectedArch, - urlPostfix: ".windows-amd64.zip", - }}) + + builder = releaseLocalSources{ + path: testDataPath, + artifactConfigurations: []buildArtifact{{ + os: expectedOs, + arch: expectedArch, + urlPostfix: ".windows-amd64.zip", + }}, + } + + relOne, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/master/grafana-enterprise", whatsNewUrl, relNotesUrl) if len(relOne.Builds) != 1 { t.Errorf("Expected 1 artifact, but was %v", len(relOne.Builds))