···
8
+
"github.com/bluesky-social/indigo/atproto/syntax"
9
+
"tangled.org/core/api/tangled"
10
+
"tangled.org/core/appview/models"
13
+
// FindReferences resolves refLinks to Issue/PR/IssueComment/PullComment ATURIs.
14
+
// It will ignore missing refLinks.
15
+
func FindReferences(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) {
17
+
issueRefs []models.ReferenceLink
18
+
pullRefs []models.ReferenceLink
20
+
for _, ref := range refLinks {
22
+
case models.RefKindIssue:
23
+
issueRefs = append(issueRefs, ref)
24
+
case models.RefKindPull:
25
+
pullRefs = append(pullRefs, ref)
28
+
issueUris, err := findIssueReferences(e, issueRefs)
32
+
pullUris, err := findPullReferences(e, pullRefs)
37
+
return append(issueUris, pullUris...), nil
40
+
func findIssueReferences(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) {
41
+
if len(refLinks) == 0 {
44
+
vals := make([]string, len(refLinks))
45
+
args := make([]any, 0, len(refLinks)*4)
46
+
for i, ref := range refLinks {
47
+
vals[i] = "(?, ?, ?, ?)"
48
+
args = append(args, ref.Handle, ref.Repo, ref.SubjectId, ref.CommentId)
50
+
query := fmt.Sprintf(
51
+
`with input(owner_did, name, issue_id, comment_id) as (
59
+
on r.did = inp.owner_did
60
+
and r.name = inp.name
62
+
on i.repo_at = r.at_uri
63
+
and i.issue_id = inp.issue_id
64
+
left join issue_comments c
65
+
on inp.comment_id is not null
66
+
and c.issue_at = i.at_uri
67
+
and c.id = inp.comment_id
69
+
strings.Join(vals, ","),
71
+
rows, err := e.Query(query, args...)
77
+
var uris []syntax.ATURI
81
+
var issueOwner, issueRkey string
82
+
var commentOwner, commentRkey sql.NullString
83
+
var uri syntax.ATURI
84
+
if err := rows.Scan(&issueOwner, &issueRkey, &commentOwner, &commentRkey); err != nil {
87
+
if commentOwner.Valid && commentRkey.Valid {
88
+
uri = syntax.ATURI(fmt.Sprintf(
90
+
commentOwner.String,
91
+
tangled.RepoIssueCommentNSID,
95
+
uri = syntax.ATURI(fmt.Sprintf(
98
+
tangled.RepoIssueNSID,
102
+
uris = append(uris, uri)
107
+
func findPullReferences(e Execer, refLinks []models.ReferenceLink) ([]syntax.ATURI, error) {
108
+
if len(refLinks) == 0 {
111
+
vals := make([]string, len(refLinks))
112
+
args := make([]any, 0, len(refLinks)*4)
113
+
for i, ref := range refLinks {
114
+
vals[i] = "(?, ?, ?, ?)"
115
+
args = append(args, ref.Handle, ref.Repo, ref.SubjectId, ref.CommentId)
117
+
query := fmt.Sprintf(
118
+
`with input(owner_did, name, pull_id, comment_id) as (
122
+
p.owner_did, p.rkey,
123
+
c.owner_did, c.rkey
126
+
on r.did = inp.owner_did
127
+
and r.name = inp.name
129
+
on p.repo_at = r.at_uri
130
+
and p.pull_id = inp.pull_id
131
+
left join pull_comments c
132
+
on inp.comment_id is not null
133
+
and c.repo_at = r.at_uri and c.pull_id = p.pull_id
134
+
and c.id = inp.comment_id
136
+
strings.Join(vals, ","),
138
+
rows, err := e.Query(query, args...)
144
+
var uris []syntax.ATURI
148
+
var pullOwner, pullRkey string
149
+
var commentOwner, commentRkey sql.NullString
150
+
var uri syntax.ATURI
151
+
if err := rows.Scan(&pullOwner, &pullRkey, &commentOwner, &commentRkey); err != nil {
154
+
if commentOwner.Valid && commentRkey.Valid {
155
+
uri = syntax.ATURI(fmt.Sprintf(
157
+
commentOwner.String,
158
+
tangled.RepoPullCommentNSID,
159
+
commentRkey.String,
162
+
uri = syntax.ATURI(fmt.Sprintf(
165
+
tangled.RepoPullNSID,
169
+
uris = append(uris, uri)