Programmatically upload attachments to GitHub Issues, Pull Requests, and Comments
摘要
作者发现了一个非官方、未文档化的 GitHub 端点,可以程序化上传图片到 Issues、PRs 和 Comments,相当于模拟浏览器拖拽上传。此前 GitHub 长期缺乏这样的 API,作者曾尝试但未成功,最近通过 Shakacode 的 Justin Gordon 分享的 Intercom 项目得知,并亲自验证有效。作者不确定该端点是否一直支持 Auth Token,但认为它很有用,希望不会被移除。
荐读理由
你可以在GitHub Actions或脚本中直接调用这个非官方端点,自动给PR附加截图或视觉差异图,省去S3中转的麻烦。正文明确说明作者已测试成功,且给出了使用方式(虽然具体URL未在摘录中显示,但作者声称已提供)。
原文
It’s possible to programmatically upload images to GitHub Issues, Pull Requests and Comments via automation. Finally, though maybe mistakenly, I dunno.
It’s fairly well known that for the longest time, GitHub has not had a programmatic interface for uploading images and attachments. It’s possible in the browser, by dragging-and-dropping your image, but not via something you can script or curl. That’s been a bummer for lots of GitHub Action-powered systems you might imagine, like: automatically attaching demo screenshots or videos on PRs for new features, or attaching failure screenshots or visual diffs from browser tests.
Until now, I guess. Here’s the unofficial, undocumented endpoint:
FILE=cat.jpg
MIME=image/jpeg
REPOSITORY=bensheldon/good_job
TOKEN=$(gh auth token)
curl -s "https://uploads.github.com/user-attachments/assets?name=$FILE&content_type=$MIME&repository_id=$(gh api "repos/$REPOSITORY" --jq .id)" \
-X POST -H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json" --data-binary "@$FILE"
# that outputs a JSON response containing the attachment URL, which you can then embed in an image tag into the GitHub content.
That’s it! I mean, you can be more clever than curl or whatever, but that’s the idea. It’s equivalent to dragging-and-dropping an image into an Issue, PR, or Comment.
I don’t know if this endpoint always accepted an Auth Token and no one tried or noticed or talked about it, or if that’s new. My memory was that this endpoint only previously accepted Cookie Auth: circa 2018 I explored different Visual Diff services and the lack of a programmatic API for GitHub attachments meant that to DIY you’d have to use a service like S3, and then a service to authorize S3, and that’s annoying to set up when GitHub Actions could do everything else. I’m pretty sure I tried to fuzz the attachment interface at the time and it didn’t work.
I recently learned all of this was possible because Justin Gordon of Shakacode shared a link to Intercom’s 2x-skills attach-github-assets and I was… skeptical… and then I tried it and it worked!

It’s very useful, I hope it doesn’t go away.
这条对你有帮助吗?