| @@ -158,6 +158,18 @@ Gitea or set your environment appropriately.`, "") | |||||
| prID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchPRID), 10, 64) | prID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchPRID), 10, 64) | ||||
| isDeployKey, _ := strconv.ParseBool(os.Getenv(models.EnvIsDeployKey)) | 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{ | hookOptions := private.HookOptions{ | ||||
| UserID: userID, | UserID: userID, | ||||
| GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories), | GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories), | ||||
| @@ -208,9 +208,6 @@ func runServ(c *cli.Context) error { | |||||
| os.Setenv(models.ProtectedBranchPRID, fmt.Sprintf("%d", 0)) | os.Setenv(models.ProtectedBranchPRID, fmt.Sprintf("%d", 0)) | ||||
| os.Setenv(models.EnvIsDeployKey, fmt.Sprintf("%t", results.IsDeployKey)) | os.Setenv(models.EnvIsDeployKey, fmt.Sprintf("%t", results.IsDeployKey)) | ||||
| os.Setenv(models.EnvKeyID, fmt.Sprintf("%d", results.KeyID)) | 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 | //LFS token authentication | ||||
| if verb == lfsAuthenticateVerb { | if verb == lfsAuthenticateVerb { | ||||
| @@ -50,6 +50,11 @@ type HookPostReceiveBranchResult struct { | |||||
| URL string | URL string | ||||
| } | } | ||||
| // HookEnvResult | |||||
| type HookEnvResult struct { | |||||
| Config map[string]string | |||||
| } | |||||
| // HookPreReceive check whether the provided commits are allowed | // HookPreReceive check whether the provided commits are allowed | ||||
| func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string) { | func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string) { | ||||
| reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s", | 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 | 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, "" | |||||
| } | |||||
| @@ -199,10 +199,6 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) { | |||||
| env = append(env, | env = append(env, | ||||
| private.GitQuarantinePath+"="+opts.GitQuarantinePath) | 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 { | for i := range opts.OldCommitIDs { | ||||
| oldCommitID := opts.OldCommitIDs[i] | oldCommitID := opts.OldCommitIDs[i] | ||||
| newCommitID := opts.NewCommitIDs[i] | newCommitID := opts.NewCommitIDs[i] | ||||
| @@ -371,6 +367,23 @@ func HookPreReceive(ctx *macaron.Context, opts private.HookOptions) { | |||||
| ctx.PlainText(http.StatusOK, []byte("ok")) | 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 | // HookPostReceive updates services and users | ||||
| func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) { | func HookPostReceive(ctx *macaron.Context, opts private.HookOptions) { | ||||
| ownerName := ctx.Params(":owner") | ownerName := ctx.Params(":owner") | ||||
| @@ -38,6 +38,7 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Post("/hook/pre-receive/:owner/:repo", bind(private.HookOptions{}), HookPreReceive) | 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/post-receive/:owner/:repo", bind(private.HookOptions{}), HookPostReceive) | ||||
| m.Post("/hook/set-default-branch/:owner/:repo/:branch", SetDefaultBranch) | 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/none/:keyid", ServNoCommand) | ||||
| m.Get("/serv/command/:keyid/:owner/:repo", ServCommand) | m.Get("/serv/command/:keyid/:owner/:repo", ServCommand) | ||||
| m.Post("/manager/shutdown", Shutdown) | m.Post("/manager/shutdown", Shutdown) | ||||