Handling some of the error in bitbucksetserver

Fixed returning the http(s) version of the clone link which had the username in it however should not to work with netrc
Quick typo fix
This commit is contained in:
Joachim Hill-Grannec
2016-06-11 18:42:55 -07:00
parent 7e5a6ec9fb
commit 2ce566483b
2 changed files with 42 additions and 13 deletions

View File

@@ -95,7 +95,7 @@ func (a *Agent) prep(w *queue.Work) (*yaml.Config, error) {
envs := toEnv(w) envs := toEnv(w)
w.Yaml = expander.ExpandString(w.Yaml, envs) w.Yaml = expander.ExpandString(w.Yaml, envs)
// inject the netrc file into the clone plugin if the repositroy is // inject the netrc file into the clone plugin if the repository is
// private and requires authentication. // private and requires authentication.
var secrets []*model.Secret var secrets []*model.Secret
if w.Verified { if w.Verified {

View File

@@ -17,6 +17,7 @@ import (
"github.com/drone/drone/model" "github.com/drone/drone/model"
"github.com/drone/drone/remote" "github.com/drone/drone/remote"
"github.com/mrjones/oauth" "github.com/mrjones/oauth"
"strings"
) )
// Opts defines configuration options. // Opts defines configuration options.
@@ -115,16 +116,24 @@ func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User,
// TODO errors should never be ignored like this // TODO errors should never be ignored like this
response1, err := client.Get(fmt.Sprintf("%s/rest/api/1.0/users/%s", c.URL, login)) response1, err := client.Get(fmt.Sprintf("%s/rest/api/1.0/users/%s", c.URL, login))
if err != nil {
return nil, err
}
contents, err := ioutil.ReadAll(response1.Body) contents, err := ioutil.ReadAll(response1.Body)
if err !=nil {
return nil, err
}
defer response1.Body.Close() defer response1.Body.Close()
var mUser User // TODO prefixing with m* is not a common convention in Go var user User
json.Unmarshal(contents, &mUser) // TODO should not ignore error err = json.Unmarshal(contents, &user)
if err != nil {
return nil, err
}
return &model.User{ return &model.User{
Login: login, Login: login,
Email: mUser.EmailAddress, Email: user.EmailAddress,
Token: accessToken.Token, Token: accessToken.Token,
Avatar: avatarLink(mUser.EmailAddress), Avatar: avatarLink(user.EmailAddress),
}, nil }, nil
} }
@@ -152,20 +161,28 @@ func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) {
defer response.Body.Close() defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body) contents, err := ioutil.ReadAll(response.Body)
bsRepo := BSRepo{} bsRepo := BSRepo{}
json.Unmarshal(contents, &bsRepo) err = json.Unmarshal(contents, &bsRepo)
if err !=nil {
return nil, err
}
repo := &model.Repo{ repo := &model.Repo{
Name: bsRepo.Slug, Name: bsRepo.Slug,
Owner: bsRepo.Project.Key, Owner: bsRepo.Project.Key,
Branch: "master", Branch: "master",
Kind: model.RepoGit, Kind: model.RepoGit,
IsPrivate: !bsRepo.Project.Public, // TODO(josmo) verify this is corrrect IsPrivate: true, // TODO(josmo) possibly set this as a setting - must always be private to use netrc
FullName: fmt.Sprintf("%s/%s", bsRepo.Project.Key, bsRepo.Slug), FullName: fmt.Sprintf("%s/%s", bsRepo.Project.Key, bsRepo.Slug),
} }
for _, item := range bsRepo.Links.Clone { for _, item := range bsRepo.Links.Clone {
if item.Name == "http" { if item.Name == "http" {
repo.Clone = item.Href //TODO sdhould find a clean way to do this
//We are removing the username out fo the link to allow for Netrc to work
splitUrl := strings.SplitAfterN(item.Href,"@",2)
splitProtocal := strings.SplitAfterN(splitUrl[0],"//",2)
cleanUrl := fmt.Sprintf("%s%s",splitProtocal[0], splitUrl[1])
repo.Clone = cleanUrl
} }
} }
for _, item := range bsRepo.Links.Self { for _, item := range bsRepo.Links.Self {
@@ -189,8 +206,14 @@ func (c *client) Repos(u *model.User) ([]*model.RepoLite, error) {
} }
defer response.Body.Close() defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body) contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var repoResponse Repos var repoResponse Repos
json.Unmarshal(contents, &repoResponse) err = json.Unmarshal(contents, &repoResponse)
if err != nil {
return nil, err
}
for _, repo := range repoResponse.Values { for _, repo := range repoResponse.Values {
repos = append(repos, &model.RepoLite{ repos = append(repos, &model.RepoLite{
@@ -240,12 +263,18 @@ func (*client) Status(*model.User, *model.Repo, *model.Build, string) error {
} }
func (c *client) Netrc(user *model.User, r *model.Repo) (*model.Netrc, error) { func (c *client) Netrc(user *model.User, r *model.Repo) (*model.Netrc, error) {
u, err := url.Parse(c.URL) // TODO strip port from url u, err := url.Parse(c.URL)
//remove the port
tmp := strings.Split(u.Host, ":")
var host = tmp[0]
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Info(fmt.Sprintf("machine % login %s password %s", host, c.GitUserName, c.GitPassword))
return &model.Netrc{ return &model.Netrc{
Machine: u.Host, Machine: host,
Login: c.GitUserName, Login: c.GitUserName,
Password: c.GitPassword, Password: c.GitPassword,
}, nil }, nil