diff --git a/CHANGELOG.md b/CHANGELOG.md index a1246aed..9fcbd2d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) - Token length checks ### Fixed - Gitea tokens from not being found in config.yaml; thanks @Antfere -- Self clone with new GH fine grain tokens; thanks @verybadsoldier +- HTTPS GitHub clones with new GH fine grain tokens; thanks @verybadsoldier ### Security - Bump github.com/spf13/viper from 1.14.0 to 1.15.0 - Bump github.com/fatih/color from 1.13.0 to 1.14.1 diff --git a/scm/github.go b/scm/github.go index 8a92f214..548a8883 100644 --- a/scm/github.go +++ b/scm/github.go @@ -13,9 +13,9 @@ import ( ) var ( - _ Client = Github{} - reposPerPage = 100 - selfCloneUsername = "" + _ Client = Github{} + reposPerPage = 100 + tokenUsername = "" ) func init() { @@ -41,6 +41,8 @@ func (c Github) GetOrgRepos(targetOrg string) ([]Repo, error) { ListOptions: github.ListOptions{PerPage: c.perPage}, } + c.SetTokensUsername() + // get all pages of results var allRepos []*github.Repository for { @@ -75,12 +77,9 @@ func (c Github) GetOrgRepos(targetOrg string) ([]Repo, error) { // GetUserRepos gets user repos func (c Github) GetUserRepos(targetUser string) ([]Repo, error) { - - userToken, _, _ := c.Users.Get(context.Background(), "") - - if targetUser == userToken.GetLogin() { + c.SetTokensUsername() + if targetUser == tokenUsername { colorlog.PrintSubtleInfo("\nCloning all your public/private repos. This process may take a bit longer than other clones, please be patient...") - selfCloneUsername = targetUser targetUser = "" } @@ -151,12 +150,7 @@ func (_ Github) NewClient() (Client, error) { func (_ Github) addTokenToHTTPSCloneURL(url string, token string) string { splitURL := strings.Split(url, "https://") - - // When a user is cloning themselves add the username - if selfCloneUsername != "" { - return "https://" + selfCloneUsername + ":" + token + "@" + splitURL[1] - } - return "https://" + token + "@" + splitURL[1] + return "https://" + tokenUsername + ":" + token + "@" + splitURL[1] } func (c Github) filter(allRepos []*github.Repository) []Repo { @@ -218,3 +212,11 @@ func (c Github) filter(allRepos []*github.Repository) []Repo { return repoData } + +// Sets the GitHub username tied to the github token to the package variable tokenUsername +// Then if https clone method is used the clone url will be https://username:token@github.com/org/repo.git +// The username is now needed when using the new fine-grained tokens for github +func (c Github) SetTokensUsername() { + userToken, _, _ := c.Users.Get(context.Background(), "") + tokenUsername = userToken.GetLogin() +}