This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
/
github_client.go
206 lines (173 loc) · 4.24 KB
/
github_client.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package git
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"github.com/NoUseFreak/letitgo/internal/app/ui"
"github.com/google/go-github/github"
"github.com/matryer/try"
"github.com/pkg/errors"
"golang.org/x/oauth2"
)
type githubClient struct {
owner string
repo string
client *github.Client
ctx context.Context
}
func NewGithubClient(owner, repo string) (*githubClient, error) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, errors.New("make sure to set GITHUB_TOKEN")
}
ctx := context.Background()
tc := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
return &githubClient{
owner: owner,
repo: repo,
ctx: ctx,
client: github.NewClient(tc),
}, nil
}
func (c *githubClient) PublishFile(path, content, message string, branch *string) error {
opts := github.RepositoryContentGetOptions{}
if branch != nil {
opts.Ref = *branch
if err := c.CreateBranch(*branch); err != nil {
return err
}
}
var sha string
fileContent, _, _, err := c.client.Repositories.GetContents(
c.ctx,
c.owner,
c.repo,
path,
&opts,
)
if err == nil {
if oldContent, err := fileContent.GetContent(); err == nil {
if oldContent == content {
ui.Step("No changes to %s", path)
return nil
}
}
sha = fileContent.GetSHA()
}
_, _, err = c.client.Repositories.CreateFile(
c.ctx,
c.owner,
c.repo,
path,
&github.RepositoryContentFileOptions{
SHA: &sha,
Message: &message,
Content: []byte(content),
Branch: branch,
},
)
return errors.Wrapf(err, "failed creating/update file %s", path)
}
func (c *githubClient) CreateRelease(version, title, description string) (string, error) {
release, _, err := c.client.Repositories.GetReleaseByTag(
c.ctx,
c.owner,
c.repo,
version,
)
if release != nil && err == nil {
return strconv.FormatInt(release.GetID(), 10), nil
}
release, _, err = c.client.Repositories.CreateRelease(
c.ctx,
c.owner,
c.repo,
&github.RepositoryRelease{
TagName: &version,
Name: &title,
Body: &description,
},
)
if err != nil {
return "", errors.Wrap(err, "Failed creating release.")
}
return strconv.FormatInt(release.GetID(), 10), err
}
func (c *githubClient) UploadReleaseAssets(releaseID string, assets []string) error {
rID, err := strconv.ParseInt(releaseID, 10, 64)
if err != nil {
return errors.Wrapf(err, "failed to parse releaseID %s", releaseID)
}
for _, a := range assets {
if err := c.uploadReleaseAsset(rID, a); err != nil {
return err
}
}
return nil
}
func (c *githubClient) uploadReleaseAsset(rID int64, path string) error {
file, err := os.Open(path)
if err != nil {
return errors.Wrapf(err, "Failed to open file %v", path)
}
defer func() { _ = file.Close() }()
_, _, err = c.client.Repositories.UploadReleaseAsset(
c.ctx,
c.owner,
c.repo,
rID,
&github.UploadOptions{
Name: filepath.Base(path),
},
file,
)
return err
}
func (c *githubClient) CreateForkFrom(owner, repo string) error {
_, _, err := c.client.Repositories.CreateFork(c.ctx, owner, repo, &github.RepositoryCreateForkOptions{})
if _, ok := err.(*github.AcceptedError); !ok {
if err != nil {
return errors.Wrapf(err, "Failed to fork %s/%s to %s/%s", owner, repo, c.owner, repo)
}
}
err = try.Do(func(attempt int) (bool, error) {
time.Sleep(5 * time.Second)
var err error
if !c.Exists() {
err = errors.New("does not exist")
}
return attempt < 6, err // try 5 times
})
return errors.Wrap(err, "Failed to verify fork got created.")
}
func (c *githubClient) Exists() bool {
_, _, err := c.client.Repositories.Get(
c.ctx,
c.owner,
c.repo,
)
return err == nil
}
func (c *githubClient) CreateBranch(branch string) error {
ref := fmt.Sprintf("heads/%s", branch)
r, _, _ := c.client.Git.GetRef(c.ctx, c.owner, c.repo, ref)
if r == nil {
r, _, err := c.client.Git.GetRef(c.ctx, c.owner, c.repo, "heads/master")
if err != nil {
return errors.Wrap(err, "Failed to find master branch sha")
}
_, _, err = c.client.Git.CreateRef(c.ctx, c.owner, c.repo, &github.Reference{
Ref: &ref,
Object: r.GetObject(),
})
if err != nil {
return errors.Wrapf(err, "Failed to create branch %s on %s/%s", branch, c.owner, c.repo)
}
}
return nil
}