Skip to content

Commit

Permalink
Fixed an error that occurred when an author name contains a string th…
Browse files Browse the repository at this point in the history
…at is not suitable for JSON (#3638)

* fix(pkg): Fixed an issue where wails new would throw an error if the author name contained non-JSON legal characters.

* refactor(pkg): Incorporating coderabbit's suggestions

* docs: write changelog.mdx

* Escape using json package. Add tests.

* Update test.

---------

Co-authored-by: Lea O'Anthony <lea.anthony@gmail.com>
  • Loading branch information
taisei-86 and leaanthony authored Jul 29, 2024
1 parent fe9495d commit 5b091db
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
29 changes: 26 additions & 3 deletions v2/pkg/git/git.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package git

import (
"html/template"
"encoding/json"
"fmt"
"runtime"
"strings"

Expand Down Expand Up @@ -30,9 +31,31 @@ func Email() (string, error) {

// Name tries to retrieve the
func Name() (string, error) {
errMsg := "failed to retrieve git user name: %w"
stdout, _, err := shell.RunCommand(".", gitcommand(), "config", "user.name")
name := template.JSEscapeString(strings.TrimSpace(stdout))
return name, err
if err != nil {
return "", fmt.Errorf(errMsg, err)
}
name := strings.TrimSpace(stdout)
return EscapeName(name)
}

func EscapeName(str string) (string, error) {
b, err := json.Marshal(str)
if err != nil {
return "", err
}
// Remove the surrounding quotes
escaped := string(b[1 : len(b)-1])

// Check if username is JSON compliant
var js json.RawMessage
jsonVal := fmt.Sprintf(`{"name": "%s"}`, escaped)
err = json.Unmarshal([]byte(jsonVal), &js)
if err != nil {
return "", fmt.Errorf("failed to retrieve git user name: %w", err)
}
return escaped, nil
}

func InitRepo(projectDir string) error {
Expand Down
44 changes: 44 additions & 0 deletions v2/pkg/git/git_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package git

import (
"testing"
)

func TestEscapeName1(t *testing.T) {
type args struct {
str string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Escape Apostrophe",
args: args{
str: `John O'Keefe`,
},
want: `John O'Keefe`,
},
{
name: "Escape backslash",
args: args{
str: `MYDOMAIN\USER`,
},
want: `MYDOMAIN\\USER`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := EscapeName(tt.args.str)
if (err != nil) != tt.wantErr {
t.Errorf("EscapeName() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("EscapeName() got = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Fixed
- Fixed an error that occurred when an author name contains a string that is not suitable for JSON. Fixed by @taiseiotsuka in [PR](https://github.com/wailsapp/wails/pull/3638)
- Fixed MacOS build to use `outputfilename` from wails.json. [#3200](https://github.com/wailsapp/wails/issues/3200)
- Fixed file drop events on windows. Fixed in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
+ Fixed file drop events on Windows in [PR](https://github.com/wailsapp/wails/pull/3595) by @FrancescoLuzzi
Expand Down

0 comments on commit 5b091db

Please sign in to comment.