This repository has no description
0

Configure Feed

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

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