From da8e9a8ebdcf693ab708718d4cf28c7a792e3e85 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 8 Feb 2019 00:28:40 +0100 Subject: [PATCH 01/25] init --- contrib/pr/checkout.go | 171 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 contrib/pr/checkout.go diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go new file mode 100644 index 0000000000000..e49533cb259ed --- /dev/null +++ b/contrib/pr/checkout.go @@ -0,0 +1,171 @@ +package main + +/* +Checkout a PR and load the tests data into sqlite database +*/ + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "os/exec" + "os/user" + "path/filepath" + "runtime" + "time" + + "code.gitea.io/git" +) + +var envVars = []string{ + "TAGS=bindata sqlite sqlite_unlock_notify", +} + +func main() { + + if len(os.Args) != 2 { + log.Fatal("Need only one arg: the PR number") + } + pr := os.Args[1] + + branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) + log.Printf("Checkout PR #%s in %s\n", pr, branch) + + exec.Command("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)).Run() + + err := git.Checkout(".", git.CheckoutOptions{ + Branch: branch, + }) + if err != nil { + log.Fatalf("Failed to checkout pr-%s : %v", pr, err) + } + + log.Printf("Building ...\n") + run("make", "clean") + run("make", "generate") + run("make", "build") + + log.Printf("Setting up testing env ...\n") + curDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } + runDir, err := ioutil.TempDir("", "gitea-"+branch) + if err != nil { + log.Fatal(err) + } + defer func() { + err := os.RemoveAll(runDir) // clean up + if err != nil { + log.Fatal(err) + } + }() + + bin := "gitea" + if runtime.GOOS == "windows" { + bin = "gitea.exe" + } + //Copy binary + exec.Command("cp", "-a", bin, filepath.Join(runDir, bin)).Run() + /* + err = os.Rename("gitea", filepath.Join(runDir, "gitea")) + if err != nil { + log.Fatal(err) + } + */ + curUser, err := user.Current() + if err != nil { + log.Fatal(err) + } + //Write config file + content := []byte(` +APP_NAME = Gitea: Git with a cup of tea +RUN_USER = ` + curUser.Username + ` +RUN_MODE = prod + +[security] +INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NDk0ODIwNTN9.iHCXDtMYKSSceKL5LGrY8gcqSwVoNxQawlas799_8lM +INSTALL_LOCK = true +SECRET_KEY = nLGL8s8ODKfnhvebTD5VnOD41dvrpfhg8pYmIxWFhUskme8ni1wuWz7YCvAXSrp5 + +[database] +DB_TYPE = sqlite3 +PATH = ./data/gitea.db + +[repository] +ROOT = ./repositories + +[server] +SSH_DOMAIN = localhost +DOMAIN = localhost +HTTP_PORT = 3000 +ROOT_URL = http://localhost:3000/ +DISABLE_SSH = false +SSH_PORT = 22 +LFS_START_SERVER = true +LFS_CONTENT_PATH = ./data/lfs +LFS_JWT_SECRET = QGraF0QfWLE7C_ykSyU_GsJMhShIzis3VkrR07Uw2ww +OFFLINE_MODE = true + +[mailer] +ENABLED = false + +[service] +REGISTER_EMAIL_CONFIRM = false +ENABLE_NOTIFY_MAIL = false +DISABLE_REGISTRATION = true +ALLOW_ONLY_EXTERNAL_REGISTRATION = false +ENABLE_CAPTCHA = false +REQUIRE_SIGNIN_VIEW = false +DEFAULT_KEEP_EMAIL_PRIVATE = false +DEFAULT_ALLOW_CREATE_ORGANIZATION = true +DEFAULT_ENABLE_TIMETRACKING = true +NO_REPLY_ADDRESS = noreply.example.org + +[picture] +DISABLE_GRAVATAR = true +ENABLE_FEDERATED_AVATAR = false + +[openid] +ENABLE_OPENID_SIGNIN = true +ENABLE_OPENID_SIGNUP = false + +[session] +PROVIDER = file + +[log] +MODE = file +LEVEL = Info +ROOT_PATH = ./log +`) + err = os.MkdirAll(filepath.Join(runDir, "custom/conf"), 0755) + if err != nil { + log.Fatal(err) + } + confFile := filepath.Join(runDir, "custom/conf/app.ini") + if err := ioutil.WriteFile(confFile, content, 0644); err != nil { + log.Fatal(err) + } + + log.Printf("Moving to env ready at '%s' ...\n", runDir) + os.Chdir(runDir) + + log.Printf("Starting gitea ...\n") + log.Printf("User: gitea\n") + log.Printf("Pass: gitea\n") + log.Printf("Mail: gitea@localhost\n") + log.Printf("End the execution by doing $> pkill gitea\n") + run("./"+bin, "web") + + log.Printf("Moving back to gitea repo at '%s' ...\n", curDir) + os.Chdir(curDir) +} +func run(cmd ...string) { + log.Printf("Executing : %s ...\n", cmd) + c := exec.Command(cmd[0], cmd[1:]...) + c.Env = append(os.Environ(), envVars...) + if err := c.Run(); err != nil { + log.Panicln(err) + } +} From 883abcf2425039df53601a81dfd3b5ec149c6f03 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 8 Feb 2019 00:34:44 +0100 Subject: [PATCH 02/25] config user --- contrib/pr/checkout.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index e49533cb259ed..d90324d3ddeb9 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -156,6 +156,14 @@ ROOT_PATH = ./log log.Printf("Pass: gitea\n") log.Printf("Mail: gitea@localhost\n") log.Printf("End the execution by doing $> pkill gitea\n") + + //Prepare setup + go func() { + time.Sleep(5 * time.Second) + log.Printf("Starting gitea setup...\n") + run("./"+bin, "admin", "create-user", "--admin", "--name", "gitea", "--password", "gitea", "--email", "gitea@localhost") + }() + run("./"+bin, "web") log.Printf("Moving back to gitea repo at '%s' ...\n", curDir) From ac9d1b18405b7a45eedfb6d87226bc5c7d773ff0 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Fri, 8 Feb 2019 00:41:51 +0100 Subject: [PATCH 03/25] add todo --- contrib/pr/checkout.go | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index d90324d3ddeb9..4dafdcb77c8e6 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -165,6 +165,7 @@ ROOT_PATH = ./log }() run("./"+bin, "web") + //TODO better use https://github.com/go-gitea/gitea/blob/6759237eda5b7ddfe9284c81900cc9deed1f6bf9/models/unit_tests.go#L39 log.Printf("Moving back to gitea repo at '%s' ...\n", curDir) os.Chdir(curDir) From 557c3bc47d0c84f8c8808e6dd380aa8612e064b0 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 16:47:35 +0100 Subject: [PATCH 04/25] Switch method to re-call program with -run flag --- contrib/pr/checkout.go | 238 ++++++++++++++++++----------------------- models/unit_tests.go | 27 +++-- 2 files changed, 120 insertions(+), 145 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 4dafdcb77c8e6..f05cbd5e80f16 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -5,175 +5,141 @@ Checkout a PR and load the tests data into sqlite database */ import ( + "flag" "fmt" - "io/ioutil" "log" + "net/http" "os" "os/exec" "os/user" + "path" "path/filepath" - "runtime" "time" + "code.gitea.io/gitea/modules/markup/external" + "code.gitea.io/gitea/routers" + "code.gitea.io/gitea/routers/routes" + "github.com/Unknwon/com" + "github.com/facebookgo/grace/gracehttp" + context2 "github.com/gorilla/context" + "gopkg.in/testfixtures.v2" + "code.gitea.io/git" + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/setting" ) -var envVars = []string{ - "TAGS=bindata sqlite sqlite_unlock_notify", -} - -func main() { - - if len(os.Args) != 2 { - log.Fatal("Need only one arg: the PR number") - } - pr := os.Args[1] - - branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) - log.Printf("Checkout PR #%s in %s\n", pr, branch) - - exec.Command("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)).Run() - - err := git.Checkout(".", git.CheckoutOptions{ - Branch: branch, - }) +func runPR() { + log.Printf("[PR] Starting gitea ...\n") + setting.NewContext() + models.MainTestSetup(".") + setting.AppURL = "http://localhost:8080/" + setting.HTTPPort = "8080" + setting.SSH.Domain = "localhost" + setting.SSH.Port = 3000 + setting.InstallLock = true + setting.SecretKey = "9pCviYTWSb" + setting.InternalToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0OTI3OTU5ODN9.OQkH5UmzID2XBdwQ9TAI6Jj2t1X-wElVTjbE7aoN4I8" + curUser, err := user.Current() if err != nil { - log.Fatalf("Failed to checkout pr-%s : %v", pr, err) + log.Fatal(err) } - - log.Printf("Building ...\n") - run("make", "clean") - run("make", "generate") - run("make", "build") - - log.Printf("Setting up testing env ...\n") + setting.RunUser = curUser.Username curDir, err := os.Getwd() if err != nil { log.Fatal(err) } - runDir, err := ioutil.TempDir("", "gitea-"+branch) + + log.Printf("[PR] Loading fixtures data ...\n") + var helper testfixtures.Helper + helper = &testfixtures.SQLite{} + err = models.InitFixtures( + helper, + path.Join(curDir, "models/fixtures/"), + ) if err != nil { - log.Fatal(err) + fmt.Printf("Error initializing test database: %v\n", err) + os.Exit(1) } - defer func() { - err := os.RemoveAll(runDir) // clean up - if err != nil { - log.Fatal(err) + models.LoadFixtures() + os.RemoveAll(setting.RepoRootPath) + os.RemoveAll(models.LocalCopyPath()) + os.RemoveAll(models.LocalWikiPath()) + com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath) + + log.Printf("[PR] Setting up router\n") + setting.CheckLFSVersion() + models.LoadConfigs() + //routers.GlobalInit() + routers.NewServices() + external.RegisterParsers() + m := routes.NewMacaron() + routes.RegisterRoutes(m) + + log.Printf("[PR] Ready for testing !\n") + log.Printf("[PR] Login with user1, user2, user3, ... with pass: passsword\n") + /* + log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL) + + if setting.LFS.StartServer { + log.Info("LFS server enabled") } - }() - bin := "gitea" - if runtime.GOOS == "windows" { - bin = "gitea.exe" - } - //Copy binary - exec.Command("cp", "-a", bin, filepath.Join(runDir, bin)).Run() - /* - err = os.Rename("gitea", filepath.Join(runDir, "gitea")) - if err != nil { - log.Fatal(err) + if setting.EnablePprof { + go func() { + log.Info("Starting pprof server on localhost:6060") + log.Info("%v", http.ListenAndServe("localhost:6060", nil)) + }() } */ - curUser, err := user.Current() - if err != nil { - log.Fatal(err) - } - //Write config file - content := []byte(` -APP_NAME = Gitea: Git with a cup of tea -RUN_USER = ` + curUser.Username + ` -RUN_MODE = prod - -[security] -INTERNAL_TOKEN = eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE1NDk0ODIwNTN9.iHCXDtMYKSSceKL5LGrY8gcqSwVoNxQawlas799_8lM -INSTALL_LOCK = true -SECRET_KEY = nLGL8s8ODKfnhvebTD5VnOD41dvrpfhg8pYmIxWFhUskme8ni1wuWz7YCvAXSrp5 - -[database] -DB_TYPE = sqlite3 -PATH = ./data/gitea.db - -[repository] -ROOT = ./repositories - -[server] -SSH_DOMAIN = localhost -DOMAIN = localhost -HTTP_PORT = 3000 -ROOT_URL = http://localhost:3000/ -DISABLE_SSH = false -SSH_PORT = 22 -LFS_START_SERVER = true -LFS_CONTENT_PATH = ./data/lfs -LFS_JWT_SECRET = QGraF0QfWLE7C_ykSyU_GsJMhShIzis3VkrR07Uw2ww -OFFLINE_MODE = true - -[mailer] -ENABLED = false - -[service] -REGISTER_EMAIL_CONFIRM = false -ENABLE_NOTIFY_MAIL = false -DISABLE_REGISTRATION = true -ALLOW_ONLY_EXTERNAL_REGISTRATION = false -ENABLE_CAPTCHA = false -REQUIRE_SIGNIN_VIEW = false -DEFAULT_KEEP_EMAIL_PRIVATE = false -DEFAULT_ALLOW_CREATE_ORGANIZATION = true -DEFAULT_ENABLE_TIMETRACKING = true -NO_REPLY_ADDRESS = noreply.example.org - -[picture] -DISABLE_GRAVATAR = true -ENABLE_FEDERATED_AVATAR = false - -[openid] -ENABLE_OPENID_SIGNIN = true -ENABLE_OPENID_SIGNUP = false - -[session] -PROVIDER = file + gracehttp.Serve(&http.Server{ + Addr: ":8080", + Handler: context2.ClearHandler(m), + }) + //time.Sleep(5 * time.Minute) //TODO wait for input -[log] -MODE = file -LEVEL = Info -ROOT_PATH = ./log -`) - err = os.MkdirAll(filepath.Join(runDir, "custom/conf"), 0755) - if err != nil { - log.Fatal(err) + log.Printf("[PR] Cleaning up ...\n") + if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil { + fmt.Printf("os.RemoveAll: %v\n", err) + os.Exit(1) } - confFile := filepath.Join(runDir, "custom/conf/app.ini") - if err := ioutil.WriteFile(confFile, content, 0644); err != nil { - log.Fatal(err) + if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil { + fmt.Printf("Unable to remove repo indexer: %v\n", err) + os.Exit(1) } + models.MainTestCleanup(0) +} - log.Printf("Moving to env ready at '%s' ...\n", runDir) - os.Chdir(runDir) - - log.Printf("Starting gitea ...\n") - log.Printf("User: gitea\n") - log.Printf("Pass: gitea\n") - log.Printf("Mail: gitea@localhost\n") - log.Printf("End the execution by doing $> pkill gitea\n") +func main() { + var runPRFlag = flag.Bool("run", false, "Run the PR code") + flag.Parse() + if *runPRFlag { + runPR() + return + } - //Prepare setup - go func() { - time.Sleep(5 * time.Second) - log.Printf("Starting gitea setup...\n") - run("./"+bin, "admin", "create-user", "--admin", "--name", "gitea", "--password", "gitea", "--email", "gitea@localhost") - }() + //Otherwise checkout PR + if len(os.Args) != 2 { + log.Fatal("Need only one arg: the PR number") + } + pr := os.Args[1] - run("./"+bin, "web") - //TODO better use https://github.com/go-gitea/gitea/blob/6759237eda5b7ddfe9284c81900cc9deed1f6bf9/models/unit_tests.go#L39 + branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) + log.Printf("Checkout PR #%s in %s\n", pr, branch) + runCmd("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)) + err := git.Checkout(".", git.CheckoutOptions{ + Branch: branch, + }) + if err != nil { + log.Fatalf("Failed to checkout pr-%s : %v", pr, err) + } - log.Printf("Moving back to gitea repo at '%s' ...\n", curDir) - os.Chdir(curDir) + //Start with integration test + runCmd("go", "run", "-tags='sqlite sqlite_unlock_notify'", "contrib/pr/checkout.go", "-run") } -func run(cmd ...string) { +func runCmd(cmd ...string) { log.Printf("Executing : %s ...\n", cmd) c := exec.Command(cmd[0], cmd[1:]...) - c.Env = append(os.Environ(), envVars...) if err := c.Run(); err != nil { log.Panicln(err) } diff --git a/models/unit_tests.go b/models/unit_tests.go index 28cd91215edc0..9efca6925a9ff 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -37,6 +37,24 @@ func fatalTestError(fmtStr string, args ...interface{}) { // MainTest a reusable TestMain(..) function for unit tests that need to use a // test database. Creates the test database, and sets necessary settings. func MainTest(m *testing.M, pathToGiteaRoot string) { + MainTestSetup(pathToGiteaRoot) + MainTestCleanup(m.Run()) +} + +//MainTestCleanup Clean up folder after testing +func MainTestCleanup(exitStatus int) { + var err error + if err = removeAllWithRetry(setting.RepoRootPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + if err = removeAllWithRetry(setting.AppDataPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + os.Exit(exitStatus) +} + +//MainTestSetup Setup setting and folder for testing +func MainTestSetup(pathToGiteaRoot string) { var err error giteaRoot = pathToGiteaRoot fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures") @@ -63,15 +81,6 @@ func MainTest(m *testing.M, pathToGiteaRoot string) { if err != nil { fatalTestError("url.Parse: %v\n", err) } - - exitStatus := m.Run() - if err = removeAllWithRetry(setting.RepoRootPath); err != nil { - fatalTestError("os.RemoveAll: %v\n", err) - } - if err = removeAllWithRetry(setting.AppDataPath); err != nil { - fatalTestError("os.RemoveAll: %v\n", err) - } - os.Exit(exitStatus) } func createTestEngine(fixturesDir string) error { From 6e5d8e249e1dcc4fc797f5bf2c786b53ea035836 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 16:55:33 +0100 Subject: [PATCH 05/25] fix meta path --- contrib/pr/checkout.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index f05cbd5e80f16..0e0305a3df84b 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -13,7 +13,6 @@ import ( "os/exec" "os/user" "path" - "path/filepath" "time" "code.gitea.io/gitea/modules/markup/external" @@ -65,7 +64,7 @@ func runPR() { os.RemoveAll(setting.RepoRootPath) os.RemoveAll(models.LocalCopyPath()) os.RemoveAll(models.LocalWikiPath()) - com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath) + com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath) log.Printf("[PR] Setting up router\n") setting.CheckLFSVersion() From 36e81b98ff4f5ea2c3380fb3d25be50d4cb9d83d Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 17:05:31 +0100 Subject: [PATCH 06/25] be more resilient --- contrib/pr/checkout.go | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 0e0305a3df84b..1456b54dc36ad 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -7,12 +7,14 @@ Checkout a PR and load the tests data into sqlite database import ( "flag" "fmt" + "io/ioutil" "log" "net/http" "os" "os/exec" "os/user" "path" + "path/filepath" "time" "code.gitea.io/gitea/modules/markup/external" @@ -28,6 +30,8 @@ import ( "code.gitea.io/gitea/modules/setting" ) +var codeFilePath = "contrib/pr/checkout.go" + func runPR() { log.Printf("[PR] Starting gitea ...\n") setting.NewContext() @@ -123,18 +127,36 @@ func main() { } pr := os.Args[1] + //Copy this file if it will not exist in the PR branch + dat, err := ioutil.ReadFile(codeFilePath) + if err != nil { + log.Fatalf("Failed to cache this code file : %v", err) + } + branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) log.Printf("Checkout PR #%s in %s\n", pr, branch) runCmd("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)) - err := git.Checkout(".", git.CheckoutOptions{ + err = git.Checkout(".", git.CheckoutOptions{ Branch: branch, }) if err != nil { log.Fatalf("Failed to checkout pr-%s : %v", pr, err) } + //Copy this file if not exist + if _, err := os.Stat(codeFilePath); os.IsNotExist(err) { + err = os.MkdirAll(filepath.Dir(codeFilePath), 0755) + if err != nil { + log.Fatalf("Failed to duplicate this code file in PR : %v", err) + } + err = ioutil.WriteFile(codeFilePath, dat, 0644) + if err != nil { + log.Fatalf("Failed to duplicate this code file in PR : %v", err) + } + } + //Start with integration test - runCmd("go", "run", "-tags='sqlite sqlite_unlock_notify'", "contrib/pr/checkout.go", "-run") + runCmd("go", "run", "-tags='sqlite sqlite_unlock_notify'", codeFilePath, "-run") } func runCmd(cmd ...string) { log.Printf("Executing : %s ...\n", cmd) From 9f10a239b3368044e424340e464a47f59ea55de5 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 17:39:40 +0100 Subject: [PATCH 07/25] don't depends on external change --- contrib/pr/checkout.go | 71 ++++++++++++++++++++++++++++++++---------- models/unit_tests.go | 27 ++++++---------- 2 files changed, 64 insertions(+), 34 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 1456b54dc36ad..ce05a8edfec46 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -10,6 +10,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "os" "os/exec" "os/user" @@ -22,6 +23,7 @@ import ( "code.gitea.io/gitea/routers/routes" "github.com/Unknwon/com" "github.com/facebookgo/grace/gracehttp" + "github.com/go-xorm/xorm" context2 "github.com/gorilla/context" "gopkg.in/testfixtures.v2" @@ -34,8 +36,27 @@ var codeFilePath = "contrib/pr/checkout.go" func runPR() { log.Printf("[PR] Starting gitea ...\n") + curDir, err := os.Getwd() + if err != nil { + log.Fatal(err) + } setting.NewContext() - models.MainTestSetup(".") + + setting.RepoRootPath, err = ioutil.TempDir(os.TempDir(), "repos") + if err != nil { + log.Fatalf("TempDir: %v\n", err) + } + setting.AppDataPath, err = ioutil.TempDir(os.TempDir(), "appdata") + if err != nil { + log.Fatalf("TempDir: %v\n", err) + } + setting.AppWorkPath = curDir + setting.StaticRootPath = curDir + setting.GravatarSourceURL, err = url.Parse("https://secure.gravatar.com/avatar/") + if err != nil { + log.Fatalf("url.Parse: %v\n", err) + } + setting.AppURL = "http://localhost:8080/" setting.HTTPPort = "8080" setting.SSH.Domain = "localhost" @@ -48,14 +69,29 @@ func runPR() { log.Fatal(err) } setting.RunUser = curUser.Username - curDir, err := os.Getwd() - if err != nil { - log.Fatal(err) - } log.Printf("[PR] Loading fixtures data ...\n") + setting.CheckLFSVersion() + //models.LoadConfigs() + /* + models.DbCfg.Type = "sqlite3" + models.DbCfg.Path = ":memory:" + models.DbCfg.Timeout = 500 + */ + db := setting.Cfg.Section("database") + db.NewKey("DB_TYPE", "sqlite3") + db.NewKey("PATH", ":memory:") + setting.LogSQL = true + models.LoadConfigs() + routers.NewServices() + //x, err = xorm.NewEngine("sqlite3", "file::memory:?cache=shared") + var helper testfixtures.Helper helper = &testfixtures.SQLite{} + models.NewEngine(func(_ *xorm.Engine) error { + return nil + }) + //x.ShowSQL(true) err = models.InitFixtures( helper, path.Join(curDir, "models/fixtures/"), @@ -71,10 +107,7 @@ func runPR() { com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath) log.Printf("[PR] Setting up router\n") - setting.CheckLFSVersion() - models.LoadConfigs() //routers.GlobalInit() - routers.NewServices() external.RegisterParsers() m := routes.NewMacaron() routes.RegisterRoutes(m) @@ -99,18 +132,24 @@ func runPR() { Addr: ":8080", Handler: context2.ClearHandler(m), }) - //time.Sleep(5 * time.Minute) //TODO wait for input log.Printf("[PR] Cleaning up ...\n") - if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil { - fmt.Printf("os.RemoveAll: %v\n", err) - os.Exit(1) + /* + if err = os.RemoveAll(setting.Indexer.IssuePath); err != nil { + fmt.Printf("os.RemoveAll: %v\n", err) + os.Exit(1) + } + if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil { + fmt.Printf("Unable to remove repo indexer: %v\n", err) + os.Exit(1) + } + */ + if err = os.RemoveAll(setting.RepoRootPath); err != nil { + log.Fatalf("os.RemoveAll: %v\n", err) } - if err = os.RemoveAll(setting.Indexer.RepoPath); err != nil { - fmt.Printf("Unable to remove repo indexer: %v\n", err) - os.Exit(1) + if err = os.RemoveAll(setting.AppDataPath); err != nil { + log.Fatalf("os.RemoveAll: %v\n", err) } - models.MainTestCleanup(0) } func main() { diff --git a/models/unit_tests.go b/models/unit_tests.go index 9efca6925a9ff..28cd91215edc0 100644 --- a/models/unit_tests.go +++ b/models/unit_tests.go @@ -37,24 +37,6 @@ func fatalTestError(fmtStr string, args ...interface{}) { // MainTest a reusable TestMain(..) function for unit tests that need to use a // test database. Creates the test database, and sets necessary settings. func MainTest(m *testing.M, pathToGiteaRoot string) { - MainTestSetup(pathToGiteaRoot) - MainTestCleanup(m.Run()) -} - -//MainTestCleanup Clean up folder after testing -func MainTestCleanup(exitStatus int) { - var err error - if err = removeAllWithRetry(setting.RepoRootPath); err != nil { - fatalTestError("os.RemoveAll: %v\n", err) - } - if err = removeAllWithRetry(setting.AppDataPath); err != nil { - fatalTestError("os.RemoveAll: %v\n", err) - } - os.Exit(exitStatus) -} - -//MainTestSetup Setup setting and folder for testing -func MainTestSetup(pathToGiteaRoot string) { var err error giteaRoot = pathToGiteaRoot fixturesDir := filepath.Join(pathToGiteaRoot, "models", "fixtures") @@ -81,6 +63,15 @@ func MainTestSetup(pathToGiteaRoot string) { if err != nil { fatalTestError("url.Parse: %v\n", err) } + + exitStatus := m.Run() + if err = removeAllWithRetry(setting.RepoRootPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + if err = removeAllWithRetry(setting.AppDataPath); err != nil { + fatalTestError("os.RemoveAll: %v\n", err) + } + os.Exit(exitStatus) } func createTestEngine(fixturesDir string) error { From e2fdc422faa43b42360f63df501a9551088b9a81 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 17:42:06 +0100 Subject: [PATCH 08/25] add time sleep --- contrib/pr/checkout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index ce05a8edfec46..c20809a524951 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -193,7 +193,7 @@ func main() { log.Fatalf("Failed to duplicate this code file in PR : %v", err) } } - + time.Sleep(5 * time.Second) //Start with integration test runCmd("go", "run", "-tags='sqlite sqlite_unlock_notify'", codeFilePath, "-run") } From f41037741089d96abad533e300c9adbaf831aad8 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 17:44:24 +0100 Subject: [PATCH 09/25] fix args --- contrib/pr/checkout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index c20809a524951..37cf1a984b911 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -195,7 +195,7 @@ func main() { } time.Sleep(5 * time.Second) //Start with integration test - runCmd("go", "run", "-tags='sqlite sqlite_unlock_notify'", codeFilePath, "-run") + runCmd("go", "run", "-tags", "sqlite sqlite_unlock_notify", codeFilePath, "-run") } func runCmd(cmd ...string) { log.Printf("Executing : %s ...\n", cmd) From 480a92982617028da15b3693efc8e9b082d5343b Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sat, 9 Feb 2019 17:51:45 +0100 Subject: [PATCH 10/25] Display output --- contrib/pr/checkout.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 37cf1a984b911..cfd594ae7e435 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -200,7 +200,17 @@ func main() { func runCmd(cmd ...string) { log.Printf("Executing : %s ...\n", cmd) c := exec.Command(cmd[0], cmd[1:]...) - if err := c.Run(); err != nil { + stdout, err := c.StdoutPipe() + if err != nil { + log.Panicln(err) + } + if err := c.Start(); err != nil { + log.Panicln(err) + } + if err := c.Wait(); err != nil { log.Panicln(err) } + var b []byte + stdout.Read(b) + log.Println("Log: " + string(b)) //TODO stream to output } From a9927f81fc6066c3c74ffe74006d4b34d438fbc4 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 17:39:12 +0100 Subject: [PATCH 11/25] Find the upstream + use src-d/go-git --- contrib/pr/checkout.go | 46 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index cfd594ae7e435..025d685a1b643 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -25,9 +25,11 @@ import ( "github.com/facebookgo/grace/gracehttp" "github.com/go-xorm/xorm" context2 "github.com/gorilla/context" + "gopkg.in/src-d/go-git.v4" + "gopkg.in/src-d/go-git.v4/config" + "gopkg.in/src-d/go-git.v4/plumbing" "gopkg.in/testfixtures.v2" - "code.gitea.io/git" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/setting" ) @@ -152,6 +154,8 @@ func runPR() { } } +//TODO Add clean branch pr- + func main() { var runPRFlag = flag.Bool("run", false, "Run the PR code") flag.Parse() @@ -172,14 +176,46 @@ func main() { log.Fatalf("Failed to cache this code file : %v", err) } + repo, err := git.PlainOpen(".") + if err != nil { + log.Fatalf("Failed to open the repo : %v", err) + } + + //Find remote upstream + remotes, err := repo.Remotes() + if err != nil { + log.Fatalf("Failed to list remotes of repo : %v", err) + } + remoteBranch := "origin" //Default + for _, r := range remotes { + 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 + remoteBranch = r.Config().Name + break + } + } + branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) log.Printf("Checkout PR #%s in %s\n", pr, branch) - runCmd("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)) - err = git.Checkout(".", git.CheckoutOptions{ - Branch: branch, + + err = repo.Fetch(&git.FetchOptions{ + RemoteName: remoteBranch, + RefSpecs: []config.RefSpec{ + config.RefSpec(fmt.Sprintf("pull/%s/head:%s", pr, branch)), + }, + }) + if err != nil { + log.Fatalf("Failed to fetch pr-%s : %v", pr, err) + } + + tree, err := repo.Worktree() + if err != nil { + log.Fatalf("Failed to parse git tree : %v", err) + } + err = tree.Checkout(&git.CheckoutOptions{ + Branch: plumbing.ReferenceName(branch), }) if err != nil { - log.Fatalf("Failed to checkout pr-%s : %v", pr, err) + log.Fatalf("Failed to checkout %s : %v", branch, err) } //Copy this file if not exist From 0307340c4a9624ec13ef415c1687ea212c3e2294 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 17:41:27 +0100 Subject: [PATCH 12/25] stream output of command. thx @jolheiser --- contrib/pr/checkout.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 025d685a1b643..5e4fa7455d61e 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -236,17 +236,12 @@ func main() { func runCmd(cmd ...string) { log.Printf("Executing : %s ...\n", cmd) c := exec.Command(cmd[0], cmd[1:]...) - stdout, err := c.StdoutPipe() - if err != nil { - log.Panicln(err) - } + c.Stdout = os.Stdout + c.Stderr = os.Stderr if err := c.Start(); err != nil { log.Panicln(err) } if err := c.Wait(); err != nil { log.Panicln(err) } - var b []byte - stdout.Read(b) - log.Println("Log: " + string(b)) //TODO stream to output } From b103f8523696bce945c5fb21f5392551c5d60cb4 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 17:42:27 +0100 Subject: [PATCH 13/25] Fix auth by @jolheiser --- contrib/pr/checkout.go | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 5e4fa7455d61e..5d5e2177b83bd 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -93,6 +93,7 @@ func runPR() { models.NewEngine(func(_ *xorm.Engine) error { return nil }) + models.HasEngine = true //x.ShowSQL(true) err = models.InitFixtures( helper, From 0ec9d3dfb9d787031f71bef1036278b7cef77905 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 17:48:34 +0100 Subject: [PATCH 14/25] migrate to native http listener for windows user --- contrib/pr/checkout.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 5d5e2177b83bd..9a67d00d34304 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -22,7 +22,6 @@ import ( "code.gitea.io/gitea/routers" "code.gitea.io/gitea/routers/routes" "github.com/Unknwon/com" - "github.com/facebookgo/grace/gracehttp" "github.com/go-xorm/xorm" context2 "github.com/gorilla/context" "gopkg.in/src-d/go-git.v4" @@ -131,10 +130,9 @@ func runPR() { }() } */ - gracehttp.Serve(&http.Server{ - Addr: ":8080", - Handler: context2.ClearHandler(m), - }) + + //Start the server + http.ListenAndServe(":8080", context2.ClearHandler(m)) log.Printf("[PR] Cleaning up ...\n") /* @@ -155,8 +153,6 @@ func runPR() { } } -//TODO Add clean branch pr- - func main() { var runPRFlag = flag.Bool("run", false, "Run the PR code") flag.Parse() From 53fdf33399e8a53bb6e9fbe779fd3683d0b92e2a Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 18:00:01 +0100 Subject: [PATCH 15/25] fix ref error --- contrib/pr/checkout.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 9a67d00d34304..08570858a0b36 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -183,10 +183,10 @@ func main() { if err != nil { log.Fatalf("Failed to list remotes of repo : %v", err) } - remoteBranch := "origin" //Default + remoteUpstream := "origin" //Default for _, r := range remotes { 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 - remoteBranch = r.Config().Name + remoteUpstream = r.Config().Name break } } @@ -194,14 +194,15 @@ func main() { branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) log.Printf("Checkout PR #%s in %s\n", pr, branch) + ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branch) err = repo.Fetch(&git.FetchOptions{ - RemoteName: remoteBranch, + RemoteName: remoteUpstream, RefSpecs: []config.RefSpec{ - config.RefSpec(fmt.Sprintf("pull/%s/head:%s", pr, branch)), + config.RefSpec(ref), }, }) if err != nil { - log.Fatalf("Failed to fetch pr-%s : %v", pr, err) + log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err) } tree, err := repo.Worktree() From 8976b7238de7b25ff31493a643a7070ae29f2261 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 18:10:02 +0100 Subject: [PATCH 16/25] convert to os path to be compatible to windows --- contrib/pr/checkout.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 08570858a0b36..2275222c4ed44 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -167,6 +167,8 @@ func main() { } pr := os.Args[1] + codeFilePath = filepath.FromSlash(codeFilePath) //Convert to running OS + //Copy this file if it will not exist in the PR branch dat, err := ioutil.ReadFile(codeFilePath) if err != nil { From 1739a0cf55ecdb8c483ef96e778664060d8e136f Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Sun, 24 Feb 2019 18:17:13 +0100 Subject: [PATCH 17/25] fix typo --- contrib/pr/checkout.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 2275222c4ed44..90128c059daaf 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -115,7 +115,7 @@ func runPR() { routes.RegisterRoutes(m) log.Printf("[PR] Ready for testing !\n") - log.Printf("[PR] Login with user1, user2, user3, ... with pass: passsword\n") + log.Printf("[PR] Login with user1, user2, user3, ... with pass: password\n") /* log.Info("Listen: %v://%s%s", setting.Protocol, listenAddr, setting.AppSubURL) From 9f6820bb4805e05d4c06d446e09fab78ae553df3 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Mon, 4 Mar 2019 09:48:46 +0100 Subject: [PATCH 18/25] testing --- contrib/pr/checkout.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 90128c059daaf..ab0bce4d25d3b 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -194,7 +194,7 @@ func main() { } branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) - log.Printf("Checkout PR #%s in %s\n", pr, branch) + log.Printf("Fetch PR #%s in %s\n", pr, branch) ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branch) err = repo.Fetch(&git.FetchOptions{ @@ -211,6 +211,7 @@ func main() { if err != nil { log.Fatalf("Failed to parse git tree : %v", err) } + log.Printf("Checkout PR #%s in %s\n", pr, branch) err = tree.Checkout(&git.CheckoutOptions{ Branch: plumbing.ReferenceName(branch), }) @@ -218,6 +219,9 @@ func main() { log.Fatalf("Failed to checkout %s : %v", branch, err) } + os.Exit(0) + //Temporary stop here + //Copy this file if not exist if _, err := os.Stat(codeFilePath); os.IsNotExist(err) { err = os.MkdirAll(filepath.Dir(codeFilePath), 0755) From ad1bed44a289e5b0d212a4380338a21e4ea4879a Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Mon, 4 Mar 2019 11:20:08 +0100 Subject: [PATCH 19/25] Fix branch creation --- contrib/pr/checkout.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 90128c059daaf..37ac2fff0aa55 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -194,30 +194,46 @@ func main() { } branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) - log.Printf("Checkout PR #%s in %s\n", pr, branch) + branchRef := plumbing.NewBranchReferenceName(branch) + log.Println("DEBUG", branchRef) - ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branch) + log.Printf("Fetching PR #%s in %s\n", pr, branch) + ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef) err = repo.Fetch(&git.FetchOptions{ RemoteName: remoteUpstream, RefSpecs: []config.RefSpec{ config.RefSpec(ref), }, + //Force: true, }) if err != nil { log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err) } + /* + brs, err := repo.Branches() + log.Println("DEBUG", brs, err) + err = brs.ForEach(func(ref *plumbing.Reference) error { + log.Println("DEBUG", ref) + return nil + }) + log.Println("DEBUG", err) + */ + tree, err := repo.Worktree() if err != nil { log.Fatalf("Failed to parse git tree : %v", err) } + log.Printf("Checkout PR #%s in %s\n", pr, branch) err = tree.Checkout(&git.CheckoutOptions{ - Branch: plumbing.ReferenceName(branch), + Branch: branchRef, }) if err != nil { log.Fatalf("Failed to checkout %s : %v", branch, err) } + //os.Exit(0) //Temporary end point + //Copy this file if not exist if _, err := os.Stat(codeFilePath); os.IsNotExist(err) { err = os.MkdirAll(filepath.Dir(codeFilePath), 0755) From 88a1506812df0b80503bfcf64a01f2b5086acef1 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Mon, 4 Mar 2019 12:01:55 +0100 Subject: [PATCH 20/25] force checkout on windows --- contrib/pr/checkout.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 37ac2fff0aa55..2075f92addbb2 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -17,6 +17,7 @@ import ( "path" "path/filepath" "time" + "runtime" "code.gitea.io/gitea/modules/markup/external" "code.gitea.io/gitea/routers" @@ -227,6 +228,7 @@ func main() { log.Printf("Checkout PR #%s in %s\n", pr, branch) err = tree.Checkout(&git.CheckoutOptions{ Branch: branchRef, + Force: runtime.GOOS == "windows", }) if err != nil { log.Fatalf("Failed to checkout %s : %v", branch, err) From fd536eafd0ccfb617da410cf200881a9d85a4d15 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Mon, 4 Mar 2019 12:34:57 +0100 Subject: [PATCH 21/25] cleaning --- contrib/pr/checkout.go | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 2075f92addbb2..96681009ce8cf 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -196,7 +196,6 @@ func main() { branch := fmt.Sprintf("pr-%s-%d", pr, time.Now().Unix()) branchRef := plumbing.NewBranchReferenceName(branch) - log.Println("DEBUG", branchRef) log.Printf("Fetching PR #%s in %s\n", pr, branch) ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef) @@ -205,22 +204,11 @@ func main() { RefSpecs: []config.RefSpec{ config.RefSpec(ref), }, - //Force: true, }) if err != nil { log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err) } - /* - brs, err := repo.Branches() - log.Println("DEBUG", brs, err) - err = brs.ForEach(func(ref *plumbing.Reference) error { - log.Println("DEBUG", ref) - return nil - }) - log.Println("DEBUG", err) - */ - tree, err := repo.Worktree() if err != nil { log.Fatalf("Failed to parse git tree : %v", err) @@ -234,8 +222,6 @@ func main() { log.Fatalf("Failed to checkout %s : %v", branch, err) } - //os.Exit(0) //Temporary end point - //Copy this file if not exist if _, err := os.Stat(codeFilePath); os.IsNotExist(err) { err = os.MkdirAll(filepath.Dir(codeFilePath), 0755) From ed62748037e0738cc40026388cf82d0a424f4f90 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Mon, 4 Mar 2019 12:53:10 +0100 Subject: [PATCH 22/25] go fmt --- contrib/pr/checkout.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index 96681009ce8cf..e1008fc46959f 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -16,8 +16,8 @@ import ( "os/user" "path" "path/filepath" - "time" "runtime" + "time" "code.gitea.io/gitea/modules/markup/external" "code.gitea.io/gitea/routers" @@ -216,7 +216,7 @@ func main() { log.Printf("Checkout PR #%s in %s\n", pr, branch) err = tree.Checkout(&git.CheckoutOptions{ Branch: branchRef, - Force: runtime.GOOS == "windows", + Force: runtime.GOOS == "windows", }) if err != nil { log.Fatalf("Failed to checkout %s : %v", branch, err) From fce17f4d87e9f00b624402ceffaa49c12419d661 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Tue, 5 Mar 2019 10:07:58 +0100 Subject: [PATCH 23/25] use git cli for fetch on windows --- contrib/pr/checkout.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/contrib/pr/checkout.go b/contrib/pr/checkout.go index e1008fc46959f..903a94454cd97 100644 --- a/contrib/pr/checkout.go +++ b/contrib/pr/checkout.go @@ -198,15 +198,20 @@ func main() { branchRef := plumbing.NewBranchReferenceName(branch) log.Printf("Fetching PR #%s in %s\n", pr, branch) - ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef) - err = repo.Fetch(&git.FetchOptions{ - RemoteName: remoteUpstream, - RefSpecs: []config.RefSpec{ - config.RefSpec(ref), - }, - }) - if err != nil { - log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err) + if runtime.GOOS == "windows" { + //Use git cli command for windows + runCmd("git", "fetch", "origin", fmt.Sprintf("pull/%s/head:%s", pr, branch)) + } else { + ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef) + err = repo.Fetch(&git.FetchOptions{ + RemoteName: remoteUpstream, + RefSpecs: []config.RefSpec{ + config.RefSpec(ref), + }, + }) + if err != nil { + log.Fatalf("Failed to fetch %s from %s : %v", ref, remoteUpstream, err) + } } tree, err := repo.Worktree() @@ -216,7 +221,7 @@ func main() { log.Printf("Checkout PR #%s in %s\n", pr, branch) err = tree.Checkout(&git.CheckoutOptions{ Branch: branchRef, - Force: runtime.GOOS == "windows", + //Force: runtime.GOOS == "windows", }) if err != nil { log.Fatalf("Failed to checkout %s : %v", branch, err) From d348ec46b22ceeb567b4e6cc8a9ff4cf7517a698 Mon Sep 17 00:00:00 2001 From: Antoine GIRARD Date: Tue, 5 Mar 2019 10:30:31 +0100 Subject: [PATCH 24/25] Add make pr PR=0000 --- Makefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Makefile b/Makefile index 01e4a13f15f3f..638b6a1aff2e8 100644 --- a/Makefile +++ b/Makefile @@ -420,3 +420,7 @@ generate-images: $(TMPDIR)/images/64.png $(TMPDIR)/images/128.png \ $(PWD)/public/img/favicon.ico rm -rf $(TMPDIR)/images + +.PHONY: pr +pr: + $(GO) run contrib/pr/checkout.go $(PR) From 2d6483a0db9b46bbdb08eb6b9e4f33095ee8a0c1 Mon Sep 17 00:00:00 2001 From: John Olheiser <42128690+jolheiser@users.noreply.github.com> Date: Wed, 6 Mar 2019 04:02:24 +0100 Subject: [PATCH 25/25] Fix tab Co-Authored-By: sapk --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 638b6a1aff2e8..9a4dbf6ba0635 100644 --- a/Makefile +++ b/Makefile @@ -423,4 +423,4 @@ generate-images: .PHONY: pr pr: - $(GO) run contrib/pr/checkout.go $(PR) + $(GO) run contrib/pr/checkout.go $(PR)