Bläddra i källkod

API endpoint for creating teams

New API endpoint for creating a team under an organisation for
members of the Owner team.
Previously, team creation through the API was only possible for site
admins.
Achilleas Koutsou 5 år sedan
förälder
incheckning
be3f6f2124
2 ändrade filer med 34 tillägg och 3 borttagningar
  1. 8 3
      internal/route/api/v1/api.go
  2. 26 0
      internal/route/api/v1/org/team.go

+ 8 - 3
internal/route/api/v1/api.go

@@ -5,14 +5,15 @@
 package v1
 
 import (
+	"net/http"
+	"strings"
+
 	admin2 "github.com/G-Node/gogs/internal/route/api/v1/admin"
 	misc2 "github.com/G-Node/gogs/internal/route/api/v1/misc"
 	org2 "github.com/G-Node/gogs/internal/route/api/v1/org"
 	repo2 "github.com/G-Node/gogs/internal/route/api/v1/repo"
 	search2 "github.com/G-Node/gogs/internal/route/api/v1/search"
 	user2 "github.com/G-Node/gogs/internal/route/api/v1/user"
-	"net/http"
-	"strings"
 
 	"github.com/go-macaron/binding"
 	"gopkg.in/macaron.v1"
@@ -368,7 +369,11 @@ func RegisterRoutes(m *macaron.Macaron) {
 			m.Combo("").
 				Get(org2.Get).
 				Patch(bind(api.EditOrgOption{}), org2.Edit)
-			m.Get("/teams", org2.ListTeams)
+			m.Group("/teams", func() {
+				m.Combo("").
+					Get(org2.ListTeams).
+					Post(bind(api.CreateTeamOption{}), org2.CreateTeam)
+			})
 		}, orgAssignment(true))
 
 		m.Group("/admin", func() {

+ 26 - 0
internal/route/api/v1/org/team.go

@@ -5,6 +5,9 @@
 package org
 
 import (
+	"net/http"
+
+	"github.com/G-Node/gogs/internal/db"
 	convert2 "github.com/G-Node/gogs/internal/route/api/v1/convert"
 	api "github.com/gogs/go-gogs-client"
 
@@ -24,3 +27,26 @@ func ListTeams(c *context.APIContext) {
 	}
 	c.JSON(200, apiTeams)
 }
+
+func CreateTeam(c *context.APIContext, opt api.CreateTeamOption) {
+	if !c.Org.Organization.IsOwnedBy(c.User.ID) {
+		c.Error(http.StatusForbidden, "", "given user is not owner of organization")
+		return
+	}
+	team := &db.Team{
+		OrgID:       c.Org.Organization.ID,
+		Name:        opt.Name,
+		Description: opt.Description,
+		Authorize:   db.ParseAccessMode(opt.Permission),
+	}
+	if err := db.NewTeam(team); err != nil {
+		if db.IsErrTeamAlreadyExist(err) {
+			c.Error(http.StatusUnprocessableEntity, "", err)
+		} else {
+			c.ServerError("NewTeam", err)
+		}
+		return
+	}
+
+	c.JSON(http.StatusCreated, convert2.ToTeam(team))
+}