Browse Source

Merge pull request '解决ssh方式仓库大小限制脚本未正常运行的问题' (#2087) from fix-ssh into V20220519

Reviewed-on: https://git.openi.org.cn/OpenI/aiforge/pulls/2087
Reviewed-by: lewis <747342561@qq.com>
tags/v1.22.5.1^2
lewis 3 years ago
parent
commit
bd14ef1b94
4 changed files with 62 additions and 1 deletions
  1. +13
    -0
      cmd/serv.go
  2. +30
    -0
      modules/private/hook.go
  3. +18
    -1
      routers/private/hook.go
  4. +1
    -0
      routers/private/internal.go

+ 13
- 0
cmd/serv.go View File

@@ -208,6 +208,19 @@ func runServ(c *cli.Context) error {
os.Setenv(models.ProtectedBranchPRID, fmt.Sprintf("%d", 0))
os.Setenv(models.EnvIsDeployKey, fmt.Sprintf("%t", results.IsDeployKey))
os.Setenv(models.EnvKeyID, fmt.Sprintf("%d", results.KeyID))
//set environment for pre-receive hook script
if verb == "git-receive-pack" {
os.Setenv(models.EnvRepoMaxFileSize, fmt.Sprint(setting.Repository.Upload.FileMaxSize))
os.Setenv(models.EnvRepoMaxSize, fmt.Sprint(setting.Repository.RepoMaxSize))
os.Setenv(models.EnvPushSizeCheckFlag, fmt.Sprint(setting.Repository.Upload.ShellFlag))
env, _ := private.GetHookConfig(username, reponame)
if env != nil && len(env) > 0 {
repoSize := env[models.EnvRepoSize]
if repoSize != "" {
os.Setenv(models.EnvRepoSize, repoSize)
}
}
}

//LFS token authentication
if verb == lfsAuthenticateVerb {


+ 30
- 0
modules/private/hook.go View File

@@ -50,6 +50,11 @@ type HookPostReceiveBranchResult struct {
URL string
}

// HookEnvResult
type HookEnvResult struct {
Config map[string]string
}

// HookPreReceive check whether the provided commits are allowed
func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string) {
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s",
@@ -122,3 +127,28 @@ func SetDefaultBranch(ownerName, repoName, branch string) error {
}
return nil
}

// GetHookConfig get hook config to set environment for hook script
func GetHookConfig(ownerName, repoName string) (map[string]string, string) {
reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/env/%s/%s",
url.PathEscape(ownerName),
url.PathEscape(repoName),
)

req := newInternalRequest(reqURL, "GET")
req = req.Header("Content-Type", "application/json")
req.SetTimeout(60*time.Second, time.Duration(60)*time.Second)
resp, err := req.Response()
if err != nil {
return nil, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, decodeJSONError(resp).Err
}
res := &HookEnvResult{}
_ = json.NewDecoder(resp.Body).Decode(res)

return res.Config, ""
}

+ 18
- 1
routers/private/hook.go View File

@@ -199,7 +199,6 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
env = append(env,
private.GitQuarantinePath+"="+opts.GitQuarantinePath)
}

for i := range opts.OldCommitIDs {
oldCommitID := opts.OldCommitIDs[i]
newCommitID := opts.NewCommitIDs[i]
@@ -368,6 +367,24 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
ctx.PlainText(http.StatusOK, []byte("ok"))
}

// HookEnv
func HookEnv(ctx *macaron.Context) {
ownerName := ctx.Params(":owner")
repoName := ctx.Params(":repo")
log.Info("try to get hook env.ownerName=%s repoName=%s", ownerName, repoName)
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
if err != nil {
log.Error("Unable to get repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"err": err.Error(),
})
return
}
result := make(map[string]string, 1)
result[models.EnvRepoSize] = fmt.Sprint(repo.Size)
ctx.JSON(http.StatusOK, &private.HookEnvResult{Config: result})
}

// HookPostReceive updates services and users
func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) {
ownerName := ctx.Params(":owner")


+ 1
- 0
routers/private/internal.go View File

@@ -38,6 +38,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/hook/pre-receive/:owner/:repo", bind(private.HookOptions{}), HookPreReceive)
m.Post("/hook/post-receive/:owner/:repo", bind(private.HookOptions{}), HookPostReceive)
m.Post("/hook/set-default-branch/:owner/:repo/:branch", SetDefaultBranch)
m.Get("/hook/env/:owner/:repo", HookEnv)
m.Get("/serv/none/:keyid", ServNoCommand)
m.Get("/serv/command/:keyid/:owner/:repo", ServCommand)
m.Post("/manager/shutdown", Shutdown)


Loading…
Cancel
Save