Просмотр исходного кода

#1692 api: admin list and create team under organization

Unknwon лет назад: 8
Родитель
Сommit
e6f927f61a

+ 1 - 1
README.md

@@ -3,7 +3,7 @@ Gogs - Go Git Service [![Build Status](https://travis-ci.org/gogits/gogs.svg?bra
 
 ![](https://github.com/gogits/gogs/blob/master/public/img/gogs-large-resize.png?raw=true)
 
-##### Current version: 0.9.13
+##### Current version: 0.9.14
 
 | Web | UI  | Preview  |
 |:-------------:|:-------:|:-------:|

+ 1 - 1
gogs.go

@@ -17,7 +17,7 @@ import (
 	"github.com/gogits/gogs/modules/setting"
 )
 
-const APP_VER = "0.9.13.0321"
+const APP_VER = "0.9.14.0321"
 
 func init() {
 	runtime.GOMAXPROCS(runtime.NumCPU())

+ 27 - 0
models/access.go

@@ -20,6 +20,33 @@ const (
 	ACCESS_MODE_OWNER                   // 4
 )
 
+func (mode AccessMode) String() string {
+	switch mode {
+	case ACCESS_MODE_READ:
+		return "read"
+	case ACCESS_MODE_WRITE:
+		return "write"
+	case ACCESS_MODE_ADMIN:
+		return "admin"
+	case ACCESS_MODE_OWNER:
+		return "owner"
+	default:
+		return "none"
+	}
+}
+
+// ParseAccessMode returns corresponding access mode to given permission string.
+func ParseAccessMode(permission string) AccessMode {
+	switch permission {
+	case "write":
+		return ACCESS_MODE_WRITE
+	case "admin":
+		return ACCESS_MODE_ADMIN
+	default:
+		return ACCESS_MODE_READ
+	}
+}
+
 // Access represents the highest access level of a user to the repository. The only access type
 // that is not in this table is the real owner of a repository. In case of an organization
 // repository, the members of the owners team are in this table.

routers/api/v1/admin/orgs.go → routers/api/v1/admin/org.go


+ 56 - 0
routers/api/v1/admin/org_team.go

@@ -0,0 +1,56 @@
+// Copyright 2016 The Gogs Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package admin
+
+import (
+	api "github.com/gogits/go-gogs-client"
+
+	"github.com/gogits/gogs/models"
+	"github.com/gogits/gogs/modules/context"
+	"github.com/gogits/gogs/routers/api/v1/convert"
+	"github.com/gogits/gogs/routers/api/v1/user"
+)
+
+func ListTeams(ctx *context.APIContext) {
+	org := user.GetUserByParamsName(ctx, ":orgname")
+	if ctx.Written() {
+		return
+	}
+
+	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)
+}
+
+func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
+	org := user.GetUserByParamsName(ctx, ":orgname")
+	if ctx.Written() {
+		return
+	}
+
+	team := &models.Team{
+		OrgID:       org.Id,
+		Name:        form.Name,
+		Description: form.Description,
+		Authorize:   models.ParseAccessMode(form.Permission),
+	}
+	if err := models.NewTeam(team); err != nil {
+		if models.IsErrTeamAlreadyExist(err) {
+			ctx.Error(422, "NewTeam", err)
+		} else {
+			ctx.Error(500, "NewTeam", err)
+		}
+		return
+	}
+
+	ctx.JSON(200, convert.ToTeam(team))
+}

routers/api/v1/admin/repos.go → routers/api/v1/admin/repo.go


routers/api/v1/admin/users.go → routers/api/v1/admin/user.go


+ 4 - 0
routers/api/v1/api.go

@@ -223,6 +223,10 @@ func RegisterRoutes(m *macaron.Macaron) {
 					m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
 				})
 			})
+
+			m.Group("/orgs/:orgname", func() {
+				m.Combo("/teams").Get(admin.ListTeams).Post(bind(api.CreateTeamOption{}), admin.CreateTeam)
+			})
 		}, ReqAdmin())
 	}, context.APIContexter())
 }

+ 9 - 0
routers/api/v1/convert/convert.go

@@ -196,3 +196,12 @@ func ToOrganization(org *models.User) *api.Organization {
 		Location:    org.Location,
 	}
 }
+
+func ToTeam(team *models.Team) *api.Team {
+	return &api.Team{
+		ID:          team.ID,
+		Name:        team.Name,
+		Description: team.Description,
+		Permission:  team.Authorize.String(),
+	}
+}

routers/api/v1/repo/hooks.go → routers/api/v1/repo/hook.go


routers/api/v1/repo/keys.go → routers/api/v1/repo/key.go


routers/api/v1/user/followers.go → routers/api/v1/user/follower.go


routers/api/v1/user/keys.go → routers/api/v1/user/key.go


+ 1 - 15
routers/org/teams.go

@@ -154,25 +154,11 @@ func NewTeamPost(ctx *context.Context, form auth.CreateTeamForm) {
 	ctx.Data["PageIsOrgTeams"] = true
 	ctx.Data["PageIsOrgTeamsNew"] = true
 
-	// Validate permission level.
-	var auth models.AccessMode
-	switch form.Permission {
-	case "read":
-		auth = models.ACCESS_MODE_READ
-	case "write":
-		auth = models.ACCESS_MODE_WRITE
-	case "admin":
-		auth = models.ACCESS_MODE_ADMIN
-	default:
-		ctx.Error(401)
-		return
-	}
-
 	t := &models.Team{
 		OrgID:       ctx.Org.Organization.Id,
 		Name:        form.TeamName,
 		Description: form.Description,
-		Authorize:   auth,
+		Authorize:   models.ParseAccessMode(form.Permission),
 	}
 	ctx.Data["Team"] = t
 

+ 1 - 1
templates/.VERSION

@@ -1 +1 @@
-0.9.13.0321
+0.9.14.0321