// https://github.com/gogits/go-gogs-client/wiki/Users-Public-Keys#list-public-keys-for-a-user func ListPublicKeys(ctx *context.APIContext) { user := GetUserByParams(ctx) if ctx.Written() { return } listPublicKeys(ctx, user.ID) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-you-are-following-a-user func CheckMyFollowing(ctx *context.APIContext) { target := GetUserByParams(ctx) if ctx.Written() { return } checkUserFollowing(ctx, ctx.User, target.ID) }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user func CreatePublicKey(ctx *context.APIContext, form api.CreateKeyOption) { u := user.GetUserByParams(ctx) if ctx.Written() { return } user.CreateUserPublicKey(ctx, form, u.ID) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#list-users-followed-by-another-user func ListFollowing(ctx *context.APIContext) { u := GetUserByParams(ctx) if ctx.Written() { return } listUserFollowing(ctx, u) }
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 HandleCheckKeyStringError(ctx *context.APIContext, err error) { if models.IsErrKeyUnableVerify(err) { ctx.Error(422, "", "Unable to verify key content") } else { ctx.Error(422, "", fmt.Errorf("Invalid key content: %v", err)) } }
func checkUserFollowing(ctx *context.APIContext, u *models.User, followID int64) { if u.IsFollowing(followID) { ctx.Status(204) } else { ctx.Status(404) } }
func responseApiUsers(ctx *context.APIContext, users []*models.User) { apiUsers := make([]*api.User, len(users)) for i := range users { apiUsers[i] = users[i].APIFormat() } ctx.JSON(200, &apiUsers) }
// https://github.com/gogits/go-gogs-client/wiki/Organizations#list-user-organizations func ListUserOrgs(ctx *context.APIContext) { u := user.GetUserByParams(ctx) if ctx.Written() { return } listUserOrgs(ctx, u, false) }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Repositories#create-a-new-repository func CreateRepo(ctx *context.APIContext, form api.CreateRepoOption) { owner := user.GetUserByParams(ctx) if ctx.Written() { return } repo.CreateUserRepo(ctx, owner, form) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#get func Get(ctx *context.APIContext) { _, repo := parseOwnerAndRepo(ctx) if ctx.Written() { return } ctx.JSON(200, repo.APIFormat(&api.Permission{true, true, true})) }
func listUserFollowing(ctx *context.APIContext, u *models.User) { users, err := u.GetFollowing(ctx.QueryInt("page")) if err != nil { ctx.Error(500, "GetFollowing", err) return } responseApiUsers(ctx, users) }
// https://github.com/gogits/go-gogs-client/wiki/Miscellaneous#render-a-markdown-document-in-raw-mode func MarkdownRaw(ctx *context.APIContext) { body, err := ctx.Req.Body().Bytes() if err != nil { ctx.Error(422, "", err) return } ctx.Write(markdown.RenderRaw(body, "")) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#create func Create(ctx *context.APIContext, opt api.CreateRepoOption) { // Shouldn't reach this condition, but just in case. if ctx.User.IsOrganization() { ctx.Error(422, "", "not allowed creating repository for organization") return } CreateUserRepo(ctx, ctx.User, opt) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Followers#check-if-one-user-follows-another func CheckFollowing(ctx *context.APIContext) { u := GetUserByParams(ctx) if ctx.Written() { return } target := GetUserByParamsName(ctx, ":target") if ctx.Written() { return } checkUserFollowing(ctx, u, target.ID) }
// https://github.com/gogits/go-gogs-client/wiki/Users#create-a-access-token func CreateAccessToken(ctx *context.APIContext, form api.CreateAccessTokenOption) { t := &models.AccessToken{ UID: ctx.User.ID, Name: form.Name, } if err := models.NewAccessToken(t); err != nil { ctx.Error(500, "NewAccessToken", err) return } ctx.JSON(201, &api.AccessToken{t.Name, t.Sha1}) }
// https://github.com/gogits/go-gogs-client/wiki/Users-Emails#list-email-addresses-for-a-user func ListEmails(ctx *context.APIContext) { emails, err := models.GetEmailAddresses(ctx.User.ID) if err != nil { ctx.Error(500, "GetEmailAddresses", err) return } apiEmails := make([]*api.Email, len(emails)) for i := range emails { apiEmails[i] = convert.ToEmail(emails[i]) } ctx.JSON(200, &apiEmails) }
func listUserOrgs(ctx *context.APIContext, u *models.User, all bool) { if err := u.GetOrganizations(all); err != nil { ctx.Error(500, "GetOrganizations", err) return } apiOrgs := make([]*api.Organization, len(u.Orgs)) for i := range u.Orgs { apiOrgs[i] = convert.ToOrganization(u.Orgs[i]) } ctx.JSON(200, &apiOrgs) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories#list-hooks func ListHooks(ctx *context.APIContext) { hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) if err != nil { ctx.Error(500, "GetWebhooksByRepoID", err) return } apiHooks := make([]*api.Hook, len(hooks)) for i := range hooks { apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i]) } ctx.JSON(200, &apiHooks) }
func ListMilestones(ctx *context.APIContext) { milestones, err := models.GetMilestonesByRepoID(ctx.Repo.Repository.ID) if err != nil { ctx.Error(500, "GetMilestonesByRepoID", err) return } apiMilestones := make([]*api.Milestone, len(milestones)) for i := range milestones { apiMilestones[i] = milestones[i].APIFormat() } ctx.JSON(200, &apiMilestones) }
func ListTeams(ctx *context.APIContext) { org := ctx.Org.Organization if err := org.GetTeams(); err != nil { ctx.Error(500, "GetTeams", err) return } apiTeams := make([]*api.Team, len(org.Teams)) for i := range org.Teams { apiTeams[i] = convert.ToTeam(org.Teams[i]) } ctx.JSON(200, apiTeams) }
// https://github.com/gogits/go-gogs-client/wiki/Users#list-access-tokens-for-a-user func ListAccessTokens(ctx *context.APIContext) { tokens, err := models.ListAccessTokens(ctx.User.ID) if err != nil { ctx.Error(500, "ListAccessTokens", err) return } apiTokens := make([]*api.AccessToken, len(tokens)) for i := range tokens { apiTokens[i] = &api.AccessToken{tokens[i].Name, tokens[i].Sha1} } ctx.JSON(200, &apiTokens) }
func ListLabels(ctx *context.APIContext) { labels, err := models.GetLabelsByRepoID(ctx.Repo.Repository.ID) if err != nil { ctx.Error(500, "GetLabelsByRepoID", err) return } apiLabels := make([]*api.Label, len(labels)) for i := range labels { apiLabels[i] = labels[i].APIFormat() } ctx.JSON(200, &apiLabels) }
// 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) }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user func CreateUser(ctx *context.APIContext, form api.CreateUserOption) { u := &models.User{ Name: form.Username, FullName: form.FullName, Email: form.Email, Passwd: form.Password, IsActive: true, LoginType: models.LOGIN_PLAIN, } parseLoginSource(ctx, u, form.SourceID, form.LoginName) if ctx.Written() { return } if err := models.CreateUser(u); err != nil { if models.IsErrUserAlreadyExist(err) || models.IsErrEmailAlreadyUsed(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "CreateUser", err) } return } log.Trace("Account created by admin (%s): %s", ctx.User.Name, u.Name) // Send email notification. if form.SendNotify && setting.MailService != nil { models.SendRegisterNotifyMail(ctx.Context.Context, u) } ctx.JSON(201, u.APIFormat()) }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user func EditUser(ctx *context.APIContext, form api.EditUserOption) { u := user.GetUserByParams(ctx) if ctx.Written() { return } parseLoginSource(ctx, u, form.SourceID, form.LoginName) if ctx.Written() { return } if len(form.Password) > 0 { u.Passwd = form.Password u.Salt = models.GetUserSalt() u.EncodePasswd() } u.LoginName = form.LoginName u.FullName = form.FullName u.Email = form.Email u.Website = form.Website u.Location = form.Location if form.Active != nil { u.IsActive = *form.Active } if form.Admin != nil { u.IsAdmin = *form.Admin } if form.AllowGitHook != nil { u.AllowGitHook = *form.AllowGitHook } if form.AllowImportLocal != nil { u.AllowImportLocal = *form.AllowImportLocal } if form.MaxRepoCreation != nil { u.MaxRepoCreation = *form.MaxRepoCreation } if err := models.UpdateUser(u); err != nil { if models.IsErrEmailAlreadyUsed(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "UpdateUser", err) } return } log.Trace("Account profile updated by admin (%s): %s", ctx.User.Name, u.Name) ctx.JSON(200, u.APIFormat()) }
// https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization func CreateOrg(ctx *context.APIContext, form api.CreateOrgOption) { u := user.GetUserByParams(ctx) if ctx.Written() { return } org := &models.User{ Name: form.UserName, FullName: form.FullName, Description: form.Description, Website: form.Website, Location: form.Location, IsActive: true, Type: models.USER_TYPE_ORGANIZATION, } if err := models.CreateOrganization(org, u); err != nil { if models.IsErrUserAlreadyExist(err) || models.IsErrNameReserved(err) || models.IsErrNamePatternNotAllowed(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "CreateOrganization", err) } return } ctx.JSON(201, convert.ToOrganization(org)) }
// CreateUserPublicKey creates new public key to given user by ID. func CreateUserPublicKey(ctx *context.APIContext, form api.CreateKeyOption, uid int64) { content, err := models.CheckPublicKeyString(form.Key) if err != nil { repo.HandleCheckKeyStringError(ctx, err) return } key, err := models.AddPublicKey(uid, form.Title, content) if err != nil { repo.HandleAddKeyError(ctx, err) return } apiLink := composePublicKeysAPILink() ctx.JSON(201, convert.ToPublicKey(apiLink, key)) }
func listPublicKeys(ctx *context.APIContext, uid int64) { keys, err := models.ListPublicKeys(uid) if err != nil { ctx.Error(500, "ListPublicKeys", err) return } apiLink := composePublicKeysAPILink() apiKeys := make([]*api.PublicKey, len(keys)) for i := range keys { apiKeys[i] = convert.ToPublicKey(apiLink, keys[i]) } ctx.JSON(200, &apiKeys) }
// https://github.com/gogits/go-gogs-client/wiki/Repositories-Deploy-Keys#add-a-new-deploy-key func CreateDeployKey(ctx *context.APIContext, form api.CreateKeyOption) { content, err := models.CheckPublicKeyString(form.Key) if err != nil { HandleCheckKeyStringError(ctx, err) return } key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content) if err != nil { HandleAddKeyError(ctx, err) return } key.Content = content apiLink := composeDeployKeysAPILink(ctx.Repo.Owner.Name + "/" + ctx.Repo.Repository.Name) ctx.JSON(201, convert.ToDeployKey(apiLink, key)) }