-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathrunnerapi.go
More file actions
122 lines (101 loc) · 3.55 KB
/
runnerapi.go
File metadata and controls
122 lines (101 loc) · 3.55 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
110
111
112
113
114
115
116
117
118
119
120
121
122
package githubapi
import (
"context"
"github.com/go-logr/logr"
"github.com/google/go-github/v47/github"
"github.com/gregjones/httpcache"
"github.com/palantir/go-githubapp/githubapp"
)
// IRunnerAPI is a service towards GitHubs runners
type IRunnerAPI interface {
GetRunners(ctx context.Context, organization string, repository string, token string) ([]*github.Runner, error)
UnregisterRunner(ctx context.Context, organization string, repository string, token string, runnerID int64) error
CreateRegistrationToken(ctx context.Context, organization string, repository string, token string) (*github.RegistrationToken, error)
}
type runnerAPI struct {
clientCreator githubapp.ClientCreator
}
// NewRunnerAPI gets a new instance of the API.
func NewRunnerAPI() (runnerAPI, error) {
config := githubapp.Config{
V3APIURL: "https://api.github.com",
V4APIURL: "https://api.github.com",
}
config.SetValuesFromEnv("")
clientCreator, err := githubapp.NewDefaultCachingClientCreator(config,
githubapp.WithClientUserAgent("evryfs/garo"),
githubapp.WithClientCaching(true, func() httpcache.Cache { return httpcache.NewMemoryCache() }),
)
return runnerAPI{
clientCreator: clientCreator,
}, err
}
func (r runnerAPI) getClient(ctx context.Context, organization string, token string) (*github.Client, error) {
if token != "" {
return r.clientCreator.NewTokenClient(token)
}
client, err := r.clientCreator.NewAppClient()
if err != nil {
return nil, err
}
installationsService := githubapp.NewInstallationsService(client)
installation, err := installationsService.GetByOwner(ctx, organization)
if err != nil {
return nil, err
}
return r.clientCreator.NewInstallationClient(installation.ID)
}
// Return all runners for the org
func (r runnerAPI) GetRunners(ctx context.Context, organization string, repository string, token string) ([]*github.Runner, error) {
logger := logr.FromContextOrDiscard(ctx)
client, err := r.getClient(ctx, organization, token)
if err != nil {
return nil, err
}
var allRunners []*github.Runner
opts := &github.ListOptions{PerPage: 30}
for {
var runners *github.Runners
var response *github.Response
var err error
if repository != "" {
runners, response, err = client.Actions.ListRunners(ctx, organization, repository, opts)
} else {
runners, response, err = client.Actions.ListOrganizationRunners(ctx, organization, opts)
}
if err != nil {
return allRunners, err
}
logger.Info("GerRunners GitHub Api Rate limit", "Rate", response.Rate)
allRunners = append(allRunners, runners.Runners...)
if response.NextPage == 0 {
break
}
opts.Page = response.NextPage
}
return allRunners, nil
}
func (r runnerAPI) UnregisterRunner(ctx context.Context, organization string, repository string, token string, runnerID int64) error {
client, err := r.getClient(ctx, organization, token)
if err != nil {
return err
}
if repository != "" {
_, err := client.Actions.RemoveRunner(ctx, organization, repository, runnerID)
return err
}
_, err = client.Actions.RemoveOrganizationRunner(ctx, organization, runnerID)
return err
}
func (r runnerAPI) CreateRegistrationToken(ctx context.Context, organization string, repository string, token string) (*github.RegistrationToken, error) {
client, err := r.getClient(ctx, organization, token)
if err != nil {
return nil, err
}
if repository != "" {
regToken, _, err := client.Actions.CreateRegistrationToken(ctx, organization, repository)
return regToken, err
}
regToken, _, err := client.Actions.CreateOrganizationRegistrationToken(ctx, organization)
return regToken, err
}