// GetMaintainer gets the MAINTAINER configuration file and returns // a subset of the file with members belonging to the specified organization. func GetMaintainerOrg(c *gin.Context) { var ( owner = c.Param("owner") name = c.Param("repo") team = c.Param("org") user = session.User(c) ) repo, err := store.GetRepoOwnerName(c, owner, name) if err != nil { log.Errorf("Error getting repository %s. %s", name, err) c.AbortWithStatus(404) return } file, err := remote.GetContents(c, user, repo, "MAINTAINERS") if err != nil { log.Errorf("Error getting repository %s. %s", repo.Slug, err) c.String(404, "MAINTAINERS file not found. %s", err) return } maintainer, err := model.ParseMaintainer(file) if err != nil { log.Errorf("Error parsing MAINTAINERS file for %s. %s", repo.Slug, err) c.String(500, "Error parsing MAINTAINERS file. %s.", err) return } subset, err := model.FromOrg(maintainer, team) if err != nil { log.Errorf("Error getting subset of MAINTAINERS file for %s/%s. %s", repo.Slug, team, err) c.String(500, "Error getting subset of MAINTAINERS file. %s.", err) return } c.JSON(200, subset) }
// DeleteRepo deletes a repository configuration. func DeleteRepo(c *gin.Context) { var ( owner = c.Param("owner") name = c.Param("repo") user = session.User(c) ) repo, err := store.GetRepoOwnerName(c, owner, name) if err != nil { logrus.Errorf("Error getting repository %s. %s", name, err) c.AbortWithStatus(404) return } err = store.DeleteRepo(c, repo) if err != nil { logrus.Errorf("Error deleting repository %s. %s", name, err) c.AbortWithStatus(500) return } link := fmt.Sprintf( "%s/hook", httputil.GetURL(c.Request), ) err = remote.DelHook(c, user, repo, link) if err != nil { logrus.Errorf("Error deleting repository hook for %s. %s", name, err) } c.String(200, "") }
// GetRepo gets the repository by slug. func GetRepo(c *gin.Context) { var ( owner = c.Param("owner") name = c.Param("repo") ) repo, err := store.GetRepoOwnerName(c, owner, name) if err != nil { logrus.Errorf("Error getting repository %s. %s", name, err) c.String(404, "Error getting repository %s", name) return } c.JSON(200, repo) }
// PostRepo activates a new repository. func PostRepo(c *gin.Context) { var ( owner = c.Param("owner") name = c.Param("repo") user = session.User(c) ) // verify repo doesn't already exist if _, err := store.GetRepoOwnerName(c, owner, name); err == nil { c.AbortWithStatus(409) c.String(409, "Error activating a repository that is already active.") return } repo, err := remote.GetRepo(c, user, owner, name) if err != nil { c.String(404, "Error finding repository in GitHub. %s") return } repo.UserID = user.ID repo.Secret = model.Rand() // creates a token to authorize the link callback url t := token.New(token.HookToken, repo.Slug) sig, err := t.Sign(repo.Secret) if err != nil { c.String(500, "Error activating repository. %s") return } // create the hook callback url link := fmt.Sprintf( "%s/hook?access_token=%s", httputil.GetURL(c.Request), sig, ) err = remote.SetHook(c, user, repo, link) if err != nil { c.String(500, "Error creating hook. %s", err) return } err = store.CreateRepo(c, repo) if err != nil { c.String(500, "Error activating the repository. %s", err) return } c.JSON(200, repo) }
// GetMaintainer gets the MAINTAINER configuration file. func GetMaintainer(c *gin.Context) { var ( owner = c.Param("owner") name = c.Param("repo") user = session.User(c) ) repo, err := store.GetRepoOwnerName(c, owner, name) if err != nil { log.Errorf("Error getting repository %s. %s", name, err) c.AbortWithStatus(404) return } file, err := remote.GetContents(c, user, repo, "MAINTAINERS") if err != nil { log.Debugf("no MAINTAINERS file for %s. Checking for team members.", repo.Slug) members, merr := cache.GetMembers(c, user, repo.Owner) if merr != nil { log.Errorf("Error getting repository %s. %s", repo.Slug, err) log.Errorf("Error getting org members %s. %s", repo.Owner, merr) c.String(404, "MAINTAINERS file not found. %s", err) return } else { log.Printf("found %v members", len(members)) for _, member := range members { file = append(file, member.Login...) file = append(file, '\n') } } } maintainer, err := model.ParseMaintainer(file) if err != nil { log.Errorf("Error parsing MAINTAINERS file for %s. %s", repo.Slug, err) c.String(500, "Error parsing MAINTAINERS file. %s.", err) return } c.JSON(200, maintainer) }