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.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 repository
  5. import (
  6. "fmt"
  7. "os"
  8. "strings"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // env keys for git hooks need
  14. const (
  15. EnvRepoName = "GITEA_REPO_NAME"
  16. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  17. EnvRepoID = "GITEA_REPO_ID"
  18. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  19. EnvPusherName = "GITEA_PUSHER_NAME"
  20. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  21. EnvPusherID = "GITEA_PUSHER_ID"
  22. EnvKeyID = "GITEA_KEY_ID" // public key ID
  23. EnvDeployKeyID = "GITEA_DEPLOY_KEY_ID"
  24. EnvPRID = "GITEA_PR_ID"
  25. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  26. EnvAppURL = "GITEA_ROOT_URL"
  27. )
  28. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  29. // It is recommended to avoid using this unless you are pushing within a transaction
  30. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  31. // We provide the full pushing-environment for other hook providers
  32. func InternalPushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  33. return append(PushingEnvironment(doer, repo),
  34. EnvIsInternal+"=true",
  35. )
  36. }
  37. // PushingEnvironment returns an os environment to allow hooks to work on push
  38. func PushingEnvironment(doer *user_model.User, repo *repo_model.Repository) []string {
  39. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  40. }
  41. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  42. func FullPushingEnvironment(author, committer *user_model.User, repo *repo_model.Repository, repoName string, prID int64) []string {
  43. isWiki := "false"
  44. if strings.HasSuffix(repoName, ".wiki") {
  45. isWiki = "true"
  46. }
  47. authorSig := author.NewGitSig()
  48. committerSig := committer.NewGitSig()
  49. environ := 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. EnvRepoID+"="+fmt.Sprintf("%d", repo.ID),
  60. EnvPRID+"="+fmt.Sprintf("%d", prID),
  61. EnvAppURL+"="+setting.AppURL,
  62. "SSH_ORIGINAL_COMMAND=gitea-internal",
  63. )
  64. if !committer.KeepEmailPrivate {
  65. environ = append(environ, EnvPusherEmail+"="+committer.Email)
  66. }
  67. return environ
  68. }