func RemoveUserAccess(ctx *context.APIContext) { owner, repo := parseOwnerAndRepo(ctx) if ctx.Written() { return } repo.Owner = owner u, err := models.GetUserByName(ctx.Params(":user")) if err != nil { ctx.Error(404, "user does not exist", err) return } err = repo.DeleteCollaboration(u.Id) if err != nil { ctx.Error(500, "delete collab", err) return } ctx.Status(204) }
// https://github.com/gigforks/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 checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { if u.IsFollowing(followID) { ctx.Status(204) } else { ctx.Status(404) } }
// https://github.com/gigforks/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) }
// https://github.com/gigforks/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 AddTeamMember(ctx *context.APIContext) { u := user.GetUserByParams(ctx) if ctx.Written() { return } if err := ctx.Org.Team.AddMember(u.Id); err != nil { ctx.Error(500, "AddMember", 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 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, convert.ToIssue(issue)) }
// https://github.com/gigforks/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 DeleteOrg(ctx *context.APIContext) { org := user.GetUserByParamsName(ctx, ":orgname") if ctx.Written() { return } err := models.DeleteOrganization(org) if err != nil { ctx.Error(500, "", err) } ctx.Status(204) }
// https://github.com/gigforks/go-gogs-client/wiki/Users#get-a-single-user 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, &api.User{u.Id, u.Name, u.FullName, u.Email, u.AvatarLink()}) }
// https://github.com/gigforks/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/gigforks/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) }
// https://github.com/gigforks/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.TreeName) 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/gigforks/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/gigforks/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)) }
func AddOrganizationUser(ctx *context.APIContext, form api.AddUserOption) { u, err := models.GetUserByName(form.UserName) if err != nil { ctx.Error(404, "user does not exist", err) return } org := user.GetUserByParamsName(ctx, ":orgname") err = models.AddOrgUser(org.Id, u.Id) if ctx.Written() { return } if err != nil { ctx.Error(500, "", err) } ctx.Status(201) }
// https://github.com/gigforks/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) }
func RemoveOrganizationUser(ctx *context.APIContext) { u := user.GetUserByParamsName(ctx, ":user") if ctx.Written() { return } org := user.GetUserByParamsName(ctx, ":orgname") if ctx.Written() { return } err := models.RemoveOrgUser(org.Id, u.Id) if err != nil { ctx.Error(500, "", err) } ctx.Status(204) }
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 EditIssue(ctx *context.APIContext, form api.EditIssueOption) { 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 !issue.IsPoster(ctx.User.Id) && !ctx.Repo.IsWriter() { ctx.Status(403) return } if len(form.Title) > 0 { issue.Name = form.Title } if form.Body != nil { issue.Content = *form.Body } if ctx.Repo.IsWriter() && form.Assignee != nil && (issue.Assignee == nil || issue.Assignee.LowerName != strings.ToLower(*form.Assignee)) { if len(*form.Assignee) == 0 { issue.AssigneeID = 0 } else { assignee, err := models.GetUserByName(*form.Assignee) if err != nil { if models.IsErrUserNotExist(err) { ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", *form.Assignee)) } else { ctx.Error(500, "GetUserByName", err) } return } issue.AssigneeID = assignee.Id } if err = models.UpdateIssueUserByAssignee(issue); err != nil { ctx.Error(500, "UpdateIssueUserByAssignee", err) return } } if ctx.Repo.IsWriter() && form.Milestone != nil && issue.MilestoneID != *form.Milestone { oldMid := issue.MilestoneID issue.MilestoneID = *form.Milestone if err = models.ChangeMilestoneAssign(oldMid, issue); err != nil { ctx.Error(500, "ChangeMilestoneAssign", err) return } } if err = models.UpdateIssue(issue); err != nil { ctx.Error(500, "UpdateIssue", err) return } // Refetch from database to assign some automatic values issue, err = models.GetIssueByID(issue.ID) if err != nil { ctx.Error(500, "GetIssueByID", err) return } ctx.JSON(201, convert.ToIssue(issue)) }
// https://github.com/gigforks/go-gogs-client/wiki/Repositories#edit-a-hook func EditHook(ctx *context.APIContext, form api.EditHookOption) { w, err := models.GetWebhookByID(ctx.ParamsInt64(":id")) if err != nil { if models.IsErrWebhookNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetWebhookByID", err) } return } if form.Config != nil { if url, ok := form.Config["url"]; ok { w.URL = url } if ct, ok := form.Config["content_type"]; ok { if !models.IsValidHookContentType(ct) { ctx.Error(422, "", "Invalid content type") return } w.ContentType = models.ToHookContentType(ct) } if w.HookTaskType == models.SLACK { if channel, ok := form.Config["channel"]; ok { meta, err := json.Marshal(&models.SlackMeta{ Channel: channel, Username: form.Config["username"], IconURL: form.Config["icon_url"], Color: form.Config["color"], }) if err != nil { ctx.Error(500, "slack: JSON marshal failed", err) return } w.Meta = string(meta) } } } // Update events if len(form.Events) == 0 { form.Events = []string{"push"} } w.PushOnly = false w.SendEverything = false w.ChooseEvents = true w.Create = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_CREATE)) w.Push = com.IsSliceContainsStr(form.Events, string(models.HOOK_EVENT_PUSH)) if err = w.UpdateEvent(); err != nil { ctx.Error(500, "UpdateEvent", err) return } if form.Active != nil { w.IsActive = *form.Active } if err := models.UpdateWebhook(w); err != nil { ctx.Error(500, "UpdateWebhook", err) return } ctx.JSON(200, convert.ToHook(ctx.Repo.RepoLink, w)) }