From 09b54870390c2be9f95a9caef5059c42a0b96766 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Wed, 10 Apr 2024 10:54:49 +0200 Subject: [PATCH 1/3] refactor: use slack's internal markdown converter This is a companion PR that goes with https://github.com/rneatherway/slack/pull/4 to showcase what changes will look like in the gh-slack client. It keeps the code as untouched as possible, and cleans the obsolete functions/types. --- go.mod | 2 +- go.sum | 4 ++ internal/markdown/markdown.go | 65 ++----------------------- internal/markdown/markdown_test.go | 77 ------------------------------ 4 files changed, 9 insertions(+), 139 deletions(-) diff --git a/go.mod b/go.mod index 40c2bb1..9414943 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.21.4 require ( github.com/cli/go-gh v1.2.1 - github.com/rneatherway/slack v0.0.0-20240323132214-0af54ceb5c65 + github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 nhooyr.io/websocket v1.8.7 diff --git a/go.sum b/go.sum index 411152d..53df60e 100644 --- a/go.sum +++ b/go.sum @@ -116,6 +116,10 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rneatherway/slack v0.0.0-20240323132214-0af54ceb5c65 h1:aKxOcmudHNIZz2C0ncYCoRIiHAP1Js4EMsmOPG19eDQ= github.com/rneatherway/slack v0.0.0-20240323132214-0af54ceb5c65/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= +github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57 h1:MgpkjhZx/+Kb7ILhjKiXBkbktobLSzdufvpphGJG8Bo= +github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= +github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802 h1:nX3eONrktjfHvc8oIgLnYLlhVY3fy/QSKVfKTYw1PLk= +github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= diff --git a/internal/markdown/markdown.go b/internal/markdown/markdown.go index d1aeb55..f85bc84 100644 --- a/internal/markdown/markdown.go +++ b/internal/markdown/markdown.go @@ -2,77 +2,20 @@ package markdown import ( "fmt" - "regexp" "sort" - "strconv" "strings" "time" "github.com/rneatherway/gh-slack/internal/slackclient" + "github.com/rneatherway/slack/pkg/markdown" ) -var userRE = regexp.MustCompile("<@[A-Z0-9]+>") -var linkRE = regexp.MustCompile(`<(https?://[^|>]+)\|([^>]+)>`) -var openCodefence = regexp.MustCompile("(?m)^```") -var closeCodefence = regexp.MustCompile("(?m)(.)```$") - -type UserProvider interface { - UsernameForID(string) (string, error) -} - -func interpolateUsers(client UserProvider, s string) (string, error) { - userLocations := userRE.FindAllStringIndex(s, -1) - out := &strings.Builder{} - last := 0 - for _, userLocation := range userLocations { - start := userLocation[0] - end := userLocation[1] - - username, err := client.UsernameForID(s[start+2 : end-1]) - if err != nil { - return "", err - } - out.WriteString(s[last:start]) - out.WriteString("`@") - out.WriteString(username) - out.WriteRune('`') - last = end - } - out.WriteString(s[last:]) - - return out.String(), nil -} - -func parseUnixTimestamp(s string) (*time.Time, error) { - tsParts := strings.Split(s, ".") - if len(tsParts) != 2 { - return nil, fmt.Errorf("timestamp '%s' is not in . format", s) - } - - seconds, err := strconv.ParseInt(tsParts[0], 10, 64) - if err != nil { - return nil, err - } - - nanos, err := strconv.ParseInt(tsParts[1], 10, 64) - if err != nil { - return nil, err - } - - result := time.Unix(seconds, nanos) - return &result, nil -} - -func convert(client UserProvider, b *strings.Builder, s string) error { - text, err := interpolateUsers(client, s) +func convert(client *slackclient.SlackClient, b *strings.Builder, s string) error { + text, err := markdown.Convert(client, s) if err != nil { return err } - text = linkRE.ReplaceAllString(text, "[$2]($1)") - text = openCodefence.ReplaceAllString(text, "```\n") - text = closeCodefence.ReplaceAllString(text, "$1\n```") - for _, line := range strings.Split(text, "\n") { // TODO: Might be a good idea to escape 'line' fmt.Fprintf(b, "> %s\n", line) @@ -87,7 +30,7 @@ func FromMessages(client *slackclient.SlackClient, history *slackclient.HistoryR msgTimes := make(map[string]time.Time, len(messages)) for _, message := range messages { - tm, err := parseUnixTimestamp(message.Ts) + tm, err := markdown.ParseUnixTimestamp(message.Ts) if err != nil { return "", err } diff --git a/internal/markdown/markdown_test.go b/internal/markdown/markdown_test.go index c8efbf9..2123eea 100644 --- a/internal/markdown/markdown_test.go +++ b/internal/markdown/markdown_test.go @@ -1,7 +1,6 @@ package markdown import ( - "fmt" "strings" "testing" @@ -127,79 +126,3 @@ func TestFromMessagesSeparatesMessagesFromSameUserWhenNotAdjacent(t *testing.T) t.Fatal("expected:\n\n", expected, "\n\ngot:\n\n", actual) } } - -func TestUserRE(t *testing.T) { - results := userRE.FindAllStringIndex("<@UP7UAV3NH> <@UPA5ANVNJ> hello", -1) - if len(results) != 2 { - t.Errorf("results length %d", len(results)) - } - if !(results[0][0] == 0 && results[0][1] == 12) { - t.Errorf("first match %v", results[0]) - } - if !(results[1][0] == 13 && results[1][1] == 25) { - t.Errorf("second match %v", results[1]) - } -} - -type TestUserProvider struct { - counter int -} - -func (c *TestUserProvider) UsernameForID(id string) (string, error) { - c.counter += 1 - return fmt.Sprintf("test_username_%d", c.counter), nil -} - -func TestInterpolateUsers(t *testing.T) { - table := [][]string{ - {"<@UP7UAV3NH>", "`@test_username_1`"}, - {"<@UP7UAV3NH> hi hi", "`@test_username_1` hi hi"}, - {"hi<@UP7UAV3NH> hi hi", "hi`@test_username_1` hi hi"}, - {"<@UP7UAV3NH> hello <@UP756V3NH>", "`@test_username_1` hello `@test_username_2`"}, - {"<@UP7UAV3NH> <@UP756V3NH> hello", "`@test_username_1` `@test_username_2` hello"}, - } - - for _, test := range table { - input := test[0] - expected := test[1] - actual, _ := interpolateUsers(&TestUserProvider{}, input) - - if actual != expected { - t.Errorf("expected %q, actual %q", expected, actual) - } - } -} - -func TestLinkify(t *testing.T) { - table := [][]string{ - {"Hello end", "Hello [text](https://example.com) end"}, - {"Hello end and here is another link go check it out", "Hello [text](https://example.com) end and here is another link [GitHub!!](http://github.com) go check it out"}, - } - - for _, test := range table { - input := test[0] - expected := test[1] - actual := linkRE.ReplaceAllString(input, "[$2]($1)") - - if actual != expected { - t.Errorf("expected %q, actual %q", expected, actual) - } - } -} - -func TestFixCodefence(t *testing.T) { - table := [][]string{ - {"```{\n x: y,\n a: b\n}```", "```\n{\n x: y,\n a: b\n}\n```"}, - } - - for _, test := range table { - input := test[0] - expected := test[1] - actual := openCodefence.ReplaceAllLiteralString(input, "```\n") - actual = closeCodefence.ReplaceAllString(actual, "$1\n```") - - if actual != expected { - t.Errorf("expected %q, actual %q", expected, actual) - } - } -} From 97c834548c48bd13d977d78402535f80c9c3a0f2 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Fri, 12 Apr 2024 09:04:36 +0200 Subject: [PATCH 2/3] feat: update to latest commit of slack --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9414943..dc08e83 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.21.4 require ( github.com/cli/go-gh v1.2.1 - github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802 + github.com/rneatherway/slack v0.0.0-20240412070247-8ea7ec9fab1c github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 nhooyr.io/websocket v1.8.7 diff --git a/go.sum b/go.sum index 53df60e..13788a4 100644 --- a/go.sum +++ b/go.sum @@ -120,6 +120,8 @@ github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57 h1:MgpkjhZx/+Kb7 github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802 h1:nX3eONrktjfHvc8oIgLnYLlhVY3fy/QSKVfKTYw1PLk= github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= +github.com/rneatherway/slack v0.0.0-20240412070247-8ea7ec9fab1c h1:P6rQJVjkF3pLp1nCJbE/S82nEV5ilx2AVoUY4PwkC5g= +github.com/rneatherway/slack v0.0.0-20240412070247-8ea7ec9fab1c/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.6.1 h1:o94oiPyS4KD1mPy2fmcYYHHfCxLqYjJOhGsCHFZtEzA= github.com/spf13/cobra v1.6.1/go.mod h1:IOw/AERYS7UzyrGinqmz6HLUo219MORXGxhbaJUqzrY= From b4a4c31a7edc943554d2366efb3f49ac5159f0e0 Mon Sep 17 00:00:00 2001 From: nobe4 Date: Sun, 14 Apr 2024 08:23:24 +0200 Subject: [PATCH 3/3] fix: go mod tidy --- go.sum | 6 ------ 1 file changed, 6 deletions(-) diff --git a/go.sum b/go.sum index 13788a4..efb172e 100644 --- a/go.sum +++ b/go.sum @@ -114,12 +114,6 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rneatherway/slack v0.0.0-20240323132214-0af54ceb5c65 h1:aKxOcmudHNIZz2C0ncYCoRIiHAP1Js4EMsmOPG19eDQ= -github.com/rneatherway/slack v0.0.0-20240323132214-0af54ceb5c65/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= -github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57 h1:MgpkjhZx/+Kb7ILhjKiXBkbktobLSzdufvpphGJG8Bo= -github.com/rneatherway/slack v0.0.0-20240410084118-1b1f3c7dce57/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= -github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802 h1:nX3eONrktjfHvc8oIgLnYLlhVY3fy/QSKVfKTYw1PLk= -github.com/rneatherway/slack v0.0.0-20240410084904-d41e05cbe802/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= github.com/rneatherway/slack v0.0.0-20240412070247-8ea7ec9fab1c h1:P6rQJVjkF3pLp1nCJbE/S82nEV5ilx2AVoUY4PwkC5g= github.com/rneatherway/slack v0.0.0-20240412070247-8ea7ec9fab1c/go.mod h1:2WA0D8ytQf+d/hE4QqppWRjFOz86WFG6XX8rpsRLHT8= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=