forked from
tangled.org/core
Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).
1package pulls
2
3import (
4 "context"
5 "log"
6 "net/http"
7 "time"
8
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 var ownerHandle string
29 owner, err := s.idResolver.ResolveIdent(context.Background(), f.Did)
30 if err != nil {
31 ownerHandle = f.Did
32 } else {
33 ownerHandle = owner.Handle.String()
34 }
35
36 var authorHandle string
37 author, err := s.idResolver.ResolveIdent(context.Background(), pull.OwnerDid)
38 if err != nil {
39 authorHandle = pull.OwnerDid
40 } else {
41 authorHandle = "@" + author.Handle.String()
42 }
43
44 avatarUrl := s.pages.AvatarUrl(authorHandle, "256")
45
46 var status string
47 if pull.State.IsOpen() {
48 status = "open"
49 } else if pull.State.IsMerged() {
50 status = "merged"
51 } else {
52 status = "closed"
53 }
54
55 var filesChanged int
56 var additions int64
57 var deletions int64
58
59 if len(pull.Submissions) > 0 {
60 latestSubmission := pull.LatestSubmission()
61 niceDiff := patchutil.AsNiceDiff(latestSubmission.Patch, pull.TargetBranch)
62 filesChanged = niceDiff.Stat.FilesChanged
63 additions = int64(niceDiff.Stat.Insertions)
64 deletions = int64(niceDiff.Stat.Deletions)
65 }
66
67 commentCount := pull.TotalComments()
68
69 rounds := len(pull.Submissions)
70 if rounds == 0 {
71 rounds = 1
72 }
73
74 payload := ogre.PullRequestCardPayload{
75 Type: "pullRequest",
76 RepoName: f.Name,
77 OwnerHandle: ownerHandle,
78 AvatarUrl: avatarUrl,
79 Title: pull.Title,
80 PullRequestNumber: pull.PullId,
81 Status: status,
82 FilesChanged: filesChanged,
83 Additions: int(additions),
84 Deletions: int(deletions),
85 Rounds: rounds,
86 CommentCount: commentCount,
87 ReactionCount: 0,
88 CreatedAt: pull.Created.Format(time.RFC3339),
89 }
90
91 imageBytes, err := s.ogreClient.RenderPullRequestCard(r.Context(), payload)
92 if err != nil {
93 log.Println("failed to render pull request card", err)
94 http.Error(w, "failed to render pull request card", http.StatusInternalServerError)
95 return
96 }
97
98 w.Header().Set("Content-Type", "image/png")
99 w.Header().Set("Cache-Control", "public, max-age=3600")
100 w.WriteHeader(http.StatusOK)
101 _, err = w.Write(imageBytes)
102 if err != nil {
103 log.Println("failed to write pull request card", err)
104 return
105 }
106}