Commit 5da9760a by Leonard Gram

build: publisher supports both local and remote.

parent d9eaec99
...@@ -11,15 +11,16 @@ import ( ...@@ -11,15 +11,16 @@ import (
type releaseFromExternalContent struct { type releaseFromExternalContent struct {
getter urlGetter getter urlGetter
rawVersion string 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:] version := re.rawVersion[1:]
now := time.Now() now := time.Now()
isBeta := strings.Contains(version, "beta") isBeta := strings.Contains(version, "beta")
builds := []build{} 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))) sha256, err := re.getter.getContents(fmt.Sprintf("%s.sha256", ba.getUrl(baseArchiveUrl, version, isBeta)))
if err != nil { if err != nil {
return nil, err return nil, err
......
...@@ -14,14 +14,16 @@ import ( ...@@ -14,14 +14,16 @@ import (
type releaseLocalSources struct { type releaseLocalSources struct {
path string path string
artifactConfigurations []buildArtifact
} }
func (r releaseLocalSources) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string, artifactConfigurations []buildArtifact) (*release, error) { func (r releaseLocalSources) prepareRelease(baseArchiveUrl, whatsNewUrl string, releaseNotesUrl string) (*release, error) {
buildData := r.findBuilds(artifactConfigurations, baseArchiveUrl) buildData := r.findBuilds(baseArchiveUrl)
now := time.Now()
rel := release{ rel := release{
Version: buildData.version, Version: buildData.version,
ReleaseDate: time.Time{}, ReleaseDate: time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local),
Stable: false, Stable: false,
Beta: false, Beta: false,
Nightly: true, Nightly: true,
...@@ -38,9 +40,9 @@ type buildData struct { ...@@ -38,9 +40,9 @@ type buildData struct {
builds []build builds []build
} }
func (r releaseLocalSources) findBuilds(buildArtifacts []buildArtifact, baseArchiveUrl string) buildData { func (r releaseLocalSources) findBuilds(baseArchiveUrl string) buildData {
data := 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 return data
} }
...@@ -54,14 +56,13 @@ func createBuildWalker(path string, data *buildData, archiveTypes []buildArtifac ...@@ -54,14 +56,13 @@ func createBuildWalker(path string, data *buildData, archiveTypes []buildArtifac
return nil return nil
} }
shaBytes, err := ioutil.ReadFile(path + ".sha256")
if err != nil {
log.Fatalf("Failed to read sha256 file %v", err)
}
for _, archive := range archiveTypes { for _, archive := range archiveTypes {
if strings.HasSuffix(f.Name(), archive.urlPostfix) { 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) version, err := grabVersion(f.Name(), archive.urlPostfix)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
......
...@@ -13,6 +13,7 @@ func main() { ...@@ -13,6 +13,7 @@ func main() {
var releaseNotesUrl string var releaseNotesUrl string
var dryRun bool var dryRun bool
var enterprise bool var enterprise bool
var fromLocal bool
var apiKey string var apiKey string
flag.StringVar(&version, "version", "", "Grafana version (ex: --version v5.2.0-beta1)") flag.StringVar(&version, "version", "", "Grafana version (ex: --version v5.2.0-beta1)")
...@@ -21,6 +22,7 @@ func main() { ...@@ -21,6 +22,7 @@ func main() {
flag.StringVar(&apiKey, "apikey", "", "Grafana.com API key (ex: --apikey ABCDEF)") flag.StringVar(&apiKey, "apikey", "", "Grafana.com API key (ex: --apikey ABCDEF)")
flag.BoolVar(&dryRun, "dry-run", false, "--dry-run") flag.BoolVar(&dryRun, "dry-run", false, "--dry-run")
flag.BoolVar(&enterprise, "enterprise", false, "--enterprise") flag.BoolVar(&enterprise, "enterprise", false, "--enterprise")
flag.BoolVar(&fromLocal, "from-local", false, "--from-local")
flag.Parse() flag.Parse()
if len(os.Args) == 1 { if len(os.Args) == 1 {
...@@ -33,24 +35,39 @@ func main() { ...@@ -33,24 +35,39 @@ func main() {
log.Println("Dry-run has been enabled.") log.Println("Dry-run has been enabled.")
} }
var baseUrl string 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 { 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 { } 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{ p := publisher{
apiKey: apiKey, apiKey: apiKey,
baseUri: "https://grafana.com/api", apiUri: "https://grafana.com/api",
product: "grafana", product: product,
dryRun: dryRun, dryRun: dryRun,
enterprise: enterprise, enterprise: enterprise,
baseArchiveUrl: baseUrl, baseArchiveUrl: baseUrl,
builder: releaseFromExternalContent{ builder: builder,
getter: getHttpContents{},
rawVersion: version,
},
} }
if err := p.doRelease(whatsNewUrl, releaseNotesUrl); err != nil { if err := p.doRelease(whatsNewUrl, releaseNotesUrl); err != nil {
log.Fatalf("error: %v", err) log.Fatalf("error: %v", err)
......
...@@ -13,7 +13,7 @@ import ( ...@@ -13,7 +13,7 @@ import (
type publisher struct { type publisher struct {
apiKey string apiKey string
baseUri string apiUri string
product string product string
dryRun bool dryRun bool
enterprise bool enterprise bool
...@@ -22,11 +22,11 @@ type publisher struct { ...@@ -22,11 +22,11 @@ type publisher struct {
} }
type releaseBuilder interface { 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 { 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 { if err != nil {
return err return err
} }
...@@ -151,7 +151,7 @@ func newBuild(baseArchiveUrl string, ba buildArtifact, version string, isBeta bo ...@@ -151,7 +151,7 @@ func newBuild(baseArchiveUrl string, ba buildArtifact, version string, isBeta bo
} }
func (p *publisher) apiUrl(url string) string { 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 { func (p *publisher) postRequest(url string, obj interface{}, desc string) error {
......
...@@ -16,9 +16,10 @@ func TestPreparingReleaseFromRemote(t *testing.T) { ...@@ -16,9 +16,10 @@ func TestPreparingReleaseFromRemote(t *testing.T) {
builder = releaseFromExternalContent{ builder = releaseFromExternalContent{
getter: mockHttpGetter{}, getter: mockHttpGetter{},
rawVersion: versionIn, 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 { if !rel.Beta || rel.Stable {
t.Errorf("%s should have been tagged as beta (not stable), but wasn't .", versionIn) t.Errorf("%s should have been tagged as beta (not stable), but wasn't .", versionIn)
...@@ -57,11 +58,13 @@ func TestPreparingReleaseFromLocal(t *testing.T) { ...@@ -57,11 +58,13 @@ func TestPreparingReleaseFromLocal(t *testing.T) {
expectedBuilds := 4 expectedBuilds := 4
var builder releaseBuilder var builder releaseBuilder
testDataPath := "local_test_data"
builder = releaseLocalSources{ 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 { if relAll.Stable || !relAll.Nightly {
t.Error("Expected a nightly release but wasn't.") t.Error("Expected a nightly release but wasn't.")
...@@ -88,11 +91,17 @@ func TestPreparingReleaseFromLocal(t *testing.T) { ...@@ -88,11 +91,17 @@ func TestPreparingReleaseFromLocal(t *testing.T) {
expectedArch := "amd64" expectedArch := "amd64"
expectedOs := "win" expectedOs := "win"
relOne, _ := builder.prepareRelease("https://s3-us-west-2.amazonaws.com/grafana-enterprise-releases/master/grafana-enterprise", whatsNewUrl, relNotesUrl, []buildArtifact{{
os: expectedOs, builder = releaseLocalSources{
arch: expectedArch, path: testDataPath,
urlPostfix: ".windows-amd64.zip", 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 { if len(relOne.Builds) != 1 {
t.Errorf("Expected 1 artifact, but was %v", len(relOne.Builds)) t.Errorf("Expected 1 artifact, but was %v", len(relOne.Builds))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment