Skip to content

Commit

Permalink
move func PartStructure from webhook to queue, so it isn't tracked an…
Browse files Browse the repository at this point in the history
…ymore for apidiff changes

the types in webhook should be subjected to apidiff'ing, this was a shared
function. it is better off in package queue. also change the apidiff script so
it leaves apidiff/next.txt empty when there aren't any changes. makes it easier
to rotate the files after releases where nothing changed (a common occurrence).
  • Loading branch information
mjl- committed Dec 7, 2024
1 parent 0871bf5 commit 69a4995
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 52 deletions.
28 changes: 19 additions & 9 deletions apidiff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ if ! test -d tmp/mox-$prevversion; then
fi
(rm -r tmp/apidiff || exit 0)
mkdir -p tmp/apidiff/$prevversion tmp/apidiff/next
(rm apidiff/next.txt || exit 0)
(
echo "Below are the incompatible changes between $prevversion and next, per package."
echo
) >>apidiff/next.txt
(rm apidiff/next.txt apidiff/next.txt.new 2>/dev/null || exit 0)
for p in $(cat apidiff/packages.txt); do
if ! test -d tmp/mox-$prevversion/$p; then
continue
fi
(cd tmp/mox-$prevversion && apidiff -w ../apidiff/$prevversion/$p.api ./$p)
apidiff -w tmp/apidiff/next/$p.api ./$p
apidiff -incompatible tmp/apidiff/$prevversion/$p.api tmp/apidiff/next/$p.api >$p.diff
if test -s $p.diff; then
(
echo '#' $p
cat $p.diff
echo
) >>apidiff/next.txt.new
fi
rm $p.diff
done
if test -s apidiff/next.txt.new; then
(
echo '#' $p
apidiff -incompatible tmp/apidiff/$prevversion/$p.api tmp/apidiff/next/$p.api
echo "Below are the incompatible changes between $prevversion and next, per package."
echo
) >>apidiff/next.txt
done
cat apidiff/next.txt.new
) >apidiff/next.txt
rm apidiff/next.txt.new
else
mv apidiff/next.txt.new apidiff/next.txt
fi
5 changes: 5 additions & 0 deletions apidiff/next.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Below are the incompatible changes between v0.0.13 and next, per package.

# webhook
- PartStructure: removed

35 changes: 34 additions & 1 deletion queue/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package queue
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
Expand Down Expand Up @@ -796,7 +797,7 @@ func Incoming(ctx context.Context, log mlog.Log, acc *store.Account, messageID s

log.Debug("composing webhook for incoming message")

structure, err := webhook.PartStructure(log, &part)
structure, err := PartStructure(log, &part)
if err != nil {
return fmt.Errorf("parsing part structure: %v", err)
}
Expand Down Expand Up @@ -912,6 +913,38 @@ func Incoming(ctx context.Context, log mlog.Log, acc *store.Account, messageID s
return nil
}

// PartStructure returns a webhook.Structure for a parsed message part.
func PartStructure(log mlog.Log, p *message.Part) (webhook.Structure, error) {
parts := make([]webhook.Structure, len(p.Parts))
for i := range p.Parts {
var err error
parts[i], err = PartStructure(log, &p.Parts[i])
if err != nil && !errors.Is(err, message.ErrParamEncoding) {
return webhook.Structure{}, err
}
}
disp, filename, err := p.DispositionFilename()
if err != nil && errors.Is(err, message.ErrParamEncoding) {
log.Debugx("parsing disposition/filename", err)
} else if err != nil {
return webhook.Structure{}, err
}
s := webhook.Structure{
ContentType: strings.ToLower(p.MediaType + "/" + p.MediaSubType),
ContentTypeParams: p.ContentTypeParams,
ContentID: p.ContentID,
ContentDisposition: strings.ToLower(disp),
Filename: filename,
DecodedSize: p.DecodedSize,
Parts: parts,
}
// Replace nil map with empty map, for easier to use JSON.
if s.ContentTypeParams == nil {
s.ContentTypeParams = map[string]string{}
}
return s, nil
}

func isAutomated(h textproto.MIMEHeader) bool {
l := []string{"List-Id", "List-Unsubscribe", "List-Unsubscribe-Post", "Precedence"}
for _, k := range l {
Expand Down
2 changes: 1 addition & 1 deletion queue/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestHookIncoming(t *testing.T) {
tcheck(t, err, "decode incoming webhook")
in.Meta.Received = in.Meta.Received.Local() // For TZ UTC.

structure, err := webhook.PartStructure(pkglog, &part)
structure, err := PartStructure(pkglog, &part)
tcheck(t, err, "part structure")

expIncoming := webhook.Incoming{
Expand Down
3 changes: 1 addition & 2 deletions webapisrv/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import (
"github.com/mjl-/mox/store"
"github.com/mjl-/mox/webapi"
"github.com/mjl-/mox/webauth"
"github.com/mjl-/mox/webhook"
"github.com/mjl-/mox/webops"
)

Expand Down Expand Up @@ -1263,7 +1262,7 @@ func (s server) MessageGet(ctx context.Context, req webapi.MessageGetRequest) (r
MailboxName: mb.Name,
}

structure, err := webhook.PartStructure(log, &p)
structure, err := queue.PartStructure(log, &p)
xcheckf(err, "parsing structure")

result := webapi.MessageGetResult{
Expand Down
3 changes: 1 addition & 2 deletions webapisrv/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/mjl-/mox/queue"
"github.com/mjl-/mox/store"
"github.com/mjl-/mox/webapi"
"github.com/mjl-/mox/webhook"
)

var ctxbg = context.Background()
Expand Down Expand Up @@ -418,7 +417,7 @@ func TestServer(t *testing.T) {
tcheckf(t, err, "reading raw message")
part, err := message.EnsurePart(log.Logger, true, bytes.NewReader(b.Bytes()), int64(b.Len()))
tcheckf(t, err, "parsing raw message")
structure, err := webhook.PartStructure(log, &part)
structure, err := queue.PartStructure(log, &part)
tcheckf(t, err, "part structure")
tcompare(t, structure, msgRes.Structure)

Expand Down
37 changes: 0 additions & 37 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
package webhook

import (
"errors"
"strings"
"time"

"github.com/mjl-/mox/message"
"github.com/mjl-/mox/mlog"
)

// OutgoingEvent is an activity for an outgoing delivery. Either generated by the
Expand Down Expand Up @@ -145,35 +140,3 @@ type Structure struct {
DecodedSize int64 // Size of content after decoding content-transfer-encoding. For text and HTML parts, this can be larger than the data returned since this size includes \r\n line endings.
Parts []Structure // Subparts of a multipart message, possibly recursive.
}

// PartStructure returns a Structure for a parsed message part.
func PartStructure(log mlog.Log, p *message.Part) (Structure, error) {
parts := make([]Structure, len(p.Parts))
for i := range p.Parts {
var err error
parts[i], err = PartStructure(log, &p.Parts[i])
if err != nil && !errors.Is(err, message.ErrParamEncoding) {
return Structure{}, err
}
}
disp, filename, err := p.DispositionFilename()
if err != nil && errors.Is(err, message.ErrParamEncoding) {
log.Debugx("parsing disposition/filename", err)
} else if err != nil {
return Structure{}, err
}
s := Structure{
ContentType: strings.ToLower(p.MediaType + "/" + p.MediaSubType),
ContentTypeParams: p.ContentTypeParams,
ContentID: p.ContentID,
ContentDisposition: strings.ToLower(disp),
Filename: filename,
DecodedSize: p.DecodedSize,
Parts: parts,
}
// Replace nil map with empty map, for easier to use JSON.
if s.ContentTypeParams == nil {
s.ContentTypeParams = map[string]string{}
}
return s, nil
}

0 comments on commit 69a4995

Please sign in to comment.