From ec1843da8e8f207f527969fa8566fee4334ce924 Mon Sep 17 00:00:00 2001 From: AK Date: Sat, 8 Aug 2026 05:39:37 -0700 Subject: [PATCH] fix: use current Opengist gist API --- plugins/paste.go | 21 +++++++++++++++------ plugins/paste_test.go | 9 ++++----- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/plugins/paste.go b/plugins/paste.go index 0fa0658..3990a9a 100644 --- a/plugins/paste.go +++ b/plugins/paste.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -85,7 +86,12 @@ func (p *Paste) Handle(b *bot.Bot, m bot.Message) bool { } result, err := p.createPaste(context.Background(), input) if err != nil { - b.Send(m.ReplyTarget(), "paste creation failed") + var providerErr pasteHTTPError + if errors.As(err, &providerErr) { + b.Send(m.ReplyTarget(), fmt.Sprintf("paste creation failed: Opengist HTTP %d", providerErr.status)) + } else { + b.Send(m.ReplyTarget(), "paste creation failed") + } return true } notice := "" @@ -96,6 +102,10 @@ func (p *Paste) Handle(b *bot.Bot, m bot.Message) bool { return true } +type pasteHTTPError struct{ status int } + +func (e pasteHTTPError) Error() string { return fmt.Sprintf("Opengist returned HTTP %d", e.status) } + func fetchPasteURL(parent context.Context, rawURL string, maxLength int) (string, bool, error) { ctx, cancel := context.WithTimeout(parent, 8*time.Second) defer cancel() @@ -129,9 +139,8 @@ func (p *Paste) createPaste(parent context.Context, content string) (string, err ctx, cancel := context.WithTimeout(parent, 8*time.Second) defer cancel() body, err := json.Marshal(map[string]interface{}{ - "files": map[string]map[string]string{"paste.txt": {"content": content}}, - "public": p.visibility == "public", - "unlisted": p.visibility == "unlisted", + "visibility": p.visibility, + "files": map[string]map[string]string{"paste.txt": {"content": content}}, }) if err != nil { return "", err @@ -140,7 +149,7 @@ func (p *Paste) createPaste(parent context.Context, content string) (string, err if err != nil { return "", err } - req.Header.Set("Authorization", "token "+p.token) + req.Header.Set("Authorization", "Bearer "+p.token) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") res, err := apiHTTPClient.Do(req) @@ -149,7 +158,7 @@ func (p *Paste) createPaste(parent context.Context, content string) (string, err } defer res.Body.Close() if res.StatusCode < 200 || res.StatusCode >= 300 { - return "", fmt.Errorf("Opengist returned HTTP %d", res.StatusCode) + return "", pasteHTTPError{status: res.StatusCode} } var response struct { URL string `json:"url"` diff --git a/plugins/paste_test.go b/plugins/paste_test.go index a97beae..f54648e 100644 --- a/plugins/paste_test.go +++ b/plugins/paste_test.go @@ -20,20 +20,19 @@ func TestPasteCreateHonorsUnlistedVisibilityAndAuth(t *testing.T) { if r.Method != http.MethodPost || r.URL.String() != "https://paste.example.test/api/gists" { t.Fatalf("unexpected request: %s %s", r.Method, r.URL) } - if got := r.Header.Get("Authorization"); got != "token secret" { + if got := r.Header.Get("Authorization"); got != "Bearer secret" { t.Fatalf("unexpected auth header: %q", got) } var body struct { - Public bool `json:"public"` - Unlisted bool `json:"unlisted"` + Visibility string `json:"visibility"` } if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Fatal(err) } - if body.Public || !body.Unlisted { + if body.Visibility != "unlisted" { t.Fatalf("unexpected visibility: %+v", body) } - return newPluginResponse(http.StatusCreated, `{"url":"https://paste.example.test/abc"}`), nil + return newPluginResponse(http.StatusCreated, `{"html_url":"https://paste.example.test/abc"}`), nil })} got, err := p.createPaste(t.Context(), "hello") if err != nil || got != "https://paste.example.test/abc" {