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.

188 lines
4.8 KiB

  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. isDeployKey, _ := strconv.ParseBool(os.Getenv(models.EnvIsDeployKey))
  61. buf := bytes.NewBuffer(nil)
  62. scanner := bufio.NewScanner(os.Stdin)
  63. for scanner.Scan() {
  64. buf.Write(scanner.Bytes())
  65. buf.WriteByte('\n')
  66. // TODO: support news feeds for wiki
  67. if isWiki {
  68. continue
  69. }
  70. fields := bytes.Fields(scanner.Bytes())
  71. if len(fields) != 3 {
  72. continue
  73. }
  74. oldCommitID := string(fields[0])
  75. newCommitID := string(fields[1])
  76. refFullName := string(fields[2])
  77. // If the ref is a branch, check if it's protected
  78. if strings.HasPrefix(refFullName, git.BranchPrefix) {
  79. statusCode, msg := private.HookPreReceive(username, reponame, private.HookOptions{
  80. OldCommitID: oldCommitID,
  81. NewCommitID: newCommitID,
  82. RefFullName: refFullName,
  83. UserID: userID,
  84. GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories),
  85. GitObjectDirectory: os.Getenv(private.GitObjectDirectory),
  86. GitQuarantinePath: os.Getenv(private.GitQuarantinePath),
  87. ProtectedBranchID: prID,
  88. IsDeployKey: isDeployKey,
  89. })
  90. switch statusCode {
  91. case http.StatusInternalServerError:
  92. fail("Internal Server Error", msg)
  93. case http.StatusForbidden:
  94. fail(msg, "")
  95. }
  96. }
  97. }
  98. return nil
  99. }
  100. func runHookUpdate(c *cli.Context) error {
  101. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  102. return nil
  103. }
  104. setup("hooks/update.log")
  105. return nil
  106. }
  107. func runHookPostReceive(c *cli.Context) error {
  108. if len(os.Getenv("SSH_ORIGINAL_COMMAND")) == 0 {
  109. return nil
  110. }
  111. setup("hooks/post-receive.log")
  112. // the environment setted on serv command
  113. repoUser := os.Getenv(models.EnvRepoUsername)
  114. isWiki := (os.Getenv(models.EnvRepoIsWiki) == "true")
  115. repoName := os.Getenv(models.EnvRepoName)
  116. pusherID, _ := strconv.ParseInt(os.Getenv(models.EnvPusherID), 10, 64)
  117. pusherName := os.Getenv(models.EnvPusherName)
  118. buf := bytes.NewBuffer(nil)
  119. scanner := bufio.NewScanner(os.Stdin)
  120. for scanner.Scan() {
  121. buf.Write(scanner.Bytes())
  122. buf.WriteByte('\n')
  123. // TODO: support news feeds for wiki
  124. if isWiki {
  125. continue
  126. }
  127. fields := bytes.Fields(scanner.Bytes())
  128. if len(fields) != 3 {
  129. continue
  130. }
  131. oldCommitID := string(fields[0])
  132. newCommitID := string(fields[1])
  133. refFullName := string(fields[2])
  134. res, err := private.HookPostReceive(repoUser, repoName, private.HookOptions{
  135. OldCommitID: oldCommitID,
  136. NewCommitID: newCommitID,
  137. RefFullName: refFullName,
  138. UserID: pusherID,
  139. UserName: pusherName,
  140. })
  141. if res == nil {
  142. fail("Internal Server Error", err)
  143. }
  144. if res["message"] == false {
  145. continue
  146. }
  147. fmt.Fprintln(os.Stderr, "")
  148. if res["create"] == true {
  149. fmt.Fprintf(os.Stderr, "Create a new pull request for '%s':\n", res["branch"])
  150. fmt.Fprintf(os.Stderr, " %s\n", res["url"])
  151. } else {
  152. fmt.Fprint(os.Stderr, "Visit the existing pull request:\n")
  153. fmt.Fprintf(os.Stderr, " %s\n", res["url"])
  154. }
  155. fmt.Fprintln(os.Stderr, "")
  156. }
  157. return nil
  158. }