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

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