-
Notifications
You must be signed in to change notification settings - Fork 3
/
github.go
40 lines (32 loc) · 863 Bytes
/
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
package main
import (
"context"
"fmt"
"time"
"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)
// Config contains the config for the Github Client.
type Config struct {
Token string
Organization string
TimeOut time.Duration
}
// Client wraps the Github Client and some other metadata together.
type Client struct {
*Config
Client *github.Client
}
// CreateClient creates a Github client
func (c *Config) CreateClient() (*Client, error) {
if c.Token == "" {
return nil, fmt.Errorf("unable to create a Github Client: No token was provided")
}
ctx, _ := context.WithTimeout(context.Background(), c.TimeOut)
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: c.Token}))
ghClient := github.NewClient(httpClient)
return &Client{
Client: ghClient,
Config: ghConfig,
}, nil
}