接口修复 [patch]/orgs/{org} on 2021/01/14

This commit is contained in:
hcxm 2021-01-14 14:43:13 +08:00
parent 79d8abce8c
commit da5cce281c
4 changed files with 178 additions and 11 deletions

View File

@ -0,0 +1,58 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea 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 git
import (
"fmt"
"strings"
"sync"
)
// 自定义:多协程查询版本库,减小查询延时; add by hcxm
func (repo *Repository) GetTagInfosExt(page, pageSize int) ([]*Tag, error) {
// TODO this a slow implementation, makes one git command per tag
stdout, err := NewCommand("tag").RunInDir(repo.Path)
if err != nil {
return nil, err
}
tagNames := strings.Split(strings.TrimRight(stdout, "\n"), "\n")
if page != 0 {
skip := (page - 1) * pageSize
if skip >= len(tagNames) {
return nil, nil
}
if (len(tagNames) - skip) < pageSize {
pageSize = len(tagNames) - skip
}
tagNames = tagNames[skip : skip+pageSize]
}
grp:= &sync.WaitGroup{}
var tags = make([]*Tag, 0, len(tagNames))
for _, tagName := range tagNames {
tagName = strings.TrimSpace(tagName)
if len(tagName) == 0 {
continue
}
grp.Add(1)
go func(tagsExt *[]*Tag,tagName string,grp *sync.WaitGroup) {
defer func() {
grp.Done()
}()
tag, err := repo.GetTag(tagName)
if err != nil {
fmt.Println("repo.GetTag:",err)
return
}
tag.Name = tagName
*tagsExt = append(*tagsExt, tag)
}(&tags,tagName,grp)
}
grp.Wait()
sortTagsByTime(tags)
return tags, nil
}

View File

@ -0,0 +1,20 @@
// Copyright 2015 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 structs
// EditOrgOption options for editing an organization
type EditOrgOptionExt struct {
Name string `json:"name"` // 添加对name的修改,lower_name 其值跟随name变化;
FullName string `json:"full_name"`
Description string `json:"description"`
Website string `json:"website"`
Location string `json:"location"`
// possible values are `public`, `limited` or `private`
// enum: public,limited,private
Visibility string `json:"visibility" binding:"In(,public,limited,private)"`
RepoAdminChangeTeamAccess bool `json:"repo_admin_change_team_access"`
}

View File

@ -251,26 +251,20 @@ func reqAdmin() macaron.Handler {
// reqRepoWriter user should have a permission to write to a repo, or be a site admin
func reqRepoWriter(unitTypes ...models.UnitType) macaron.Handler {
return func(ctx *context.Context) {
start:=time.Now()
log.Info("*************reqRepoWriter enter:")
if !ctx.IsUserRepoWriter(unitTypes) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
ctx.Error(http.StatusForbidden)
return
}
log.Info("*************reqRepoWriter leave:%v",time.Now().Sub(start))
}
}
// reqRepoReader user should have specific read permission or be a repo admin or a site admin
func reqRepoReader(unitType models.UnitType) macaron.Handler {
return func(ctx *context.Context) {
start:=time.Now()
log.Info("*************reqRepoReader")
if !ctx.IsUserRepoReaderSpecific(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
ctx.Error(http.StatusForbidden)
return
}
log.Info("*************reqRepoReader:%v",time.Now().Sub(start))
}
}
@ -290,7 +284,6 @@ func reqOrgOwnership() macaron.Handler {
if ctx.Context.IsUserSiteAdmin() {
return
}
var orgID int64
if ctx.Org.Organization != nil {
orgID = ctx.Org.Organization.ID
@ -300,7 +293,6 @@ func reqOrgOwnership() macaron.Handler {
ctx.Error(http.StatusInternalServerError, "", "reqOrgOwnership: unprepared context")
return
}
isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "IsOrganizationOwner", err)
@ -421,16 +413,21 @@ func orgAssignment(args ...bool) macaron.Handler {
}
if assignTeam {
ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetTeamById", err)
}
return
}
}
}
}
@ -663,15 +660,18 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Group("/:username/:reponame", func() {
m.Group("/readme", func() {
//update by 2021-01-12 begin
//m.Get("", context.RepoRefByType(context.RepoRefBranch), viewfile.ViewFile)
//m.Get("/branch/*", context.RepoRefByType(context.RepoRefBranch), viewfile.ViewFile)
//m.Get("/tag/*",context.RepoRefByType(context.RepoRefTag), viewfile.ViewFile)
//m.Get("/commit/*", context.RepoRefByType(context.RepoRefCommit), viewfile.ViewFile)
//update by 2021-01-12
m.Get("", viewfile.RepoRefByType(context.RepoRefBranch), viewfile.ViewFile)
m.Get("/branch/*", viewfile.RepoRefByType(context.RepoRefBranch), viewfile.ViewFile)
m.Get("/tag/*",viewfile.RepoRefByType(context.RepoRefTag), viewfile.ViewFile)
m.Get("/commit/*", viewfile.RepoRefByType(context.RepoRefCommit), viewfile.ViewFile)
//update by 2021-01-12 end 引用自定义包;
})
m.Combo("").Get(reqAnyRepoReader(), repo.Get).
Delete(reqToken(), reqOwner(), repo.Delete).
@ -906,7 +906,6 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/tags/:sha", context.RepoRef(), repo.GetTag)
}, reqRepoReader(models.UnitTypeCode))
//********************
m.Group("/contents", func() { //***********
m.Get("", repo.GetContentsList)
m.Get("/*", repo.GetContents)
@ -936,7 +935,10 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Get("/orgs", org.GetAll)
m.Group("/orgs/:org", func() {
m.Combo("").Get(org.Get).
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
//modified on 2021/01/14 end
Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOptionExt{}), org.Edit_Ext).
//Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
//modified on 2021/01/14 end
Delete(reqToken(), reqOrgOwnership(), org.Delete)
m.Combo("/repos").Get(user.ListOrgRepos).
Post(reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)

View File

@ -0,0 +1,87 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea 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 org
import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
api "code.gitea.io/gitea/modules/structs"
"fmt"
"net/http"
"strings"
)
// Edit change an organization's information modified on 2021/01/14
func Edit_Ext(ctx *context.APIContext, form api.EditOrgOptionExt) {
// swagger:operation PATCH /orgs/{org} organization orgEdit
// ---
// summary: Edit an organization
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: org
// in: path
// description: name of the organization to edit
// type: string
// required: true
// - name: body
// in: body
// required: true
// schema:
// "$ref": "#/definitions/EditOrgOption"
// responses:
// "200":
// "$ref": "#/responses/Organization"
org := ctx.Org.Organization
if org.LowerName!=strings.ToLower(form.Name) && form.Name!="" {
if len(form.Name)>40 {
ctx.Error(http.StatusBadRequest, "EditOrganization", "name长度不能超过40个字符")
return
}
org.Name=form.Name //add by hcxm 2021/01/14
org.LowerName=strings.ToLower(org.Name) //add by hcxm 2021/01/14
}
org.RepoAdminChangeTeamAccess=form.RepoAdminChangeTeamAccess //add by hcxm 2021/01/14
org.FullName = form.FullName
org.Description = form.Description
org.Website = form.Website
org.Location = form.Location
var visibilityChanged bool=false
if form.Visibility != "" {
visibilityChanged = form.Visibility != org.Visibility.String()
fmt.Println("***打印参数: visibilityChanged:",visibilityChanged," form.Visibility:",form.Visibility," org.Visibility:",org.Visibility.String())
org.Visibility = api.VisibilityModes[form.Visibility]
}
if err := models.UpdateUserCols(org, "full_name", "description", "website", "location", "visibility","name","lower_name","repo_admin_change_team_access"); err != nil {
ctx.Error(http.StatusInternalServerError, "EditOrganization", err)
return
}
/*
// update forks visibility 更新库的访问权限;
if visibilityChanged {
if err := org.GetRepositories(models.ListOptions{Page: 1, PageSize: org.NumRepos}); err != nil {
ctx.ServerError("GetRepositories", err)
return
}
for _, repo := range org.Repos {
if err := models.UpdateRepository(repo, true); err != nil {
ctx.ServerError("UpdateRepository", err)
return
}
}
}
*/
ctx.JSON(http.StatusOK, convert.ToOrganization(org))
}