Skip to content

Commit 93a08e2

Browse files
Fixing the comment handling for devcontainer.json file. (#3610)
Using the open-source `github.com/tidwall/jsonc` library to parse and filter out the comments. Co-authored-by: dhruv <[email protected]>
1 parent 2315368 commit 93a08e2

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

app/gitspace/scm/scm.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import (
2626
"github.com/harness/gitness/git/command"
2727
"github.com/harness/gitness/types"
2828
"github.com/harness/gitness/types/enum"
29+
30+
"github.com/tidwall/jsonc"
2931
)
3032

3133
var (
@@ -102,13 +104,6 @@ func (s *SCM) GetSCMRepoDetails(
102104
return resolvedDetails, nil
103105
}
104106

105-
func removeComments(input []byte) []byte {
106-
blockCommentRegex := regexp.MustCompile(`(?s)/\*.*?\*/`)
107-
input = blockCommentRegex.ReplaceAll(input, nil)
108-
lineCommentRegex := regexp.MustCompile(`//.*`)
109-
return lineCommentRegex.ReplaceAll(input, nil)
110-
}
111-
112107
func detectDefaultGitBranch(ctx context.Context, gitRepoDir string) (string, error) {
113108
cmd := command.New("ls-remote",
114109
command.WithFlag("--symref"),
@@ -192,8 +187,7 @@ func (s *SCM) getDevcontainerConfig(
192187
return config, nil // Return an empty config if the file is empty
193188
}
194189

195-
sanitizedJSON := removeComments(catFileOutputBytes)
196-
if err = json.Unmarshal(sanitizedJSON, &config); err != nil {
190+
if err = json.Unmarshal(jsonc.ToJSON(catFileOutputBytes), &config); err != nil {
197191
return config, fmt.Errorf("failed to parse devcontainer JSON: %w", err)
198192
}
199193

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/distribution/reference v0.6.0
1515
github.com/docker/docker v27.1.1+incompatible
1616
github.com/docker/go-connections v0.5.0
17+
github.com/docker/go-units v0.5.0
1718
github.com/drone-runners/drone-runner-docker v1.8.4-0.20240815103043-c6c3a3e33ce3
1819
github.com/drone/drone-go v1.7.1
1920
github.com/drone/drone-yaml v1.2.3
@@ -71,6 +72,7 @@ require (
7172
github.com/swaggest/swgui v1.8.1
7273
github.com/swaggo/http-swagger v1.3.4
7374
github.com/swaggo/swag v1.16.2
75+
github.com/tidwall/jsonc v0.3.2
7476
github.com/unrolled/secure v1.15.0
7577
github.com/zricethezav/gitleaks/v8 v8.18.5-0.20240912004812-e93a7c0d2604
7678
go.starlark.net v0.0.0-20231121155337-90ade8b19d09
@@ -106,7 +108,6 @@ require (
106108
github.com/charmbracelet/lipgloss v0.12.1 // indirect
107109
github.com/charmbracelet/x/ansi v0.1.4 // indirect
108110
github.com/docker/distribution v2.8.2+incompatible // indirect
109-
github.com/docker/go-units v0.5.0 // indirect
110111
github.com/drone/envsubst v1.0.3 // indirect
111112
github.com/fatih/semgroup v1.2.0 // indirect
112113
github.com/felixge/httpsnoop v1.0.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ github.com/swaggo/http-swagger v1.3.4 h1:q7t/XLx0n15H1Q9/tk3Y9L4n210XzJF5WtnDX64
762762
github.com/swaggo/http-swagger v1.3.4/go.mod h1:9dAh0unqMBAlbp1uE2Uc2mQTxNMU/ha4UbucIg1MFkQ=
763763
github.com/swaggo/swag v1.16.2 h1:28Pp+8DkQoV+HLzLx8RGJZXNGKbFqnuvSbAAtoxiY04=
764764
github.com/swaggo/swag v1.16.2/go.mod h1:6YzXnDcpr0767iOejs318CwYkCQqyGer6BizOg03f+E=
765+
github.com/tidwall/jsonc v0.3.2 h1:ZTKrmejRlAJYdn0kcaFqRAKlxxFIC21pYq8vLa4p2Wc=
766+
github.com/tidwall/jsonc v0.3.2/go.mod h1:dw+3CIxqHi+t8eFSpzzMlcVYxKp08UP5CD8/uSFCyJE=
765767
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
766768
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
767769
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=

0 commit comments

Comments
 (0)