-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathgithub.go
More file actions
109 lines (93 loc) · 2.72 KB
/
github.go
File metadata and controls
109 lines (93 loc) · 2.72 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package github
import (
"context"
_ "embed" // used to print the embedded assets
"github.com/pkg/errors"
"github.com/google/go-github/v37/github"
"github.com/odpf/meteor/models"
commonv1beta1 "github.com/odpf/meteor/models/odpf/assets/common/v1beta1"
assetsv1beta1 "github.com/odpf/meteor/models/odpf/assets/v1beta1"
"github.com/odpf/meteor/plugins"
"github.com/odpf/meteor/registry"
"github.com/odpf/meteor/utils"
"github.com/odpf/salt/log"
"golang.org/x/oauth2"
)
//go:embed README.md
var summary string
// Config holds the set of configuration for the extractor
type Config struct {
Org string `mapstructure:"org" validate:"required"`
Token string `mapstructure:"token" validate:"required"`
}
var sampleConfig = `
org: odpf
token: github_token`
// Extractor manages the extraction of data from the extractor
type Extractor struct {
logger log.Logger
config Config
client *github.Client
}
// Info returns the brief information about the extractor
func (e *Extractor) Info() plugins.Info {
return plugins.Info{
Description: "User list from Github organisation.",
SampleConfig: sampleConfig,
Summary: summary,
Tags: []string{"platform", "extractor"},
}
}
// Validate validates the configuration of the extractor
func (e *Extractor) Validate(configMap map[string]interface{}) (err error) {
return utils.BuildConfig(configMap, &Config{})
}
// Init initializes the extractor
func (e *Extractor) Init(ctx context.Context, configMap map[string]interface{}) (err error) {
err = utils.BuildConfig(configMap, &e.config)
if err != nil {
return plugins.InvalidConfigError{}
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: e.config.Token},
)
tc := oauth2.NewClient(ctx, ts)
e.client = github.NewClient(tc)
return
}
// Extract extracts the data from the extractor
// The data is returned as a list of assets.Asset
func (e *Extractor) Extract(ctx context.Context, emit plugins.Emit) (err error) {
users, _, err := e.client.Organizations.ListMembers(ctx, e.config.Org, nil)
if err != nil {
return errors.Wrap(err, "failed to fetch organizations")
}
for _, user := range users {
usr, _, err := e.client.Users.Get(ctx, *user.Login)
if err != nil {
e.logger.Error("failed to fetch user", "error", err)
continue
}
emit(models.NewRecord(&assetsv1beta1.User{
Resource: &commonv1beta1.Resource{
Urn: usr.GetURL(),
Type: "user",
},
Email: usr.GetEmail(),
Username: usr.GetLogin(),
FullName: usr.GetName(),
Status: "active",
}))
}
return nil
}
// init registers the extractor to catalog
func init() {
if err := registry.Extractors.Register("github", func() plugins.Extractor {
return &Extractor{
logger: plugins.GetLog(),
}
}); err != nil {
panic(err)
}
}