-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithub.go
62 lines (51 loc) · 1.74 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
package fetchers
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
// GithubCopilotFetcher implements the IPRangeFetcher interface for GitHub Copilot.
type GithubCopilotFetcher struct{}
func (f GithubCopilotFetcher) Name() string {
return "GithubCopilot"
}
func (f GithubCopilotFetcher) Description() string {
return "Fetches IP ranges for GitHub Copilot services."
}
func (f GithubCopilotFetcher) FetchIPRanges() ([]string, error) {
// https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/about-githubs-ip-addresses
resp, err := http.Get("https://api.github.com/meta")
if err != nil {
return nil, fmt.Errorf("failed to fetch GitHub Copilot IP ranges: %v", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body from GitHub Copilot: %v", err)
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal GitHub Copilot JSON: %v", err)
}
// Use the "copilot" key to get the IP ranges
copilotValue, ok := result["copilot"]
if !ok {
return nil, fmt.Errorf("no 'copilot' key found in GitHub Copilot response")
}
// Convert the copilot value to a []interface{}
copilotRangesInterface, ok := copilotValue.([]interface{})
if !ok {
return nil, fmt.Errorf("unexpected type for 'copilot' key: expected []interface{}, got %T", copilotValue)
}
// Convert []interface{} to []string
var copilotRanges []string
for _, v := range copilotRangesInterface {
ipRange, ok := v.(string)
if !ok {
return nil, fmt.Errorf("unexpected type in 'copilot' IP ranges: expected string, got %T", v)
}
copilotRanges = append(copilotRanges, ipRange)
}
return copilotRanges, nil
}