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