Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gabrie30/ghorg
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrie30 committed Mar 26, 2023
2 parents b85190c + e31802c commit 2bcac49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 16 additions & 14 deletions scm/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

var (
_ Client = Github{}
reposPerPage = 100
selfCloneUsername = ""
_ Client = Github{}
reposPerPage = 100
tokenUsername = ""
)

func init() {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 = ""
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:[email protected]/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()
}

0 comments on commit 2bcac49

Please sign in to comment.