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.

checkout.go 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package main
  2. /*
  3. Checkout a PR and load the tests data into sqlite database
  4. */
  5. import (
  6. "flag"
  7. "fmt"
  8. "io/ioutil"
  9. "log"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "os/exec"
  14. "os/user"
  15. "path"
  16. "path/filepath"
  17. "runtime"
  18. "strconv"
  19. "time"
  20. "code.gitea.io/gitea/modules/markup/external"
  21. "code.gitea.io/gitea/routers"
  22. "code.gitea.io/gitea/routers/routes"
  23. "github.com/Unknwon/com"
  24. "github.com/go-xorm/xorm"
  25. context2 "github.com/gorilla/context"
  26. "gopkg.in/src-d/go-git.v4"
  27. "gopkg.in/src-d/go-git.v4/config"
  28. "gopkg.in/src-d/go-git.v4/plumbing"
  29. "gopkg.in/testfixtures.v2"
  30. "code.gitea.io/gitea/models"
  31. "code.gitea.io/gitea/modules/setting"
  32. )
  33. var codeFilePath = "contrib/pr/checkout.go"
  34. func runPR() {
  35. log.Printf("[PR] Starting gitea ...\n")
  36. curDir, err := os.Getwd()
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. setting.SetCustomPathAndConf("", "")
  41. setting.NewContext()
  42. setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos")
  43. if err != nil {
  44. log.Fatalf("TempDir: %v\n", err)
  45. }
  46. setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata")
  47. if err != nil {
  48. log.Fatalf("TempDir: %v\n", err)
  49. }
  50. setting.AppWorkPath = curDir
  51. setting.StaticRootPath = curDir
  52. setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/")
  53. if err != nil {
  54. log.Fatalf("url.Parse: %v\n", err)
  55. }
  56. setting.AppURL = "http://localhost:8080/"
  57. setting.HTTPPort = "8080"
  58. setting.SSH.Domain = "localhost"
  59. setting.SSH.Port = 3000
  60. setting.InstallLock = true
  61. setting.SecretKey = "9pCviYTWSb"
  62. setting.InternalToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8"
  63. curUser, err := user.Current()
  64. if err != nil {
  65. log.Fatal(err)
  66. }
  67. setting.RunUser = curUser.Username
  68. log.Printf("[PR] Loading fixtures data ...\n")
  69. setting.CheckLFSVersion()
  70. //models.LoadConfigs()
  71. /*
  72. models.DbCfg.Type = "sqlite3"
  73. models.DbCfg.Path = ":memory:"
  74. models.DbCfg.Timeout = 500
  75. */
  76. db := setting.Cfg.Section("database")
  77. db.NewKey("DB_TYPE", "sqlite3")
  78. db.NewKey("PATH", ":memory:")
  79. setting.LogSQL = true
  80. models.LoadConfigs()
  81. routers.NewServices()
  82. //x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared")
  83. var helper testfixtures.Helper
  84. helper = &testfixtures.SQLite{}
  85. models.NewEngine(func(_ *xorm.Engine) error {
  86. return nil
  87. })
  88. models.HasEngine = true
  89. //x.ShowSQL(true)
  90. err = models.InitFixtures(
  91. helper,
  92. path.Join(curDir, "models/fixtures/"),
  93. )
  94. if err != nil {
  95. fmt.Printf("Error initializing test database: %v\n", err)
  96. os.Exit(1)
  97. }
  98. models.LoadFixtures()
  99. os.RemoveAll(setting.RepoRootPath)
  100. os.RemoveAll(models.LocalCopyPath())
  101. os.RemoveAll(models.LocalWikiPath())
  102. com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
  103. log.Printf("[PR] Setting up router\n")
  104. //routers.GlobalInit()
  105. external.RegisterParsers()
  106. m := routes.NewMacaron()
  107. routes.RegisterRoutes(m)
  108. log.Printf("[PR] Ready for testing !\n")
  109. log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n")
  110. /*
  111. log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL)
  112. if setting.LFS.StartServer {
  113. log.Info("LFS server enabled")
  114. }
  115. if setting.EnablePprof {
  116. go func() {
  117. log.Info("Starting pprof server on localhost:6060")
  118. log.Info("%v", http.ListenAndServe("localhost:6060", nil))
  119. }()
  120. }
  121. */
  122. //Start the server
  123. http.ListenAndServe(":8080", context2.ClearHandler(m))
  124. log.Printf("[PR] Cleaning up ...\n")
  125. /*
  126. if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil {
  127. fmt.Printf("os.RemoveAll: %v\n", err)
  128. os.Exit(1)
  129. }
  130. if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil {
  131. fmt.Printf("Unable to remove repo indexer: %v\n", err)
  132. os.Exit(1)
  133. }
  134. */
  135. if err = os.RemoveAll(setting.RepoRootPath); err != nil {
  136. log.Fatalf("os.RemoveAll: %v\n", err)
  137. }
  138. if err = os.RemoveAll(setting.AppDataPath); err != nil {
  139. log.Fatalf("os.RemoveAll: %v\n", err)
  140. }
  141. }
  142. func main() {
  143. var runPRFlag = flag.Bool("run", false, "Run the PR code")
  144. flag.Parse()
  145. if *runPRFlag {
  146. runPR()
  147. return
  148. }
  149. // To force checkout (e.g. Windows complains about unclean work tree) set env variable FORCE=true
  150. force, err := strconv.ParseBool(os.Getenv("FORCE"))
  151. if err != nil {
  152. force = false
  153. }
  154. //Otherwise checkout PR
  155. if len(os.Args) != 2 {
  156. log.Fatal("Need only one arg: the PR number")
  157. }
  158. pr := os.Args[1]
  159. codeFilePath = filepath.FromSlash(codeFilePath) //Convert to running OS
  160. //Copy this file if it will not exist in the PR branch
  161. dat, err := ioutil.ReadFile(codeFilePath)
  162. if err != nil {
  163. log.Fatalf("Failed to cache this code file : %v", err)
  164. }
  165. repo, err := git.PlainOpen(".")
  166. if err != nil {
  167. log.Fatalf("Failed to open the repo : %v", err)
  168. }
  169. //Find remote upstream
  170. remotes, err := repo.Remotes()
  171. if err != nil {
  172. log.Fatalf("Failed to list remotes of repo : %v", err)
  173. }
  174. remoteUpstream := "origin" //Default
  175. for _, r := range remotes {
  176. if r.Config().URLs[0] == "https://github.com/go-gitea/gitea" || r.Config().URLs[0] == "git@github.com:go-gitea/gitea.git" { //fetch at index 0
  177. remoteUpstream = r.Config().Name
  178. break
  179. }
  180. }
  181. branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix())
  182. branchRef := plumbing.NewBranchReferenceName(branch)
  183. log.Printf("Fetching PR #%s in %s\n", pr, branch)
  184. if runtime.GOOS == "windows" {
  185. //Use git cli command for windows
  186. runCmd("git", "fetch", remoteUpstream, fmt.Sprintf("pull/%s/head:%s", pr, branch))
  187. } else {
  188. ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef)
  189. err = repo.Fetch(&git.FetchOptions{
  190. RemoteName: remoteUpstream,
  191. RefSpecs: []config.RefSpec{
  192. config.RefSpec(ref),
  193. },
  194. })
  195. if err != nil {
  196. log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err)
  197. }
  198. }
  199. tree, err := repo.Worktree()
  200. if err != nil {
  201. log.Fatalf("Failed to parse git tree : %v", err)
  202. }
  203. log.Printf("Checkout PR #%s in %s\n", pr, branch)
  204. err = tree.Checkout(&git.CheckoutOptions{
  205. Branch: branchRef,
  206. Force: force,
  207. })
  208. if err != nil {
  209. log.Fatalf("Failed to checkout %s : %v", branch, err)
  210. }
  211. //Copy this file if not exist
  212. if _, err := os.Stat(codeFilePath); os.IsNotExist(err) {
  213. err = os.MkdirAll(filepath.Dir(codeFilePath), 0755)
  214. if err != nil {
  215. log.Fatalf("Failed to duplicate this code file in PR : %v", err)
  216. }
  217. err = ioutil.WriteFile(codeFilePath, dat, 0644)
  218. if err != nil {
  219. log.Fatalf("Failed to duplicate this code file in PR : %v", err)
  220. }
  221. }
  222. time.Sleep(5 * time.Second)
  223. //Start with integration test
  224. runCmd("go", "run", "-tags", "sqlite sqlite_unlock_notify", codeFilePath, "-run")
  225. }
  226. func runCmd(cmd ...string) {
  227. log.Printf("Executing : %s ...\n", cmd)
  228. c := exec.Command(cmd[0], cmd[1:]...)
  229. c.Stdout = os.Stdout
  230. c.Stderr = os.Stderr
  231. if err := c.Start(); err != nil {
  232. log.Panicln(err)
  233. }
  234. if err := c.Wait(); err != nil {
  235. log.Panicln(err)
  236. }
  237. }