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.

hook.go 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. }
  30. // HookPreReceive check whether the provided commits are allowed
  31. func HookPreReceive(ownerName, repoName string, opts HookOptions) (int, string) {
  32. 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",
  33. url.PathEscape(ownerName),
  34. url.PathEscape(repoName),
  35. url.QueryEscape(opts.OldCommitID),
  36. url.QueryEscape(opts.NewCommitID),
  37. url.QueryEscape(opts.RefFullName),
  38. opts.UserID,
  39. url.QueryEscape(opts.GitObjectDirectory),
  40. url.QueryEscape(opts.GitAlternativeObjectDirectories),
  41. url.QueryEscape(opts.GitQuarantinePath),
  42. opts.ProtectedBranchID,
  43. )
  44. resp, err := newInternalRequest(reqURL, "GET").Response()
  45. if err != nil {
  46. return http.StatusInternalServerError, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  47. }
  48. defer resp.Body.Close()
  49. if resp.StatusCode != http.StatusOK {
  50. return resp.StatusCode, decodeJSONError(resp).Err
  51. }
  52. return http.StatusOK, ""
  53. }
  54. // HookPostReceive updates services and users
  55. func HookPostReceive(ownerName, repoName string, opts HookOptions) (map[string]interface{}, string) {
  56. reqURL := setting.LocalURL + fmt.Sprintf("api/internal/hook/post-receive/%s/%s?old=%s&new=%s&ref=%s&userID=%d&username=%s",
  57. url.PathEscape(ownerName),
  58. url.PathEscape(repoName),
  59. url.QueryEscape(opts.OldCommitID),
  60. url.QueryEscape(opts.NewCommitID),
  61. url.QueryEscape(opts.RefFullName),
  62. opts.UserID,
  63. url.QueryEscape(opts.UserName))
  64. resp, err := newInternalRequest(reqURL, "GET").Response()
  65. if err != nil {
  66. return nil, fmt.Sprintf("Unable to contact gitea: %v", err.Error())
  67. }
  68. defer resp.Body.Close()
  69. if resp.StatusCode != http.StatusOK {
  70. return nil, decodeJSONError(resp).Err
  71. }
  72. res := map[string]interface{}{}
  73. _ = json.NewDecoder(resp.Body).Decode(&res)
  74. return res, ""
  75. }