-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub.go
168 lines (151 loc) · 4.25 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/google/go-github/v66/github"
)
type Action struct {
RegistrationToken *github.RegistrationToken
OrgRunner bool
}
type GitHubClient struct {
Client *github.Client
Token string
Repo string
Owner string
Action *Action
}
func (c *GitHubClient) NewGithubClient(config *GithubRunnerConfig) (*GitHubClient, error) {
httpClient := &http.Client{}
client := github.NewClient(httpClient).WithAuthToken(config.GitHubToken)
//
owner, repository, err := parseRepositoryURL(os.Getenv("GITHUB_REPOSITORY"))
if err != nil {
return nil, err
}
return &GitHubClient{
Client: client,
Token: config.GitHubToken,
Owner: owner,
Repo: repository,
Action: &Action{OrgRunner: config.GitHubOrgRunner},
}, nil
}
func parseRepositoryURL(url string) (string, string, error) {
// Basic repository URL parsing
parts := strings.Split(strings.TrimPrefix(url, "https://github.com/"), "/")
if len(parts) < 2 {
return "", "", fmt.Errorf("invalid repository URL")
}
return parts[0], parts[1], nil
}
// WaitForRunnerRegistered waits for a GitHub self-hosted runner to be registered and come online.
// It checks the runner's status every 10 seconds for up to 5 minutes.
//
// Parameters:
//
// ctx - The context to control cancellation and timeout.
// label - The label of the runner to wait for.
//
// Returns:
//
// error - An error if the runner is not registered within the timeout period or if there is an issue listing the runners.
func (r *GitHubClient) WaitForRunnerRegistered(ctx context.Context, label string) error {
timeout := time.After(5 * time.Minute)
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-timeout:
return errors.New("timeout waiting for runner to register")
case <-ticker.C:
runners, _, err := r.Client.Actions.ListRunners(ctx, r.Owner, r.Repo, nil)
if err != nil {
return err
}
for _, runner := range runners.Runners {
if runner.GetName() == label && runner.GetStatus() == "online" {
fmt.Printf("GitHub self-hosted runner %s is registered and ready to use\n", runner.GetName())
return nil
}
}
}
fmt.Println("Waiting for runner to register...")
}
}
// CreateGitHubRegistrationToken creates a registration token for adding a self-hosted runner.
//
// Parameters:
//
// ctx - The context to control cancellation and timeout.
// githubRunner - The type of runner ("org" for organization runner, otherwise repository runner).
//
// Returns:
//
// *github.RegistrationToken - The registration token.
// error - An error if the token creation fails.
func (r *GitHubClient) CreateGitHubRegistrationToken(ctx context.Context, githubRunner string) (*github.RegistrationToken, error) {
var (
token *github.RegistrationToken
err error
)
if r.Action.OrgRunner {
token, _, err = r.Client.Actions.CreateOrganizationRegistrationToken(ctx, r.Owner)
} else {
token, _, err = r.Client.Actions.CreateRegistrationToken(ctx, r.Owner, r.Repo)
}
if err != nil {
return nil, err
}
return token, nil
}
// getGithubRunnerID retrieves the ID of a GitHub self-hosted runner by its label.
//
// Parameters:
//
// ctx - The context to control cancellation and timeout.
// label - The label of the runner.
//
// Returns:
//
// int64 - The ID of the runner, or 0 if not found.
func (r *GitHubClient) getGithubRunnerID(ctx context.Context, label string) int64 {
runners, _, err := r.Client.Actions.ListRunners(ctx, r.Owner, r.Repo, nil)
if err != nil {
return 0
}
for _, runner := range runners.Runners {
if runner.GetName() == label {
return runner.GetID()
}
}
return 0
}
// RemoveGithubRunner removes a self-hosted runner from the repository or organization.
//
// Parameters:
//
// ctx - The context to control cancellation and timeout.
// label - The label of the runner to remove.
//
// Returns:
//
// error - An error if the runner removal fails.
func (r *GitHubClient) RemoveGithubRunner(ctx context.Context, label string) error {
runnerID := r.getGithubRunnerID(ctx, label)
if runnerID == 0 {
return errors.New("runner not found")
}
_, err := r.Client.Actions.RemoveRunner(ctx, r.Owner, r.Repo, runnerID)
if err != nil {
return err
}
return nil
}