Browse Source

update

pull/2087/head
chenyifan01 3 years ago
parent
commit
c081a64f19
5 changed files with 60 additions and 7 deletions
  1. +12
    -0
      cmd/hook.go
  2. +0
    -3
      cmd/serv.go
  3. +30
    -0
      modules/private/hook.go
  4. +17
    -4
      routers/private/hook.go
  5. +1
    -0
      routers/private/internal.go

+ 12
- 0
cmd/hook.go View File

@@ -158,6 +158,18 @@ Gitea or set your environment appropriately.`, "")
prID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchPRID), 10, 64)
isDeployKey, _ := strconv.ParseBool(os.Getenv(models.EnvIsDeployKey))

//set environment for pre-receive hook script
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)
}
}

hookOptions := private.HookOptions{
UserID: userID,
GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),


+ 0
- 3
cmd/serv.go View File

@@ -208,9 +208,6 @@ 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))
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))

//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, ""
}

+ 17
- 4
routers/private/hook.go View File

@@ -199,10 +199,6 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) {
env = append(env,
private.GitQuarantinePath+"="+opts.GitQuarantinePath)
}
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))
os.Setenv(models.EnvRepoSize, fmt.Sprint(repo.Size))
for i := range opts.OldCommitIDs {
oldCommitID := opts.OldCommitIDs[i]
newCommitID := opts.NewCommitIDs[i]
@@ -371,6 +367,23 @@ 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")
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