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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. EnvActionPerm = "GITEA_ACTION_PERM"
  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. }