本站源代码
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

91 linhas
2.8KB

  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 private
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. // Git environment variables
  13. const (
  14. GitAlternativeObjectDirectories = "GIT_ALTERNATE_OBJECT_DIRECTORIES"
  15. GitObjectDirectory = "GIT_OBJECT_DIRECTORY"
  16. GitQuarantinePath = "GIT_QUARANTINE_PATH"
  17. )
  18. // HookOptions represents the options for the Hook calls
  19. type HookOptions struct {
  20. OldCommitID string
  21. NewCommitID string
  22. RefFullName string
  23. UserID int64
  24. UserName string
  25. GitObjectDirectory string
  26. GitAlternativeObjectDirectories string
  27. GitQuarantinePath string
  28. ProtectedBranchID int64
  29. IsDeployKey bool
  30. }
  31. // HookPreReceive check whether the provided commits are allowed
  32. func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string) {
  33. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/pre-receive/%s/%s?old=%s&new=%s&ref=%s&userID=%d&gitObjectDirectory=%s&gitAlternativeObjectDirectories=%s&gitQuarantinePath=%s&prID=%d&isDeployKey=%t",
  34. url.PathEscape(ownerName),
  35. url.PathEscape(repoName),
  36. url.QueryEscape(opts.OldCommitID),
  37. url.QueryEscape(opts.NewCommitID),
  38. url.QueryEscape(opts.RefFullName),
  39. opts.UserID,
  40. url.QueryEscape(opts.GitObjectDirectory),
  41. url.QueryEscape(opts.GitAlternativeObjectDirectories),
  42. url.QueryEscape(opts.GitQuarantinePath),
  43. opts.ProtectedBranchID,
  44. opts.IsDeployKey,
  45. )
  46. resp, err := newInternalRequest(reqURL, "GET").Response()
  47. if err != nil {
  48. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  49. }
  50. defer resp.Body.Close()
  51. if resp.StatusCode != http.StatusOK {
  52. return resp.StatusCode, decodeJSONError(resp).Err
  53. }
  54. return http.StatusOK, ""
  55. }
  56. // HookPostReceive updates services and users
  57. func HookPostReceive(ownerName, repoName string, opts HookOptions) (map[string]interface{}, string) {
  58. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s?old=%s&new=%s&ref=%s&userID=%d&username=%s",
  59. url.PathEscape(ownerName),
  60. url.PathEscape(repoName),
  61. url.QueryEscape(opts.OldCommitID),
  62. url.QueryEscape(opts.NewCommitID),
  63. url.QueryEscape(opts.RefFullName),
  64. opts.UserID,
  65. url.QueryEscape(opts.UserName))
  66. resp, err := newInternalRequest(reqURL, "GET").Response()
  67. if err != nil {
  68. return nil, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  69. }
  70. defer resp.Body.Close()
  71. if resp.StatusCode != http.StatusOK {
  72. return nil, decodeJSONError(resp).Err
  73. }
  74. res := map[string]interface{}{}
  75. _ = json.NewDecoder(resp.Body).Decode(&res)
  76. return res, ""
  77. }
上海开阖软件有限公司 沪ICP备12045867号-1