Merge pull request #1671 from josmo/netrc-stash-fix

Netrc stash fix
This commit is contained in:
Brad Rydzewski 2016-06-13 22:13:24 -07:00 committed by GitHub
commit f723e8f84c
2 changed files with 46 additions and 15 deletions

View file

@ -95,7 +95,7 @@ func (a *Agent) prep(w *queue.Work) (*yaml.Config, error) {
envs := toEnv(w)
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.
var secrets []*model.Secret
if w.Verified {

View file

@ -17,6 +17,7 @@ import (
"github.com/drone/drone/model"
"github.com/drone/drone/remote"
"github.com/mrjones/oauth"
"strings"
)
// Opts defines configuration options.
@ -115,16 +116,26 @@ func (c *client) Login(res http.ResponseWriter, req *http.Request) (*model.User,
// TODO errors should never be ignored like this
response1, err := client.Get(fmt.Sprintf("%s/rest/api/1.0/users/%s", c.URL, login))
contents, err := ioutil.ReadAll(response1.Body)
if err != nil {
return nil, err
}
defer response1.Body.Close()
var mUser User // TODO prefixing with m* is not a common convention in Go
json.Unmarshal(contents, &mUser) // TODO should not ignore error
contents, err := ioutil.ReadAll(response1.Body)
if err !=nil {
return nil, err
}
var user User
err = json.Unmarshal(contents, &user)
if err != nil {
return nil, err
}
return &model.User{
Login: login,
Email: mUser.EmailAddress,
Email: user.EmailAddress,
Token: accessToken.Token,
Avatar: avatarLink(mUser.EmailAddress),
Avatar: avatarLink(user.EmailAddress),
}, nil
}
@ -143,29 +154,36 @@ func (c *client) Repo(u *model.User, owner, name string) (*model.Repo, error) {
client := NewClientWithToken(&c.Consumer, u.Token)
url := fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s", c.URL, owner, name)
urlString := fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s", c.URL, owner, name)
response, err := client.Get(url)
response, err := client.Get(urlString)
if err != nil {
log.Error(err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
bsRepo := BSRepo{}
json.Unmarshal(contents, &bsRepo)
err = json.Unmarshal(contents, &bsRepo)
if err !=nil {
return nil, err
}
repo := &model.Repo{
Name: bsRepo.Slug,
Owner: bsRepo.Project.Key,
Branch: "master",
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),
}
for _, item := range bsRepo.Links.Clone {
if item.Name == "http" {
repo.Clone = item.Href
uri, err := url.Parse(item.Href)
if err != nil {
return nil, err
}
uri.User = nil
repo.Clone = uri.String()
}
}
for _, item := range bsRepo.Links.Self {
@ -189,8 +207,14 @@ func (c *client) Repos(u *model.User) ([]*model.RepoLite, error) {
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var repoResponse Repos
json.Unmarshal(contents, &repoResponse)
err = json.Unmarshal(contents, &repoResponse)
if err != nil {
return nil, err
}
for _, repo := range repoResponse.Values {
repos = append(repos, &model.RepoLite{
@ -240,12 +264,19 @@ func (*client) Status(*model.User, *model.Repo, *model.Build, string) 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)
if err != nil {
return nil, err
}
//remove the port
tmp := strings.Split(u.Host, ":")
var host = tmp[0]
if err != nil {
return nil, err
}
return &model.Netrc{
Machine: u.Host,
Machine: host,
Login: c.GitUserName,
Password: c.GitPassword,
}, nil