MCPcopy Create free account
hub / github.com/crossjoin-io/crossjoin / fetchGitHubFile

Method fetchGitHubFile

api/github.go:20–50  ·  view source on GitHub ↗
(path string)

Source from the content-addressed store, hash-verified

18}
19
20func (api *API) fetchGitHubFile(path string) ([]byte, error) {
21 req, err := http.NewRequest("GET", path, nil)
22 if err != nil {
23 return nil, fmt.Errorf("create request: %w", err)
24 }
25 if githubToken := os.Getenv("GITHUB_TOKEN"); githubToken != "" {
26 req.Header.Add("authorization", "token "+githubToken)
27 }
28 resp, err := http.DefaultClient.Do(req)
29 if err != nil {
30 return nil, fmt.Errorf("do request: %w", err)
31 }
32 defer resp.Body.Close()
33 githubResp := GitHubContentResponse{}
34 err = json.NewDecoder(resp.Body).Decode(&githubResp)
35 if err != nil {
36 return nil, fmt.Errorf("decode response: %w", err)
37 }
38 if githubResp.Content == "" {
39 return nil, errors.New("missing content")
40 }
41 if githubResp.Encoding != "base64" {
42 return nil, fmt.Errorf("unknown encoding %s", githubResp.Encoding)
43 }
44
45 decoded, err := base64.StdEncoding.DecodeString(strings.ReplaceAll(githubResp.Content, "\n", ""))
46 if err != nil {
47 return nil, fmt.Errorf("decode base64 content: %w", err)
48 }
49 return decoded, nil
50}

Callers 2

LoadConfigMethod · 0.95
readFileMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected