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.7 kB

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