Monorepo for Tangled tangled.org
9

Configure Feed

Select the types of activity you want to include in your feed.

1package models 2 3import ( 4 "bytes" 5 "compress/gzip" 6 "fmt" 7 "io" 8 "log" 9 "slices" 10 "strings" 11 "time" 12 13 "tangled.org/core/api/tangled" 14 "tangled.org/core/appview/pages/markup/sanitizer" 15 "tangled.org/core/patchutil" 16 "tangled.org/core/types" 17 18 "github.com/bluesky-social/indigo/atproto/syntax" 19 lexutil "github.com/bluesky-social/indigo/lex/util" 20) 21 22type PullState int 23 24const ( 25 PullClosed PullState = iota 26 PullOpen 27 PullMerged 28 PullAbandoned 29) 30 31func (p PullState) String() string { 32 switch p { 33 case PullOpen: 34 return "open" 35 case PullMerged: 36 return "merged" 37 case PullClosed: 38 return "closed" 39 case PullAbandoned: 40 return "abandoned" 41 default: 42 return "closed" 43 } 44} 45 46func (p PullState) IsOpen() bool { 47 return p == PullOpen 48} 49func (p PullState) IsMerged() bool { 50 return p == PullMerged 51} 52func (p PullState) IsClosed() bool { 53 return p == PullClosed 54} 55func (p PullState) IsAbandoned() bool { 56 return p == PullAbandoned 57} 58 59type Pull struct { 60 // ids 61 ID int 62 PullId int 63 64 // at ids 65 RepoDid syntax.DID 66 OwnerDid string 67 Rkey string 68 69 // content 70 Title string 71 Body string 72 TargetBranch string 73 State PullState 74 Submissions []*PullSubmission 75 Mentions []syntax.DID 76 References []syntax.ATURI 77 78 // stacking 79 DependentOn *syntax.ATURI 80 81 // meta 82 Created time.Time 83 PullSource *PullSource 84 85 // optionally, populate this when querying for reverse mappings 86 Labels LabelState 87 Repo *Repo 88} 89 90// NOTE: This method does not include patch blob in returned atproto record 91func (p Pull) AsRecord() tangled.RepoPull { 92 mentions := make([]string, len(p.Mentions)) 93 for i, did := range p.Mentions { 94 mentions[i] = string(did) 95 } 96 references := make([]string, len(p.References)) 97 for i, uri := range p.References { 98 references[i] = string(uri) 99 } 100 101 rounds := make([]*tangled.RepoPull_Round, len(p.Submissions)) 102 for i, submission := range p.Submissions { 103 rounds[i] = submission.AsRecord() 104 } 105 106 var dependentOn *string 107 if p.DependentOn != nil { 108 x := p.DependentOn.String() 109 dependentOn = &x 110 } 111 112 return tangled.RepoPull{ 113 Title: p.Title, 114 Body: &p.Body, 115 Mentions: mentions, 116 References: references, 117 CreatedAt: p.Created.Format(time.RFC3339), 118 Target: &tangled.RepoPull_Target{ 119 Repo: string(p.RepoDid), 120 Branch: p.TargetBranch, 121 }, 122 Rounds: rounds, 123 Source: p.PullSource.AsRecord(), 124 DependentOn: dependentOn, 125 } 126} 127 128func (pull *Pull) Validate() error { 129 if len(pull.Submissions) == 0 { 130 return fmt.Errorf("pull must have at least one submission") 131 } 132 133 latestSubmission := pull.LatestSubmission() 134 if latestSubmission == nil { 135 return fmt.Errorf("pull must have a valid latest submission") 136 } 137 138 isFormatPatch := patchutil.IsFormatPatch(latestSubmission.Patch) 139 140 // title and body can only be empty if the patch is a format-patch 141 if !isFormatPatch { 142 if pull.Title == "" { 143 return fmt.Errorf("pull title is empty (required for non-format-patch pulls)") 144 } 145 146 if pull.Body == "" { 147 return fmt.Errorf("pull body is empty (required for non-format-patch pulls)") 148 } 149 150 if st := strings.TrimSpace(sanitizer.SanitizeDescription(pull.Title)); st == "" { 151 return fmt.Errorf("title is empty after HTML sanitization") 152 } 153 154 if sb := strings.TrimSpace(sanitizer.SanitizeDefault(pull.Body)); sb == "" { 155 return fmt.Errorf("body is empty after HTML sanitization") 156 } 157 } 158 return nil 159} 160 161func PullFromRecord(did, rkey string, record tangled.RepoPull, blobs []*io.ReadCloser) (*Pull, error) { 162 created, err := time.Parse(time.RFC3339, record.CreatedAt) 163 if err != nil { 164 return nil, fmt.Errorf("invalid createdAt: %w", err) 165 } 166 167 body := "" 168 if record.Body != nil { 169 body = *record.Body 170 } 171 172 var mentions []syntax.DID 173 for _, m := range record.Mentions { 174 if did, err := syntax.ParseDID(m); err == nil { 175 mentions = append(mentions, did) 176 } 177 } 178 179 var targetRepoDid syntax.DID 180 var targetBranch string 181 if record.Target != nil { 182 did, err := syntax.ParseDID(record.Target.Repo) 183 if err != nil { 184 return nil, fmt.Errorf("invalid target.repo did: %w", err) 185 } 186 targetRepoDid = did 187 targetBranch = record.Target.Branch 188 } 189 190 var pullSource *PullSource 191 if record.Source != nil { 192 pullSource = &PullSource{ 193 Branch: record.Source.Branch, 194 } 195 196 if record.Source.Repo != nil { 197 did, err := syntax.ParseDID(*record.Source.Repo) 198 if err != nil { 199 return nil, fmt.Errorf("invalid source.repo did: %w", err) 200 } 201 pullSource.RepoDid = &did 202 } 203 } 204 205 var dependentOn *syntax.ATURI 206 if record.DependentOn != nil { 207 uri, err := syntax.ParseATURI(*record.DependentOn) 208 if err != nil { 209 return nil, fmt.Errorf("invalid dependentOn aturi: %w", err) 210 } 211 dependentOn = &uri 212 } 213 214 var submissions []*PullSubmission 215 for i, s := range record.Rounds { 216 var blob *io.ReadCloser 217 if i < len(blobs) { 218 blob = blobs[i] 219 } 220 submission, err := PullSubmissionFromRecord(did, rkey, i, s, blob) 221 if err != nil { 222 return nil, fmt.Errorf("invalid pull round at index %d: %w", i, err) 223 } 224 submissions = append(submissions, submission) 225 } 226 227 return &Pull{ 228 RepoDid: targetRepoDid, 229 OwnerDid: did, 230 Rkey: rkey, 231 Title: record.Title, 232 Body: body, 233 TargetBranch: targetBranch, 234 PullSource: pullSource, 235 State: PullOpen, 236 Submissions: submissions, 237 Created: created, 238 DependentOn: dependentOn, 239 }, nil 240} 241 242func PullSubmissionFromRecord(did, rkey string, roundNumber int, round *tangled.RepoPull_Round, blob *io.ReadCloser) (*PullSubmission, error) { 243 created, err := time.Parse(time.RFC3339, round.CreatedAt) 244 if err != nil { 245 return nil, fmt.Errorf("invalid createdAt: %w", err) 246 } 247 248 var patch, sourceRev string 249 if blob != nil { 250 p, err := extractGzip(*blob) 251 if err != nil { 252 return nil, fmt.Errorf("failed to extract gzip: %w", err) 253 } 254 patch = p 255 if patchutil.IsFormatPatch(p) { 256 patches, err := patchutil.ExtractPatches(p) 257 if err != nil { 258 return nil, fmt.Errorf("failed to extract patches: %w", err) 259 } 260 261 for _, part := range patches { 262 sourceRev = part.SHA 263 } 264 } 265 } 266 267 return &PullSubmission{ 268 PullAt: syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", did, tangled.RepoPullNSID, rkey)), 269 RoundNumber: roundNumber, 270 Blob: *round.PatchBlob, 271 Created: created, 272 Patch: patch, 273 SourceRev: sourceRev, 274 }, nil 275} 276 277type PullSource struct { 278 Branch string 279 RepoDid *syntax.DID 280 281 // optionally populate this for reverse mappings 282 Repo *Repo 283} 284 285func (s *PullSource) AsRecord() *tangled.RepoPull_Source { 286 if s == nil { 287 return nil 288 } 289 var repo *string 290 if s.RepoDid != nil { 291 r := s.RepoDid.String() 292 repo = &r 293 } 294 return &tangled.RepoPull_Source{ 295 Branch: s.Branch, 296 Repo: repo, 297 } 298} 299 300type PullSubmission struct { 301 // ids 302 ID int 303 304 // at ids 305 PullAt syntax.ATURI 306 307 // content 308 RoundNumber int 309 Blob lexutil.LexBlob 310 Patch string 311 Combined string 312 Comments []Comment 313 SourceRev string // include the rev that was used to create this submission: only for branch/fork PRs 314 315 // meta 316 Created time.Time 317} 318 319func (p *Pull) TotalComments() int { 320 total := 0 321 for _, s := range p.Submissions { 322 total += len(s.Comments) 323 } 324 return total 325} 326 327func (p *Pull) LastRoundNumber() int { 328 return len(p.Submissions) - 1 329} 330 331func (p *Pull) LatestSubmission() *PullSubmission { 332 return p.Submissions[p.LastRoundNumber()] 333} 334 335func (p *Pull) LatestPatch() string { 336 return p.LatestSubmission().Patch 337} 338 339func (p *Pull) LatestSha() string { 340 return p.LatestSubmission().SourceRev 341} 342 343func (p *Pull) AtUri() syntax.ATURI { 344 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", p.OwnerDid, tangled.RepoPullNSID, p.Rkey)) 345} 346 347func (p *Pull) IsPatchBased() bool { 348 return p.PullSource == nil 349} 350 351func (p *Pull) IsBranchBased() bool { 352 if p.PullSource != nil { 353 if p.PullSource.RepoDid != nil { 354 return *p.PullSource.RepoDid == p.RepoDid 355 } 356 // no repo specified 357 return true 358 } 359 return false 360} 361 362func (p *Pull) IsForkBased() bool { 363 if p.PullSource != nil { 364 if p.PullSource.RepoDid != nil { 365 // make sure repos are different 366 return *p.PullSource.RepoDid != p.RepoDid 367 } 368 } 369 return false 370} 371 372func (p *Pull) Participants() []syntax.DID { 373 participantSet := make(map[syntax.DID]struct{}) 374 participants := []syntax.DID{} 375 376 addParticipant := func(did syntax.DID) { 377 if _, exists := participantSet[did]; !exists { 378 participantSet[did] = struct{}{} 379 participants = append(participants, did) 380 } 381 } 382 383 addParticipant(syntax.DID(p.OwnerDid)) 384 385 for _, s := range p.Submissions { 386 for _, sp := range s.Participants() { 387 addParticipant(syntax.DID(sp)) 388 } 389 } 390 391 return participants 392} 393 394func (s PullSubmission) IsFormatPatch() bool { 395 return patchutil.IsFormatPatch(s.Patch) 396} 397 398func (s PullSubmission) AsFormatPatch() []types.FormatPatch { 399 patches, err := patchutil.ExtractPatches(s.Patch) 400 if err != nil { 401 log.Println("error extracting patches from submission:", err) 402 return []types.FormatPatch{} 403 } 404 405 return patches 406} 407 408// empty if invalid, not otherwise 409func (s PullSubmission) ChangeId() string { 410 patches := s.AsFormatPatch() 411 if len(patches) != 1 { 412 return "" 413 } 414 415 c, err := patches[0].ChangeId() 416 if err != nil { 417 return "" 418 } 419 420 return c 421} 422 423func (s *PullSubmission) Participants() []string { 424 participantSet := make(map[string]struct{}) 425 participants := []string{} 426 427 addParticipant := func(did string) { 428 if _, exists := participantSet[did]; !exists { 429 participantSet[did] = struct{}{} 430 participants = append(participants, did) 431 } 432 } 433 434 addParticipant(s.PullAt.Authority().String()) 435 436 for _, c := range s.Comments { 437 addParticipant(c.Did.String()) 438 } 439 440 return participants 441} 442 443func (s PullSubmission) CombinedPatch() string { 444 if s.Combined == "" { 445 return s.Patch 446 } 447 448 return s.Combined 449} 450 451func (s *PullSubmission) GetBlob() *lexutil.LexBlob { 452 if !s.Blob.Ref.Defined() { 453 return nil 454 } 455 456 return &s.Blob 457} 458 459func (s *PullSubmission) AsRecord() *tangled.RepoPull_Round { 460 return &tangled.RepoPull_Round{ 461 CreatedAt: s.Created.Format(time.RFC3339), 462 PatchBlob: s.GetBlob(), 463 } 464} 465 466type Stack []*Pull 467 468// position of this pull in the stack 469func (stack Stack) Position(pull *Pull) int { 470 return slices.IndexFunc(stack, func(p *Pull) bool { 471 return p.AtUri() == pull.AtUri() 472 }) 473} 474 475// all pulls below this pull (including self) in this stack 476// 477// nil if this pull does not belong to this stack 478func (stack Stack) Below(pull *Pull) Stack { 479 position := stack.Position(pull) 480 481 if position < 0 { 482 return nil 483 } 484 485 return stack[position:] 486} 487 488// all pulls below this pull (excluding self) in this stack 489func (stack Stack) StrictlyBelow(pull *Pull) Stack { 490 below := stack.Below(pull) 491 492 if len(below) > 0 { 493 return below[1:] 494 } 495 496 return nil 497} 498 499// all pulls above this pull (including self) in this stack 500func (stack Stack) Above(pull *Pull) Stack { 501 position := stack.Position(pull) 502 503 if position < 0 { 504 return nil 505 } 506 507 return stack[:position+1] 508} 509 510// all pulls below this pull (excluding self) in this stack 511func (stack Stack) StrictlyAbove(pull *Pull) Stack { 512 above := stack.Above(pull) 513 514 if len(above) > 0 { 515 return above[:len(above)-1] 516 } 517 518 return nil 519} 520 521// the combined format-patches of all the newest submissions in this stack 522func (stack Stack) CombinedPatch() string { 523 // go in reverse order because the bottom of the stack is the last element in the slice 524 var combined strings.Builder 525 for idx := range stack { 526 pull := stack[len(stack)-1-idx] 527 combined.WriteString(pull.LatestPatch()) 528 combined.WriteString("\n") 529 } 530 return combined.String() 531} 532 533// filter out PRs that are "active" 534// 535// PRs that are still open are active 536func (stack Stack) Mergeable() Stack { 537 var mergeable Stack 538 539 for _, p := range stack { 540 // stop at the first merged PR 541 if p.State == PullMerged || p.State == PullClosed { 542 break 543 } 544 545 // skip over abandoned PRs 546 if p.State != PullAbandoned { 547 mergeable = append(mergeable, p) 548 } 549 } 550 551 return mergeable 552} 553 554type BranchDeleteStatus struct { 555 Repo *Repo 556 Branch string 557} 558 559func extractGzip(blob io.Reader) (string, error) { 560 var b bytes.Buffer 561 r, err := gzip.NewReader(blob) 562 if err != nil { 563 return "", err 564 } 565 defer r.Close() 566 567 const maxSize = 15 * 1024 * 1024 568 limitedReader := io.LimitReader(r, maxSize) 569 570 _, err = io.Copy(&b, limitedReader) 571 if err != nil { 572 return "", err 573 } 574 575 return b.String(), nil 576}