Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub Enterprise Support #23

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions pkg/build/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ func (r *Repo) IsGit() bool {
return true
case strings.HasPrefix(r.Path, "ssh:https://git@"):
return true
case strings.HasPrefix(r.Path, "https://github.com/"):
case strings.HasPrefix(r.Path, "https://github"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if perhaps we don't need this particular branch in the validation?

Seems like case strings.HasSuffix(r.Path, ".git") should be enough while https://github.com/ might be a bit superfluous since I don't believe there is any restriction on hosted github's hostname having to be prefixed with https://github (though it is a nice convention). I believe it is optional to use https (though it is recommended AFAIK).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, I don't see a perfect way to check for a subversion URL repo which github also supports.

return true
case strings.HasPrefix(r.Path, "http:https://github.com"):
case strings.HasPrefix(r.Path, "http:https://github"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see there is a second check for the http version; though I still wonder if this check is useful given the lack of a restriction on the hostname.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is never used in code, but I try'd to change it just in case.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is never used in code

Maybe it was once used but never got deleted once the use-case went away. @bradrydzewski, is it OK to delete?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we added the code in anticipation of bitbucket support to differentiate between hg and git. I'll be adding bitbucket this week. it may turn out that we don't need it, in which case I'll remove it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it may turn out that we don't need it, in which case I'll remove it

👍

return true
case strings.HasSuffix(r.Path, ".git"):
return true
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/schema/sample.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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');
insert into settings values (1,'','','github.com','https://api.github.com','','','','','','','','localhost:8080','http');

-- add public & private keys to all repositories

Expand Down
2 changes: 2 additions & 0 deletions pkg/database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions pkg/database/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/database/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
FROM settings WHERE id = 1
`
Expand Down
4 changes: 4 additions & 0 deletions pkg/handler/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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")
Expand Down Expand Up @@ -242,6 +244,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
Expand Down
5 changes: 3 additions & 2 deletions pkg/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions pkg/handler/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions pkg/handler/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ 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
}
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 {
Expand All @@ -82,12 +84,13 @@ 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
}

repo, err := NewGitHubRepo(owner, name, githubRepo.Private)
repo, err := NewGitHubRepo(settings.GitHubDomain, owner, name, githubRepo.Private)
if err != nil {
return err
}
Expand Down
15 changes: 7 additions & 8 deletions pkg/model/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const (
)

const (
HostGithub = "github.com"
HostBitbucket = "bitbucket.org"
HostGoogle = "code.google.com"
HostCustom = "custom"
Expand All @@ -25,8 +24,8 @@ const (
)

const (
githubRepoPattern = "git:https://github.com/%s/%s.git"
githubRepoPatternPrivate = "git@github.com:%s/%s.git"
githubRepoPattern = "git:https://%s/%s/%s.git"
githubRepoPatternPrivate = "git@%s:%s/%s.git"
bitbucketRepoPattern = "https://bitbucket.org/%s/%s.git"
bitbucketRepoPatternPrivate = "[email protected]:%s/%s.git"
)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions pkg/queue/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pkg/template/pages/admin_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ <h1>Sysadmin</h1>
<input class="form-control form-control-large" type="text" name="GitHubKey" value="{{.Settings.GitHubKey}}" />
<input class="form-control form-control-large" type="password" name="GitHubSecret" value="{{.Settings.GitHubSecret}}" />
</div>
<label>GitHub Settings:</label>
<div>
<input class="form-control form-control-large" type="text" name="GitHubDomain" value="{{.Settings.GitHubDomain}}" />
<input class="form-control form-control-large" type="text" name="GitHubApiUrl" value="{{.Settings.GitHubApiUrl}}" />
</div>
</div>
<div class="form-group hide">
<div class="alert">Bitbucket OAuth Consumer Key and Secret.</div>
Expand Down
4 changes: 3 additions & 1 deletion pkg/template/pages/github_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h1>
<a class="btn btn-default pull-right" href="/auth/login/github" style="font-size: 18px;background:#f4f4f4;">Re-Link Account</a>
</div>
<form class="form-repo" method="POST" action="/new/github.com">
<input type="hidden" name="domain" autocomplete="off" value="{{.Settings.GitHubDomain}}">
<div class="field-group">
<div>
<label>GitHub Owner</label>
Expand Down Expand Up @@ -84,7 +85,8 @@ <h1>
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");
Expand Down