2024-08-04 19:44:50 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2024-08-04 22:59:57 +00:00
|
|
|
"fmt"
|
2024-08-04 19:44:50 +00:00
|
|
|
"log/slog"
|
2024-08-04 22:59:57 +00:00
|
|
|
"sync"
|
2024-08-04 19:44:50 +00:00
|
|
|
|
|
|
|
"github.com/badjware/gitlabfs/config"
|
|
|
|
"github.com/badjware/gitlabfs/fstree"
|
|
|
|
"github.com/google/go-github/v63/github"
|
|
|
|
)
|
|
|
|
|
|
|
|
type githubClient struct {
|
|
|
|
config.GithubClientConfig
|
|
|
|
client *github.Client
|
|
|
|
|
|
|
|
logger *slog.Logger
|
|
|
|
|
|
|
|
rootContent map[string]fstree.GroupSource
|
2024-08-04 22:59:57 +00:00
|
|
|
|
|
|
|
// API response cache
|
|
|
|
organizationCacheMux sync.RWMutex
|
|
|
|
organizationNameToIDMap map[string]int64
|
|
|
|
organizationCache map[int64]*Organization
|
2024-08-05 01:00:34 +00:00
|
|
|
userCacheMux sync.RWMutex
|
|
|
|
userNameToIDMap map[string]int64
|
|
|
|
userCache map[int64]*User
|
2024-08-04 19:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClient(logger *slog.Logger, config config.GithubClientConfig) (*githubClient, error) {
|
|
|
|
client := github.NewClient(nil)
|
|
|
|
if config.Token != "" {
|
|
|
|
client = client.WithAuthToken(config.Token)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitHubClient := &githubClient{
|
|
|
|
GithubClientConfig: config,
|
|
|
|
client: client,
|
|
|
|
|
|
|
|
logger: logger,
|
2024-08-04 22:59:57 +00:00
|
|
|
|
|
|
|
rootContent: nil,
|
|
|
|
|
|
|
|
organizationNameToIDMap: map[string]int64{},
|
|
|
|
organizationCache: map[int64]*Organization{},
|
2024-08-05 01:00:34 +00:00
|
|
|
userNameToIDMap: map[string]int64{},
|
|
|
|
userCache: map[int64]*User{},
|
2024-08-04 19:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return gitHubClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubClient) FetchRootGroupContent() (map[string]fstree.GroupSource, error) {
|
2024-08-04 22:59:57 +00:00
|
|
|
if c.rootContent == nil {
|
|
|
|
rootContent := make(map[string]fstree.GroupSource)
|
|
|
|
|
|
|
|
for _, org_name := range c.GithubClientConfig.OrgNames {
|
|
|
|
org, err := c.fetchOrganization(org_name)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn(err.Error())
|
|
|
|
} else {
|
|
|
|
rootContent[org.Name] = org
|
|
|
|
}
|
|
|
|
}
|
2024-08-05 01:00:34 +00:00
|
|
|
|
|
|
|
for _, user_name := range c.GithubClientConfig.UserNames {
|
|
|
|
user, err := c.fetchUser(user_name)
|
|
|
|
if err != nil {
|
|
|
|
c.logger.Warn(err.Error())
|
|
|
|
} else {
|
|
|
|
rootContent[user.Name] = user
|
|
|
|
}
|
|
|
|
}
|
2024-08-04 22:59:57 +00:00
|
|
|
// TODO: user + current user
|
|
|
|
|
|
|
|
c.rootContent = rootContent
|
|
|
|
}
|
|
|
|
return c.rootContent, nil
|
2024-08-04 19:44:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *githubClient) FetchGroupContent(gid uint64) (map[string]fstree.GroupSource, map[string]fstree.RepositorySource, error) {
|
2024-08-04 22:59:57 +00:00
|
|
|
if org, found := c.organizationCache[int64(gid)]; found {
|
|
|
|
return c.fetchOrganizationContent(org)
|
|
|
|
}
|
2024-08-05 01:00:34 +00:00
|
|
|
if user, found := c.userCache[int64(gid)]; found {
|
|
|
|
return c.fetchUserContent(user)
|
|
|
|
}
|
2024-08-04 22:59:57 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid gid: %v", gid)
|
2024-08-04 19:44:50 +00:00
|
|
|
}
|