func EditIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption) { comment, err := models.GetCommentByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrCommentNotExist(err) { ctx.Error(404, "GetCommentByID", err) } else { ctx.Error(500, "GetCommentByID", err) } return } if !ctx.IsSigned || (ctx.User.ID != comment.PosterID && !ctx.Repo.IsAdmin()) { ctx.Status(403) return } else if comment.Type != models.COMMENT_TYPE_COMMENT { ctx.Status(204) return } comment.Content = form.Body if err := models.UpdateComment(comment); err != nil { ctx.Error(500, "UpdateComment", err) return } ctx.JSON(200, comment.APIFormat()) }
func EditMilestone(ctx *context.APIContext, form api.EditMilestoneOption) { milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetMilestoneByRepoID", err) } return } if len(form.Title) > 0 { milestone.Name = form.Title } if form.Description != nil { milestone.Content = *form.Description } if form.Deadline != nil && !form.Deadline.IsZero() { milestone.Deadline = *form.Deadline } if err := models.UpdateMilestone(milestone); err != nil { ctx.Handle(500, "UpdateMilestone", err) return } ctx.JSON(200, milestone.APIFormat()) }
func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { if u.IsFollowing(followID) { ctx.Status(204) } else { ctx.Status(404) } }
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#add-email-addresses func AddEmail(ctx *context.APIContext, form api.CreateEmailOption) { if len(form.Emails) == 0 { ctx.Status(422) return } emails := make([]*models.EmailAddress, len(form.Emails)) for i := range form.Emails { emails[i] = &models.EmailAddress{ UID: ctx.User.ID, Email: form.Emails[i], IsActivated: !setting.Service.RegisterEmailConfirm, } } if err := models.AddEmailAddresses(emails); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.Error(422, "", "Email address has been used: "+err.(models.ErrEmailAlreadyUsed).Email) } else { ctx.Error(500, "AddEmailAddresses", err) } return } apiEmails := make([]*api.Email, len(emails)) for i := range emails { apiEmails[i] = convert.ToEmail(emails[i]) } ctx.JSON(201, &apiEmails) }
func EditLabel(ctx *context.APIContext, form api.EditLabelOption) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetLabelByRepoID", err) } return } if form.Name != nil { label.Name = *form.Name } if form.Color != nil { label.Color = *form.Color } if err := models.UpdateLabel(label); err != nil { ctx.Handle(500, "UpdateLabel", err) return } ctx.JSON(200, label.APIFormat()) }
func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) } return } if err := ctx.Repo.Repository.AddCollaborator(collaborator); err != nil { ctx.Error(500, "AddCollaborator", err) return } if form.Permission != nil { if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(collaborator.ID, models.ParseAccessMode(*form.Permission)); err != nil { ctx.Error(500, "ChangeCollaborationAccessMode", err) return } } ctx.Status(204) }
func DeleteMilestone(ctx *context.APIContext) { if err := models.DeleteMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { ctx.Error(500, "DeleteMilestoneByRepoID", err) return } ctx.Status(204) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#follow-a-user func Follow(ctx *context.APIContext) { target := GetUserByParams(ctx) if ctx.Written() { return } if err := models.FollowUser(ctx.User.ID, target.ID); err != nil { ctx.Error(500, "FollowUser", err) return } ctx.Status(204) }
func GetMilestone(ctx *context.APIContext) { milestone, err := models.GetMilestoneByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrMilestoneNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetMilestoneByRepoID", err) } return } ctx.JSON(200, milestone.APIFormat()) }
func RemoveTeamRepository(ctx *context.APIContext) { repo := GetRepositoryByParams(ctx) if ctx.Written() { return } if err := ctx.Org.Team.RemoveRepository(repo.ID); err != nil { ctx.Error(500, "RemoveRepository", err) return } ctx.Status(204) }
func GetRepositoryByParams(ctx *context.APIContext) *models.Repository { repo, err := models.GetRepositoryByName(ctx.Org.Team.OrgID, ctx.Params(":reponame")) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetRepositoryByName", err) } return nil } return repo }
func GetIssue(ctx *context.APIContext) { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetIssueByIndex", err) } return } ctx.JSON(200, issue.APIFormat()) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#remove-a-deploy-key func DeleteDeploykey(ctx *context.APIContext) { if err := models.DeleteDeployKey(ctx.User, ctx.ParamsInt64(":id")); err != nil { if models.IsErrKeyAccessDenied(err) { ctx.Error(403, "", "You do not have access to this key") } else { ctx.Error(500, "DeleteDeployKey", err) } return } ctx.Status(204) }
func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { user, err := models.GetUserByName(ctx.Params(name)) if err != nil { if models.IsErrUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) } return nil } return user }
func DeleteLabel(ctx *context.APIContext) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } if err := models.DeleteLabel(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil { ctx.Error(500, "DeleteLabel", err) return } ctx.Status(204) }
func GetLabel(ctx *context.APIContext) { label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetLabelByRepoID", err) } return } ctx.JSON(200, label.APIFormat()) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#get-a-single-public-key func GetPublicKey(ctx *context.APIContext) { key, err := models.GetPublicKeyByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrKeyNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetPublicKeyByID", err) } return } apiLink := composePublicKeysAPILink() ctx.JSON(200, convert.ToPublicKey(apiLink, key)) }
func GetInfo(ctx *context.APIContext) { u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) } return } // Hide user e-mail when API caller isn't signed in. if !ctx.IsSigned { u.Email = "" } ctx.JSON(200, u.APIFormat()) }
func ListIssueLabels(ctx *context.APIContext) { issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetIssueByIndex", err) } return } apiLabels := make([]*api.Label, len(issue.Labels)) for i := range issue.Labels { apiLabels[i] = issue.Labels[i].APIFormat() } ctx.JSON(200, &apiLabels) }
func CreateLabel(ctx *context.APIContext, form api.CreateLabelOption) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } label := &models.Label{ Name: form.Name, Color: form.Color, RepoID: ctx.Repo.Repository.ID, } if err := models.NewLabels(label); err != nil { ctx.Error(500, "NewLabel", err) return } ctx.JSON(201, label.APIFormat()) }
// https://github.com/gogits/go-gogs-client/wiki/Organizations#edit-an-organization func Edit(ctx *context.APIContext, form api.EditOrgOption) { org := ctx.Org.Organization if !org.IsOwnedBy(ctx.User.ID) { ctx.Status(403) return } org.FullName = form.FullName org.Description = form.Description org.Website = form.Website org.Location = form.Location if err := models.UpdateUser(org); err != nil { ctx.Error(500, "UpdateUser", err) return } ctx.JSON(200, convert.ToOrganization(org)) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#delete-email-addresses func DeleteEmail(ctx *context.APIContext, form api.CreateEmailOption) { if len(form.Emails) == 0 { ctx.Status(204) return } emails := make([]*models.EmailAddress, len(form.Emails)) for i := range form.Emails { emails[i] = &models.EmailAddress{ Email: form.Emails[i], } } if err := models.DeleteEmailAddresses(emails); err != nil { ctx.Error(500, "DeleteEmailAddresses", err) return } ctx.Status(204) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#get-a-deploy-key func GetDeployKey(ctx *context.APIContext) { key, err := models.GetDeployKeyByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrDeployKeyNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetDeployKeyByID", err) } return } if err = key.GetContent(); err != nil { ctx.Error(500, "GetContent", err) return } apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) ctx.JSON(200, convert.ToDeployKey(apiLink, key)) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#delete func Delete(ctx *context.APIContext) { owner, repo := parseOwnerAndRepo(ctx) if ctx.Written() { return } if owner.IsOrganization() && !owner.IsOwnedBy(ctx.User.ID) { ctx.Error(403, "", "Given user is not owner of organization.") return } if err := models.DeleteRepository(owner.ID, repo.ID); err != nil { ctx.Error(500, "DeleteRepository", err) return } log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name) ctx.Status(204) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Contents#download-raw-content func GetRawFile(ctx *context.APIContext) { if !ctx.Repo.HasAccess() { ctx.Status(404) return } blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath) if err != nil { if git.IsErrNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetBlobByPath", err) } return } if err = repo.ServeBlob(ctx.Context, blob); err != nil { ctx.Error(500, "ServeBlob", err) } }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user func DeleteUser(ctx *context.APIContext) { u := user.GetUserByParams(ctx) if ctx.Written() { return } if err := models.DeleteUser(u); err != nil { if models.IsErrUserOwnRepos(err) || models.IsErrUserHasOrgs(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "DeleteUser", err) } return } log.Trace("Account deleted by admin(%s): %s", ctx.User.Name, u.Name) ctx.Status(204) }
func DeleteIssueLabel(ctx *context.APIContext) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetIssueByIndex", err) } return } label, err := models.GetLabelInRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) if err != nil { if models.IsErrLabelNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetLabelInRepoByID", err) } return } if err := models.DeleteIssueLabel(issue, label); err != nil { ctx.Error(500, "DeleteIssueLabel", err) return } ctx.Status(204) }
func AddIssueLabels(ctx *context.APIContext, form api.IssueLabelsOption) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetIssueByIndex", err) } return } labels, err := models.GetLabelsInRepoByIDs(ctx.Repo.Repository.ID, form.Labels) if err != nil { ctx.Error(500, "GetLabelsInRepoByIDs", err) return } if err = issue.AddLabels(ctx.User, labels); err != nil { ctx.Error(500, "AddLabels", err) return } labels, err = models.GetLabelsByIssueID(issue.ID) if err != nil { ctx.Error(500, "GetLabelsByIssueID", err) return } apiLabels := make([]*api.Label, len(labels)) for i := range labels { apiLabels[i] = issue.Labels[i].APIFormat() } ctx.JSON(200, &apiLabels) }
func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) { owner, err := models.GetUserByName(ctx.Params(":username")) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) } return nil, nil } repo, err := models.GetRepositoryByName(owner.ID, ctx.Params(":reponame")) if err != nil { if models.IsErrRepoNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetRepositoryByName", err) } return nil, nil } return owner, repo }
func ClearIssueLabels(ctx *context.APIContext) { if !ctx.Repo.IsWriter() { ctx.Status(403) return } issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) if err != nil { if models.IsErrIssueNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetIssueByIndex", err) } return } if err := issue.ClearLabels(ctx.User); err != nil { ctx.Error(500, "ClearLabels", err) return } ctx.Status(204) }