1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-23 02:38:35 +00:00

Use a standalone struct name for Organization (#17632)

* Use a standalone struct name for Organization

* recover unnecessary change

* make the code readable

* Fix template failure

* Fix template failure

* Move HasMemberWithUserID to org

* Fix test

* Remove unnecessary user type check

* Fix test

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
Lunny Xiao
2021-11-19 19:41:40 +08:00
committed by GitHub
parent a09b40de8d
commit 7a03473159
43 changed files with 335 additions and 259 deletions

View File

@@ -54,7 +54,7 @@ func CreateOrg(ctx *context.APIContext) {
visibility = api.VisibilityModes[form.Visibility]
}
org := &models.User{
org := &models.Organization{
Name: form.UserName,
FullName: form.FullName,
Description: form.Description,
@@ -117,7 +117,7 @@ func GetAllOrgs(ctx *context.APIContext) {
}
orgs := make([]*api.Organization, len(users))
for i := range users {
orgs[i] = convert.ToOrganization(users[i])
orgs[i] = convert.ToOrganization(models.OrgFromUser(users[i]))
}
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)

View File

@@ -59,7 +59,7 @@ func ListHooks(ctx *context.APIContext) {
hooks := make([]*api.Hook, len(orgHooks))
for i, hook := range orgHooks {
hooks[i] = convert.ToHook(ctx.Org.Organization.HomeLink(), hook)
hooks[i] = convert.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook)
}
ctx.SetTotalCountHeader(count)
@@ -95,7 +95,7 @@ func GetHook(ctx *context.APIContext) {
if err != nil {
return
}
ctx.JSON(http.StatusOK, convert.ToHook(org.HomeLink(), hook))
ctx.JSON(http.StatusOK, convert.ToHook(org.AsUser().HomeLink(), hook))
}
// CreateHook create a hook for an organization

View File

@@ -56,7 +56,7 @@ func ListLabels(ctx *context.APIContext) {
}
ctx.SetTotalCountHeader(count)
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization))
ctx.JSON(http.StatusOK, convert.ToLabelList(labels, nil, ctx.Org.Organization.AsUser()))
}
// CreateLabel create a label for a repository
@@ -104,7 +104,7 @@ func CreateLabel(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization))
ctx.JSON(http.StatusCreated, convert.ToLabel(label, nil, ctx.Org.Organization.AsUser()))
}
// GetLabel get label by organization and label id
@@ -149,7 +149,7 @@ func GetLabel(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization.AsUser()))
}
// EditLabel modify a label for an Organization
@@ -214,7 +214,7 @@ func EditLabel(ctx *context.APIContext) {
return
}
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization))
ctx.JSON(http.StatusOK, convert.ToLabel(label, nil, ctx.Org.Organization.AsUser()))
}
// DeleteLabel delete a label for an organization

View File

@@ -31,7 +31,7 @@ func listUserOrgs(ctx *context.APIContext, u *models.User) {
}
maxResults := len(orgs)
orgs, _ = util.PaginateSlice(orgs, listOptions.Page, listOptions.PageSize).([]*models.User)
orgs, _ = util.PaginateSlice(orgs, listOptions.Page, listOptions.PageSize).([]*models.Organization)
apiOrgs := make([]*api.Organization, len(orgs))
for i := range orgs {
@@ -141,7 +141,8 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
return
}
authorizeLevel, err := o.GetOrgUserMaxAuthorizeLevel(u.ID)
org := models.OrgFromUser(o)
authorizeLevel, err := org.GetOrgUserMaxAuthorizeLevel(u.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetOrgUserAuthorizeLevel", err)
return
@@ -160,7 +161,7 @@ func GetUserOrgsPermissions(ctx *context.APIContext) {
op.IsOwner = true
}
op.CanCreateRepository, err = o.CanCreateOrgRepo(u.ID)
op.CanCreateRepository, err = org.CanCreateOrgRepo(u.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "CanCreateOrgRepo", err)
return
@@ -212,7 +213,7 @@ func GetAll(ctx *context.APIContext) {
}
orgs := make([]*api.Organization, len(publicOrgs))
for i := range publicOrgs {
orgs[i] = convert.ToOrganization(publicOrgs[i])
orgs[i] = convert.ToOrganization(models.OrgFromUser(publicOrgs[i]))
}
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
@@ -252,7 +253,7 @@ func Create(ctx *context.APIContext) {
visibility = api.VisibilityModes[form.Visibility]
}
org := &models.User{
org := &models.Organization{
Name: form.UserName,
FullName: form.FullName,
Description: form.Description,
@@ -295,7 +296,7 @@ func Get(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/Organization"
if !models.HasOrgOrUserVisible(ctx.Org.Organization, ctx.User) {
if !models.HasOrgOrUserVisible(ctx.Org.Organization.AsUser(), ctx.User) {
ctx.NotFound("HasOrgOrUserVisible", nil)
return
}
@@ -337,7 +338,7 @@ func Edit(ctx *context.APIContext) {
if form.RepoAdminChangeTeamAccess != nil {
org.RepoAdminChangeTeamAccess = *form.RepoAdminChangeTeamAccess
}
if err := models.UpdateUserCols(org,
if err := models.UpdateUserCols(org.AsUser(),
"full_name", "description", "website", "location",
"visibility", "repo_admin_change_team_access",
); err != nil {

View File

@@ -102,7 +102,7 @@ func ListUserTeams(ctx *context.APIContext) {
for i := range teams {
apiOrg, ok := cache[teams[i].OrgID]
if !ok {
org, err := models.GetUserByID(teams[i].OrgID)
org, err := models.GetOrgByID(teams[i].OrgID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetUserByID", err)
return

View File

@@ -120,7 +120,7 @@ func CreateFork(ctx *context.APIContext) {
ctx.Error(http.StatusForbidden, "isMemberNot", fmt.Sprintf("User is no Member of Organisation '%s'", org.Name))
return
}
forker = org
forker = org.AsUser()
}
fork, err := repo_service.ForkRepository(ctx.User, forker, models.ForkRepoOptions{

View File

@@ -86,7 +86,7 @@ func Migrate(ctx *context.APIContext) {
if repoOwner.IsOrganization() {
// Check ownership of organization.
isOwner, err := repoOwner.IsOwnedBy(ctx.User.ID)
isOwner, err := models.OrgFromUser(repoOwner).IsOwnedBy(ctx.User.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "IsOwnedBy", err)
return

View File

@@ -393,7 +393,7 @@ func Generate(ctx *context.APIContext) {
}
if !ctx.User.IsAdmin {
canCreate, err := ctxUser.CanCreateOrgRepo(ctx.User.ID)
canCreate, err := models.OrgFromUser(ctxUser).CanCreateOrgRepo(ctx.User.ID)
if err != nil {
ctx.ServerError("CanCreateOrgRepo", err)
return
@@ -489,7 +489,7 @@ func CreateOrgRepo(ctx *context.APIContext) {
return
}
if !models.HasOrgOrUserVisible(org, ctx.User) {
if !models.HasOrgOrUserVisible(org.AsUser(), ctx.User) {
ctx.NotFound("HasOrgOrUserVisible", nil)
return
}
@@ -504,7 +504,7 @@ func CreateOrgRepo(ctx *context.APIContext) {
return
}
}
CreateUserRepo(ctx, org, *opt)
CreateUserRepo(ctx, org.AsUser(), *opt)
}
// Get one repository

View File

@@ -64,7 +64,7 @@ func Transfer(ctx *context.APIContext) {
}
if newOwner.Type == models.UserTypeOrganization {
if !ctx.User.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !newOwner.HasMemberWithUserID(ctx.User.ID) {
if !ctx.User.IsAdmin && newOwner.Visibility == api.VisibleTypePrivate && !models.OrgFromUser(newOwner).HasMemberWithUserID(ctx.User.ID) {
// The user shouldn't know about this organization
ctx.Error(http.StatusNotFound, "", "The new owner does not exist or cannot be found")
return
@@ -78,7 +78,7 @@ func Transfer(ctx *context.APIContext) {
return
}
org := convert.ToOrganization(newOwner)
org := convert.ToOrganization(models.OrgFromUser(newOwner))
for _, tID := range *opts.TeamIDs {
team, err := models.GetTeamByID(tID)
if err != nil {

View File

@@ -157,5 +157,5 @@ func ListOrgRepos(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/RepositoryList"
listUserRepos(ctx, ctx.Org.Organization, ctx.IsSigned)
listUserRepos(ctx, ctx.Org.Organization.AsUser(), ctx.IsSigned)
}

View File

@@ -75,7 +75,7 @@ func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
org := ctx.Org.Organization
hook, ok := addHook(ctx, form, org.ID, 0)
if ok {
ctx.JSON(http.StatusCreated, convert.ToHook(org.HomeLink(), hook))
ctx.JSON(http.StatusCreated, convert.ToHook(org.AsUser().HomeLink(), hook))
}
}
@@ -185,7 +185,7 @@ func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64
if err != nil {
return
}
ctx.JSON(http.StatusOK, convert.ToHook(org.HomeLink(), updated))
ctx.JSON(http.StatusOK, convert.ToHook(org.AsUser().HomeLink(), updated))
}
// EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly