Monorepo for Tangled
0
fork

Configure Feed

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

at eti/gitignore 91 lines 2.3 kB view raw
1package pulls 2 3import ( 4 "log" 5 "net/http" 6 "time" 7 8 "tangled.org/core/appview/db" 9 "tangled.org/core/appview/models" 10 "tangled.org/core/ogre" 11 "tangled.org/core/patchutil" 12) 13 14func (s *Pulls) PullOpenGraphSummary(w http.ResponseWriter, r *http.Request) { 15 f, err := s.repoResolver.Resolve(r) 16 if err != nil { 17 log.Println("failed to get repo and knot", err) 18 return 19 } 20 21 pull, ok := r.Context().Value("pull").(*models.Pull) 22 if !ok { 23 log.Println("pull not found in context") 24 http.Error(w, "pull not found", http.StatusNotFound) 25 return 26 } 27 28 ownerHandle := s.pages.DisplayHandle(r.Context(), f.Did) 29 30 avatarUrl := s.pages.AvatarUrl(f.Did, "256") 31 32 var status string 33 if pull.State.IsOpen() { 34 status = "open" 35 } else if pull.State.IsMerged() { 36 status = "merged" 37 } else { 38 status = "closed" 39 } 40 41 var filesChanged int 42 var additions int64 43 var deletions int64 44 45 if len(pull.Submissions) > 0 { 46 latestSubmission := pull.LatestSubmission() 47 niceDiff := patchutil.AsNiceDiff(latestSubmission.Patch, pull.TargetBranch) 48 filesChanged = niceDiff.Stat.FilesChanged 49 additions = int64(niceDiff.Stat.Insertions) 50 deletions = int64(niceDiff.Stat.Deletions) 51 } 52 53 commentCount := pull.TotalComments() 54 55 reactionCount, _ := db.GetReactionCount(s.db, pull.AtUri()) 56 57 rounds := max(1, len(pull.Submissions)) 58 59 payload := ogre.PullRequestCardPayload{ 60 Type: "pullRequest", 61 RepoName: f.Name, 62 OwnerHandle: ownerHandle, 63 AvatarUrl: avatarUrl, 64 Title: pull.Title, 65 PullRequestNumber: pull.PullId, 66 Status: status, 67 FilesChanged: filesChanged, 68 Additions: int(additions), 69 Deletions: int(deletions), 70 Rounds: rounds, 71 CommentCount: commentCount, 72 ReactionCount: reactionCount, 73 CreatedAt: pull.Created.Format(time.RFC3339), 74 } 75 76 imageBytes, err := s.ogreClient.RenderPullRequestCard(r.Context(), payload) 77 if err != nil { 78 log.Println("failed to render pull request card", err) 79 http.Error(w, "failed to render pull request card", http.StatusInternalServerError) 80 return 81 } 82 83 w.Header().Set("Content-Type", "image/png") 84 w.Header().Set("Cache-Control", "public, max-age=3600") 85 w.WriteHeader(http.StatusOK) 86 _, err = w.Write(imageBytes) 87 if err != nil { 88 log.Println("failed to write pull request card", err) 89 return 90 } 91}