From 418ee3a8b3dd6edab5da3b3366c9bcc9b26c8cad Mon Sep 17 00:00:00 2001 From: Kim Lindhardt Madsen Date: Thu, 26 Nov 2015 11:31:32 +0100 Subject: [PATCH] add collaborator --- cmd/web.go | 2 ++ routers/api/v1/repo.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/cmd/web.go b/cmd/web.go index f6bfaeeaf..6ff010668 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -244,6 +244,8 @@ func runWeb(ctx *cli.Context) { m.Post("/forks", bind(api.ForkRepoOption{}), v1.ForkRepo) + m.Combo("/collaboration").Post(bind(api.CollaboratorOption{}), v1.AddCollaborator) + m.Get("/commits/:commitid", middleware.RepoRef(), v1.CommitByID) m.Get("/commits/head", middleware.RepoRef(), v1.HEADCommit) m.Get("/commits/branch/*", middleware.RepoRef(), v1.ListCommits) diff --git a/routers/api/v1/repo.go b/routers/api/v1/repo.go index 37674f52b..0033ddfb4 100644 --- a/routers/api/v1/repo.go +++ b/routers/api/v1/repo.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/gogs/modules/log" "github.com/gogits/gogs/modules/middleware" "github.com/gogits/gogs/modules/setting" + "github.com/gogits/gogs/modules/mailer" ) // ToApiRepository converts repository to API format. @@ -235,6 +236,38 @@ func forkRepoTo(ctx *middleware.Context, targetUser *models.User, name string, d ctx.JSON(201, ToApiRepository(ctx.User, forkedRepo, api.Permission{true, true, true})) } +func AddCollaborator(ctx *middleware.Context, opt api.CollaboratorOption) { + u, err := models.GetUserByName(opt.UserName) + if err != nil { + if models.IsErrUserNotExist(err) { + ctx.APIError(422, "", err) + } else { + ctx.Handle(500, "GetUserByName", err) + } + return + } + + // Check if user is organization member. + if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) { + ctx.APIError(422, "", "User is organization member") + return + } + + if err = ctx.Repo.Repository.AddCollaborator(u); err != nil { + ctx.Handle(500, "AddCollaborator", err) + return + } + + if setting.Service.EnableNotifyMail { + if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil { + ctx.Handle(500, "SendCollaboratorMail", err) + return + } + } + + ctx.Status(201) +} + func MigrateRepo(ctx *middleware.Context, form auth.MigrateRepoForm) { ctxUser := ctx.User