-
Notifications
You must be signed in to change notification settings - Fork 68
/
upload.go
189 lines (161 loc) · 4.59 KB
/
upload.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
package repoSource
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"github.com/codersrank-org/repo_info_extractor/v2/entities"
"github.com/codersrank-org/repo_info_extractor/v2/ui"
"github.com/pkg/browser"
)
// ArtifactUploader uploads and merge results with codersrank
type ArtifactUploader interface {
UploadRepos(repos []*entities.Repository)
}
type artifactUploader struct {
// UploadRepoURL the API end pont where the artifacts will be uploaded
UploadRepoURL string
UploadResultURL string
ProcessURL string
// OutputPath contains the artifacts that need to be uploaded
OutputPath string
}
// NewArtifactUploader constructor
func NewArtifactUploader(outputPath string) ArtifactUploader {
return &artifactUploader{
UploadRepoURL: "https://grpcgateway.codersrank.io/candidate/privaterepo/Upload",
//UploadRepoURL: "http://localhost:9900/candidate/privaterepo/Upload",
UploadResultURL: "https://grpcgateway.codersrank.io/multi/repo/results",
//UploadResultURL: "http://localhost:9900/multi/repo/results",
ProcessURL: "https://profile.codersrank.io/repo?multiToken=",
//ProcessURL: "http://localhost:8080/repo?multiToken=",
OutputPath: outputPath,
}
}
func (c *artifactUploader) UploadRepos(repos []*entities.Repository) {
uploadResults := make(map[string]string)
done := 1
for _, repo := range repos {
fmt.Printf("Uploading %s results (%d/%d)\n", repo.FullName, done, len(repos))
uploadToken, err := c.uploadRepo(repo.GetSafeFullName())
if err != nil {
fmt.Printf("Couldn't upload, error: %s", err.Error())
continue
}
uploadResults[repo.Name] = uploadToken
done++
}
resultToken := c.uploadResults(uploadResults)
c.processResults(resultToken)
}
func (c *artifactUploader) uploadRepo(repoName string) (string, error) {
// Read file
filename := fmt.Sprintf("%s/%s_v2.json.zip", c.getSaveResultPath(), repoName)
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
// Add file as multipart/form-data
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", filepath.Base(file.Name()))
if err != nil {
return "", err
}
io.Copy(part, file)
writer.Close()
// Create and make the request
request, err := http.NewRequest("POST", c.UploadRepoURL, body)
if err != nil {
return "", err
}
request.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return "", err
}
if response.StatusCode != http.StatusOK {
return "", errors.New("Server returned non 200 response")
}
defer response.Body.Close()
content, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
// Get response and return resulting token
var result CRUploadResult
err = json.Unmarshal(content, &result)
if err != nil {
return "", err
}
return result.Token, nil
}
func (c *artifactUploader) uploadResults(results map[string]string) string {
multiUpload := MultiUpload{}
multiUpload.Results = make([]CRUploadResultWithRepoName, len(results))
i := 0
for reponame, token := range results {
multiUpload.Results[i] = CRUploadResultWithRepoName{
Token: token,
Reponame: reponame,
}
i++
}
b, err := json.Marshal(multiUpload)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("POST", c.UploadResultURL, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var result CRUploadResult
err = json.Unmarshal(body, &result)
if err != nil {
log.Fatal(err)
}
return result.Token
}
func (c *artifactUploader) processResults(resultToken string) {
browserURL := c.ProcessURL + resultToken
ok := ui.Confirm(fmt.Sprintf("You are being navigated to '%s'. Do you wish to proceed?", browserURL))
if ok {
browser.OpenURL(browserURL)
} else {
fmt.Println("Finished")
}
}
func (c *artifactUploader) getSaveResultPath() string {
resultPath := c.OutputPath
if _, err := os.Stat(resultPath); os.IsNotExist(err) {
os.Mkdir(resultPath, 0700)
}
return resultPath
}
// CRUploadResult is the result of single repo upload
type CRUploadResult struct {
Token string `json:"token"`
}
// MultiUpload is the request body
type MultiUpload struct {
Results []CRUploadResultWithRepoName `json:"results"`
}
// CRUploadResultWithRepoName token-reponame pair
type CRUploadResultWithRepoName struct {
Token string `json:"token"`
Reponame string `json:"reponame"`
}