-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
58 lines (50 loc) · 1.66 KB
/
github.go
File metadata and controls
58 lines (50 loc) · 1.66 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
package main
import (
"context"
"errors"
"fmt"
"github.com/bartventer/httpcache"
_ "github.com/bartventer/httpcache/store/memcache" // Register the in-memory backend
"github.com/google/go-github/v73/github"
"github.com/pufferpanel/github-runner-scaler/env"
)
var githubClient = github.NewClient(httpcache.NewClient("memcache://")).WithAuthToken(env.Get("github.token"))
var githubOrganization = env.Get("github.organization")
var githubLabel = env.Get("github.label")
var githubGroup = env.Get("github.group")
var githubRunnerPrefix = env.Get("github.runnerprefix")
func GetJITConfig(id int) (string, error) {
groupId, err := GetRunnerGroupId()
if err != nil {
return "", err
}
config, response, err := githubClient.Actions.GenerateOrgJITConfig(context.Background(), githubOrganization, &github.GenerateJITConfigRequest{
Name: fmt.Sprintf("%s-%d", githubRunnerPrefix, id),
Labels: []string{githubLabel},
RunnerGroupID: groupId,
})
defer CloseGithubResponse(response)
if err != nil {
return "", err
}
return config.GetEncodedJITConfig(), err
}
func GetRunnerGroupId() (int64, error) {
groups, response, err := githubClient.Actions.ListOrganizationRunnerGroups(context.Background(), githubOrganization, &github.ListOrgRunnerGroupOptions{})
defer CloseGithubResponse(response)
if err != nil {
return 0, err
}
for _, g := range groups.RunnerGroups {
if *g.Name == githubGroup {
return *g.ID, nil
}
}
return 0, errors.New("runner group not found")
}
// CloseGithubResponse GitHub's wrapper means we can't use our own one...
func CloseGithubResponse(response *github.Response) {
if response != nil {
CloseResponse(response.Response)
}
}