gitea源码

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "os"
  6. "strconv"
  7. "strings"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // env keys for git hooks need
  13. const (
  14. EnvRepoName = "GITEA_REPO_NAME"
  15. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  16. EnvRepoID = "GITEA_REPO_ID"
  17. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  18. EnvPusherName = "GITEA_PUSHER_NAME"
  19. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  20. EnvPusherID = "GITEA_PUSHER_ID"
  21. EnvKeyID = "GITEA_KEY_ID" // public key ID
  22. EnvDeployKeyID = "GITEA_DEPLOY_KEY_ID"
  23. EnvPRID = "GITEA_PR_ID"
  24. EnvPushTrigger = "GITEA_PUSH_TRIGGER"
  25. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  26. EnvAppURL = "GITEA_ROOT_URL"
  27. EnvActionPerm = "GITEA_ACTION_PERM"
  28. )
  29. type PushTrigger string
  30. const (
  31. PushTriggerPRMergeToBase PushTrigger = "pr-merge-to-base"
  32. PushTriggerPRUpdateWithBase PushTrigger = "pr-update-with-base"
  33. )
  34. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  35. // It is recommended to avoid using this unless you are pushing within a transaction
  36. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  37. // We provide the full pushing-environment for other hook providers
  38. func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  39. return append(PushingEnvironment(doer, repo),
  40. EnvIsInternal+"=true",
  41. )
  42. }
  43. // PushingEnvironment returns an os environment to allow hooks to work on push
  44. func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  45. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  46. }
  47. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  48. func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID int64) []string {
  49. isWiki := "false"
  50. if strings.HasSuffix(repoName, ".wiki") {
  51. isWiki = "true"
  52. }
  53. authorSig := author.NewGitSig()
  54. committerSig := committer.NewGitSig()
  55. environ := append(os.Environ(),
  56. "GIT_AUTHOR_NAME="+authorSig.Name,
  57. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  58. "GIT_COMMITTER_NAME="+committerSig.Name,
  59. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  60. EnvRepoName+"="+repoName,
  61. EnvRepoUsername+"="+repo.OwnerName,
  62. EnvRepoIsWiki+"="+isWiki,
  63. EnvPusherName+"="+committer.Name,
  64. EnvPusherID+"="+strconv.FormatInt(committer.ID, 10),
  65. EnvRepoID+"="+strconv.FormatInt(repo.ID, 10),
  66. EnvPRID+"="+strconv.FormatInt(prID, 10),
  67. EnvAppURL+"="+setting.AppURL,
  68. "SSH_ORIGINAL_COMMAND=gitea-internal",
  69. )
  70. if !committer.KeepEmailPrivate {
  71. environ = append(environ, EnvPusherEmail+"="+committer.Email)
  72. }
  73. return environ
  74. }