iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🪦

The Day My Custom CLI Tool Retired: Replacing gh-attach with Playwright CLI

に公開

Eyecatch

Introduction

I previously created a CLI tool called gh-attach for uploading images to GitHub Issues/PRs.

https://zenn.dev/atani/articles/gh-attach-github-image-upload

GitHub does not have an API for uploading images to issue comments. While you can attach images via drag-and-drop in the Web UI, you cannot do so from the CLI or CI. To overcome this limitation, I built a tool that automates browser operations using Playwright, which eventually grew to support three upload modes.

I have now archived gh-attach, as playwright-cli can now achieve what I intended to do.

The Problem gh-attach Solved

GitHub's Issue comment API only accepts Markdown text in the body parameter. The upload destination, uploads.github.com, does not accept authentication via OAuth tokens and requires browser session cookies.

gh-attach took three approaches to address this limitation:

Mode Method Characteristics
Browser Uploads images by automating the browser with Playwright Default
Release Uploads images to GitHub Releases and gets the URL No browser required, but images disappear on revert
Direct Gets upload policy from the browser and uploads via curl For GitHub Enterprise (GHE)

Every mode was built on the premise that "there is no GitHub API for this, so I have to use a browser."

Why gh-attach Stopped Working

The trigger was the update to GHE 3.19. The Direct mode of gh-attach works by fetching the upload policy API and uploading files via curl, but fetching the API started failing in GHE 3.19.

Since Browser mode also runs Playwright scripts internally, it required constant updates to follow GHE's UI changes. There was a limit to how much I could keep hacking GitHub's internal APIs with a custom tool.

Solving it with playwright-cli's File Chooser

What I could use instead is playwright-cli published by Microsoft. It is a tool that provides browser operations as CLI commands, allowing me to directly execute what gh-attach was doing internally through a combination of commands.

The steps are as follows:

# 1. Open the Issue page with Playwright
playwright-cli open "https://github.com/owner/repo/issues/123"

# 2. Get the page structure to check the element ref number
playwright-cli snapshot

# 3. Click the "Paste, drop, or click to add files" button
playwright-cli click <ref>

# 4. A file chooser opens; specify the image
#    -> After upload, the following is automatically inserted into the textarea:
#       ![image](https://github.com/.../user-attachments/assets/xxx)

# 5. Extract the URL from the textarea
playwright-cli snapshot

The key point is that posting the comment itself is not done by playwright-cli. I only retrieve the user-attachments URL that was automatically inserted into the textarea, and the actual comment posting is done using gh api. This minimizes browser operations.

The upload policy retrieval and fetch interception that gh-attach implemented independently are no longer necessary. Since I am just using the file attachment feature provided by the GitHub UI, it is easier to keep up with GHE version upgrades.

Retrospective

When I started building gh-attach, playwright-cli did not exist. Therefore, gh-attach was a necessary tool.

However, looking back, the essence of what gh-attach was doing was "automating the browser's file attachment UI to retrieve an image URL." If playwright-cli had been available from the start, it should have been possible to accomplish this with just a few lines of shell script.

What I learned during the process of developing my own tool was not in vain: the implementation of Browser mode, the revert issue in Release mode and its evolution into Direct mode, and the technology of fetch interception. However, the approach of depending on GitHub's internal APIs always carried the risk of breaking with every update. The approach of playwright-cli, which directly automates the official UI, is simpler and more robust.

Archiving gh-attach

I have archived the gh-attach repository. The README now includes instructions on how to use playwright-cli as an alternative.

If you are already using gh-attach, please consider migrating to playwright-cli.

Discussion