You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

helper_environment.go 2.5 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. )
  10. // env keys for git hooks need
  11. const (
  12. EnvRepoName = "GITEA_REPO_NAME"
  13. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  14. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  15. EnvPusherName = "GITEA_PUSHER_NAME"
  16. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  17. EnvPusherID = "GITEA_PUSHER_ID"
  18. EnvKeyID = "GITEA_KEY_ID"
  19. EnvIsDeployKey = "GITEA_IS_DEPLOY_KEY"
  20. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  21. EnvRepoSize = "REPO_CURRENT_SIZE"
  22. EnvRepoMaxFileSize = "REPO_MAX_FILE_SIZE"
  23. EnvRepoMaxSize = "REPO_MAX_SIZE"
  24. EnvPushSizeCheckFlag = "PUSH_SIZE_CHECK_FLAG"
  25. )
  26. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  27. // It is recommended to avoid using this unless you are pushing within a transaction
  28. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  29. // We provide the full pushing-environment for other hook providers
  30. func InternalPushingEnvironment(doer *User, repo *Repository) []string {
  31. return append(PushingEnvironment(doer, repo),
  32. EnvIsInternal+"=true",
  33. )
  34. }
  35. // PushingEnvironment returns an os environment to allow hooks to work on push
  36. func PushingEnvironment(doer *User, repo *Repository) []string {
  37. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  38. }
  39. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  40. func FullPushingEnvironment(author, committer *User, repo *Repository, repoName string, prID int64) []string {
  41. isWiki := "false"
  42. if strings.HasSuffix(repoName, ".wiki") {
  43. isWiki = "true"
  44. }
  45. authorSig := author.NewGitSig()
  46. committerSig := committer.NewGitSig()
  47. // We should add "SSH_ORIGINAL_COMMAND=gitea-internal",
  48. // once we have hook and pushing infrastructure working correctly
  49. return append(os.Environ(),
  50. "GIT_AUTHOR_NAME="+authorSig.Name,
  51. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  52. "GIT_COMMITTER_NAME="+committerSig.Name,
  53. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  54. EnvRepoName+"="+repoName,
  55. EnvRepoUsername+"="+repo.OwnerName,
  56. EnvRepoIsWiki+"="+isWiki,
  57. EnvPusherName+"="+committer.Name,
  58. EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
  59. ProtectedBranchRepoID+"="+fmt.Sprintf("%d", repo.ID),
  60. ProtectedBranchPRID+"="+fmt.Sprintf("%d", prID),
  61. "SSH_ORIGINAL_COMMAND=gitea-internal",
  62. )
  63. }