|
|
|
@ -13,23 +13,28 @@ import ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
// ToURL returns the API-endpoint including identifier and API-KEY
|
|
|
|
|
func (c Config) ToURL() string { |
|
|
|
|
return fmt.Sprintf("https://api.crowdin.com/api/project/%s/update-file?key=%s", c.Identifier, c.Key) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Exec starts the plugin and updates the crowdin translation by uploading files from the files map
|
|
|
|
|
func (p Plugin) Exec() error { |
|
|
|
|
if len(p.Files) > 20 { |
|
|
|
|
return fmt.Errorf("20 files max are allowed to upload. %d files given", len(p.Files)) |
|
|
|
@ -37,7 +42,7 @@ func (p Plugin) Exec() error { |
|
|
|
|
|
|
|
|
|
body := &bytes.Buffer{} |
|
|
|
|
writer := multipart.NewWriter(body) |
|
|
|
|
for crowdin_path, path := range p.Files { |
|
|
|
|
for crowdinPath, path := range p.Files { |
|
|
|
|
var err error |
|
|
|
|
var file *os.File |
|
|
|
|
if file, err = os.Open(path); err != nil { |
|
|
|
@ -45,7 +50,7 @@ func (p Plugin) Exec() error { |
|
|
|
|
} |
|
|
|
|
defer file.Close() |
|
|
|
|
|
|
|
|
|
part, err := writer.CreateFormFile(fmt.Sprintf("files[%s]", crowdin_path), crowdin_path) |
|
|
|
|
part, err := writer.CreateFormFile(fmt.Sprintf("files[%s]", crowdinPath), crowdinPath) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
@ -77,13 +82,13 @@ func (p Plugin) Exec() error { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
if resp.StatusCode != 200 { |
|
|
|
|
var err_response = new(responses.Error) |
|
|
|
|
var errResponse = new(responses.Error) |
|
|
|
|
decoder := xml.NewDecoder(body) |
|
|
|
|
decoder.CharsetReader = charset.NewReaderLabel |
|
|
|
|
if err := decoder.Decode(&err_response); err != nil { |
|
|
|
|
if err := decoder.Decode(&errResponse); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
return err_response |
|
|
|
|
return errResponse |
|
|
|
|
} else { |
|
|
|
|
var success = new(responses.Success) |
|
|
|
|
decoder := xml.NewDecoder(body) |
|
|
|
|