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
+12 -12
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
+19 -14
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
}