From 23e5b5104c380524a2fb64e5b1de9dd96773454b Mon Sep 17 00:00:00 2001 From: nelind Date: Sat, 31 May 2025 23:20:41 +0200 Subject: [PATCH] lexicons: pulls: put target repo and branch together in their own object Change-Id: qrtnmvyqtvvkkvxyovyokruwwqorrkor Signed-off-by: nelind --- api/tangled/cbor_gen.go | 230 ++++++++++++++++++++++++++++----------- api/tangled/repopull.go | 9 +- appview/db/pulls.go | 6 +- appview/pulls/pulls.go | 12 +- cmd/gen.go | 1 + lexicons/pulls/pull.json | 28 +++-- 6 files changed, 206 insertions(+), 80 deletions(-) diff --git a/api/tangled/cbor_gen.go b/api/tangled/cbor_gen.go index 56d29cf..f8e5299 100644 --- a/api/tangled/cbor_gen.go +++ b/api/tangled/cbor_gen.go @@ -6741,6 +6741,140 @@ func (t *RepoIssueState) UnmarshalCBOR(r io.Reader) (err error) { return nil } +func (t *RepoPull_Target) MarshalCBOR(w io.Writer) error { + if t == nil { + _, err := w.Write(cbg.CborNull) + return err + } + + cw := cbg.NewCborWriter(w) + + if _, err := cw.Write([]byte{162}); err != nil { + return err + } + + // t.Repo (string) (string) + if len("repo") > 1000000 { + return xerrors.Errorf("Value in field \"repo\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("repo"))); err != nil { + return err + } + if _, err := cw.WriteString(string("repo")); err != nil { + return err + } + + if len(t.Repo) > 1000000 { + return xerrors.Errorf("Value in field t.Repo was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Repo))); err != nil { + return err + } + if _, err := cw.WriteString(string(t.Repo)); err != nil { + return err + } + + // t.Branch (string) (string) + if len("branch") > 1000000 { + return xerrors.Errorf("Value in field \"branch\" was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("branch"))); err != nil { + return err + } + if _, err := cw.WriteString(string("branch")); err != nil { + return err + } + + if len(t.Branch) > 1000000 { + return xerrors.Errorf("Value in field t.Branch was too long") + } + + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.Branch))); err != nil { + return err + } + if _, err := cw.WriteString(string(t.Branch)); err != nil { + return err + } + return nil +} + +func (t *RepoPull_Target) UnmarshalCBOR(r io.Reader) (err error) { + *t = RepoPull_Target{} + + cr := cbg.NewCborReader(r) + + maj, extra, err := cr.ReadHeader() + if err != nil { + return err + } + defer func() { + if err == io.EOF { + err = io.ErrUnexpectedEOF + } + }() + + if maj != cbg.MajMap { + return fmt.Errorf("cbor input should be of type map") + } + + if extra > cbg.MaxLength { + return fmt.Errorf("RepoPull_Target: map struct too large (%d)", extra) + } + + n := extra + + nameBuf := make([]byte, 6) + for i := uint64(0); i < n; i++ { + nameLen, ok, err := cbg.ReadFullStringIntoBuf(cr, nameBuf, 1000000) + if err != nil { + return err + } + + if !ok { + // Field doesn't exist on this type, so ignore it + if err := cbg.ScanForLinks(cr, func(cid.Cid) {}); err != nil { + return err + } + continue + } + + switch string(nameBuf[:nameLen]) { + // t.Repo (string) (string) + case "repo": + + { + sval, err := cbg.ReadStringWithMax(cr, 1000000) + if err != nil { + return err + } + + t.Repo = string(sval) + } + // t.Branch (string) (string) + case "branch": + + { + sval, err := cbg.ReadStringWithMax(cr, 1000000) + if err != nil { + return err + } + + t.Branch = string(sval) + } + + default: + // Field doesn't exist on this type, so ignore it + if err := cbg.ScanForLinks(r, func(cid.Cid) {}); err != nil { + return err + } + } + } + + return nil +} func (t *RepoPull) MarshalCBOR(w io.Writer) error { if t == nil { _, err := w.Write(cbg.CborNull) @@ -6748,7 +6882,7 @@ func (t *RepoPull) MarshalCBOR(w io.Writer) error { } cw := cbg.NewCborWriter(w) - fieldCount := 8 + fieldCount := 7 if t.Body == nil { fieldCount-- @@ -6878,72 +7012,42 @@ func (t *RepoPull) MarshalCBOR(w io.Writer) error { } } - // t.CreatedAt (string) (string) - if len("createdAt") > 1000000 { - return xerrors.Errorf("Value in field \"createdAt\" was too long") + // t.Target (tangled.RepoPull_Target) (struct) + if len("target") > 1000000 { + return xerrors.Errorf("Value in field \"target\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("createdAt"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("target"))); err != nil { return err } - if _, err := cw.WriteString(string("createdAt")); err != nil { + if _, err := cw.WriteString(string("target")); err != nil { return err } - if len(t.CreatedAt) > 1000000 { - return xerrors.Errorf("Value in field t.CreatedAt was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.CreatedAt))); err != nil { - return err - } - if _, err := cw.WriteString(string(t.CreatedAt)); err != nil { - return err - } - - // t.TargetRepo (string) (string) - if len("targetRepo") > 1000000 { - return xerrors.Errorf("Value in field \"targetRepo\" was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("targetRepo"))); err != nil { - return err - } - if _, err := cw.WriteString(string("targetRepo")); err != nil { + if err := t.Target.MarshalCBOR(cw); err != nil { return err } - if len(t.TargetRepo) > 1000000 { - return xerrors.Errorf("Value in field t.TargetRepo was too long") - } - - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.TargetRepo))); err != nil { - return err - } - if _, err := cw.WriteString(string(t.TargetRepo)); err != nil { - return err - } - - // t.TargetBranch (string) (string) - if len("targetBranch") > 1000000 { - return xerrors.Errorf("Value in field \"targetBranch\" was too long") + // t.CreatedAt (string) (string) + if len("createdAt") > 1000000 { + return xerrors.Errorf("Value in field \"createdAt\" was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("targetBranch"))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("createdAt"))); err != nil { return err } - if _, err := cw.WriteString(string("targetBranch")); err != nil { + if _, err := cw.WriteString(string("createdAt")); err != nil { return err } - if len(t.TargetBranch) > 1000000 { - return xerrors.Errorf("Value in field t.TargetBranch was too long") + if len(t.CreatedAt) > 1000000 { + return xerrors.Errorf("Value in field t.CreatedAt was too long") } - if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.TargetBranch))); err != nil { + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.CreatedAt))); err != nil { return err } - if _, err := cw.WriteString(string(t.TargetBranch)); err != nil { + if _, err := cw.WriteString(string(t.CreatedAt)); err != nil { return err } return nil @@ -6974,7 +7078,7 @@ func (t *RepoPull) UnmarshalCBOR(r io.Reader) (err error) { n := extra - nameBuf := make([]byte, 12) + nameBuf := make([]byte, 9) for i := uint64(0); i < n; i++ { nameLen, ok, err := cbg.ReadFullStringIntoBuf(cr, nameBuf, 1000000) if err != nil { @@ -7064,30 +7168,28 @@ func (t *RepoPull) UnmarshalCBOR(r io.Reader) (err error) { } } - // t.CreatedAt (string) (string) - case "createdAt": + // t.Target (tangled.RepoPull_Target) (struct) + case "target": { - sval, err := cbg.ReadStringWithMax(cr, 1000000) - if err != nil { - return err - } - t.CreatedAt = string(sval) - } - // t.TargetRepo (string) (string) - case "targetRepo": - - { - sval, err := cbg.ReadStringWithMax(cr, 1000000) + b, err := cr.ReadByte() if err != nil { return err } + if b != cbg.CborNull[0] { + if err := cr.UnreadByte(); err != nil { + return err + } + t.Target = new(RepoPull_Target) + if err := t.Target.UnmarshalCBOR(cr); err != nil { + return xerrors.Errorf("unmarshaling t.Target pointer: %w", err) + } + } - t.TargetRepo = string(sval) } - // t.TargetBranch (string) (string) - case "targetBranch": + // t.CreatedAt (string) (string) + case "createdAt": { sval, err := cbg.ReadStringWithMax(cr, 1000000) @@ -7095,7 +7197,7 @@ func (t *RepoPull) UnmarshalCBOR(r io.Reader) (err error) { return err } - t.TargetBranch = string(sval) + t.CreatedAt = string(sval) } default: diff --git a/api/tangled/repopull.go b/api/tangled/repopull.go index dbed245..c56931c 100644 --- a/api/tangled/repopull.go +++ b/api/tangled/repopull.go @@ -22,8 +22,7 @@ type RepoPull struct { CreatedAt string `json:"createdAt" cborgen:"createdAt"` Patch string `json:"patch" cborgen:"patch"` Source *RepoPull_Source `json:"source,omitempty" cborgen:"source,omitempty"` - TargetBranch string `json:"targetBranch" cborgen:"targetBranch"` - TargetRepo string `json:"targetRepo" cborgen:"targetRepo"` + Target *RepoPull_Target `json:"target" cborgen:"target"` Title string `json:"title" cborgen:"title"` } @@ -33,3 +32,9 @@ type RepoPull_Source struct { Repo *string `json:"repo,omitempty" cborgen:"repo,omitempty"` Sha string `json:"sha" cborgen:"sha"` } + +// RepoPull_Target is a "target" in the sh.tangled.repo.pull schema. +type RepoPull_Target struct { + Branch string `json:"branch" cborgen:"branch"` + Repo string `json:"repo" cborgen:"repo"` +} diff --git a/appview/db/pulls.go b/appview/db/pulls.go index 69d8edf..ffeefb5 100644 --- a/appview/db/pulls.go +++ b/appview/db/pulls.go @@ -94,8 +94,10 @@ func (p Pull) AsRecord() tangled.RepoPull { Title: p.Title, Body: &p.Body, CreatedAt: p.Created.Format(time.RFC3339), - TargetRepo: p.RepoAt.String(), - TargetBranch: p.TargetBranch, + Target: &tangled.RepoPull_Target{ + Repo: p.RepoAt.String(), + Branch: p.TargetBranch, + }, Patch: p.LatestPatch(), Source: source, } diff --git a/appview/pulls/pulls.go b/appview/pulls/pulls.go index fc36ada..1fbc403 100644 --- a/appview/pulls/pulls.go +++ b/appview/pulls/pulls.go @@ -1091,8 +1091,10 @@ func (s *Pulls) createPullRequest( Record: &lexutil.LexiconTypeDecoder{ Val: &tangled.RepoPull{ Title: title, - TargetRepo: string(f.RepoAt), - TargetBranch: targetBranch, + Target: &tangled.RepoPull_Target{ + Repo: string(f.RepoAt), + Branch: targetBranch, + }, Patch: patch, Source: recordPullSource, }, @@ -1652,8 +1654,10 @@ func (s *Pulls) resubmitPullHelper( Record: &lexutil.LexiconTypeDecoder{ Val: &tangled.RepoPull{ Title: pull.Title, - TargetRepo: string(f.RepoAt), - TargetBranch: pull.TargetBranch, + Target: &tangled.RepoPull_Target{ + Repo: string(f.RepoAt), + Branch: pull.TargetBranch, + }, Patch: patch, // new patch Source: recordPullSource, }, diff --git a/cmd/gen.go b/cmd/gen.go index ca42aa7..4915e8e 100644 --- a/cmd/gen.go +++ b/cmd/gen.go @@ -43,6 +43,7 @@ func main() { tangled.RepoIssue{}, tangled.RepoIssueComment{}, tangled.RepoIssueState{}, + tangled.RepoPull_Target{}, tangled.RepoPull{}, tangled.RepoPullComment{}, tangled.RepoPull_Source{}, diff --git a/lexicons/pulls/pull.json b/lexicons/pulls/pull.json index 597f147..143131d 100644 --- a/lexicons/pulls/pull.json +++ b/lexicons/pulls/pull.json @@ -10,19 +10,15 @@ "record": { "type": "object", "required": [ - "targetRepo", - "targetBranch", + "target", "title", "patch", "createdAt" ], "properties": { - "targetRepo": { - "type": "string", - "format": "at-uri" - }, - "targetBranch": { - "type": "string" + "target": { + "type": "ref", + "ref": "#target" }, "title": { "type": "string" @@ -44,6 +40,22 @@ } } }, + "target": { + "type": "object", + "required": [ + "repo", + "branch" + ], + "properties": { + "repo": { + "type": "string", + "format": "at-uri" + }, + "branch": { + "type": "string" + } + } + }, "source": { "type": "object", "required": [ -- 2.50.0