refactored how remotes work and how (some) config is loaded

This commit is contained in:
Brad Rydzewski
2014-09-03 00:23:36 -07:00
parent 38379992bf
commit ca3d15bca2
8 changed files with 173 additions and 118 deletions

View File

@@ -5,9 +5,9 @@ import (
"fmt"
"net"
"net/smtp"
"os"
"strings"
"github.com/drone/config"
"github.com/drone/drone/shared/model"
)
@@ -28,11 +28,11 @@ const (
)
var (
DefaultHost = os.Getenv("SMTP_HOST")
DefaultPort = os.Getenv("SMTP_PORT")
DefaultFrom = os.Getenv("SMTP_FROM")
DefaultUser = os.Getenv("SMTP_USER")
DefaultPass = os.Getenv("SMTP_PASS")
DefaultHost = config.String("smtp-host", "")
DefaultPort = config.String("smtp-port", "")
DefaultFrom = config.String("smtp-from", "")
DefaultUser = config.String("smtp-user", "")
DefaultPass = config.String("smtp-pass", "")
)
type Email struct {
@@ -139,12 +139,12 @@ func (e *Email) send(subject, body string, recipients []string) error {
// configuration. If None provided, attempt to
// use the global configuration set in the environet
// variables.
if len(DefaultHost) != 0 {
e.Host = DefaultHost
e.Port = DefaultPort
e.From = DefaultFrom
e.Username = DefaultUser
e.Password = DefaultPass
if len(*DefaultHost) != 0 {
e.Host = *DefaultHost
e.Port = *DefaultPort
e.From = *DefaultFrom
e.Username = *DefaultUser
e.Password = *DefaultPass
}
var auth smtp.Auth

View File

@@ -3,21 +3,12 @@ package github
import (
"fmt"
"net/url"
"os"
"strings"
"code.google.com/p/goauth2/oauth"
"github.com/drone/drone/shared/model"
"github.com/google/go-github/github"
)
// TODO (bradrydzewski) explore using the Repo.URL to parse the GitHub
// Entperprise Scheme+Hostname, instead of the environment variable. Is
// there any reason not to use the environment variable?
// GitHub enterprise URL
var URL = os.Getenv("GITHUB_ENTERPRISE_API")
const (
NotifyDisabled = "disabled"
NotifyFalse = "false"
@@ -38,6 +29,10 @@ const (
DescError = "oops, something went wrong"
)
const (
BaseURL = "https://api.github.com/"
)
type GitHub string
// Send uses the github status API to update the build
@@ -70,6 +65,7 @@ func (g GitHub) Send(context *model.Request) error {
)
return send(
context.Repo.URL,
context.Repo.Host,
context.Repo.Owner,
context.Repo.Name,
@@ -81,7 +77,7 @@ func (g GitHub) Send(context *model.Request) error {
)
}
func send(host, owner, repo, status, desc, target, ref, token string) error {
func send(rawurl, host, owner, repo, status, desc, target, ref, token string) error {
transport := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
@@ -99,10 +95,7 @@ func send(host, owner, repo, status, desc, target, ref, token string) error {
// the base url. Per the documentation, we need to
// ensure there is a trailing slash.
if host != model.RemoteGithub {
client.BaseURL, _ = url.Parse(URL)
if !strings.HasSuffix(client.BaseURL.Path, "/") {
client.BaseURL.Path = client.BaseURL.Path + "/"
}
client.BaseURL, _ = getEndpoint(rawurl)
}
_, _, err := client.Repositories.CreateStatus(owner, repo, ref, &data)
@@ -151,3 +144,15 @@ func getDesc(status string) string {
func getTarget(url, host, owner, repo, branch, commit string) string {
return fmt.Sprintf("%s/%s/%s/%s/%s/%s", url, host, owner, repo, branch, commit)
}
// getEndpoint is a helper funcation that parsed the
// repository HTML URL to determine the API URL. It is
// intended for use with GitHub enterprise.
func getEndpoint(rawurl string) (*url.URL, error) {
uri, err := url.Parse(rawurl)
if err != nil {
return nil, err
}
uri.Path = "/api/v3"
return uri, nil
}

View File

@@ -1,16 +1,24 @@
package bitbucket
import (
"os"
"github.com/drone/config"
"github.com/drone/drone/plugin/remote"
)
func init() {
var cli = os.Getenv("BITBUCKET_CLIENT")
var sec = os.Getenv("BITBUCKET_SECRET")
if len(cli) == 0 || len(sec) == 0 {
var (
// Bitbucket cloud configuration details
bitbucketClient = config.String("bitbucket-client", "")
bitbucketSecret = config.String("bitbucket-secret", "")
)
// Registers the Bitbucket plugin using the default
// settings from the config file or environment
// variables.
func Register() {
if len(*bitbucketClient) == 0 || len(*bitbucketSecret) == 0 {
return
}
remote.Register(NewDefault(cli, sec))
remote.Register(
NewDefault(*bitbucketClient, *bitbucketSecret),
)
}

View File

@@ -140,7 +140,7 @@ func (r *GitHub) GetRepos(user *model.User) ([]*model.Repo, error) {
Owner: *item.Owner.Login,
Name: *item.Name,
Private: *item.Private,
URL: *item.URL,
URL: *item.HTMLURL,
CloneURL: *item.GitURL,
GitURL: *item.GitURL,
SSHURL: *item.SSHURL,

View File

@@ -1,41 +1,54 @@
package github
import (
"os"
"github.com/drone/config"
"github.com/drone/drone/plugin/remote"
)
func init() {
init_github()
init_github_enterprise()
var (
// GitHub cloud configuration details
githubClient = config.String("github-client", "")
githubSecret = config.String("github-secret", "")
// GitHub Enterprise configuration details
githubEnterpriseURL = config.String("github-enterprise-url", "")
githubEnterpriseAPI = config.String("github-enterprise-api", "")
githubEnterpriseClient = config.String("github-enterprise-client", "")
githubEnterpriseSecret = config.String("github-enterprise-secret", "")
)
// Registers the GitHub plugins using the default
// settings from the config file or environment
// variables.
func Register() {
registerGitHub()
registerGitHubEnterprise()
}
// registers the GitHub (github.com) plugin
func init_github() {
var cli = os.Getenv("GITHUB_CLIENT")
var sec = os.Getenv("GITHUB_SECRET")
if len(cli) == 0 ||
len(sec) == 0 {
func registerGitHub() {
if len(*githubClient) == 0 || len(*githubSecret) == 0 {
return
}
var github = NewDefault(cli, sec)
remote.Register(github)
remote.Register(
NewDefault(*githubClient, *githubSecret),
)
}
// registers the GitHub Enterprise plugin
func init_github_enterprise() {
var url = os.Getenv("GITHUB_ENTERPRISE_URL")
var api = os.Getenv("GITHUB_ENTERPRISE_API")
var cli = os.Getenv("GITHUB_ENTERPRISE_CLIENT")
var sec = os.Getenv("GITHUB_ENTERPRISE_SECRET")
if len(url) == 0 ||
len(api) == 0 ||
len(cli) == 0 ||
len(sec) == 0 {
func registerGitHubEnterprise() {
if len(*githubEnterpriseURL) == 0 ||
len(*githubEnterpriseAPI) == 0 ||
len(*githubEnterpriseClient) == 0 ||
len(*githubEnterpriseSecret) == 0 {
return
}
var github = New(url, api, cli, sec)
remote.Register(github)
remote.Register(
New(
*githubEnterpriseURL,
*githubEnterpriseAPI,
*githubEnterpriseClient,
*githubEnterpriseSecret,
),
)
}

View File

@@ -1,16 +1,22 @@
package gitlab
import (
"os"
"github.com/drone/config"
"github.com/drone/drone/plugin/remote"
)
// registers the Gitlab plugin
func init() {
var url = os.Getenv("GITLAB_URL")
if len(url) == 0 {
var (
gitlabURL = config.String("gitlab-url", "")
)
// Registers the Gitlab plugin using the default
// settings from the config file or environment
// variables.
func Register() {
if len(*gitlabURL) == 0 {
return
}
remote.Register(New(url))
remote.Register(
New(*gitlabURL),
)
}