Repository navigation
--attach stack 2/8: Describe and validate a file gh can attach - #14178
Conversation
--attach stack 2/8: Describe and validate a file gh can attach
There was a problem hiding this comment.
Pull request overview
Adds internal attachment validation and rendering metadata for the future --attach workflow.
Changes:
- Validates supported image/video files, types, and size limits.
- Generates image markdown or video URLs with safe alt text.
- Adds comprehensive unit tests and package documentation.
Show a summary per file
| File | Description |
|---|---|
internal/attachments/userasset.go |
Implements attachment validation and rendering. |
internal/attachments/userasset_test.go |
Tests supported formats, validation, and markdown. |
internal/attachments/doc.go |
Documents the attachment package workflow. |
Review details
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
- Files reviewed: 3/3 changed files
- Comments generated: 3
- Review effort level: Balanced
Co-authored-by: Copilot App <[email protected]>
2613beb to
8baae70
Compare
babakks
left a comment
There was a problem hiding this comment.
Thanks for putting this together! 🙏
Nice, careful work here, especially the validation coverage and the attention to alt-text escaping. I left a handful of inline notes. Most are nitpicks, but two are worth a proper look:
- The
UserAssetabstraction leaks some mechanical/construction concerns (file()returning itself,sizeLimit()on the interface). I sketched a reshape that keepsassetas pure data and moves the accessors onto the concrete types. - Symlink handling:
os.Statfollows links today, so a symlink to a regular file slips through. Switching toos.Lstatcloses that.
I also floated an idea around hardening the alt-text escaping with a compose-and-verify parse. Not blocking, just something to consider. Nothing here is a hard blocker, happy to talk through any of it.
|
|
||
| // newAsset validates one attached file. | ||
| func newAsset(path, alt string) (UserAsset, error) { | ||
| fi, err := os.Stat(path) |
There was a problem hiding this comment.
nitpick: let's add an indirection for os.Stat/os.Lstat here so the tests can swap in a mock.
// top-level in this file
var lstatFunc = os.LstatThen call lstatFunc(path) in newAsset instead of os.Lstat(path).
// in a test
orig := lstatFunc
t.Cleanup(func() { lstatFunc = orig })
lstatFunc = func(name string) (os.FileInfo, error) {
return fakeInfo{name: name, size: 42, mode: 0}, nil // or return an error to exercise the failure path
}Note: since lstatFunc is package-level state, tests that reassign it can't use t.Parallel().
With this indirection the tests no longer need to create real temp files (the writeFile helper and the sparse-file Truncate trick), since the mock supplies whatever FileInfo (size, mode) each case needs.
babakks
left a comment
There was a problem hiding this comment.
Approving as we decided to apply the comments in a follow-up PR.
Part of the pull request stack tracked in #14186.
Description
This is the second layer of the stack. It adds an internal package for attachments, holding the rules about what a file is and whether
ghwill accept it. Nothing here is reachable from a command yet.Nine file extensions are accepted. Six are images: png, jpg, jpeg, gif, webp and svg. Three are videos: mp4, mov and webm. Each maps to the content type the upload endpoint expects. The extension is the only thing checked, and it is matched without regard to case.
An image and a video are separate types, because GitHub renders them differently. An image becomes a markdown image embed. A video becomes a bare URL on its own line, which is what GitHub needs in order to show a player. A video has no alt text in that form, so asking for alt text on a video is refused rather than quietly ignored.
Validation reads the file's metadata and never opens the file. It refuses a path that does not exist, a directory, anything that is not a regular file, and an empty file.
How did you test this change?
The whole stack was built and every one of the nine accepted types was uploaded to a private test repository and downloaded again. The content type sent matched the content type served back in every case, and the bytes were identical in every case. The six image types produced image embeds and the three video types produced bare URLs.
Refusals were exercised by hand as well: an unsupported file type, an image over the size limit, a directory, a missing file, an empty file, and alt text on a video. In each case the command exited with an error and uploaded nothing.
Key points
Deciding by extension rather than by content is deliberate. The question being asked is how GitHub will render the file, not what the bytes are, and the endpoint accepts mislabelled bytes anyway.
Images are refused over 10 MB and videos over 100 MB.
ghcannot know the real video limit, because it depends on the account plan. The local limit only catches what could never succeed, and the server reports the rest.The image and video split lives in the type system rather than in conditionals spread around the package, because the two really do render differently and almost every rule downstream depends on which one it is.
Notes for reviewers
Nothing in this pull request is reachable from a command. The flag that reaches it arrives further up the stack.
Authorship and follow-up
Who wrote this:
Who answers review comments: