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.

helper_environment.go 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "code.gitea.io/gitea/modules/setting"
  10. )
  11. // env keys for git hooks need
  12. const (
  13. EnvRepoName = "GITEA_REPO_NAME"
  14. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  15. EnvRepoID = "GITEA_REPO_ID"
  16. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  17. EnvPusherName = "GITEA_PUSHER_NAME"
  18. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  19. EnvPusherID = "GITEA_PUSHER_ID"
  20. EnvKeyID = "GITEA_KEY_ID"
  21. EnvIsDeployKey = "GITEA_IS_DEPLOY_KEY"
  22. EnvPRID = "GITEA_PR_ID"
  23. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  24. EnvAppURL = "GITEA_ROOT_URL"
  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. environ := append(os.Environ(),
  48. "GIT_AUTHOR_NAME="+authorSig.Name,
  49. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  50. "GIT_COMMITTER_NAME="+committerSig.Name,
  51. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  52. EnvRepoName+"="+repoName,
  53. EnvRepoUsername+"="+repo.OwnerName,
  54. EnvRepoIsWiki+"="+isWiki,
  55. EnvPusherName+"="+committer.Name,
  56. EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
  57. EnvRepoID+"="+fmt.Sprintf("%d", repo.ID),
  58. EnvPRID+"="+fmt.Sprintf("%d", prID),
  59. EnvAppURL+"="+setting.AppURL,
  60. "SSH_ORIGINAL_COMMAND=gitea-internal",
  61. )
  62. if !committer.KeepEmailPrivate {
  63. environ = append(environ, EnvPusherEmail+"="+committer.Email)
  64. }
  65. return environ
  66. }