From 7208d95a76e2b4b8d04b8092b4dd805936bf953e Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 19:36:45 +0600 Subject: [PATCH 1/9] Added GitHubApiUrl and GitHubDomain to settings and template --- Makefile | 1 + pkg/database/schema/sample.sql | 4 ++-- pkg/database/schema/schema.go | 2 ++ pkg/database/schema/schema.sql | 2 ++ pkg/database/settings.go | 2 +- pkg/handler/admin.go | 4 ++++ pkg/model/settings.go | 2 ++ pkg/template/pages/admin_settings.html | 5 +++++ 8 files changed, 19 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 3e8b8321..3b4074de 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ clean: rm -rf usr/local/bin/drone rm -rf usr/local/bin/droned rm -rf drone.sqlite + rm -rf /tmp/drone.sqlite # creates a debian package for drone # to install `sudo dpkg -i drone.deb` diff --git a/pkg/database/schema/sample.sql b/pkg/database/schema/sample.sql index 594caaa3..c19dbe4b 100644 --- a/pkg/database/schema/sample.sql +++ b/pkg/database/schema/sample.sql @@ -49,7 +49,7 @@ insert into builds values (9, 3, 'node_0.80', 'Success', '2013-09-16 00:00:00',' -- insert default, dummy settings -insert into settings values (1,'','','','','','','','','','localhost:8080','http', 0); +insert into settings values (1,'','','github.com','https://api.github.com','','','','','','','','localhost:8080','http',0); -- add public & private keys to all repositories @@ -123,4 +123,4 @@ Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 10.849 sec Results : -Tests run: 7, Failures: 0, Errors: 0, Skipped: 0'; \ No newline at end of file +Tests run: 7, Failures: 0, Errors: 0, Skipped: 0'; diff --git a/pkg/database/schema/schema.go b/pkg/database/schema/schema.go index ec93774a..5cbf89a6 100644 --- a/pkg/database/schema/schema.go +++ b/pkg/database/schema/schema.go @@ -118,6 +118,8 @@ CREATE TABLE settings ( id INTEGER PRIMARY KEY ,github_key VARCHAR(255) ,github_secret VARCHAR(255) + ,github_domain VARCHAR(255) + ,github_apiurl VARCHAR(255) ,bitbucket_key VARCHAR(255) ,bitbucket_secret VARCHAR(255) ,smtp_server VARCHAR(1024) diff --git a/pkg/database/schema/schema.sql b/pkg/database/schema/schema.sql index d0babf32..b5ed513c 100644 --- a/pkg/database/schema/schema.sql +++ b/pkg/database/schema/schema.sql @@ -103,6 +103,8 @@ CREATE TABLE settings ( id INTEGER PRIMARY KEY ,github_key VARCHAR(255) ,github_secret VARCHAR(255) + ,github_domain VARCHAR(255) + ,github_apiurl VARCHAR(255) ,bitbucket_key VARCHAR(255) ,bitbucket_secret VARCHAR(255) ,smtp_server VARCHAR(1024) diff --git a/pkg/database/settings.go b/pkg/database/settings.go index 188ac3a5..883b87d3 100644 --- a/pkg/database/settings.go +++ b/pkg/database/settings.go @@ -10,7 +10,7 @@ const settingsTable = "settings" // SQL Queries to retrieve the system settings const settingsStmt = ` -SELECT id, github_key, github_secret, bitbucket_key, bitbucket_secret, +SELECT id, github_key, github_secret, github_domain, github_apiurl, bitbucket_key, bitbucket_secret, smtp_server, smtp_port, smtp_address, smtp_username, smtp_password, hostname, scheme, open_invitations FROM settings WHERE id = 1 ` diff --git a/pkg/handler/admin.go b/pkg/handler/admin.go index 3d7fa8b5..d7840c7f 100644 --- a/pkg/handler/admin.go +++ b/pkg/handler/admin.go @@ -167,6 +167,8 @@ func AdminSettingsUpdate(w http.ResponseWriter, r *http.Request, u *User) error // update github settings settings.GitHubKey = r.FormValue("GitHubKey") settings.GitHubSecret = r.FormValue("GitHubSecret") + settings.GitHubDomain = r.FormValue("GitHubDomain") + settings.GitHubApiUrl = r.FormValue("GitHubApiUrl") // update smtp settings settings.SmtpServer = r.FormValue("SmtpServer") @@ -243,6 +245,8 @@ func InstallPost(w http.ResponseWriter, r *http.Request) error { settings := Settings{} settings.Domain = r.FormValue("Domain") settings.Scheme = r.FormValue("Scheme") + settings.GitHubApiUrl = "https://api.github.com"; + settings.GitHubDomain = "github.com"; database.SaveSettings(&settings) // add the user to the session object diff --git a/pkg/model/settings.go b/pkg/model/settings.go index 04db729f..3112d1d4 100644 --- a/pkg/model/settings.go +++ b/pkg/model/settings.go @@ -17,6 +17,8 @@ type Settings struct { // GitHub Consumer key and secret. GitHubKey string `meddler:"github_key"` GitHubSecret string `meddler:"github_secret"` + GitHubDomain string `meddler:"github_domain"` + GitHubApiUrl string `meddler:"github_apiurl"` // Bitbucket Consumer Key and secret. BitbucketKey string `meddler:"bitbucket_key"` diff --git a/pkg/template/pages/admin_settings.html b/pkg/template/pages/admin_settings.html index bc607fa5..824015e5 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -44,6 +44,11 @@ + +
+ + +
Bitbucket OAuth Consumer Key and Secret.
From 6c9ff00f99bd61eec0e83b979035a2948d5ebe31 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 19:39:25 +0600 Subject: [PATCH 2/9] Put values from settings into handler/auth.go --- pkg/handler/auth.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/handler/auth.go b/pkg/handler/auth.go index 091bb494..7e91787e 100644 --- a/pkg/handler/auth.go +++ b/pkg/handler/auth.go @@ -48,8 +48,8 @@ func LinkGithub(w http.ResponseWriter, r *http.Request, u *User) error { // github OAuth2 Data var oauth = oauth2.Client{ RedirectURL: settings.URL().String() + "/auth/login/github", - AccessTokenURL: "https://github.com/login/oauth/access_token", - AuthorizationURL: "https://github.com/login/oauth/authorize", + AccessTokenURL: "https://" + settings.GitHubDomain + "/login/oauth/access_token", + AuthorizationURL: "https://" + settings.GitHubDomain + "/login/oauth/authorize", ClientId: settings.GitHubKey, ClientSecret: settings.GitHubSecret, } @@ -72,6 +72,7 @@ func LinkGithub(w http.ResponseWriter, r *http.Request, u *User) error { // create the client client := github.New(token.AccessToken) + client.ApiUrl = settings.GitHubApiUrl // get the user information githubUser, err := client.Users.Current() From 425cbbc32432c9c10884b7b65798d2e7d2397f4a Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 19:45:15 +0600 Subject: [PATCH 3/9] Use custom github domain for webhook --- pkg/handler/repos.go | 2 +- pkg/model/repo.go | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/handler/repos.go b/pkg/handler/repos.go index 65f49073..53801244 100644 --- a/pkg/handler/repos.go +++ b/pkg/handler/repos.go @@ -87,7 +87,7 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { return err } - repo, err := NewGitHubRepo(owner, name, githubRepo.Private) + repo, err := NewGitHubRepo(settings.GitHubDomain, owner, name, githubRepo.Private) if err != nil { return err } diff --git a/pkg/model/repo.go b/pkg/model/repo.go index 5b89ad71..bf47b5ef 100644 --- a/pkg/model/repo.go +++ b/pkg/model/repo.go @@ -12,7 +12,6 @@ const ( ) const ( - HostGithub = "github.com" HostBitbucket = "bitbucket.org" HostGoogle = "code.google.com" HostCustom = "custom" @@ -25,8 +24,8 @@ const ( ) const ( - githubRepoPattern = "git://github.com/%s/%s.git" - githubRepoPatternPrivate = "git@github.com:%s/%s.git" + githubRepoPattern = "git://%s/%s/%s.git" + githubRepoPatternPrivate = "git@%s:%s/%s.git" bitbucketRepoPattern = "https://bitbucket.org/%s/%s.git" bitbucketRepoPatternPrivate = "git@bitbucket.org:%s/%s.git" ) @@ -122,15 +121,15 @@ func NewRepo(host, owner, name, scm, url string) (*Repo, error) { } // Creates a new GitHub repository -func NewGitHubRepo(owner, name string, private bool) (*Repo, error) { +func NewGitHubRepo(domain, owner, name string, private bool) (*Repo, error) { var url string switch private { case false: - url = fmt.Sprintf(githubRepoPattern, owner, name) + url = fmt.Sprintf(githubRepoPattern, domain, owner, name) case true: - url = fmt.Sprintf(githubRepoPatternPrivate, owner, name) + url = fmt.Sprintf(githubRepoPatternPrivate, domain, owner, name) } - return NewRepo(HostGithub, owner, name, ScmGit, url) + return NewRepo(domain, owner, name, ScmGit, url) } // Creates a new Bitbucket repository @@ -142,7 +141,7 @@ func NewBitbucketRepo(owner, name string, private bool) (*Repo, error) { case true: url = fmt.Sprintf(bitbucketRepoPatternPrivate, owner, name) } - return NewRepo(HostGithub, owner, name, ScmGit, url) + return NewRepo(HostBitbucket, owner, name, ScmGit, url) } func (r *Repo) DefaultBranch() string { From 44d5f3baca3da7696c03fac9ee5ec785d2b5c69c Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 19:59:51 +0600 Subject: [PATCH 4/9] Update all github clients in code --- pkg/handler/hooks.go | 10 ++++++++++ pkg/handler/repos.go | 1 + 2 files changed, 11 insertions(+) diff --git a/pkg/handler/hooks.go b/pkg/handler/hooks.go index 4abfe8ae..2cb71968 100644 --- a/pkg/handler/hooks.go +++ b/pkg/handler/hooks.go @@ -100,8 +100,13 @@ func Hook(w http.ResponseWriter, r *http.Request) error { commit.SetAuthor(hook.Commits[0].Author.Email) } + // get the github settings from the database + settings := database.SettingsMust() + // get the drone.yml file from GitHub client := github.New(user.GithubToken) + client.ApiUrl = settings.GitHubApiUrl + content, err := client.Contents.FindRef(repo.Owner, repo.Name, ".drone.yml", commit.Hash) if err != nil { msg := "No .drone.yml was found in this repository. You need to add one.\n" @@ -216,8 +221,13 @@ func PullRequestHook(w http.ResponseWriter, r *http.Request) { commit.Message = hook.PullRequest.Title // label := p.PullRequest.Head.Labe + // get the github settings from the database + settings := database.SettingsMust() + // get the drone.yml file from GitHub client := github.New(user.GithubToken) + client.ApiUrl = settings.GitHubApiUrl + content, err := client.Contents.FindRef(repo.Owner, repo.Name, ".drone.yml", commit.Hash) // TODO should this really be the hash?? if err != nil { println(err.Error()) diff --git a/pkg/handler/repos.go b/pkg/handler/repos.go index 53801244..d5d9f184 100644 --- a/pkg/handler/repos.go +++ b/pkg/handler/repos.go @@ -82,6 +82,7 @@ func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error { // create the GitHub client client := github.New(u.GithubToken) + client.ApiUrl = settings.GitHubApiUrl githubRepo, err := client.Repos.Find(owner, name) if err != nil { return err From 833563763cb69d1b8e9362015a47d41478929cda Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 20:09:38 +0600 Subject: [PATCH 5/9] Added settings to github_add page (fix redirect) --- pkg/handler/repos.go | 4 +++- pkg/template/pages/github_add.html | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/handler/repos.go b/pkg/handler/repos.go index d5d9f184..fe3336c0 100644 --- a/pkg/handler/repos.go +++ b/pkg/handler/repos.go @@ -54,6 +54,7 @@ func RepoDashboard(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) } func RepoAdd(w http.ResponseWriter, r *http.Request, u *User) error { + settings := database.SettingsMust() teams, err := database.ListTeams(u.ID) if err != nil { return err @@ -61,7 +62,8 @@ func RepoAdd(w http.ResponseWriter, r *http.Request, u *User) error { data := struct { User *User Teams []*Team - }{u, teams} + Settings *Settings + }{u, teams, settings} // if the user hasn't linked their GitHub account // render a different template if len(u.GithubToken) == 0 { diff --git a/pkg/template/pages/github_add.html b/pkg/template/pages/github_add.html index 2a6b7079..f1c5c233 100644 --- a/pkg/template/pages/github_add.html +++ b/pkg/template/pages/github_add.html @@ -25,6 +25,7 @@ Re-Link Account
+
@@ -85,7 +86,8 @@ if (this.status == 200) { var name = $("input[name=name]").val() var owner = $("input[name=owner]").val() - window.location.pathname = "/github.com/"+owner+"/"+name + var domain = $("input[name=domain]").val() + window.location.pathname = "/" + domain + "/"+owner+"/"+name } else { $("#failureAlert").text("Unable to setup the Repository"); $("#failureAlert").show().removeClass("hide"); From 87cc740e8a6c69c8cafd764da69a6886ecbe35de Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 20:14:10 +0600 Subject: [PATCH 6/9] Fixed identation --- pkg/template/pages/admin_settings.html | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/template/pages/admin_settings.html b/pkg/template/pages/admin_settings.html index 824015e5..d3f1b66e 100644 --- a/pkg/template/pages/admin_settings.html +++ b/pkg/template/pages/admin_settings.html @@ -44,11 +44,11 @@
- -
- - -
+ +
+ + +
Bitbucket OAuth Consumer Key and Secret.
From b113b2ac333b6d03c801355d05a8873b63a46177 Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 9 Feb 2014 20:24:13 +0600 Subject: [PATCH 7/9] Make IsGit check less strict --- pkg/build/repo/repo.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/build/repo/repo.go b/pkg/build/repo/repo.go index 27a47865..d1af38fa 100644 --- a/pkg/build/repo/repo.go +++ b/pkg/build/repo/repo.go @@ -73,9 +73,9 @@ func (r *Repo) IsGit() bool { return true case strings.HasPrefix(r.Path, "ssh://git@"): return true - case strings.HasPrefix(r.Path, "https://github.com/"): + case strings.HasPrefix(r.Path, "https://github"): return true - case strings.HasPrefix(r.Path, "http://github.com"): + case strings.HasPrefix(r.Path, "http://github"): return true case strings.HasSuffix(r.Path, ".git"): return true From 407faca612f02e29ca9d7d6939442eb638d8da5a Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Mon, 10 Feb 2014 12:40:18 +0600 Subject: [PATCH 8/9] Fixed another github client instance in code --- pkg/queue/queue.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/queue/queue.go b/pkg/queue/queue.go index db49944c..e931c46f 100644 --- a/pkg/queue/queue.go +++ b/pkg/queue/queue.go @@ -225,6 +225,7 @@ func updateGitHubStatus(repo *Repo, commit *Commit) error { } client := github.New(user.GithubToken) + client.ApiUrl = settings.GitHubApiUrl; var url string url = settings.URL().String() + "/" + repo.Slug + "/commit/" + commit.Hash From 150517528813399ba1490923aaafdbc07e16fb8f Mon Sep 17 00:00:00 2001 From: Vsevolod Strukchinsky Date: Sun, 16 Feb 2014 11:55:11 +0600 Subject: [PATCH 9/9] Migration file based on #83 I choosed Rev3, because Rev2 will be about "open invitations" feature. --- .../201402211147_github_enterprise_support.go | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 pkg/database/migrate/201402211147_github_enterprise_support.go diff --git a/pkg/database/migrate/201402211147_github_enterprise_support.go b/pkg/database/migrate/201402211147_github_enterprise_support.go new file mode 100644 index 00000000..2c2eb3a3 --- /dev/null +++ b/pkg/database/migrate/201402211147_github_enterprise_support.go @@ -0,0 +1,23 @@ +package migrate + +type Rev3 struct{} + +var GitHubEnterpriseSupport = &Rev3{} + +func (r *Rev3) Revision() int64 { + return 201402211147 +} + +func (r *Rev3) Up(op Operation) error { + _, err := op.AddColumn("settings", "github_domain VARCHAR(255)") + if err { + return err + } + _, err = op.AddColumn("settings", "github_apiurl VARCHAR(255)") + return err +} + +func (r *Rev3) Down(op Operation) error { + _, err := op.DropColumns("settings", []string{"github_domain", "github_apiurl"}) + return err +}