Skip to content

Commit

Permalink
🧹 github provider improvements (#3911)
Browse files Browse the repository at this point in the history
Signed-off-by: Ivan Milchev <[email protected]>
  • Loading branch information
imilchev authored May 2, 2024
1 parent 88a22dd commit 471213b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
22 changes: 21 additions & 1 deletion providers/github/resources/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func getUser(ctx context.Context, runtime *plugin.Runtime, conn *connection.Gith
if g.memoize == nil {
g.memoize = memoize.NewMemoizer(30*time.Minute, 1*time.Hour)
}
res, err, _ := g.memoize.Memoize("user", func() (interface{}, error) {
res, err, _ := g.memoize.Memoize("user-"+user, func() (interface{}, error) {
log.Debug().Msgf("fetching user %s", user)
user, _, err := conn.Client().Users.Get(ctx, user)
return user, err
Expand All @@ -42,6 +42,26 @@ func getUser(ctx context.Context, runtime *plugin.Runtime, conn *connection.Gith
return res.(*github.User), nil
}

func getOrg(ctx context.Context, runtime *plugin.Runtime, conn *connection.GithubConnection, name string) (*github.Organization, error) {
obj, err := CreateResource(runtime, "github-", map[string]*llx.RawData{})
if err != nil {
return nil, err
}
g := obj.(*mqlGithub)
if g.memoize == nil {
g.memoize = memoize.NewMemoizer(30*time.Minute, 1*time.Hour)
}
res, err, _ := g.memoize.Memoize("org"+name, func() (interface{}, error) {
log.Debug().Msgf("fetching organization %s", name)
user, _, err := conn.Client().Organizations.Get(ctx, name)
return user, err
})
if err != nil {
return nil, err
}
return res.(*github.Organization), nil
}

func githubTimestamp(ts *github.Timestamp) *time.Time {
if ts == nil {
return nil
Expand Down
2 changes: 1 addition & 1 deletion providers/github/resources/github_org.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func initGithubOrganization(runtime *plugin.Runtime, args map[string]*llx.RawDat
}
}

org, _, err := conn.Client().Organizations.Get(context.Background(), name)
org, err := getOrg(context.Background(), runtime, conn, name)
if err != nil {
return args, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions providers/github/resources/github_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func initGithubRepository(runtime *plugin.Runtime, args map[string]*llx.RawData)
var err error
orgId, err := conn.Organization()
if err == nil {
obj, err := CreateResource(runtime, "github.organization", map[string]*llx.RawData{
obj, err := NewResource(runtime, "github.organization", map[string]*llx.RawData{
"login": llx.StringData(orgId.Name),
})
if err != nil {
Expand Down Expand Up @@ -584,7 +584,7 @@ func newMqlGithubCommit(runtime *plugin.Runtime, rc *github.RepositoryCommit, ow
}
}

if rc.Author != nil {
if rc.Author != nil && rc.Author.ID != nil && rc.Author.Login != nil {
githubAuthor, err = NewResource(runtime, "github.user", map[string]*llx.RawData{
"id": llx.IntDataPtr(rc.Author.ID),
"login": llx.StringDataPtr(rc.Author.Login),
Expand All @@ -594,7 +594,7 @@ func newMqlGithubCommit(runtime *plugin.Runtime, rc *github.RepositoryCommit, ow
}
}
var githubCommitter interface{}
if rc.Committer != nil {
if rc.Committer != nil && rc.Committer.ID != nil && rc.Committer.Login != nil {
githubCommitter, err = NewResource(runtime, "github.user", map[string]*llx.RawData{
"id": llx.IntDataPtr(rc.Committer.ID),
"login": llx.StringDataPtr(rc.Committer.Login),
Expand Down

0 comments on commit 471213b

Please sign in to comment.