Bumps the go-modules-updates group in /backend with 9 updates: | Package | From | To | | --- | --- | --- | | [github.com/PuerkitoBio/goquery](https://github.com/PuerkitoBio/goquery) | `1.9.2` | `1.10.0` | | [github.com/alecthomas/chroma/v2](https://github.com/alecthomas/chroma) | `2.13.0` | `2.14.0` | | [github.com/go-pkgz/notify](https://github.com/go-pkgz/notify) | `1.1.1` | `1.2.0` | | [github.com/go-pkgz/repeater](https://github.com/go-pkgz/repeater) | `1.1.3` | `1.2.0` | | [github.com/rs/xid](https://github.com/rs/xid) | `1.5.0` | `1.6.0` | | [go.etcd.io/bbolt](https://github.com/etcd-io/bbolt) | `1.3.10` | `1.3.11` | | [golang.org/x/crypto](https://github.com/golang/crypto) | `0.25.0` | `0.27.0` | | [golang.org/x/image](https://github.com/golang/image) | `0.18.0` | `0.22.0` | | [golang.org/x/net](https://github.com/golang/net) | `0.27.0` | `0.29.0` | Updates `github.com/PuerkitoBio/goquery` from 1.9.2 to 1.10.0 - [Release notes](https://github.com/PuerkitoBio/goquery/releases) - [Commits](https://github.com/PuerkitoBio/goquery/compare/v1.9.2...v1.10.0) Updates `github.com/alecthomas/chroma/v2` from 2.13.0 to 2.14.0 - [Release notes](https://github.com/alecthomas/chroma/releases) - [Changelog](https://github.com/alecthomas/chroma/blob/master/.goreleaser.yml) - [Commits](https://github.com/alecthomas/chroma/compare/v2.13.0...v2.14.0) Updates `github.com/go-pkgz/notify` from 1.1.1 to 1.2.0 - [Release notes](https://github.com/go-pkgz/notify/releases) - [Commits](https://github.com/go-pkgz/notify/compare/v1.1.1...v1.2.0) Updates `github.com/go-pkgz/repeater` from 1.1.3 to 1.2.0 - [Release notes](https://github.com/go-pkgz/repeater/releases) - [Commits](https://github.com/go-pkgz/repeater/compare/v1.1.3...v1.2.0) Updates `github.com/rs/xid` from 1.5.0 to 1.6.0 - [Release notes](https://github.com/rs/xid/releases) - [Commits](https://github.com/rs/xid/compare/v1.5.0...v1.6.0) Updates `go.etcd.io/bbolt` from 1.3.10 to 1.3.11 - [Release notes](https://github.com/etcd-io/bbolt/releases) - [Commits](https://github.com/etcd-io/bbolt/compare/v1.3.10...v1.3.11) Updates `golang.org/x/crypto` from 0.25.0 to 0.27.0 - [Commits](https://github.com/golang/crypto/compare/v0.25.0...v0.27.0) Updates `golang.org/x/image` from 0.18.0 to 0.22.0 - [Commits](https://github.com/golang/image/compare/v0.18.0...v0.22.0) Updates `golang.org/x/net` from 0.27.0 to 0.29.0 - [Commits](https://github.com/golang/net/compare/v0.27.0...v0.29.0) --- updated-dependencies: - dependency-name: github.com/PuerkitoBio/goquery dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/alecthomas/chroma/v2 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/notify dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/go-pkgz/repeater dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: github.com/rs/xid dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: go.etcd.io/bbolt dependency-type: direct:production update-type: version-update:semver-patch dependency-group: go-modules-updates - dependency-name: golang.org/x/crypto dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/image dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates - dependency-name: golang.org/x/net dependency-type: direct:production update-type: version-update:semver-minor dependency-group: go-modules-updates ... Signed-off-by: dependabot[bot] <support@github.com>
370 lines
8.8 KiB
Go
370 lines
8.8 KiB
Go
package slack
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"mime"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// SlackResponse handles parsing out errors from the web api.
|
|
type SlackResponse struct {
|
|
Ok bool `json:"ok"`
|
|
Error string `json:"error"`
|
|
ResponseMetadata ResponseMetadata `json:"response_metadata"`
|
|
}
|
|
|
|
func (t SlackResponse) Err() error {
|
|
if t.Ok {
|
|
return nil
|
|
}
|
|
|
|
// handle pure text based responses like chat.post
|
|
// which while they have a slack response in their data structure
|
|
// it doesn't actually get set during parsing.
|
|
if strings.TrimSpace(t.Error) == "" {
|
|
return nil
|
|
}
|
|
|
|
return SlackErrorResponse{Err: t.Error, ResponseMetadata: t.ResponseMetadata}
|
|
}
|
|
|
|
// SlackErrorResponse brings along the metadata of errors returned by the Slack API.
|
|
type SlackErrorResponse struct {
|
|
Err string
|
|
ResponseMetadata ResponseMetadata
|
|
}
|
|
|
|
func (r SlackErrorResponse) Error() string { return r.Err }
|
|
|
|
// RateLimitedError represents the rate limit response from slack
|
|
type RateLimitedError struct {
|
|
RetryAfter time.Duration
|
|
}
|
|
|
|
func (e *RateLimitedError) Error() string {
|
|
return fmt.Sprintf("slack rate limit exceeded, retry after %s", e.RetryAfter)
|
|
}
|
|
|
|
func (e *RateLimitedError) Retryable() bool {
|
|
return true
|
|
}
|
|
|
|
func fileUploadReq(ctx context.Context, path string, r io.Reader) (*http.Request, error) {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, path, r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return req, nil
|
|
}
|
|
|
|
func downloadFile(ctx context.Context, client httpClient, token string, downloadURL string, writer io.Writer, d Debug) error {
|
|
if downloadURL == "" {
|
|
return fmt.Errorf("received empty download URL")
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, &bytes.Buffer{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var bearer = "Bearer " + token
|
|
req.Header.Add("Authorization", bearer)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
err = checkStatusCode(resp, d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = io.Copy(writer, resp.Body)
|
|
|
|
return err
|
|
}
|
|
|
|
func formReq(ctx context.Context, endpoint string, values url.Values) (req *http.Request, err error) {
|
|
if req, err = http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(values.Encode())); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
return req, nil
|
|
}
|
|
|
|
func jsonReq(ctx context.Context, endpoint string, body interface{}) (req *http.Request, err error) {
|
|
buffer := bytes.NewBuffer([]byte{})
|
|
if err = json.NewEncoder(buffer).Encode(body); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if req, err = http.NewRequestWithContext(ctx, http.MethodPost, endpoint, buffer); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
|
return req, nil
|
|
}
|
|
|
|
func parseResponseBody(body io.ReadCloser, intf interface{}, d Debug) error {
|
|
response, err := io.ReadAll(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if d.Debug() {
|
|
d.Debugln("parseResponseBody", string(response))
|
|
}
|
|
|
|
return json.Unmarshal(response, intf)
|
|
}
|
|
|
|
func postLocalWithMultipartResponse(ctx context.Context, client httpClient, method, fpath, fieldname, token string, values url.Values, intf interface{}, d Debug) error {
|
|
fullpath, err := filepath.Abs(fpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
file, err := os.Open(fullpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
return postWithMultipartResponse(ctx, client, method, filepath.Base(fpath), fieldname, token, values, file, intf, d)
|
|
}
|
|
|
|
func postWithMultipartResponse(ctx context.Context, client httpClient, path, name, fieldname, token string, values url.Values, r io.Reader, intf interface{}, d Debug) error {
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
wr := multipart.NewWriter(pipeWriter)
|
|
|
|
errc := make(chan error)
|
|
go func() {
|
|
defer pipeWriter.Close()
|
|
defer wr.Close()
|
|
err := createFormFields(wr, values)
|
|
if err != nil {
|
|
errc <- err
|
|
return
|
|
}
|
|
ioWriter, err := wr.CreateFormFile(fieldname, name)
|
|
if err != nil {
|
|
errc <- err
|
|
return
|
|
}
|
|
_, err = io.Copy(ioWriter, r)
|
|
if err != nil {
|
|
errc <- err
|
|
return
|
|
}
|
|
if err = wr.Close(); err != nil {
|
|
errc <- err
|
|
return
|
|
}
|
|
}()
|
|
|
|
req, err := fileUploadReq(ctx, path, pipeReader)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Add("Content-Type", wr.FormDataContentType())
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
err = checkStatusCode(resp, d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
select {
|
|
case err = <-errc:
|
|
return err
|
|
default:
|
|
return newJSONParser(intf)(resp)
|
|
}
|
|
}
|
|
|
|
func createFormFields(mw *multipart.Writer, values url.Values) error {
|
|
for key, value := range values {
|
|
writer, err := mw.CreateFormField(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = writer.Write([]byte(value[0]))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func doPost(ctx context.Context, client httpClient, req *http.Request, parser responseParser, d Debug) error {
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
err = checkStatusCode(resp, d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return parser(resp)
|
|
}
|
|
|
|
// post JSON.
|
|
func postJSON(ctx context.Context, client httpClient, endpoint, token string, json []byte, intf interface{}, d Debug) error {
|
|
reqBody := bytes.NewBuffer(json)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, reqBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
return doPost(ctx, client, req, newJSONParser(intf), d)
|
|
}
|
|
|
|
// post a url encoded form.
|
|
func postForm(ctx context.Context, client httpClient, endpoint string, values url.Values, intf interface{}, d Debug) error {
|
|
reqBody := strings.NewReader(values.Encode())
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, reqBody)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
return doPost(ctx, client, req, newJSONParser(intf), d)
|
|
}
|
|
|
|
func getResource(ctx context.Context, client httpClient, endpoint, token string, values url.Values, intf interface{}, d Debug) error {
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
req.URL.RawQuery = values.Encode()
|
|
|
|
return doPost(ctx, client, req, newJSONParser(intf), d)
|
|
}
|
|
|
|
func parseAdminResponse(ctx context.Context, client httpClient, method string, teamName string, values url.Values, intf interface{}, d Debug) error {
|
|
endpoint := fmt.Sprintf(WEBAPIURLFormat, teamName, method, time.Now().Unix())
|
|
return postForm(ctx, client, endpoint, values, intf, d)
|
|
}
|
|
|
|
func logResponse(resp *http.Response, d Debug) error {
|
|
if d.Debug() {
|
|
text, err := httputil.DumpResponse(resp, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.Debugln(string(text))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func okJSONHandler(rw http.ResponseWriter, r *http.Request) {
|
|
rw.Header().Set("Content-Type", "application/json")
|
|
response, _ := json.Marshal(SlackResponse{
|
|
Ok: true,
|
|
})
|
|
rw.Write(response)
|
|
}
|
|
|
|
// timerReset safely reset a timer, see time.Timer.Reset for details.
|
|
func timerReset(t *time.Timer, d time.Duration) {
|
|
if !t.Stop() {
|
|
<-t.C
|
|
}
|
|
t.Reset(d)
|
|
}
|
|
|
|
func checkStatusCode(resp *http.Response, d Debug) error {
|
|
if resp.StatusCode == http.StatusTooManyRequests {
|
|
retry, err := strconv.ParseInt(resp.Header.Get("Retry-After"), 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return &RateLimitedError{time.Duration(retry) * time.Second}
|
|
}
|
|
|
|
// Slack seems to send an HTML body along with 5xx error codes. Don't parse it.
|
|
if resp.StatusCode != http.StatusOK {
|
|
logResponse(resp, d)
|
|
return StatusCodeError{Code: resp.StatusCode, Status: resp.Status}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type responseParser func(*http.Response) error
|
|
|
|
func newJSONParser(dst interface{}) responseParser {
|
|
return func(resp *http.Response) error {
|
|
if dst == nil {
|
|
return nil
|
|
}
|
|
return json.NewDecoder(resp.Body).Decode(dst)
|
|
}
|
|
}
|
|
|
|
func newTextParser(dst interface{}) responseParser {
|
|
return func(resp *http.Response) error {
|
|
b, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !bytes.Equal(b, []byte("ok")) {
|
|
return errors.New(string(b))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func newContentTypeParser(dst interface{}) responseParser {
|
|
return func(req *http.Response) (err error) {
|
|
var (
|
|
ctype string
|
|
)
|
|
|
|
if ctype, _, err = mime.ParseMediaType(req.Header.Get("Content-Type")); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch ctype {
|
|
case "application/json":
|
|
return newJSONParser(dst)(req)
|
|
default:
|
|
return newTextParser(dst)(req)
|
|
}
|
|
}
|
|
}
|