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 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2017 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 cmd
  5. import (
  6. "bufio"
  7. "bytes"
  8. "fmt"
  9. "net/http"
  10. "os"
  11. "strconv"
  12. "strings"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/git"
  15. "code.gitea.io/gitea/modules/private"
  16. "github.com/urfave/cli"
  17. )
  18. var (
  19. // CmdHook represents the available hooks sub-command.
  20. CmdHook = cli.Command{
  21. Name: "hook",
  22. Usage: "Delegate commands to corresponding Git hooks",
  23. Description: "This should only be called by Git",
  24. Subcommands: []cli.Command{
  25. subcmdHookPreReceive,
  26. subcmdHookUpdate,
  27. subcmdHookPostReceive,
  28. },
  29. }
  30. subcmdHookPreReceive = cli.Command{
  31. Name: "pre-receive",
  32. Usage: "Delegate pre-receive Git hook",
  33. Description: "This command should only be called by Git",
  34. Action: runHookPreReceive,
  35. }
  36. subcmdHookUpdate = cli.Command{
  37. Name: "update",
  38. Usage: "Delegate update Git hook",
  39. Description: "This command should only be called by Git",
  40. Action: runHookUpdate,
  41. }
  42. subcmdHookPostReceive = cli.Command{
  43. Name: "post-receive",
  44. Usage: "Delegate post-receive Git hook",
  45. Description: "This command should only be called by Git",
  46. Action: runHookPostReceive,
  47. }
  48. )
  49. func runHookPreReceive(c *cli.Context) error {
  50. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  51. return nil
  52. }
  53. setup("hooks/pre-receive.log")
  54. // the environment setted on serv command
  55. isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
  56. username := os.Getenv(models.EnvRepoUsername)
  57. reponame := os.Getenv(models.EnvRepoName)
  58. userID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
  59. prID, _ := strconv.ParseInt(os.Getenv(models.ProtectedBranchPRID), 10, 64)
  60. buf := bytes.NewBuffer(nil)
  61. scanner := bufio.NewScanner(os.Stdin)
  62. for scanner.Scan() {
  63. buf.Write(scanner.Bytes())
  64. buf.WriteByte('\n')
  65. // TODO: support news feeds for wiki
  66. if isWiki {
  67. continue
  68. }
  69. fields := bytes.Fields(scanner.Bytes())
  70. if len(fields) != 3 {
  71. continue
  72. }
  73. oldCommitID := string(fields[0])
  74. newCommitID := string(fields[1])
  75. refFullName := string(fields[2])
  76. // If the ref is a branch, check if it's protected
  77. if strings.HasPrefix(refFullName, git.BranchPrefix) {
  78. statusCode, msg := private.HookPreReceive(username, reponame, private.HookOptions{
  79. OldCommitID: oldCommitID,
  80. NewCommitID: newCommitID,
  81. RefFullName: refFullName,
  82. UserID: userID,
  83. GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
  84. GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
  85. GitQuarantinePath: os.Getenv(private.GitQuarantinePath),
  86. ProtectedBranchID: prID,
  87. })
  88. switch statusCode {
  89. case http.StatusInternalServerError:
  90. fail("Internal Server Error", msg)
  91. case http.StatusForbidden:
  92. fail(msg, "")
  93. }
  94. }
  95. }
  96. return nil
  97. }
  98. func runHookUpdate(c *cli.Context) error {
  99. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  100. return nil
  101. }
  102. setup("hooks/update.log")
  103. return nil
  104. }
  105. func runHookPostReceive(c *cli.Context) error {
  106. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  107. return nil
  108. }
  109. setup("hooks/post-receive.log")
  110. // the environment setted on serv command
  111. repoUser := os.Getenv(models.EnvRepoUsername)
  112. isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
  113. repoName := os.Getenv(models.EnvRepoName)
  114. pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
  115. pusherName := os.Getenv(models.EnvPusherName)
  116. buf := bytes.NewBuffer(nil)
  117. scanner := bufio.NewScanner(os.Stdin)
  118. for scanner.Scan() {
  119. buf.Write(scanner.Bytes())
  120. buf.WriteByte('\n')
  121. // TODO: support news feeds for wiki
  122. if isWiki {
  123. continue
  124. }
  125. fields := bytes.Fields(scanner.Bytes())
  126. if len(fields) != 3 {
  127. continue
  128. }
  129. oldCommitID := string(fields[0])
  130. newCommitID := string(fields[1])
  131. refFullName := string(fields[2])
  132. res, err := private.HookPostReceive(repoUser, repoName, private.HookOptions{
  133. OldCommitID: oldCommitID,
  134. NewCommitID: newCommitID,
  135. RefFullName: refFullName,
  136. UserID: pusherID,
  137. UserName: pusherName,
  138. })
  139. if res == nil {
  140. fail("Internal Server Error", err)
  141. }
  142. if res["message"] == false {
  143. continue
  144. }
  145. fmt.Fprintln(os.Stderr, "")
  146. if res["create"] == true {
  147. fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res["branch"])
  148. fmt.Fprintf(os.Stderr, " %s\n", res["url"])
  149. } else {
  150. fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
  151. fmt.Fprintf(os.Stderr, " %s\n", res["url"])
  152. }
  153. fmt.Fprintln(os.Stderr, "")
  154. }
  155. return nil
  156. }