lexicons: pulls: add stack information to pull records #405

open
opened by nel.pet targeting master from nel.pet/core: push-kyupnpkvqmsy
Changed files
+306 -20
api
appview
cmd
lexicons
pulls
+202 -1
api/tangled/cbor_gen.go
··· 6392 6392 6393 6393 return nil 6394 6394 } 6395 + func (t *RepoPull_StackInfo) MarshalCBOR(w io.Writer) error { 6396 + if t == nil { 6397 + _, err := w.Write(cbg.CborNull) 6398 + return err 6399 + } 6400 + 6401 + cw := cbg.NewCborWriter(w) 6402 + fieldCount := 2 6403 + 6404 + if t.Parent == nil { 6405 + fieldCount-- 6406 + } 6407 + 6408 + if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(fieldCount))); err != nil { 6409 + return err 6410 + } 6411 + 6412 + // t.Parent (string) (string) 6413 + if t.Parent != nil { 6414 + 6415 + if len("parent") > 1000000 { 6416 + return xerrors.Errorf("Value in field \"parent\" was too long") 6417 + } 6418 + 6419 + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("parent"))); err != nil { 6420 + return err 6421 + } 6422 + if _, err := cw.WriteString(string("parent")); err != nil { 6423 + return err 6424 + } 6425 + 6426 + if t.Parent == nil { 6427 + if _, err := cw.Write(cbg.CborNull); err != nil { 6428 + return err 6429 + } 6430 + } else { 6431 + if len(*t.Parent) > 1000000 { 6432 + return xerrors.Errorf("Value in field t.Parent was too long") 6433 + } 6434 + 6435 + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(*t.Parent))); err != nil { 6436 + return err 6437 + } 6438 + if _, err := cw.WriteString(string(*t.Parent)); err != nil { 6439 + return err 6440 + } 6441 + } 6442 + } 6443 + 6444 + // t.ChangeId (string) (string) 6445 + if len("changeId") > 1000000 { 6446 + return xerrors.Errorf("Value in field \"changeId\" was too long") 6447 + } 6448 + 6449 + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("changeId"))); err != nil { 6450 + return err 6451 + } 6452 + if _, err := cw.WriteString(string("changeId")); err != nil { 6453 + return err 6454 + } 6455 + 6456 + if len(t.ChangeId) > 1000000 { 6457 + return xerrors.Errorf("Value in field t.ChangeId was too long") 6458 + } 6459 + 6460 + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len(t.ChangeId))); err != nil { 6461 + return err 6462 + } 6463 + if _, err := cw.WriteString(string(t.ChangeId)); err != nil { 6464 + return err 6465 + } 6466 + return nil 6467 + } 6468 + 6469 + func (t *RepoPull_StackInfo) UnmarshalCBOR(r io.Reader) (err error) { 6470 + *t = RepoPull_StackInfo{} 6471 + 6472 + cr := cbg.NewCborReader(r) 6473 + 6474 + maj, extra, err := cr.ReadHeader() 6475 + if err != nil { 6476 + return err 6477 + } 6478 + defer func() { 6479 + if err == io.EOF { 6480 + err = io.ErrUnexpectedEOF 6481 + } 6482 + }() 6483 + 6484 + if maj != cbg.MajMap { 6485 + return fmt.Errorf("cbor input should be of type map") 6486 + } 6487 + 6488 + if extra > cbg.MaxLength { 6489 + return fmt.Errorf("RepoPull_StackInfo: map struct too large (%d)", extra) 6490 + } 6491 + 6492 + n := extra 6493 + 6494 + nameBuf := make([]byte, 8) 6495 + for i := uint64(0); i < n; i++ { 6496 + nameLen, ok, err := cbg.ReadFullStringIntoBuf(cr, nameBuf, 1000000) 6497 + if err != nil { 6498 + return err 6499 + } 6500 + 6501 + if !ok { 6502 + // Field doesn't exist on this type, so ignore it 6503 + if err := cbg.ScanForLinks(cr, func(cid.Cid) {}); err != nil { 6504 + return err 6505 + } 6506 + continue 6507 + } 6508 + 6509 + switch string(nameBuf[:nameLen]) { 6510 + // t.Parent (string) (string) 6511 + case "parent": 6512 + 6513 + { 6514 + b, err := cr.ReadByte() 6515 + if err != nil { 6516 + return err 6517 + } 6518 + if b != cbg.CborNull[0] { 6519 + if err := cr.UnreadByte(); err != nil { 6520 + return err 6521 + } 6522 + 6523 + sval, err := cbg.ReadStringWithMax(cr, 1000000) 6524 + if err != nil { 6525 + return err 6526 + } 6527 + 6528 + t.Parent = (*string)(&sval) 6529 + } 6530 + } 6531 + // t.ChangeId (string) (string) 6532 + case "changeId": 6533 + 6534 + { 6535 + sval, err := cbg.ReadStringWithMax(cr, 1000000) 6536 + if err != nil { 6537 + return err 6538 + } 6539 + 6540 + t.ChangeId = string(sval) 6541 + } 6542 + 6543 + default: 6544 + // Field doesn't exist on this type, so ignore it 6545 + if err := cbg.ScanForLinks(r, func(cid.Cid) {}); err != nil { 6546 + return err 6547 + } 6548 + } 6549 + } 6550 + 6551 + return nil 6552 + } 6395 6553 func (t *RepoPull_Target) MarshalCBOR(w io.Writer) error { 6396 6554 if t == nil { 6397 6555 _, err := w.Write(cbg.CborNull) ··· 6533 6691 } 6534 6692 6535 6693 cw := cbg.NewCborWriter(w) 6536 - fieldCount := 7 6694 + fieldCount := 8 6537 6695 6538 6696 if t.Body == nil { 6539 6697 fieldCount-- ··· 6543 6701 fieldCount-- 6544 6702 } 6545 6703 6704 + if t.StackInfo == nil { 6705 + fieldCount-- 6706 + } 6707 + 6546 6708 if _, err := cw.Write(cbg.CborEncodeMajorType(cbg.MajMap, uint64(fieldCount))); err != nil { 6547 6709 return err 6548 6710 } ··· 6701 6863 if _, err := cw.WriteString(string(t.CreatedAt)); err != nil { 6702 6864 return err 6703 6865 } 6866 + 6867 + // t.StackInfo (tangled.RepoPull_StackInfo) (struct) 6868 + if t.StackInfo != nil { 6869 + 6870 + if len("stackInfo") > 1000000 { 6871 + return xerrors.Errorf("Value in field \"stackInfo\" was too long") 6872 + } 6873 + 6874 + if err := cw.WriteMajorTypeHeader(cbg.MajTextString, uint64(len("stackInfo"))); err != nil { 6875 + return err 6876 + } 6877 + if _, err := cw.WriteString(string("stackInfo")); err != nil { 6878 + return err 6879 + } 6880 + 6881 + if err := t.StackInfo.MarshalCBOR(cw); err != nil { 6882 + return err 6883 + } 6884 + } 6704 6885 return nil 6705 6886 } 6706 6887 ··· 6850 7031 6851 7032 t.CreatedAt = string(sval) 6852 7033 } 7034 + // t.StackInfo (tangled.RepoPull_StackInfo) (struct) 7035 + case "stackInfo": 7036 + 7037 + { 7038 + 7039 + b, err := cr.ReadByte() 7040 + if err != nil { 7041 + return err 7042 + } 7043 + if b != cbg.CborNull[0] { 7044 + if err := cr.UnreadByte(); err != nil { 7045 + return err 7046 + } 7047 + t.StackInfo = new(RepoPull_StackInfo) 7048 + if err := t.StackInfo.UnmarshalCBOR(cr); err != nil { 7049 + return xerrors.Errorf("unmarshaling t.StackInfo pointer: %w", err) 7050 + } 7051 + } 7052 + 7053 + } 6853 7054 6854 7055 default: 6855 7056 // Field doesn't exist on this type, so ignore it
+16 -7
api/tangled/repopull.go
··· 17 17 } // 18 18 // RECORDTYPE: RepoPull 19 19 type RepoPull struct { 20 - LexiconTypeID string `json:"$type,const=sh.tangled.repo.pull" cborgen:"$type,const=sh.tangled.repo.pull"` 21 - Body *string `json:"body,omitempty" cborgen:"body,omitempty"` 22 - CreatedAt string `json:"createdAt" cborgen:"createdAt"` 23 - Patch string `json:"patch" cborgen:"patch"` 24 - Source *RepoPull_Source `json:"source,omitempty" cborgen:"source,omitempty"` 25 - Target *RepoPull_Target `json:"target" cborgen:"target"` 26 - Title string `json:"title" cborgen:"title"` 20 + LexiconTypeID string `json:"$type,const=sh.tangled.repo.pull" cborgen:"$type,const=sh.tangled.repo.pull"` 21 + Body *string `json:"body,omitempty" cborgen:"body,omitempty"` 22 + CreatedAt string `json:"createdAt" cborgen:"createdAt"` 23 + Patch string `json:"patch" cborgen:"patch"` 24 + Source *RepoPull_Source `json:"source,omitempty" cborgen:"source,omitempty"` 25 + StackInfo *RepoPull_StackInfo `json:"stackInfo,omitempty" cborgen:"stackInfo,omitempty"` 26 + Target *RepoPull_Target `json:"target" cborgen:"target"` 27 + Title string `json:"title" cborgen:"title"` 27 28 } 28 29 29 30 // RepoPull_Source is a "source" in the sh.tangled.repo.pull schema. ··· 33 34 Sha string `json:"sha" cborgen:"sha"` 34 35 } 35 36 37 + // RepoPull_StackInfo is a "stackInfo" in the sh.tangled.repo.pull schema. 38 + type RepoPull_StackInfo struct { 39 + // changeId: Change ID of this commit/change. Principly also available in the patch itself as a line in the commit footer. 40 + ChangeId string `json:"changeId" cborgen:"changeId"` 41 + // parent: AT-URI of the PR for the parent commit/change in the change stack. 42 + Parent *string `json:"parent,omitempty" cborgen:"parent,omitempty"` 43 + } 44 + 36 45 // RepoPull_Target is a "target" in the sh.tangled.repo.pull schema. 37 46 type RepoPull_Target struct { 38 47 Branch string `json:"branch" cborgen:"branch"`
+7
appview/db/db.go
··· 678 678 return err 679 679 }) 680 680 681 + runMigration(conn, "add-parent-at-for-stacks-to-pulls", func(tx *sql.Tx) error { 682 + _, err := tx.Exec(` 683 + alter table pulls add column parent_at text; 684 + `) 685 + return err 686 + }) 687 + 681 688 return &DB{db}, nil 682 689 } 683 690
+37 -10
appview/db/pulls.go
··· 72 72 // stacking 73 73 StackId string // nullable string 74 74 ChangeId string // nullable string 75 + ParentAt *syntax.ATURI 75 76 ParentChangeId string // nullable string 76 77 77 78 // meta ··· 91 92 } 92 93 93 94 record := tangled.RepoPull{ 94 - Title: p.Title, 95 - Body: &p.Body, 96 - CreatedAt: p.Created.Format(time.RFC3339), 95 + Title: p.Title, 96 + Body: &p.Body, 97 + CreatedAt: p.Created.Format(time.RFC3339), 97 98 Target: &tangled.RepoPull_Target{ 98 99 Repo: p.RepoAt.String(), 99 100 Branch: p.TargetBranch, 100 101 }, 101 - Patch: p.LatestPatch(), 102 - Source: source, 102 + Patch: p.LatestPatch(), 103 + Source: source, 104 + StackInfo: &tangled.RepoPull_StackInfo{ 105 + ChangeId: p.ChangeId, 106 + Parent: (*string)(p.ParentAt), 107 + }, 103 108 } 104 109 return record 105 110 } ··· 255 260 } 256 261 } 257 262 258 - var stackId, changeId, parentChangeId *string 263 + var stackId, changeId, parentAt, parentChangeId *string 259 264 if pull.StackId != "" { 260 265 stackId = &pull.StackId 261 266 } 262 267 if pull.ChangeId != "" { 263 268 changeId = &pull.ChangeId 264 269 } 270 + if pull.ParentAt != nil { 271 + parentAt = (*string)(pull.ParentAt) 272 + } 265 273 if pull.ParentChangeId != "" { 266 274 parentChangeId = &pull.ParentChangeId 267 275 } ··· 269 277 _, err = tx.Exec( 270 278 ` 271 279 insert into pulls ( 272 - repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id 280 + repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_at, parent_change_id 273 281 ) 274 - values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 282 + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, 275 283 pull.RepoAt, 276 284 pull.OwnerDid, 277 285 pull.PullId, ··· 284 292 sourceRepoAt, 285 293 stackId, 286 294 changeId, 295 + parentAt, 287 296 parentChangeId, 288 297 ) 289 298 if err != nil { ··· 345 354 source_repo_at, 346 355 stack_id, 347 356 change_id, 357 + parent_at, 348 358 parent_change_id 349 359 from 350 360 pulls ··· 363 373 for rows.Next() { 364 374 var pull Pull 365 375 var createdAt string 366 - var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString 376 + var sourceBranch, sourceRepoAt, stackId, changeId, parentAt, parentChangeId sql.NullString 367 377 err := rows.Scan( 368 378 &pull.OwnerDid, 369 379 &pull.RepoAt, ··· 378 388 &sourceRepoAt, 379 389 &stackId, 380 390 &changeId, 391 + &parentAt, 381 392 &parentChangeId, 382 393 ) 383 394 if err != nil { ··· 409 420 if changeId.Valid { 410 421 pull.ChangeId = changeId.String 411 422 } 423 + if parentAt.Valid { 424 + parentAtParsed, err := syntax.ParseATURI(parentAt.String) 425 + if err != nil { 426 + return nil, err 427 + } 428 + pull.ParentAt = &parentAtParsed 429 + } 412 430 if parentChangeId.Valid { 413 431 pull.ParentChangeId = parentChangeId.String 414 432 } ··· 549 567 source_repo_at, 550 568 stack_id, 551 569 change_id, 570 + parent_at, 552 571 parent_change_id 553 572 from 554 573 pulls ··· 559 578 560 579 var pull Pull 561 580 var createdAt string 562 - var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString 581 + var sourceBranch, sourceRepoAt, stackId, changeId, parentAt, parentChangeId sql.NullString 563 582 err := row.Scan( 564 583 &pull.OwnerDid, 565 584 &pull.PullId, ··· 574 593 &sourceRepoAt, 575 594 &stackId, 576 595 &changeId, 596 + &parentAt, 577 597 &parentChangeId, 578 598 ) 579 599 if err != nil { ··· 606 626 if changeId.Valid { 607 627 pull.ChangeId = changeId.String 608 628 } 629 + if parentAt.Valid { 630 + parsedParentAt, err := syntax.ParseATURI(parentAt.String) 631 + if err != nil { 632 + return nil, err 633 + } 634 + pull.ParentAt = &parsedParentAt 635 + } 609 636 if parentChangeId.Valid { 610 637 pull.ParentChangeId = parentChangeId.String 611 638 }
+22 -2
appview/pulls/pulls.go
··· 29 29 30 30 "github.com/bluekeyes/go-gitdiff/gitdiff" 31 31 comatproto "github.com/bluesky-social/indigo/api/atproto" 32 + "github.com/bluesky-social/indigo/atproto/syntax" 32 33 lexutil "github.com/bluesky-social/indigo/lex/util" 33 34 "github.com/go-chi/chi/v5" 34 35 "github.com/google/uuid" ··· 1633 1634 newStack, err := newStack(f, user, targetBranch, patch, pull.PullSource, stackId) 1634 1635 if err != nil { 1635 1636 log.Println("failed to create resubmitted stack", err) 1636 - s.pages.Notice(w, "pull-merge-error", "Failed to merge pull request. Try again later.") 1637 + s.pages.Notice(w, "pull-resubmit-error", "Failed to merge pull request. Try again later.") 1637 1638 return 1638 1639 } 1639 1640 1640 1641 // find the diff between the stacks, first, map them by changeId 1641 1642 origById := make(map[string]*db.Pull) 1642 1643 newById := make(map[string]*db.Pull) 1644 + chIdToAtUri := make(map[string]*syntax.ATURI) 1643 1645 for _, p := range origStack { 1644 1646 origById[p.ChangeId] = p 1647 + 1648 + // build map from change id to existing at uris (ignore error as it shouldnt be possible here) 1649 + pAtUri, _ := syntax.ParseATURI(fmt.Sprintf("at://%s/%s/%s", user.Did, tangled.RepoPullNSID, p.Rkey)) 1650 + chIdToAtUri[p.ChangeId] = &pAtUri 1645 1651 } 1646 1652 for _, p := range newStack { 1653 + // if change id has already been given a PR use its at uri instead of the newly created (and thus incorrect) 1654 + // one made by newStack 1655 + if ppAt, ok := chIdToAtUri[p.ParentChangeId]; ok { 1656 + p.ParentAt = ppAt 1657 + } 1658 + 1647 1659 newById[p.ChangeId] = p 1648 1660 } 1649 1661 ··· 1691 1703 // we still need to update the hash in submission.Patch and submission.SourceRev 1692 1704 if patchutil.Equal(newFiles, origFiles) && 1693 1705 origHeader.Title == newHeader.Title && 1694 - origHeader.Body == newHeader.Body { 1706 + origHeader.Body == newHeader.Body && 1707 + op.ParentChangeId == np.ParentChangeId { 1695 1708 unchanged[op.ChangeId] = struct{}{} 1696 1709 } else { 1697 1710 updated[op.ChangeId] = struct{}{} ··· 1775 1788 1776 1789 record := op.AsRecord() 1777 1790 record.Patch = submission.Patch 1791 + record.StackInfo.Parent = (*string)(np.ParentAt) 1778 1792 1779 1793 writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 1780 1794 RepoApplyWrites_Update: &comatproto.RepoApplyWrites_Update{ ··· 2123 2137 // the stack is identified by a UUID 2124 2138 var stack db.Stack 2125 2139 parentChangeId := "" 2140 + var parentAt *syntax.ATURI = nil 2126 2141 for _, fp := range formatPatches { 2127 2142 // all patches must have a jj change-id 2128 2143 changeId, err := fp.ChangeId() ··· 2153 2168 2154 2169 StackId: stackId, 2155 2170 ChangeId: changeId, 2171 + ParentAt: parentAt, 2156 2172 ParentChangeId: parentChangeId, 2157 2173 } 2158 2174 2159 2175 stack = append(stack, &pull) 2160 2176 2161 2177 parentChangeId = changeId 2178 + // this is a bit of an ugly way to create the ATURI but its the best we can do with the data flow here 2179 + // (igore error as it shouldnt be possible here) 2180 + parsedParentAt, _ := syntax.ParseATURI(fmt.Sprintf("at://%s/%s/%s", user.Did, tangled.RepoPullNSID, pull.Rkey)); 2181 + parentAt = &parsedParentAt 2162 2182 } 2163 2183 2164 2184 return stack, nil
+1
cmd/gen.go
··· 44 44 tangled.RepoIssueState{}, 45 45 tangled.RepoPull{}, 46 46 tangled.RepoPullComment{}, 47 + tangled.RepoPull_StackInfo{}, 47 48 tangled.RepoPull_Source{}, 48 49 tangled.RepoPull_Target{}, 49 50 tangled.RepoPullStatus{},
+21
lexicons/pulls/pull.json
··· 29 29 "patch": { 30 30 "type": "string" 31 31 }, 32 + "stackInfo": { 33 + "type": "ref", 34 + "ref": "#stackInfo" 35 + }, 32 36 "source": { 33 37 "type": "ref", 34 38 "ref": "#source" ··· 76 80 "format": "at-uri" 77 81 } 78 82 } 83 + }, 84 + "stackInfo": { 85 + "type": "object", 86 + "required": [ 87 + "changeId" 88 + ], 89 + "properties": { 90 + "changeId": { 91 + "type": "string", 92 + "description": "Change ID of this commit/change." 93 + }, 94 + "parent": { 95 + "type": "string", 96 + "description": "AT-URI of the PR for the parent commit/change in the change stack.", 97 + "format": "at-uri" 98 + } 99 + } 79 100 } 80 101 } 81 102 }