新增:组织接口返回owner团队信息

This commit is contained in:
yystopf 2022-11-30 12:41:56 +08:00
parent 25e0cd94b0
commit 4a9ef64762
4 changed files with 121 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package convert
import (
"strings"
"code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/repo"
gitea_convert "code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/git"
@ -39,3 +40,23 @@ func ToTagCommit(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (re
Message: commit.CommitMessage,
}, nil
}
func ToOrganization(org *organization.Organization, team *organization.Team) (*api.Organization, error) {
apiTeam, err := gitea_convert.ToTeam(team)
if err != nil {
return &api.Organization{}, err
}
return &api.Organization{
ID: org.ID,
Name: org.Name,
AvatarURL: org.AvatarLink(),
UserName: org.Name,
FullName: org.FullName,
Description: org.Description,
Website: org.Website,
Location: org.Location,
Visibility: org.Visibility.String(),
RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess,
OwnerTeam: apiTeam,
}, nil
}

15
modules/structs/org.go Normal file
View File

@ -0,0 +1,15 @@
package structs
type Organization struct {
ID int64 `json:"id"`
Name string `json:"name"`
UserName string `json:"username"`
FullName string `json:"full_name"`
AvatarURL string `json:"avatar_url"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
Visibility string `json:"visibility"`
RepoAdminChangeTeamAccess bool `json:"repo_admin_change_team_access"`
OwnerTeam interface{} `json:"owner_team"` //团队关系;
}

View File

@ -6,6 +6,7 @@ import (
"reflect"
"strings"
gitea_api "code.gitea.io/gitea/modules/structs"
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
"gitea.com/go-chi/binding"
@ -20,6 +21,7 @@ import (
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/misc"
"code.gitea.io/gitea/services/auth"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat/org"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat/repo"
"github.com/go-chi/cors"
)
@ -86,6 +88,7 @@ func Routers() *web.Route {
}, mustAllowPulls, reqRepoReader(unit_model.TypeCode), context.ReferencesGitRepo())
}, repoAssignment())
})
m.Post("/orgs", reqToken(), bind(gitea_api.CreateOrgOption{}), org.Create)
})
return m
@ -231,3 +234,19 @@ func mustAllowPulls(ctx *context.APIContext) {
return
}
}
func reqToken() func(ctx *context.APIContext) {
return func(ctx *context.APIContext) {
if true == ctx.Data["IsApiToken"] {
return
}
if ctx.Context.IsBasicAuth {
ctx.CheckForOTP()
return
}
if ctx.IsSigned {
return
}
ctx.Error(http.StatusUnauthorized, "reqToken", "token is required")
}
}

66
routers/hat/org/org.go Normal file
View File

@ -0,0 +1,66 @@
package org
import (
"net/http"
"code.gitea.io/gitea/models/db"
org_model "code.gitea.io/gitea/models/organization"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert"
)
func Create(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.CreateOrgOption)
if !ctx.Doer.CanCreateOrganization() {
ctx.Error(http.StatusForbidden, "create organization not allowed", nil)
return
}
visibility := api.VisibleTypePublic
if form.Visibility != "" {
visibility = api.VisibilityModes[form.Visibility]
}
org := &org_model.Organization{
Name: form.UserName,
FullName: form.FullName,
Description: form.Description,
Website: form.Website,
Location: form.Location,
IsActive: true,
Type: user_model.UserTypeOrganization,
Visibility: visibility,
RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess,
}
if err := org_model.CreateOrganization(org, ctx.Doer); err != nil {
if user_model.IsErrUserAlreadyExist(err) ||
db.IsErrNameReserved(err) ||
db.IsErrNameCharsNotAllowed(err) ||
db.IsErrNamePatternNotAllowed(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
} else {
ctx.Error(http.StatusInternalServerError, "CreateOrganization", err)
}
return
}
team, err := org_model.GetTeam(ctx, org.ID, "")
if err != nil {
if user_model.IsErrUserNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetTeam", err)
}
return
}
if apiOrg, err := hat_convert.ToOrganization(org, team); err != nil {
ctx.Error(http.StatusInternalServerError, "ToOrganization", err)
} else {
ctx.JSON(http.StatusCreated, apiOrg)
}
}