varentries[]ContentEntryiferr:=json.Unmarshal(body,&entries);err!=nil{// Maybe it is a single file, not a directoryvarsingleContentEntryiferr2:=json.Unmarshal(body,&single);err2==nil{entries=[]ContentEntry{single}}else{returnnil,fmt.Errorf("parse contents JSON: %w",err)}}
Or check the response structure before unmarshaling.
## Problem
When `ListContents()` is called with a path that points to a **file** (not a directory), Gitea returns a single JSON object:
```json
{"name":"README.md","path":"README.md","type":"file",...}
```
But the code tries to unmarshal it as an array:
```go
var entries []ContentEntry
if err := json.Unmarshal(body, &entries); err != nil {
return nil, fmt.Errorf("parse contents JSON: %w", err)
}
```
This fails with: `json: cannot unmarshal object into Go value of type []gitea.ContentEntry`
## How to Trigger
1. Set `patterns-files: README.md` (or let it default to that per #71)
2. review-bot calls `ListContents(ctx, owner, repo, "README.md")`
3. Gitea returns a single object, not an array
4. JSON unmarshal fails
## Fix
Detect single-object response and handle it:
```go
var entries []ContentEntry
if err := json.Unmarshal(body, &entries); err != nil {
// Maybe it is a single file, not a directory
var single ContentEntry
if err2 := json.Unmarshal(body, &single); err2 == nil {
entries = []ContentEntry{single}
} else {
return nil, fmt.Errorf("parse contents JSON: %w", err)
}
}
```
Or check the response structure before unmarshaling.
## Related
- #70: `.` path normalization
- #71: bad default `README.md`
rodin
added the wip label 2026-05-11 14:17:31 +00:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Problem
When
ListContents()is called with a path that points to a file (not a directory), Gitea returns a single JSON object:But the code tries to unmarshal it as an array:
This fails with:
json: cannot unmarshal object into Go value of type []gitea.ContentEntryHow to Trigger
patterns-files: README.md(or let it default to that per #71)ListContents(ctx, owner, repo, "README.md")Fix
Detect single-object response and handle it:
Or check the response structure before unmarshaling.
Related
.path normalizationREADME.md