diff --git a/pkg/database/migrate/20140328201430_add_gitlab_columns.go b/pkg/database/migrate/20140328201430_add_gitlab_columns.go index f0d6ee5ca..f47013903 100644 --- a/pkg/database/migrate/20140328201430_add_gitlab_columns.go +++ b/pkg/database/migrate/20140328201430_add_gitlab_columns.go @@ -17,9 +17,14 @@ func (r *rev20140328201430) Up(mg *MigrationDriver) error { return err } - if _, err := mg.Tx.Exec(`update settings set gitlab_domain='gitlab.com', gitlab_apiurl='https://gitlab.com'`); err != nil { + if _, err := mg.Tx.Exec(`update settings set gitlab_domain=?`, "gitlab.com"); err != nil { return err } + + if _, err := mg.Tx.Exec(`update settings set gitlab_apiurl=?`, "https://gitlab.com"); err != nil { + return err + } + _, err := mg.AddColumn("users", mg.T.String("gitlab_token")) return err } @@ -29,6 +34,6 @@ func (r *rev20140328201430) Down(mg *MigrationDriver) error { if _, err := mg.DropColumns("users", "gitlab_token"); err != nil { return err } - _, err:= mg.DropColumns("settings", "gitlab_domain", "gitlab_apiurl") + _, err := mg.DropColumns("settings", "gitlab_domain", "gitlab_apiurl") return err } diff --git a/pkg/database/users.go b/pkg/database/users.go index 3bea97452..5e229e5ba 100644 --- a/pkg/database/users.go +++ b/pkg/database/users.go @@ -13,21 +13,24 @@ const userTable = "users" // SQL Queries to retrieve a user by their unique database key const userFindIdStmt = ` SELECT id, email, password, token, name, gravatar, created, updated, admin, -github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret +github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret, +gitlab_token FROM users WHERE id = ? ` // SQL Queries to retrieve a user by their email address const userFindEmailStmt = ` SELECT id, email, password, token, name, gravatar, created, updated, admin, -github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret +github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret, +gitlab_token FROM users WHERE email = ? ` // SQL Queries to retrieve a list of all users const userStmt = ` SELECT id, email, password, token, name, gravatar, created, updated, admin, -github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret +github_login, github_token, bitbucket_login, bitbucket_token, bitbucket_secret, +gitlab_token FROM users ORDER BY name ASC ` diff --git a/pkg/handler/admin.go b/pkg/handler/admin.go index ff21090f3..858e615cc 100644 --- a/pkg/handler/admin.go +++ b/pkg/handler/admin.go @@ -3,6 +3,7 @@ package handler import ( "fmt" "net/http" + "net/url" "strconv" "time" @@ -170,6 +171,14 @@ func AdminSettingsUpdate(w http.ResponseWriter, r *http.Request, u *User) error settings.GitHubDomain = r.FormValue("GitHubDomain") settings.GitHubApiUrl = r.FormValue("GitHubApiUrl") + // update gitlab settings + settings.GitlabApiUrl = r.FormValue("GitlabApiUrl") + glUrl, err := url.Parse(settings.GitlabApiUrl) + if err != nil { + return RenderError(w, err, http.StatusBadRequest) + } + settings.GitlabDomain = glUrl.Host + // update smtp settings settings.SmtpServer = r.FormValue("SmtpServer") settings.SmtpPort = r.FormValue("SmtpPort") @@ -252,6 +261,8 @@ func InstallPost(w http.ResponseWriter, r *http.Request) error { settings.Scheme = r.FormValue("Scheme") settings.GitHubApiUrl = "https://api.github.com" settings.GitHubDomain = "github.com" + settings.GitlabApiUrl = "https://gitlab.com" + settings.GitlabDomain = "gitlab.com" database.SaveSettings(&settings) // add the user to the session object diff --git a/pkg/handler/auth.go b/pkg/handler/auth.go index 14219a2fb..0949c3b4b 100644 --- a/pkg/handler/auth.go +++ b/pkg/handler/auth.go @@ -6,10 +6,10 @@ import ( "github.com/drone/drone/pkg/database" . "github.com/drone/drone/pkg/model" - "github.com/drone/go-github/github" - "github.com/drone/go-github/oauth2" "github.com/drone/go-bitbucket/bitbucket" "github.com/drone/go-bitbucket/oauth1" + "github.com/drone/go-github/github" + "github.com/drone/go-github/oauth2" ) // Create the User session. diff --git a/pkg/handler/gitlab.go b/pkg/handler/gitlab.go index d38f6ca7a..d88bf99ba 100644 --- a/pkg/handler/gitlab.go +++ b/pkg/handler/gitlab.go @@ -44,8 +44,22 @@ func (g *GitlabHandler) Add(w http.ResponseWriter, r *http.Request, u *User) err } func (g *GitlabHandler) Link(w http.ResponseWriter, r *http.Request, u *User) error { - var err error - return err + token := r.FormValue("token") + u.GitlabToken = token + + if err := database.SaveUser(u); err != nil { + return RenderError(w, err, http.StatusBadRequest) + } + + settings := database.SettingsMust() + gl := gogitlab.NewGitlab(settings.GitlabApiUrl, g.apiPath, u.GitlabToken) + _, err := gl.CurrentUser() + if err != nil { + return fmt.Errorf("Private Token is not valid: %q", err) + } + + http.Redirect(w, r, "/new/gitlab", http.StatusSeeOther) + return nil } func (g *GitlabHandler) Create(w http.ResponseWriter, r *http.Request, u *User) error { @@ -125,5 +139,5 @@ func (g *GitlabHandler) newGitlabRepo(u *User, owner, name string) (*Repo, error // ns namespaces user and repo. // Returns user%2Frepo func ns(user, repo string) string { - return fmt.Sprintf("%s%%252F%s", user, repo) + return fmt.Sprintf("%s%%2F%s", user, repo) } diff --git a/pkg/handler/hooks.go b/pkg/handler/hooks.go index 89b098869..57c0e0178 100644 --- a/pkg/handler/hooks.go +++ b/pkg/handler/hooks.go @@ -10,8 +10,8 @@ import ( "github.com/drone/drone/pkg/database" . "github.com/drone/drone/pkg/model" "github.com/drone/drone/pkg/queue" - "github.com/drone/go-github/github" "github.com/drone/go-bitbucket/bitbucket" + "github.com/drone/go-github/github" ) type HookHandler struct { diff --git a/pkg/handler/repos.go b/pkg/handler/repos.go index 405f1928f..2e4892811 100644 --- a/pkg/handler/repos.go +++ b/pkg/handler/repos.go @@ -7,8 +7,8 @@ import ( "github.com/drone/drone/pkg/channel" "github.com/drone/drone/pkg/database" . "github.com/drone/drone/pkg/model" - "github.com/drone/go-github/github" "github.com/drone/go-bitbucket/bitbucket" + "github.com/drone/go-github/github" "launchpad.net/goyaml" ) diff --git a/pkg/handler/testing/gitlab_test.go b/pkg/handler/testing/gitlab_test.go index aa4c623d5..b66bf9809 100644 --- a/pkg/handler/testing/gitlab_test.go +++ b/pkg/handler/testing/gitlab_test.go @@ -10,8 +10,8 @@ import ( "github.com/drone/drone/pkg/database" "github.com/drone/drone/pkg/handler" - "github.com/drone/drone/pkg/queue" "github.com/drone/drone/pkg/model" + "github.com/drone/drone/pkg/queue" dbtest "github.com/drone/drone/pkg/database/testing" . "github.com/smartystreets/goconvey/convey" @@ -190,11 +190,9 @@ func Test_GitLabCreate(t *testing.T) { // it is just proof-of-concepting a testing strategy, so we'll // revisit later. - // server is a test HTTP server used to provide mock API responses. var glServer *httptest.Server - func SetupGitlabFixtures() { dbtest.Setup() diff --git a/pkg/template/pages/admin_settings.html b/pkg/template/pages/admin_settings.html index 5a1473d5a..a6f825a7b 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -50,6 +50,13 @@ +