/
plugin.go
197 lines (178 loc) · 4.92 KB
/
plugin.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
package main
import (
"bytes"
"fmt"
"github.com/JonasFranzDEV/drone-crowdin/responses"
"github.com/JonasFranzDEV/drone-crowdin/utils"
"io"
"mime/multipart"
"net/http"
"os"
)
type (
// Files is a mapping between the crowdin path and the real file path
Files map[string]string
// Config stores the credentials for the crowdin API
Config struct {
Key string
Identifier string
}
// Plugin represents the drone-crowdin plugin including config and file-mapping.
Plugin struct {
Config Config
Files Files
Branch string
Languages []string
ExportDirectory string
DoDownload bool
}
)
// ToProjectURL returns the base URL of the api endpoint
func (c Config) ToProjectURL() string {
return fmt.Sprintf("https://api.crowdin.com/api/project/%s", c.Identifier)
}
// ToUploadURL returns the API-endpoint including identifier and API-KEY
func (c Config) ToUploadURL() string {
return fmt.Sprintf("%s/update-file?key=%s", c.ToProjectURL(), c.Key)
}
// ToLanguageDownloadURL returns the download endpoint for the given language
func (p Plugin) ToLanguageDownloadURL(language string) string {
if p.Branch != "" {
return fmt.Sprintf("%s/download/%s.zip?key=%s&branch=%s", p.Config.ToProjectURL(), language, p.Config.Key, p.Branch)
}
return fmt.Sprintf("%s/download/%s.zip?key=%s", p.Config.ToProjectURL(), language, p.Config.Key)
}
// Exec starts the plugin and updates the crowdin translation by uploading files from the files map
func (p Plugin) Exec() error {
client := &http.Client{}
//SECTION: Upload
if len(p.Files) > 20 {
return fmt.Errorf("20 files max are allowed to upload. %d files given", len(p.Files))
} else if len(p.Files) > 0 {
req, err := p.buildUploadRequest()
if err != nil {
return fmt.Errorf("error while building upload request: %v", err)
}
resp, err := client.Do(req)
if err != nil {
return err
}
body := &bytes.Buffer{}
if _, err := body.ReadFrom(resp.Body); err != nil {
return err
}
if err := resp.Body.Close(); err != nil {
return err
}
if resp.StatusCode != 200 {
if e, err := responses.ParseAsError(body); err != nil {
return err
} else {
return fmt.Errorf("error while uploading: %v", e)
}
}
var success = new(responses.Success)
if success, err = responses.ParseAsSuccess(body); err != nil {
return err
}
for _, file := range success.Stats {
fmt.Printf("%s: %s\n", file.Name, file.Status)
}
}
//SECTION: Download
if p.DoDownload {
if err := p.buildTranslations(client); err != nil {
return fmt.Errorf("error while building languages: %v", err)
}
for _, language := range p.Languages {
if err := p.downloadLanguage(client, language); err != nil {
return fmt.Errorf("error while downloading %s: %v", language, err)
}
fmt.Printf("Downloaded package: %s\n", language)
}
}
return nil
}
func (p Plugin) buildUploadRequest() (*http.Request, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
for crowdinPath, path := range p.Files {
var err error
var file *os.File
if file, err = os.Open(path); err != nil {
return nil, err
}
defer file.Close()
part, err := writer.CreateFormFile(fmt.Sprintf("files[%s]", crowdinPath), crowdinPath)
if err != nil {
return nil, err
}
if _, err = io.Copy(part, file); err != nil {
return nil, err
}
}
// Adding branch if it is not ignored
if p.Branch != "" {
writer.WriteField("branch", p.Branch)
}
if err := writer.Close(); err != nil {
return nil, err
}
var req *http.Request
var err error
if req, err = http.NewRequest("POST", p.Config.ToUploadURL(), body); err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
return req, nil
}
func (p Plugin) buildTranslations(client *http.Client) error {
// Step 1: Export translations (aka generate server side)
exportURL := fmt.Sprintf("%s/export?key=%s", p.Config.ToProjectURL(), p.Config.Key)
if p.Branch != "" {
exportURL = fmt.Sprintf("%s&branch=%s", exportURL, p.Branch)
}
if resp, err := client.Get(exportURL); err != nil {
return err
} else if resp.StatusCode != 200 {
defer resp.Body.Close()
if e, err := responses.ParseAsError(resp.Body); err != nil {
return err
} else {
return e
}
} else {
defer resp.Body.Close()
}
return nil
}
func (p Plugin) downloadLanguage(client *http.Client, language string) error {
file, err := downloadFromUrl(p.ToLanguageDownloadURL(language))
if err != nil {
return err
}
err = utils.Unzip(file.Name(), p.ExportDirectory)
if err != nil {
return err
}
err = os.Remove(file.Name())
if err != nil {
return err
}
return nil
}
func downloadFromUrl(url string) (*os.File, error) {
output, err := os.Create("lang.zip")
if err != nil {
return nil, err
}
defer output.Close()
response, err := http.Get(url)
if err != nil {
os.Remove(output.Name())
return nil, err
}
defer response.Body.Close()
_, err = io.Copy(output, response.Body)
return output, err
}