From b7c7f95ad670d8809816b651a25136da0ceb9793 Mon Sep 17 00:00:00 2001 From: Brian Ojeda <9335829+sgtoj@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:46:42 -0500 Subject: [PATCH] feat: show the app's logo on the installer pages GitHub can't set an app's logo from a manifest and has no API for it, so installer.Config takes an optional Icon. The setup page shows it with a download link, and the success page links to the app's settings, where it's uploaded under Display information. New rejects icons GitHub won't take: over 1 MB, or not a PNG, JPEG, or GIF. Co-Authored-By: Claude Opus 5.5 --- README.md | 19 +++++ installer/installer.go | 70 ++++++++++++++++- installer/installer_test.go | 130 +++++++++++++++++++++++++++++++ installer/templates/index.html | 30 +++++++ installer/templates/success.html | 25 ++++++ 5 files changed, 272 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0c484f0..317e6cd 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,25 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) { } ``` +## App Logo + +GitHub can't set an app's logo from a manifest, and has no API for it. Pass +the logo as `Icon` instead: the installer pages show it with a link to +download it and, once the app is created, a link to the app's settings, where +you upload it under **Display information**. GitHub takes a PNG, JPEG, or GIF +of at most 1 MB, and `installer.New` returns an error for anything else. + +```go +//go:embed logo.png +var logo []byte + +installerHandler, err := runtime.InstallerHandler(installer.Config{ + Manifest: manifest, + AppDisplayName: "My GitHub App", + Icon: logo, +}) +``` + ## Configuration ### Environment Variables diff --git a/installer/installer.go b/installer/installer.go index 021524a..26e2959 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -9,6 +9,7 @@ import ( "bytes" "context" "embed" + "encoding/base64" "encoding/json" "fmt" "html/template" @@ -37,6 +38,9 @@ const ( disableSetupPath = "/setup/disable" ) +// maxIconSize is the largest logo GitHub takes. +const maxIconSize = 1 << 20 + // CredentialsSavedFunc is called after credentials are saved. type CredentialsSavedFunc func(ctx context.Context, creds *configstore.AppCredentials) error @@ -51,6 +55,11 @@ type Config struct { WebhookURL string OnCredentialsSaved CredentialsSavedFunc + // Icon is an optional logo for the app: a PNG, JPEG, or GIF of at most + // 1 MB. GitHub can't set a logo from a manifest, so the installer pages + // show it with a link to download it and say where to upload it. + Icon []byte + // OnReloadNeeded is called after credentials are saved to trigger // a configuration reload. This should be wired to the Runtime's // ReloadCallback() or a custom reload function. @@ -67,7 +76,9 @@ func NewConfigFromEnv() Config { // Handler handles the GitHub App manifest installation flow. type Handler struct { - config Config + config Config + iconURL template.URL + iconFile string } type indexTemplateData struct { @@ -79,6 +90,8 @@ type indexTemplateData struct { WebhookURL string NeedsWebhook bool DefaultAppName string + IconURL template.URL + IconFile string } type successTemplateData struct { @@ -89,6 +102,9 @@ type successTemplateData struct { InstallURL string DisableActionURL string InstallerDisabled bool + IconURL template.URL + IconFile string + SettingsURL string } // New creates a new installer Handler with the given configuration. @@ -103,7 +119,30 @@ func New(cfg Config) (*Handler, error) { if cfg.AppDisplayName == "" { cfg.AppDisplayName = "GitHub App" } - return &Handler{config: cfg}, nil + h := &Handler{config: cfg} + if len(cfg.Icon) > 0 { + iconURL, iconFile, err := iconData(cfg.Icon) + if err != nil { + return nil, err + } + h.iconURL, h.iconFile = iconURL, iconFile + } + return h, nil +} + +// iconData returns icon as a data URL and a file name for downloading it, +// or an error if GitHub won't take it as a logo. +func iconData(icon []byte) (template.URL, string, error) { + if len(icon) > maxIconSize { + return "", "", fmt.Errorf("icon is %d bytes; github takes at most %d", len(icon), maxIconSize) + } + ext := map[string]string{"image/png": "png", "image/jpeg": "jpg", "image/gif": "gif"} + contentType := http.DetectContentType(icon) + if ext[contentType] == "" { + return "", "", fmt.Errorf("icon is %s; github takes png, jpeg, or gif", contentType) + } + dataURL := "data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(icon) + return template.URL(dataURL), "logo." + ext[contentType], nil } // ServeHTTP implements http.Handler. @@ -216,6 +255,8 @@ func (h *Handler) handleIndex(w http.ResponseWriter, r *http.Request) { WebhookURL: webhookURL, NeedsWebhook: h.config.WebhookURL == "", DefaultAppName: defaultAppName, + IconURL: h.iconURL, + IconFile: h.iconFile, } var buf bytes.Buffer @@ -385,6 +426,7 @@ func (h *Handler) successDataFromCreds(creds *configstore.AppCredentials) succes DisableActionURL: disableSetupPath, } data.InstallURL = h.installURLFor(creds.AppSlug, creds.HTMLURL) + h.addIcon(&data) return data } @@ -401,9 +443,33 @@ func (h *Handler) successDataFromStatus(status *configstore.InstallerStatus) suc DisableActionURL: disableSetupPath, } data.InstallURL = h.installURLFor(status.AppSlug, status.HTMLURL) + h.addIcon(&data) return data } +// addIcon adds the logo, and the settings page to upload it on, when both +// are known. +func (h *Handler) addIcon(data *successTemplateData) { + settingsURL := h.settingsURLFor(data.AppSlug) + if h.iconURL == "" || settingsURL == "" { + return + } + data.IconURL, data.IconFile, data.SettingsURL = h.iconURL, h.iconFile, settingsURL +} + +// settingsURLFor returns the settings page of the app with the given slug, +// where its logo is uploaded. +func (h *Handler) settingsURLFor(slug string) string { + if slug == "" { + return "" + } + if h.config.GitHubOrg != "" { + return fmt.Sprintf("%s/organizations/%s/settings/apps/%s", + h.config.GitHubURL, url.PathEscape(h.config.GitHubOrg), url.PathEscape(slug)) + } + return fmt.Sprintf("%s/settings/apps/%s", h.config.GitHubURL, url.PathEscape(slug)) +} + func (h *Handler) installURLFor(slug, htmlURL string) string { if slug != "" { githubURL := h.config.GitHubURL diff --git a/installer/installer_test.go b/installer/installer_test.go index beb75ba..4744aff 100644 --- a/installer/installer_test.go +++ b/installer/installer_test.go @@ -365,6 +365,136 @@ func TestNew_Validation(t *testing.T) { }) } +// imageOf returns size bytes that start with header, which is all content +// sniffing reads. +func imageOf(header string, size int) []byte { + b := make([]byte, size) + copy(b, header) + return b +} + +func TestNew_Icon(t *testing.T) { + tests := []struct { + name string + icon []byte + wantFile string + wantErr string + }{ + {"png", imageOf("\x89PNG\r\n\x1a\n", 64), "logo.png", ""}, + {"jpeg", imageOf("\xff\xd8\xff", 64), "logo.jpg", ""}, + {"gif", imageOf("GIF89a", 64), "logo.gif", ""}, + {"1 MB png", imageOf("\x89PNG\r\n\x1a\n", 1<<20), "logo.png", ""}, + {"too big", imageOf("\x89PNG\r\n\x1a\n", 1<<20+1), "", "github takes at most"}, + {"not an image", []byte(""), "", "github takes png, jpeg, or gif"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, err := New(Config{Store: &mockStore{}, Icon: tt.icon}) + if tt.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { + t.Fatalf("New() error = %v, want %q", err, tt.wantErr) + } + return + } + if err != nil { + t.Fatalf("New() error = %v", err) + } + if h.iconFile != tt.wantFile { + t.Errorf("iconFile = %q, want %q", h.iconFile, tt.wantFile) + } + }) + } +} + +func TestHandler_Icon(t *testing.T) { + png := imageOf("\x89PNG\r\n\x1a\n", 64) + registered := func(slug string) *mockStore { + return &mockStore{ + statusFunc: func(ctx context.Context) (*configstore.InstallerStatus, error) { + return &configstore.InstallerStatus{Registered: true, AppID: 1, AppSlug: slug}, nil + }, + } + } + + tests := []struct { + name string + store *mockStore + org string + icon []byte + want []string + notWant []string + }{ + { + name: "setup page shows the logo", + store: &mockStore{}, + icon: png, + want: []string{`src="data:image/png;base64,`, `download="logo.png"`}, + }, + { + name: "setup page without an icon", + store: &mockStore{}, + notWant: []string{"data:image"}, + }, + { + name: "org app links its settings", + store: registered("my-app"), + org: "my-org", + icon: png, + want: []string{"Add the Logo", `href="https://github.com/organizations/my-org/settings/apps/my-app"`, `download="logo.png"`}, + }, + { + name: "personal app links its settings", + store: registered("my-app"), + icon: png, + want: []string{`href="https://github.com/settings/apps/my-app"`}, + }, + { + name: "settings link escapes the org", + store: registered("my-app"), + org: "../evil", + icon: png, + want: []string{"/organizations/..%2fevil/settings/apps/my-app"}, + }, + { + name: "no logo step without a slug", + store: registered(""), + icon: png, + notWant: []string{"Add the Logo"}, + }, + { + name: "no logo step without an icon", + store: registered("my-app"), + notWant: []string{"Add the Logo", "data:image"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + h, err := New(Config{Store: tt.store, GitHubOrg: tt.org, Icon: tt.icon}) + if err != nil { + t.Fatalf("New() error = %v", err) + } + rec := httptest.NewRecorder() + h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/setup", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want %d", rec.Code, http.StatusOK) + } + body := strings.ToLower(rec.Body.String()) + for _, w := range tt.want { + if !strings.Contains(body, strings.ToLower(w)) { + t.Errorf("page does not contain %q", w) + } + } + for _, w := range tt.notWant { + if strings.Contains(body, strings.ToLower(w)) { + t.Errorf("page contains %q", w) + } + } + }) + } +} + func TestHandler_ServeHTTP_Routing(t *testing.T) { store := &mockStore{ statusFunc: func(ctx context.Context) (*configstore.InstallerStatus, error) { diff --git a/installer/templates/index.html b/installer/templates/index.html index c79d0e7..2bdf6f0 100644 --- a/installer/templates/index.html +++ b/installer/templates/index.html @@ -83,6 +83,29 @@ font-size: 14px; color: #6e5b00; } + .logo { + display: flex; + gap: 12px; + align-items: center; + background: #f6f8fa; + border: 1px solid #d0d7de; + border-radius: 6px; + padding: 12px; + margin-bottom: 16px; + font-size: 14px; + } + .logo img { + width: 64px; + height: 64px; + border-radius: 8px; + flex: none; + } + .logo p { + margin: 0; + } + .logo a { + color: #0969da; + } @@ -104,6 +127,13 @@

{{.AppDisplayName}} Installer

{{end}} + {{if .IconURL}} + + {{end}} +
diff --git a/installer/templates/success.html b/installer/templates/success.html index 4ca6b56..d588957 100644 --- a/installer/templates/success.html +++ b/installer/templates/success.html @@ -123,6 +123,21 @@ color: #24292f; font-size: 14px; } + .logo { + display: flex; + gap: 12px; + align-items: center; + font-size: 14px; + } + .logo img { + width: 64px; + height: 64px; + border-radius: 8px; + flex: none; + } + .logo p { + margin: 0; + } @@ -145,6 +160,16 @@

Next Step: Install the App

{{end}} + {{if .IconURL}} +
+

Add the Logo

+ +
+ {{end}} + {{if .DisableActionURL}}

Disable Web Installer