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.5KB

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