Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
timoreimann committed Aug 20, 2024
1 parent ad424a6 commit 90f4d7b
Show file tree
Hide file tree
Showing 14 changed files with 2,294 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/slack-go/slack v0.6.3
github.com/urfave/cli/v2 v2.1.1
gopkg.in/yaml.v2 v2.2.8
mvdan.cc/xurls/v2 v2.5.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
mvdan.cc/xurls/v2 v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
mvdan.cc/xurls/v2 v2.5.0/go.mod h1:yQgaGQ1rFtJUzkmKiHYSSfuQxqfYmd//X6PxvholpeE=
23 changes: 23 additions & 0 deletions isolated/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package isolated

import (
"encoding/json"
"fmt"
"testing"

"github.com/slack-go/slack"
)

func TestTest(t *testing.T) {
obj := slack.NewTextBlockObject("plain_text", "here is a url: golang.org", false, false)
block := slack.NewSectionBlock(obj, nil, nil)
msg := slack.NewBlockMessage(block)

b, err := json.MarshalIndent(msg, "", " ")
if err != nil {
t.Fatal(err)
}

fmt.Println(string(b))

}
93 changes: 90 additions & 3 deletions slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"time"

"github.com/PagerDuty/go-pagerduty"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/matryer/try"

"github.com/PagerDuty/go-pagerduty"

"github.com/slack-go/slack"
"mvdan.cc/xurls/v2"
)

var rxRelaxed = xurls.Relaxed()

type slackUsers []slackUser

func (users slackUsers) findByPDUser(pdUser pagerduty.User) *slackUser {
Expand Down Expand Up @@ -276,6 +278,91 @@ func (metaClient *slackMetaClient) updateTopic(ctx context.Context, channelID st
return nil
}

func escapeText(txt string) (string, error) {
obj := slack.NewTextBlockObject("mrkdown", "foobarbar", false, false)
block := slack.NewSectionBlock(obj, nil, nil)
msg := slack.NewBlockMessage(block)
msg.

Check failure on line 285 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

non-name msg.remainingTxt on left side of :=

Check failure on line 285 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

non-name msg.remainingTxt on left side of :=

remainingTxt := escapeAmpersands(txt)

Check failure on line 287 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

msg.remainingTxt undefined (type slack.Message has no field or method remainingTxt)

Check failure on line 287 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

msg.remainingTxt undefined (type slack.Message has no field or method remainingTxt)

for {
idxPair := rxRelaxed.FindStringIndex(remainingTxt)

Check failure on line 290 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: remainingTxt

Check failure on line 290 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: remainingTxt
if idxPair == nil {
break
}

startIdx := idxPair[0]
if startIdx > 0 && startIdx != '<' && startIdx != '|' {
continue
}

rxRelaxed.ReplaceAllString()

Check failure on line 300 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

not enough arguments in call to rxRelaxed.ReplaceAllString

Check failure on line 300 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

not enough arguments in call to rxRelaxed.ReplaceAllString

match := remainingTxt[startIdx:indexPair[1]]

Check failure on line 302 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: remainingTxt

Check failure on line 302 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: indexPair

Check failure on line 302 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: remainingTxt

Check failure on line 302 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: indexPair
parsedURL, err := url.Parse(match)
if err != nil {
return "", fmt.Errorf("failed to parse URL %q: %s", match, err)
}
if parsedURL.Scheme == "" {
strings.ReplaceAll(txt, match, fmt.Sprintf("<http://%s|%s>", match, match))
} else {
strings.ReplaceAll(txt, match, fmt.Sprintf("<http://%s|%s>", match, match))
}
}

}

Check failure on line 314 in slack.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

missing return

Check failure on line 314 in slack.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

missing return

func escapeAmpersands(txt string) string {
parts := strings.Split(txt, "&")
if len(parts) == 1 {
return txt
}

const ampSuffix = "amp;"

var b strings.Builder
for i, part := range parts {
b.WriteString(part)
if i == len(parts)-1 {
break
}
b.WriteRune('&')
nextIdx := i + 1
if strings.HasPrefix(parts[nextIdx], ampSuffix) {
b.WriteString(ampSuffix)
parts[nextIdx] = strings.TrimPrefix(parts[nextIdx], ampSuffix)
}
}

return b.String()
}

func isEncodedURL(txt string, pair []int) bool {
left := pair[0] - 1
if left == 0 {
return false
}
right := pair[1] + 1
if right == len(txt)-1 {
return false
}
return txt[left] == '<' && txt[right] == '>'
}

func textMatchesAt(txt string, i int, substr string) bool {
if len(txt) < i {
return false
}

suffix := txt[i:]
lenSubstr := len(substr)
if len(suffix) < lenSubstr {
return false
}
return suffix[:lenSubstr] == substr
}

func createSlackUser(apiUser slack.User) slackUser {
return slackUser{
id: apiUser.ID,
Expand Down
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ golang.org/x/xerrors/internal
# gopkg.in/yaml.v2 v2.2.8
## explicit
gopkg.in/yaml.v2
# mvdan.cc/xurls/v2 v2.5.0
## explicit; go 1.19
mvdan.cc/xurls/v2
2 changes: 2 additions & 0 deletions vendor/mvdan.cc/xurls/v2/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# To prevent CRLF breakages on Windows for fragile files, like testdata.
* -text
3 changes: 3 additions & 0 deletions vendor/mvdan.cc/xurls/v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmd/xurls/xurls
generate/tldsgen/tldsgen
generate/regexgen/regexgen
27 changes: 27 additions & 0 deletions vendor/mvdan.cc/xurls/v2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2015, Daniel Martí. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 changes: 37 additions & 0 deletions vendor/mvdan.cc/xurls/v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# xurls

[![Go Reference](https://pkg.go.dev/badge/mvdan.cc/xurls/v2.svg)](https://pkg.go.dev/mvdan.cc/xurls/v2)

Extract urls from text using regular expressions. Requires Go 1.19 or later.

```go
import "mvdan.cc/xurls/v2"

func main() {
rxRelaxed := xurls.Relaxed()
rxRelaxed.FindString("Do gophers live in golang.org?") // "golang.org"
rxRelaxed.FindString("This string does not have a URL") // ""

rxStrict := xurls.Strict()
rxStrict.FindAllString("must have scheme: http://foo.com/.", -1) // []string{"http://foo.com/"}
rxStrict.FindAllString("no scheme, no match: foo.com", -1) // []string{}
}
```

Since API is centered around [regexp.Regexp](https://golang.org/pkg/regexp/#Regexp),
many other methods are available, such as finding the [byte indexes](https://golang.org/pkg/regexp/#Regexp.FindAllIndex)
for all matches.

The regular expressions are compiled when the API is first called.
Any subsequent calls will use the same regular expression pointers.

#### cmd/xurls

To install the tool globally:

go install mvdan.cc/xurls/v2/cmd/xurls@latest

```shell
$ echo "Do gophers live in http://golang.org?" | xurls
http://golang.org
```
Loading

0 comments on commit 90f4d7b

Please sign in to comment.