You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.go 2.6 kB

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