Commit 5da9760a by Leonard Gram

build: publisher supports both local and remote.

parent d9eaec99
......@@ -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
......
......@@ -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
}
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)
}
for _, archive := range archiveTypes {
if strings.HasSuffix(f.Name(), archive.urlPostfix) {
version, err := grabVersion(f.Name(), archive.urlPostfix)
if err != nil {
log.Println(err)
......
......@@ -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)
......
......@@ -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 {
......
......@@ -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{{
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))
......
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