Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions plugins/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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 := ""
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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"`
Expand Down
9 changes: 4 additions & 5 deletions plugins/paste_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down