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.

74 lines
2.3 KiB

  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. )
  10. // env keys for git hooks need
  11. const (
  12. EnvRepoName = "GITEA_REPO_NAME"
  13. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  14. EnvRepoID = "GITEA_REPO_ID"
  15. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  16. EnvPusherName = "GITEA_PUSHER_NAME"
  17. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  18. EnvPusherID = "GITEA_PUSHER_ID"
  19. EnvKeyID = "GITEA_KEY_ID"
  20. EnvIsDeployKey = "GITEA_IS_DEPLOY_KEY"
  21. EnvPRID = "GITEA_PR_ID"
  22. EnvIsInternal = "GITEA_INTERNAL_PUSH"
  23. )
  24. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  25. // It is recommended to avoid using this unless you are pushing within a transaction
  26. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  27. // We provide the full pushing-environment for other hook providers
  28. func InternalPushingEnvironment(doer *User, repo *Repository) []string {
  29. return append(PushingEnvironment(doer, repo),
  30. EnvIsInternal+"=true",
  31. )
  32. }
  33. // PushingEnvironment returns an os environment to allow hooks to work on push
  34. func PushingEnvironment(doer *User, repo *Repository) []string {
  35. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  36. }
  37. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  38. func FullPushingEnvironment(author, committer *User, repo *Repository, repoName string, prID int64) []string {
  39. isWiki := "false"
  40. if strings.HasSuffix(repoName, ".wiki") {
  41. isWiki = "true"
  42. }
  43. authorSig := author.NewGitSig()
  44. committerSig := committer.NewGitSig()
  45. environ := append(os.Environ(),
  46. "GIT_AUTHOR_NAME="+authorSig.Name,
  47. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  48. "GIT_COMMITTER_NAME="+committerSig.Name,
  49. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  50. EnvRepoName+"="+repoName,
  51. EnvRepoUsername+"="+repo.OwnerName,
  52. EnvRepoIsWiki+"="+isWiki,
  53. EnvPusherName+"="+committer.Name,
  54. EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
  55. EnvRepoID+"="+fmt.Sprintf("%d", repo.ID),
  56. EnvPRID+"="+fmt.Sprintf("%d", prID),
  57. "SSH_ORIGINAL_COMMAND=gitea-internal",
  58. )
  59. if !committer.KeepEmailPrivate {
  60. environ = append(environ, EnvPusherEmail+"="+committer.Email)
  61. }
  62. return environ
  63. }