This repository has been archived by the owner on Jan 5, 2024. It is now read-only.
/
gh_adapter.go
57 lines (46 loc) · 1.47 KB
/
gh_adapter.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
package adapter
import (
"context"
"fmt"
"net/http"
"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/google/go-github/v52/github"
"github.com/karahiyo/actions-job/config"
)
type GitHubAdapter interface {
DownloadContent(ctx context.Context, owner, repo, path, ref string) (string, error)
}
type gitHubAdapter struct {
ghClient *github.Client
}
func NewGitHubAdapter(conf config.GitHubAppConfig) (GitHubAdapter, error) {
// Shared transport to reuse TCP connections
tr := http.DefaultTransport
// Wrap the shared transport for use with GitHub App authentication.
itr, err := ghinstallation.New(
tr,
conf.AppID,
conf.InstallationID,
[]byte(conf.PrivateKey),
)
if err != nil {
return nil, fmt.Errorf("failed to ghinstallation.New: %w", err)
}
// Use installation transport
c := github.NewClient(&http.Client{
Transport: itr,
Timeout: conf.RequestTimeout,
})
return &gitHubAdapter{ghClient: c}, nil
}
func (c *gitHubAdapter) DownloadContent(ctx context.Context, owner, repo, path, ref string) (string, error) {
content, _, _, err := c.ghClient.Repositories.GetContents(ctx, owner, repo, path, &github.RepositoryContentGetOptions{Ref: ref})
if err != nil {
return "", fmt.Errorf("failed to download github repository content: owner=%s, repo=%s, path=%s, ref=%s, err=%w", owner, repo, path, ref, err)
}
decoded, err := content.GetContent()
if err != nil {
return "", fmt.Errorf("failed to decode content body: %w", err)
}
return decoded, nil
}