Esempio n. 1
0
// GetBuildPage accepts a request to retrieve the build
// output page for the package version, channel and SDK
// from the datastore in JSON format.
//
// If the SDK is not provided, the system will lookup
// the latest SDK version for this package.
//
//     GET /:name/:number/:channel/:sdk
//
func GetBuildPage(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["name"]
	number := c.URLParams["number"]
	channel := c.URLParams["channel"]
	sdk := c.URLParams["sdk"]

	// If no SDK is provided we should use the most recent
	// SDK number associated with the Package version.
	var build *resource.Build
	var err error
	if len(sdk) == 0 {
		build, err = datastore.GetBuildLatest(ctx, name, number, channel)
	} else {
		build, err = datastore.GetBuild(ctx, name, number, channel, sdk)
	}

	// If the error is not nil then we can
	// display some sort of NotFound page.
	if err != nil {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}

	BuildTempl.Execute(w, struct {
		Build *resource.Build
		Error error
	}{build, err})
}
Esempio n. 2
0
// GetBuildLatest accepts a request to retrieve the build
// details for the package version, channel and latest SDK
// from the datastore in JSON format.
//
//     GET /api/packages/:name/:number/channel/:channel
//
func GetBuildLatest(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["name"]
	number := c.URLParams["number"]
	channel := c.URLParams["channel"]

	build, err := datastore.GetBuildLatest(ctx, name, number, channel)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	json.NewEncoder(w).Encode(build)
}
Esempio n. 3
0
// GetBadge accepts a request to retrieve the named
// package and version build details from the datastore
// and return an SVG badges representing the build results.
//
//     GET /api/badges/:name/:number/channel/:channel/status.svg
//     GET /api/badges/:name/:number/channel/:channel/sdk/:sdk/status.svg
//
func GetBadge(c web.C, w http.ResponseWriter, r *http.Request) {
	ctx := context.FromC(c)
	name := c.URLParams["name"]
	number := c.URLParams["number"]
	channel := c.URLParams["channel"]
	sdk := c.URLParams["sdk"]

	// Ensure we set the correct content-type
	// for the badge to ensure it displays
	// correctly on GitHub.
	w.Header().Set("Content-Type", "image/svg+xml")

	// If no SDK is provided we should use the most recent
	// SDK number associated with the Package version.
	var build *resource.Build
	var err error
	if len(sdk) == 0 {
		build, err = datastore.GetBuildLatest(ctx, name, number, channel)
	} else {
		build, err = datastore.GetBuild(ctx, name, number, channel, sdk)
	}

	// If there was an error default to a build status None
	if err != nil {
		build.Status = resource.StatusNone
	}

	var badgeStr string
	var badgeMsg string = build.SDK

	switch build.Status {
	case resource.StatusSuccess:
		badgeStr = badgeSuccess
	case resource.StatusFailure:
		badgeStr = badgeFailure
	case resource.StatusWarning:
		badgeStr = badgeWarning
	case resource.StatusStarted, resource.StatusPending:
		badgeStr = badgeStarted
	case resource.StatusKilled, resource.StatusError:
		badgeStr = badgeError
	default:
		badgeStr = badgeNone
		badgeMsg = channel
	}

	// replace any - with -- for compatibility with
	// the shields.io service.
	badgeMsg = strings.Replace(badgeMsg, "-", "--", -1)

	// generate the badge url and check to see if the
	// badge already exists in the cache
	var badgeUrl = fmt.Sprintf(badgeStr, badgeMsg)
	if badgeRaw, ok := badgeCache.Get(badgeUrl); ok {
		w.Write(badgeRaw.([]byte))
		return
	}

	// retrieve the badge from shields.io
	res, err := http.Get(badgeUrl)
	if err != nil {
		http.Error(w, err.Error(), res.StatusCode)
		return
	}
	defer res.Body.Close()

	// read the svg data from the request body,
	// write to the response + the local lru cache.
	badgeRaw, err := ioutil.ReadAll(res.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	badgeCache.Add(badgeUrl, badgeRaw)
	w.Write(badgeRaw)
}