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.

55 lines
1.9 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. // InternalPushingEnvironment returns an os environment to switch off hooks on push
  11. // It is recommended to avoid using this unless you are pushing within a transaction
  12. // or if you absolutely are sure that post-receive and pre-receive will do nothing
  13. // We provide the full pushing-environment for other hook providers
  14. func InternalPushingEnvironment(doer *User, repo *Repository) []string {
  15. return append(PushingEnvironment(doer, repo),
  16. EnvIsInternal+"=true",
  17. )
  18. }
  19. // PushingEnvironment returns an os environment to allow hooks to work on push
  20. func PushingEnvironment(doer *User, repo *Repository) []string {
  21. return FullPushingEnvironment(doer, doer, repo, repo.Name, 0)
  22. }
  23. // FullPushingEnvironment returns an os environment to allow hooks to work on push
  24. func FullPushingEnvironment(author, committer *User, repo *Repository, repoName string, prID int64) []string {
  25. isWiki := "false"
  26. if strings.HasSuffix(repoName, ".wiki") {
  27. isWiki = "true"
  28. }
  29. authorSig := author.NewGitSig()
  30. committerSig := committer.NewGitSig()
  31. // We should add "SSH_ORIGINAL_COMMAND=gitea-internal",
  32. // once we have hook and pushing infrastructure working correctly
  33. return append(os.Environ(),
  34. "GIT_AUTHOR_NAME="+authorSig.Name,
  35. "GIT_AUTHOR_EMAIL="+authorSig.Email,
  36. "GIT_COMMITTER_NAME="+committerSig.Name,
  37. "GIT_COMMITTER_EMAIL="+committerSig.Email,
  38. EnvRepoName+"="+repoName,
  39. EnvRepoUsername+"="+repo.MustOwnerName(),
  40. EnvRepoIsWiki+"="+isWiki,
  41. EnvPusherName+"="+committer.Name,
  42. EnvPusherID+"="+fmt.Sprintf("%d", committer.ID),
  43. ProtectedBranchRepoID+"="+fmt.Sprintf("%d", repo.ID),
  44. ProtectedBranchPRID+"="+fmt.Sprintf("%d", prID),
  45. "SSH_ORIGINAL_COMMAND=gitea-internal",
  46. )
  47. }