Skip to content

Commit

Permalink
Merge pull request #227 from transifex/devel
Browse files Browse the repository at this point in the history
Release: truncate long messages
  • Loading branch information
foteinigk authored May 17, 2024
2 parents b5fdb51 + fc80657 commit c61099f
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 15 deletions.
22 changes: 18 additions & 4 deletions internal/txlib/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,19 @@ func (task *ResourcePullTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(

message := fmt.Sprintf(
"%s.%s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
body,
))
)

if !args.Silent {
message = truncateMessage(message)
}

send(message)
}
sendMessage("Getting info", false)

Expand Down Expand Up @@ -364,13 +371,20 @@ func (task *FilePullTask) Run(send func(string), abort func()) {
}

cyan := color.New(color.FgCyan).SprintFunc()
send(fmt.Sprintf(

message := fmt.Sprintf(
"%s.%s %s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
cyan("["+code+"]"),
body,
))
)

if !args.Silent {
message = truncateMessage(message)
}

send(message)
}
sendMessage("Pulling file", false)

Expand Down
31 changes: 24 additions & 7 deletions internal/txlib/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,16 @@ func (task *ResourcePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s.%s - %s",
cfgResource.ProjectSlug,
cfgResource.ResourceSlug,
body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}
sendMessage("Getting info", false)
resource, err := txapi.GetResourceById(api, cfgResource.GetAPv3Id())
Expand Down Expand Up @@ -542,12 +546,16 @@ func (task *LanguagePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s (%s) - %s",
parts[3],
strings.Join(languages, ", "),
body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}
sendMessage("Pushing", false)

Expand Down Expand Up @@ -594,7 +602,12 @@ func (task *SourceFilePushTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body))

message := fmt.Sprintf("%s.%s - %s", parts[3], parts[5], body)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}

file, err := os.Open(sourceFile)
Expand Down Expand Up @@ -693,10 +706,14 @@ func (task *TranslationFileTask) Run(send func(string), abort func()) {
if args.Silent && !force {
return
}
send(fmt.Sprintf(
message := fmt.Sprintf(
"%s.%s %s - %s", parts[3], parts[5],
cyan("["+languageCode+"]"), body,
))
)
if !args.Silent {
message = truncateMessage(message)
}
send(message)
}

// Only check timestamps if -f isn't set and if resource isn't new
Expand Down
8 changes: 8 additions & 0 deletions internal/txlib/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,11 @@ func isValidResolutionPolicy(policy string) (IsValid bool) {
return false

}

func truncateMessage(message string) string {
maxLength := 80
if len(message) > maxLength {
return message[:maxLength-2] + ".."
}
return message
}
24 changes: 24 additions & 0 deletions internal/txlib/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ func TestConflictResolution(t *testing.T) {
}

}

func TestTruncateMessage(t *testing.T) {
result := truncateMessage("short message")
assert.Equal(t, result, "short message")

result = truncateMessage(
"this is a long message that needs to be truncated because it exceeds " +
"the maximum length of 75 characters",
)
assert.Equal(
t,
result,
"this is a long message that needs to be truncated because it exceeds the maxim..",
)

result = truncateMessage(
"a message with exactly 75 characters - this message should not be truncated",
)
assert.Equal(
t,
result,
"a message with exactly 75 characters - this message should not be truncated",
)
}
2 changes: 1 addition & 1 deletion pkg/txapi/resource_strings_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func PollResourceStringsDownload(download *jsonapi.Resource, filePath string) er
return nil
} else if download.Attributes["status"] == "failed" {
return fmt.Errorf(
"download of translation '%s' failed",
"failed to download translation '%s'",
download.Relationships["resource"].DataSingular.Id,
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_strings_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func PollSourceUpload(upload *jsonapi.Resource) error {

if uploadAttributes.Status == "failed" {
// Wrap the "error"
return fmt.Errorf("upload of resource '%s' failed - %w",
return fmt.Errorf("failed to upload of resource '%s' - %w",
upload.Relationships["resource"].DataSingular.Id,
&uploadAttributes)
} else if uploadAttributes.Status == "succeeded" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_translations_async_downloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func PollTranslationDownload(download *jsonapi.Resource, filePath string) error
break
} else if download.Attributes["status"] == "failed" {
return fmt.Errorf(
"download of translation '%s' failed",
"failed to download translation '%s'",
download.Relationships["resource"].DataSingular.Id,
)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/txapi/resource_translations_async_uploads.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func PollTranslationUpload(upload *jsonapi.Resource) error {
if uploadAttributes.Status == "failed" {
// Wrap the "error"
return fmt.Errorf(
"upload of resource '%s', language '%s' failed - %w",
"failed to upload resource '%s', language '%s' - %w",
upload.Relationships["resource"].DataSingular.Id,
upload.Relationships["language"].DataSingular.Id,
&uploadAttributes)
Expand Down

0 comments on commit c61099f

Please sign in to comment.