go-KodeStarter/cmdLists/gitHub.go
2023-07-08 00:45:18 -04:00

82 lines
2.0 KiB
Go

package cmdLists
import (
"context"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func ListGitHubOrganization() ([]string, error) {
var orgs []string
return orgs, nil
}
func ListGitHubProjectsfromUser(username string, token string) ([]*github.Repository, error) {
// Replace with your access token
//token = "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"
// Create an oauth2 token source with the access token
tokenSource := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
// Create an oauth2 http client with the token source
oauth2Client := oauth2.NewClient(context.Background(), tokenSource)
// Create a new github client with the oauth2 http client
client := github.NewClient(oauth2Client)
// List the repositories of the user
repos, _, err := client.Repositories.List(context.Background(), username, nil)
if err != nil {
fmt.Println(err)
return nil, err
}
return repos, nil
}
// function that return a list of github projects base on a search string
func SearchGitHubProjectsfromSearch(search string, token string) ([]*github.Repository, error) {
// Create client
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client := github.NewClient(tc)
// Search repositories
results, _, err := client.Search.Repositories(context.Background(), search, &github.SearchOptions{})
if err != nil {
return nil, err
}
// Return list of repositories
var repos []*github.Repository
// for _, repo := range results.Repositories {
// repos = append(repos, &repo)
// }
fmt.Printf("Got %d results from GitHub\n", len(results.Repositories))
for _, repo := range results.Repositories {
fmt.Printf("%s %s\n", *repo.Name, *repo.HTMLURL)
repos = append(repos, &repo)
}
fmt.Printf("Appended %d repos to slice\n", len(repos))
for i, repo := range repos {
fmt.Printf("%d %s %s\n", i, *repo.Name, *repo.HTMLURL)
}
fmt.Println("Number of repos:", len(repos))
fmt.Println("Number of results:", *results.Total)
return repos, nil
}