Skip to content

Commit

Permalink
Merge pull request #161 from carlosms/fix-default-auth
Browse files Browse the repository at this point in the history
Fix default auth
  • Loading branch information
carlosms authored Aug 22, 2018
2 parents b26c567 + 121a9ed commit 15b1019
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
21 changes: 16 additions & 5 deletions cmd/lookout/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ func (c *ServeCommand) Execute(args []string) error {
}

func (c *ServeCommand) initProvider(conf Config) error {
noDeafultAuth := c.GithubUser == "" || c.GithubToken == ""
defaultConfig := github.ClientConfig{
User: c.GithubUser,
Token: c.GithubToken,
}

switch c.Provider {
case github.Provider:
urls := strings.Split(c.Positional.Repository, ",")
Expand All @@ -155,16 +161,21 @@ func (c *ServeCommand) initProvider(conf Config) error {
for _, url := range urls {
conf, ok := repoToConfig[url]
if !ok {
log.Infof("use default token for repository %s", url)
if noDeafultAuth {
// Empty github auth is only useful for --dry-run,
// we may want to enforce this as an error
log.Warningf("missing authentication for repository %s, and no default provided", url)
} else {
log.Infof("using default authentication for repository %s", url)
}

conf = defaultConfig
}
urlToConfig[url] = conf
}

cache := cache.NewValidableCache(diskcache.New("/tmp/github"))
pool, err := github.NewClientPoolFromTokens(urlToConfig, github.ClientConfig{
User: c.GithubUser,
Token: c.GithubToken,
}, cache)
pool, err := github.NewClientPoolFromTokens(urlToConfig, cache)
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions provider/github/token_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ type ClientConfig struct {
MinInterval string
}

var zeroClientConfig = &ClientConfig{}
var zeroClientConfig = ClientConfig{}

// IsZero return true if config is empty and false otherwise
func (c *ClientConfig) IsZero() bool {
func (c ClientConfig) IsZero() bool {
return c == zeroClientConfig
}

// NewClientPoolFromTokens creates new ClientPool based on map[repoURL]ClientConfig
// later we will need another constructor that would request installations and create pool from it
func NewClientPoolFromTokens(urlToConfig map[string]ClientConfig, defaultConfig ClientConfig, cache *cache.ValidableCache) (*ClientPool, error) {
func NewClientPoolFromTokens(urlToConfig map[string]ClientConfig, cache *cache.ValidableCache) (*ClientPool, error) {
byConfig := make(map[ClientConfig][]*lookout.RepositoryInfo)

for url, c := range urlToConfig {
Expand All @@ -34,10 +34,6 @@ func NewClientPoolFromTokens(urlToConfig map[string]ClientConfig, defaultConfig
return nil, err
}

if c.IsZero() {
c = defaultConfig
}

byConfig[c] = append(byConfig[c], repo)
}

Expand Down

0 comments on commit 15b1019

Please sign in to comment.