-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
78 lines (63 loc) · 2.18 KB
/
client.go
File metadata and controls
78 lines (63 loc) · 2.18 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
package ghaprofiler
import (
"context"
"net/http"
"github.com/google/go-github/v32/github"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/diskcache"
"golang.org/x/oauth2"
)
var userAgent = "github-actions-profiler (+https://github.com/utgwkk/github-actions-profiler)"
type Client struct {
githubClient *github.Client
}
type ClientConfig struct {
AccessToken string
Cache bool
CacheDirectory string
}
func NewClientWithConfig(ctx context.Context, config *ClientConfig) *Client {
client := &Client{
githubClient: github.NewClient(nil),
}
if config == nil {
return client
}
var cacheTransport *httpcache.Transport
var oauth2Client *http.Client
if config.Cache {
diskCache := diskcache.New(config.CacheDirectory)
cacheTransport = httpcache.NewTransport(diskCache)
}
if config.AccessToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.AccessToken},
)
oauth2Client = oauth2.NewClient(ctx, ts)
}
if oauth2Client != nil {
if cacheTransport != nil {
cacheTransport.Transport = oauth2Client.Transport
client.githubClient = github.NewClient(cacheTransport.Client())
} else {
client.githubClient = github.NewClient(oauth2Client)
}
} else {
if cacheTransport != nil {
client.githubClient = github.NewClient(cacheTransport.Client())
} else {
client.githubClient = github.NewClient(nil)
}
}
client.githubClient.UserAgent = userAgent
return client
}
func (c Client) GetWorkflowJobByID(ctx context.Context, owner, repo string, jobID int64) (*github.WorkflowJob, *github.Response, error) {
return c.githubClient.Actions.GetWorkflowJobByID(ctx, owner, repo, jobID)
}
func (c Client) ListWorkflowJobs(ctx context.Context, owner, repo string, runID int64, opts *github.ListWorkflowJobsOptions) (*github.Jobs, *github.Response, error) {
return c.githubClient.Actions.ListWorkflowJobs(ctx, owner, repo, runID, opts)
}
func (c Client) ListWorkflowRunsByFileName(ctx context.Context, owner, repo, workflowFileName string, opts *github.ListWorkflowRunsOptions) (*github.WorkflowRuns, *github.Response, error) {
return c.githubClient.Actions.ListWorkflowRunsByFileName(ctx, owner, repo, workflowFileName, opts)
}