Golang Context.IndentedJSON Examples

Golang Context.IndentedJSON - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.IndentedJSON extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func PatchUser(c *gin.Context) {
	me := session.User(c)
	in := &model.User{}
	err := c.Bind(in)
	if err != nil {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	user, err := store.GetUserLogin(c, c.Param("login"))
	if err != nil {
		c.AbortWithStatus(http.StatusNotFound)
		return
	}
	user.Admin = in.Admin
	user.Active = in.Active

	// cannot update self
	if me.ID == user.ID {
		c.AbortWithStatus(http.StatusForbidden)
		return
	}

	err = store.UpdateUser(c, user)
	if err != nil {
		c.AbortWithStatus(http.StatusConflict)
		return
	}

	c.IndentedJSON(http.StatusOK, user)
}
Example #2
0
// Handler is a route handler for '/movie.:format' route
func Handler(c *gin.Context) {
	requestType := c.Param("format")
	term := c.Request.PostFormValue("text")

	log.Printf("Movie Query: %s", term)

	movie, err := lookupMovie(term)

	switch requestType {
	case "json":
		if err != nil {
			c.JSON(http.StatusNotFound, gin.H{"Response": err.Error()})
		} else {
			c.IndentedJSON(http.StatusOK, movie)
		}
	case "slack":
		text, err := formatSlackResp(movie)
		if err != nil {
			c.String(http.StatusNotFound, "Not Found")
		}

		c.String(http.StatusOK, text)
	default:
		c.JSON(http.StatusUnsupportedMediaType, nil)
	}
}
Example #3
0
func processCommentHook(c *gin.Context, hook *model.Hook) {
	repo, user, err := getRepoAndUser(c, hook.Repo.Slug)
	if err != nil {
		return
	}

	config, maintainer, err := getConfigAndMaintainers(c, user, repo)
	if err != nil {
		return
	}

	approvers, err := buildApprovers(c, user, repo, config, maintainer, hook.Issue)
	if err != nil {
		return
	}

	approved := len(approvers) >= config.Approvals
	err = remote.SetStatus(c, user, repo, hook.Issue.Number, approved)
	if err != nil {
		log.Errorf("Error setting status for %s pr %d. %s", repo.Slug, hook.Issue.Number, err)
		c.String(500, "Error setting status. %s.", err)
		return
	}

	log.Debugf("processed comment for %s. received %d of %d approvals", repo.Slug, len(approvers), config.Approvals)

	c.IndentedJSON(200, gin.H{
		"approvers":   maintainer.People,
		"settings":    config,
		"approved":    approved,
		"approved_by": approvers,
	})
}
Example #4
0
func GetBuild(c *gin.Context) {
	if c.Param("number") == "latest" {
		GetBuildLast(c)
		return
	}

	repo := session.Repo(c)
	num, err := strconv.Atoi(c.Param("number"))
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	build, err := store.GetBuildNumber(c, repo, num)
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	jobs, _ := store.GetJobList(c, build)

	out := struct {
		*model.Build
		Jobs []*model.Job `json:"jobs"`
	}{build, jobs}

	c.IndentedJSON(http.StatusOK, &out)
}
Example #5
0
func GetRepos(c *gin.Context) {
	user := session.User(c)
	remote := remote.FromContext(c)
	var repos []*model.RepoLite

	// get the repository list from the cache
	reposv, ok := c.Get("repos")
	if ok {
		repos = reposv.([]*model.RepoLite)
	} else {
		var err error
		repos, err = remote.Repos(user)
		if err != nil {
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}
	}

	// for each repository in the remote system we get
	// the intersection of those repostiories in Drone
	repos_, err := store.GetRepoListOf(c, repos)
	if err != nil {
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}

	c.Set("repos", repos)
	c.IndentedJSON(http.StatusOK, repos_)
}
Example #6
0
func processPRHook(c *gin.Context, prHook *model.PRHook) {
	repo, user, err := getRepoAndUser(c, prHook.Repo.Slug)
	if err != nil {
		return
	}

	err = remote.SetStatus(c, user, repo, prHook.Number, false)
	if err != nil {
		log.Errorf("Error setting status. %s", err)
		c.String(500, "Error setting status. %s", err)
		return
	}

	config, _, err := getConfigAndMaintainers(c, user, repo)
	if err != nil {
		return
	}

	if prHook.Update && config.DoComment {
		err = remote.WriteComment(c, user, repo, prHook.Number, "The Pull Request has been updated. No comments before this one will count for approval.")
		if err != nil {
			log.Errorf("Error writing comment for status. %s", err)
			c.String(500, "Error writing comment for status. %s", err)
			return
		}
	}

	c.IndentedJSON(200, gin.H{
		"number":   prHook.Number,
		"approved": false,
	})
}