Skip to content

--attach stack 2/8: Describe and validate a file gh can attach - #14178

Merged
BagToad merged 1 commit into
bagtoad/attach-prerequisitesfrom
bagtoad/attach-file-validation
Aug 25, 2026
Merged

BagToad merged 1 commit into
bagtoad/attach-prerequisitesfrom
bagtoad/attach-file-validation

Conversation

@BagToad

@BagToad BagToad commented Aug 18, 2026 •

Copy link
Copy Markdown
Member

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 gh will 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. gh cannot 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:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @BagToad will read and reply directly. Name the account.
  • An agent will draft replies and @BagToad will read them before they are posted.
  • Nobody has explicitly committed to replying.

@BagToad BagToad changed the title Describe and validate a file gh can attach --attach stack 2/8: Describe and validate a file gh can attach Aug 18, 2026
@BagToad
BagToad marked this pull request as ready for review August 18, 2026 05:56
Copilot AI balanced review requested due to automatic review settings August 18, 2026 05:56
@BagToad
BagToad requested a review from a team as a code owner August 18, 2026 05:56
@BagToad
BagToad requested a review from sergiou87 August 18, 2026 05:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread internal/attachments/doc.go Resolved
Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset_test.go Resolved
@BagToad BagToad mentioned this pull request Aug 18, 2026

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 UserAsset abstraction leaks some mechanical/construction concerns (file() returning itself, sizeLimit() on the interface). I sketched a reshape that keeps asset as pure data and moves the accessors onto the concrete types.
  • Symlink handling: os.Stat follows links today, so a symlink to a regular file slips through. Switching to os.Lstat closes 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.

Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset.go Resolved
Comment thread internal/attachments/userasset_test.go Resolved

// newAsset validates one attached file.
func newAsset(path, alt string) (UserAsset, error) {
fi, err := os.Stat(path)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.Lstat

Then 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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answered here

@babakks babakks left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving as we decided to apply the comments in a follow-up PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants