/
client.go
217 lines (187 loc) · 5.58 KB
/
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
207
208
209
210
211
212
213
214
215
216
217
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
gohttp "net/http"
"time"
"github.com/unanet/eve/pkg/scm/types"
"github.com/unanet/go/pkg/errors"
"github.com/unanet/go/pkg/http"
"github.com/unanet/go/pkg/log"
"go.uber.org/zap"
)
type (
Config struct {
GithubAccessToken string `envconfig:"GITHUB_ACCESS_TOKEN"`
GithubBaseUrl string `envconfig:"GITHUB_BASE_URL"`
GithubTimeout time.Duration `envconfig:"GITHUB_TIMEOUT" default:"20s"`
GithubEmailAddress string `envconfig:"GITHUB_EMAIL_ADDRESS" default:"ghost@unknown.tld"`
}
Client struct {
cfg Config
cli *gohttp.Client
}
ReleaseData struct {
TagName string `json:"tag_name"`
}
Tagger struct {
Name string `json:"name"`
Email string `json:"email"`
Date string `json:"date"`
}
TagData struct {
Tag string `json:"tag"`
Object string `json:"object"`
Message string `json:"message"`
Tagger Tagger `json:"tagger"`
Type string `json:"type"`
}
RefData struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
}
)
func NewClient(cfg Config) *Client {
return &Client{
cli: &gohttp.Client{
Transport: http.LoggingTransport,
Timeout: cfg.GithubTimeout,
},
cfg: cfg,
}
}
func (c *Client) createTag(options types.TagOptions) error {
var auth = fmt.Sprintf("token %s", c.cfg.GithubAccessToken)
b, err := json.Marshal(TagData{
Tag: options.TagName,
Object: options.GitHash,
Message: options.TagName,
Tagger: Tagger{
Name: "eve",
Email: c.cfg.GithubEmailAddress,
Date: time.Now().UTC().Format(time.RFC3339),
},
Type: "commit",
})
if err != nil {
return errors.Wrap(err, "failed to marshall tag data")
}
url := fmt.Sprintf("%s/repos/%s/%s/git/tags", c.cfg.GithubBaseUrl, options.Owner, options.Repo)
log.Logger.Info("github tags url", zap.String("url", url))
req, err := gohttp.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
return errors.Wrap(err, "failed to create tag request")
}
req.Header.Set("Authorization", auth)
resp, err := c.cli.Do(req)
if err != nil {
return errors.Wrap(err, "failed to issue tag request")
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Logger.Error("failed to close the git tag body resp", zap.Error(err))
}
}()
if resp.StatusCode > 299 {
return fmt.Errorf("failed to tag github commit: %v", resp.Status)
}
return nil
}
func (c *Client) createRef(options types.TagOptions) error {
var auth = fmt.Sprintf("token %s", c.cfg.GithubAccessToken)
bRef, err := json.Marshal(RefData{
Ref: fmt.Sprintf("refs/tags/%s", options.TagName),
Sha: options.GitHash,
})
if err != nil {
return errors.Wrap(err, "failed to marshall tag data")
}
rurl := fmt.Sprintf("%s/repos/%s/%s/git/refs", c.cfg.GithubBaseUrl, options.Owner, options.Repo)
reqRef, err := gohttp.NewRequest("POST", rurl, bytes.NewBuffer(bRef))
if err != nil {
return errors.Wrap(err, "failed to create tag request")
}
reqRef.Header.Set("Authorization", auth)
refResp, err := c.cli.Do(reqRef)
if err != nil {
return errors.Wrap(err, "failed to issue tag request")
}
defer func() {
if err := refResp.Body.Close(); err != nil {
log.Logger.Error("failed to close the git ref body resp", zap.Error(err))
}
}()
if refResp.StatusCode > 299 {
return fmt.Errorf("failed to create tag ref github commit: %v", refResp.Status)
}
return nil
}
func (c *Client) createRelease(options types.TagOptions) error {
var auth = fmt.Sprintf("token %s", c.cfg.GithubAccessToken)
rurl := fmt.Sprintf("%s/repos/%s/%s/releases", c.cfg.GithubBaseUrl, options.Owner, options.Repo)
bRef, err := json.Marshal(ReleaseData{TagName: options.TagName})
if err != nil {
return errors.Wrap(err, "failed to marshall release data")
}
reqRef, err := gohttp.NewRequest("POST", rurl, bytes.NewBuffer(bRef))
if err != nil {
return errors.Wrap(err, "failed to create release request")
}
reqRef.Header.Set("Authorization", auth)
refResp, err := c.cli.Do(reqRef)
if err != nil {
return errors.Wrap(err, "failed to issue release request")
}
defer func() {
if err := refResp.Body.Close(); err != nil {
log.Logger.Error("failed to close the git release body resp", zap.Error(err))
}
}()
if refResp.StatusCode > 299 {
return fmt.Errorf("failed to create release: %v", refResp.Status)
}
return nil
}
func (c *Client) TagCommit(ctx context.Context, options types.TagOptions) (*types.Tag, error) {
log.Logger.Info("tag git commit", zap.Any("opts", options))
if err := c.createTag(options); err != nil {
return nil, err
}
if err := c.createRef(options); err != nil {
return nil, err
}
if err := c.createRelease(options); err != nil {
return nil, err
}
return &types.Tag{
Name: options.TagName,
Repo: options.Repo,
}, nil
}
func (c *Client) GetTag(ctx context.Context, options types.TagOptions) (*types.Tag, error) {
var auth = fmt.Sprintf("token %s", c.cfg.GithubAccessToken)
url := fmt.Sprintf("%s/repos/%s/%s/git/ref/tags/%s", c.cfg.GithubBaseUrl, options.Owner, options.Repo, options.TagName)
req, err := gohttp.NewRequest("GET", url, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to create get tag request")
}
req.Header.Set("Authorization", auth)
resp, err := c.cli.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to issue get tag request")
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Logger.Error("failed to close the get tag ref body resp", zap.Error(err))
}
}()
if resp.StatusCode > 299 {
return nil, fmt.Errorf("failed to get tag github commit: %v", resp.Status)
}
return &types.Tag{
Name: options.TagName,
Repo: options.Repo,
}, nil
}