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

env.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "fmt"
  6. "os"
  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. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  25. EnvAppURL = "GITEA_ROOT_URL"
  26. )
  27. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  28. // It is recommended to avoid using this unless you are pushing within a transaction
  29. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  30. // We provide the full pushing-environment for other hook providers
  31. func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  32. return append(PushingEnvironment(doer, repo),
  33. EnvIsInternal+"=true",
  34. )
  35. }
  36. // PushingEnvironment returns an os environment to allow hooks to work on push
  37. func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  38. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  39. }
  40. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  41. func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID int64) []string {
  42. isWiki := "false"
  43. if strings.HasSuffix(repoName, ".wiki") {
  44. isWiki = "true"
  45. }
  46. authorSig := author.NewGitSig()
  47. committerSig := committer.NewGitSig()
  48. environ := append(os.Environ(),
  49. "GIT_AUTHOR_NAME="+authorSig.Name,
  50. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  51. "GIT_COMMITTER_NAME="+committerSig.Name,
  52. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  53. EnvRepoName+"="+repoName,
  54. EnvRepoUsername+"="+repo.OwnerName,
  55. EnvRepoIsWiki+"="+isWiki,
  56. EnvPusherName+"="+committer.Name,
  57. EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
  58. EnvRepoID+"="+fmt.Sprintf("%d", repo.ID),
  59. EnvPRID+"="+fmt.Sprintf("%d", prID),
  60. EnvAppURL+"="+setting.AppURL,
  61. "SSH_ORIGINAL_COMMAND=gitea-internal",
  62. )
  63. if !committer.KeepEmailPrivate {
  64. environ = append(environ, EnvPusherEmail+"="+committer.Email)
  65. }
  66. return environ
  67. }