Beispiel #1
0
/* create a new request that sends the auth token */
func NewAuthRequest(log *logging.Logger, method, url, bodyType, token string, headers map[string]string, body io.Reader) (*http.Request, error) {
	log.Debugf("creating request: %s %s %s %s", method, url, bodyType, token)

	var n int64 // content length
	var err error
	if f, ok := body.(*os.File); ok {
		// Retrieve the content-length and buffer up if necessary.
		body, n, err = materializeFile(log, f)
		if err != nil {
			return nil, err
		}
	}

	req, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}

	if n != 0 {
		log.Debugf("setting content-length to '%d'", n)
		req.ContentLength = n
	}

	if bodyType != "" {
		req.Header.Set("Content-Type", bodyType)
	}
	req.Header.Set("Authorization", fmt.Sprintf("token %s", token))

	for k, v := range headers {
		req.Header.Set(k, v)
	}

	return req, nil
}
Beispiel #2
0
func LatestRelease(log *logging.Logger, user, repo, token string) (*Release, error) {
	// If latestReleaseApi DOESN'T give an error, return the release.
	if latestRelease, err := latestReleaseApi(log, user, repo, token); err == nil {
		return latestRelease, nil
	}

	// The enterprise api doesnt support the latest release endpoint. Get
	// all releases and compare the published date to get the latest.
	releases, err := Releases(log, user, repo, token)
	if err != nil {
		return nil, err
	}

	var latestRelIndex = -1
	maxDate := time.Time{}
	for i, release := range releases {
		if relDate := *release.Published; relDate.After(maxDate) {
			maxDate = relDate
			latestRelIndex = i
		}
	}
	if latestRelIndex == -1 {
		return nil, fmt.Errorf("could not find the latest release")
	}

	log.Debugf("Scanning %d releases, latest release is '%s'", len(releases), releases[latestRelIndex])
	return &releases[latestRelIndex], nil
}
Beispiel #3
0
func initMakefile(log *log.Logger, projectDir, projectType string) error {
	if projectType != ProjectTypeGo {
		return nil
	}
	path := filepath.Join(projectDir, makefilePath)
	if info, err := os.Stat(path); os.IsNotExist(err) {
		log.Infof("Creating %s", makefilePath)
		options := struct {
			GoVersion string
		}{
			GoVersion: "1.7.3-alpine",
		}
		t, err := template.New("makefile").Parse(initialMakefile)
		if err != nil {
			return maskAny(err)
		}
		buffer := &bytes.Buffer{}
		if err := t.Execute(buffer, options); err != nil {
			return maskAny(err)
		}

		if err := ioutil.WriteFile(path, buffer.Bytes(), 0644); err != nil {
			return maskAny(err)
		}
		return nil
	} else if err != nil {
		return maskAny(err)
	} else if info.IsDir() {
		return maskAny(fmt.Errorf("%s must be a file", path))
	} else {
		log.Debugf("%s already initialized in %s", gitIgnorePath, projectDir)
		return nil
	}
}
Beispiel #4
0
func GithubGet(log *logging.Logger, uri string, v interface{}) error {
	resp, err := http.Get(ApiURL() + uri)
	if resp != nil {
		defer resp.Body.Close()
	}
	if err != nil {
		return fmt.Errorf("could not fetch releases, %v", err)
	}

	log.Debugf("GET %s -> %v", ApiURL()+uri, resp)

	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("github did not response with 200 OK but with %v", resp.Status)
	}

	r := resp.Body
	if err = json.NewDecoder(r).Decode(v); err != nil {
		return fmt.Errorf("could not unmarshall JSON into Release struct, %v", err)
	}

	return nil
}
Beispiel #5
0
func migrate(from, to migration.Backend, baseKey string, log *logging.Logger) error {
	keys, err := from.List(baseKey)
	if err != nil {
		return maskAny(err)
	}
	for _, key := range keys {
		key = path.Join(baseKey, key)
		log.Debugf("Migrating %s", key)
		value, err := from.Get(key)
		if err != nil {
			return maskAny(err)
		}
		if value != nil {
			if err := to.Set(key, value); err != nil {
				return maskAny(err)
			}
		}
		if err := migrate(from, to, key, log); err != nil {
			return maskAny(err)
		}
	}

	return nil
}
Beispiel #6
0
func initGitIgnore(log *log.Logger, projectDir string) error {
	path := filepath.Join(projectDir, gitIgnorePath)
	if info, err := os.Stat(path); os.IsNotExist(err) {
		log.Infof("Creating %s", gitIgnorePath)
		if err := ioutil.WriteFile(path, []byte(initialGitIgnore), 0644); err != nil {
			return maskAny(err)
		}
		return nil
	} else if err != nil {
		return maskAny(err)
	} else if info.IsDir() {
		return maskAny(fmt.Errorf("%s must be a file", path))
	} else {
		log.Debugf("%s already initialized in %s", gitIgnorePath, projectDir)
		return nil
	}
}
Beispiel #7
0
func initGit(log *log.Logger, projectDir string) error {
	path := filepath.Join(projectDir, gitDirPath)
	if info, err := os.Stat(path); os.IsNotExist(err) {
		if err := util.ExecuteInDir(projectDir, func() error {
			output, err := util.Exec(log, "git", "init")
			if err != nil {
				log.Error(output)
				return maskAny(err)
			}
			return nil
		}); err != nil {
			return maskAny(err)
		}
	} else if err != nil {
		return maskAny(err)
	} else if !info.IsDir() {
		return maskAny(fmt.Errorf("%s must be a directory", path))
	} else {
		log.Debugf("Git already initialized in %s", projectDir)
	}
	return nil
}
Beispiel #8
0
func createGithubRelease(log *log.Logger, version string, info ProjectInfo) error {
	// Are assets specified?
	if len(info.GithubAssets) == 0 {
		log.Debugf("No github-assets specified, no github release is created")
		return nil
	}

	// Check existance of all assets
	for _, asset := range info.GithubAssets {
		if _, err := os.Stat(asset.RelPath); err != nil {
			return maskAny(fmt.Errorf("Cannot stat asset '%s': %v", asset.RelPath, err))
		}
	}

	// Is the repository URL suitable for github releases?
	url, err := git.GetRemoteOriginUrl(log)
	if err != nil {
		return maskAny(err)
	}
	repoInfo, err := vcsurl.Parse(url)
	if err != nil {
		return maskAny(err)
	}
	if repoInfo.RepoHost != vcsurl.GitHub || repoInfo.VCS != vcsurl.Git {
		return maskAny(fmt.Errorf("Cannot create github-release because repository is not a git repo or not hosted on github"))
	}

	// Load github token
	token, err := github.LoadGithubToken()
	if err != nil {
		return maskAny(err)
	}
	gs := github.GithubService{
		Logger:     log,
		Token:      token,
		User:       repoInfo.Username,
		Repository: repoInfo.Name,
	}

	// Create github release
	relOpt := github.ReleaseCreate{
		TagName: version,
		Name:    fmt.Sprintf("v%s", version),
	}
	if err := gs.CreateRelease(relOpt); err != nil {
		return maskAny(err)
	}

	// Attach assets
	for _, asset := range info.GithubAssets {
		opt := github.UploadAssetOptions{
			TagName:  version,
			FileName: filepath.Base(asset.RelPath),
			Label:    asset.Label,
			Path:     asset.RelPath,
		}
		if err := gs.UploadAsset(opt); err != nil {
			return maskAny(err)
		}
	}

	// Update tags
	if err := git.FetchTags(log, "origin"); err != nil {
		return maskAny(err)
	}

	return nil
}