fix: preserve raw file API paths #85

Merged
wbtiger merged 1 commits from dtwdtw/gitlink-cli:fix/issue-142586-raw-file-api into master 2026-06-01 01:04:26 +08:00
2 changed files with 33 additions and 2 deletions

View File

@ -49,10 +49,10 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
if idx := strings.Index(path, "?"); idx != -1 {
basePath := path[:idx]
queryStr := path[idx:]
if !strings.HasSuffix(basePath, ".json") {
if shouldAppendJSONSuffix(basePath) {
path = basePath + ".json" + queryStr
}
} else if !strings.HasSuffix(path, ".json") {
} else if shouldAppendJSONSuffix(path) {
path += ".json"
}
fullURL := c.BaseURL + path
@ -160,6 +160,19 @@ func (c *Client) Do(method, path string, body interface{}, query url.Values) (*o
return output.SuccessEnvelope(raw, meta), nil
}
func shouldAppendJSONSuffix(path string) bool {
if strings.HasSuffix(path, ".json") {
return false
}
parts := strings.Split(strings.Trim(path, "/"), "/")
for i, part := range parts {
if part == "raw" && i >= 2 && i+2 < len(parts) {
return false
}
}
return true
}
func normalizeAPIPath(baseURL, path string) string {
if strings.HasSuffix(strings.TrimRight(baseURL, "/"), "/api") {
switch {

View File

@ -25,3 +25,21 @@ func TestNormalizeAPIPathKeepsAPIPrefixForNonAPIBaseURL(t *testing.T) {
t.Fatalf("normalizeAPIPath() = %q, want %q", got, want)
}
}
func TestShouldAppendJSONSuffixSkipsRawFilePath(t *testing.T) {
if shouldAppendJSONSuffix("/Gitlink/forgeplus/raw/master/README.md") {
t.Fatal("raw file path should not get .json suffix")
}
}
func TestShouldAppendJSONSuffixKeepsRawRepositoryName(t *testing.T) {
if !shouldAppendJSONSuffix("/users/raw/projects") {
t.Fatal("regular API path should get .json suffix")
}
}
func TestShouldAppendJSONSuffixSkipsExistingJSONPath(t *testing.T) {
if shouldAppendJSONSuffix("/projects.json") {
t.Fatal("existing .json path should not get another suffix")
}
}