-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgithub.go
81 lines (71 loc) · 2.23 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package resources
import (
"context"
"time"
"github.com/google/go-github/v69/github"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/memoize"
"go.mondoo.com/cnquery/v11/providers/github/connection"
)
// We use a global MQL resource for the connection to store the memoizer.
// In this way we can cache requests for things that aren't directly attached to an MQL resource.
// For example, we don't want to fetch the same users multiple times, for that we can use the memoizer.
type mqlGithubInternal struct {
memoize *memoize.Memoizer
}
var (
cacheExpirationTime = 24 * time.Hour
cacheCleanupTime = 48 * time.Hour
)
func getUser(ctx context.Context, runtime *plugin.Runtime, conn *connection.GithubConnection, user string) (*github.User, 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(cacheExpirationTime, cacheCleanupTime)
}
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
})
if err != nil {
return nil, err
}
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(cacheExpirationTime, cacheCleanupTime)
}
res, err, _ := g.memoize.Memoize("org-"+name, func() (interface{}, error) {
log.Debug().Msgf("fetching organization %s", name)
org, _, err := conn.Client().Organizations.Get(ctx, name)
return org, err
})
if err != nil {
return nil, err
}
return res.(*github.Organization), nil
}
func githubTimestamp(ts *github.Timestamp) *time.Time {
if ts == nil {
return nil
}
return &ts.Time
}
const (
paginationPerPage = 100
workers = 10
)