forked from tangled.org/core
Monorepo for Tangled

appview/pulls: upload patches as blobs

By using blobs we massively increase our maximum patch size. When stored
directly on the record patches can be at most 2 MiB. By moving it to a
blob, we get a minimum of 50 MiB (smaller if the pds has set it but 50
is the default).

Signed-off-by: Samuel Shuert <me@thecoded.prof>
Signed-off-by: Seongmin Lee <git@boltless.me>

authored by thecoded.prof and committed by boltless.me 9fec5089 d2d68396

Changed files
+49 -37
appview
models
pulls
+1 -1
appview/models/pull.go
··· 83 Repo *Repo 84 } 85 86 func (p Pull) AsRecord() tangled.RepoPull { 87 var source *tangled.RepoPull_Source 88 if p.PullSource != nil { ··· 113 Repo: p.RepoAt.String(), 114 Branch: p.TargetBranch, 115 }, 116 - Patch: p.LatestPatch(), 117 Source: source, 118 } 119 return record
··· 83 Repo *Repo 84 } 85 86 + // NOTE: This method does not include patch blob in returned atproto record 87 func (p Pull) AsRecord() tangled.RepoPull { 88 var source *tangled.RepoPull_Source 89 if p.PullSource != nil { ··· 114 Repo: p.RepoAt.String(), 115 Branch: p.TargetBranch, 116 }, 117 Source: source, 118 } 119 return record
+48 -36
appview/pulls/pulls.go
··· 1240 return 1241 } 1242 1243 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1244 Collection: tangled.RepoPullNSID, 1245 Repo: user.Did, ··· 1251 Repo: string(repo.RepoAt()), 1252 Branch: targetBranch, 1253 }, 1254 - Patch: patch, 1255 Source: recordPullSource, 1256 CreatedAt: time.Now().Format(time.RFC3339), 1257 }, ··· 1327 // apply all record creations at once 1328 var writes []*comatproto.RepoApplyWrites_Input_Writes_Elem 1329 for _, p := range stack { 1330 record := p.AsRecord() 1331 - write := comatproto.RepoApplyWrites_Input_Writes_Elem{ 1332 RepoApplyWrites_Create: &comatproto.RepoApplyWrites_Create{ 1333 Collection: tangled.RepoPullNSID, 1334 Rkey: &p.Rkey, ··· 1336 Val: &record, 1337 }, 1338 }, 1339 - } 1340 - writes = append(writes, &write) 1341 } 1342 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1343 Repo: user.Did, ··· 1862 return 1863 } 1864 1865 - var recordPullSource *tangled.RepoPull_Source 1866 - if pull.IsBranchBased() { 1867 - recordPullSource = &tangled.RepoPull_Source{ 1868 - Branch: pull.PullSource.Branch, 1869 - Sha: sourceRev, 1870 - } 1871 } 1872 - if pull.IsForkBased() { 1873 - repoAt := pull.PullSource.RepoAt.String() 1874 - recordPullSource = &tangled.RepoPull_Source{ 1875 - Branch: pull.PullSource.Branch, 1876 - Repo: &repoAt, 1877 - Sha: sourceRev, 1878 - } 1879 - } 1880 1881 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1882 Collection: tangled.RepoPullNSID, ··· 1884 Rkey: pull.Rkey, 1885 SwapRecord: ex.Cid, 1886 Record: &lexutil.LexiconTypeDecoder{ 1887 - Val: &tangled.RepoPull{ 1888 - Title: pull.Title, 1889 - Target: &tangled.RepoPull_Target{ 1890 - Repo: string(repo.RepoAt()), 1891 - Branch: pull.TargetBranch, 1892 - }, 1893 - Patch: patch, // new patch 1894 - Source: recordPullSource, 1895 - CreatedAt: time.Now().Format(time.RFC3339), 1896 - }, 1897 }, 1898 }) 1899 if err != nil { ··· 1979 } 1980 defer tx.Rollback() 1981 1982 // pds updates to make 1983 var writes []*comatproto.RepoApplyWrites_Input_Writes_Elem 1984 ··· 2012 return 2013 } 2014 2015 record := p.AsRecord() 2016 writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 2017 RepoApplyWrites_Create: &comatproto.RepoApplyWrites_Create{ 2018 Collection: tangled.RepoPullNSID, ··· 2047 return 2048 } 2049 2050 record := np.AsRecord() 2051 - 2052 writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 2053 RepoApplyWrites_Update: &comatproto.RepoApplyWrites_Update{ 2054 Collection: tangled.RepoPullNSID, ··· 2082 if err != nil { 2083 log.Println("failed to resubmit pull", err) 2084 s.pages.Notice(w, "pull-resubmit-error", "Failed to resubmit pull request. Try again later.") 2085 - return 2086 - } 2087 - 2088 - client, err := s.oauth.AuthorizedClient(r) 2089 - if err != nil { 2090 - log.Println("failed to authorize client") 2091 - s.pages.Notice(w, "resubmit-error", "Failed to create pull request. Try again later.") 2092 return 2093 } 2094
··· 1240 return 1241 } 1242 1243 + blob, err := comatproto.RepoUploadBlob(r.Context(), client, strings.NewReader(patch)) 1244 + if err != nil { 1245 + log.Println("failed to upload patch", err) 1246 + s.pages.Notice(w, "pull", "Failed to create pull request. Try again later.") 1247 + return 1248 + } 1249 + 1250 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1251 Collection: tangled.RepoPullNSID, 1252 Repo: user.Did, ··· 1258 Repo: string(repo.RepoAt()), 1259 Branch: targetBranch, 1260 }, 1261 + Patch: blob.Blob.Ref.String(), 1262 Source: recordPullSource, 1263 CreatedAt: time.Now().Format(time.RFC3339), 1264 }, ··· 1334 // apply all record creations at once 1335 var writes []*comatproto.RepoApplyWrites_Input_Writes_Elem 1336 for _, p := range stack { 1337 + blob, err := comatproto.RepoUploadBlob(r.Context(), client, strings.NewReader(p.LatestPatch())) 1338 + if err != nil { 1339 + log.Println("failed to upload patch blob", err) 1340 + s.pages.Notice(w, "pull", "Failed to create pull request. Try again later.") 1341 + return 1342 + } 1343 + 1344 record := p.AsRecord() 1345 + record.Patch = blob.Blob.Ref.String() 1346 + writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 1347 RepoApplyWrites_Create: &comatproto.RepoApplyWrites_Create{ 1348 Collection: tangled.RepoPullNSID, 1349 Rkey: &p.Rkey, ··· 1351 Val: &record, 1352 }, 1353 }, 1354 + }) 1355 } 1356 _, err = comatproto.RepoApplyWrites(r.Context(), client, &comatproto.RepoApplyWrites_Input{ 1357 Repo: user.Did, ··· 1876 return 1877 } 1878 1879 + blob, err := comatproto.RepoUploadBlob(r.Context(), client, strings.NewReader(patch)) 1880 + if err != nil { 1881 + log.Println("failed to upload patch blob", err) 1882 + s.pages.Notice(w, "resubmit-error", "Failed to update pull request on the PDS. Try again later.") 1883 + return 1884 } 1885 + record := pull.AsRecord() 1886 + record.Patch = blob.Blob.Ref.String() 1887 + record.CreatedAt = time.Now().Format(time.RFC3339) 1888 1889 _, err = comatproto.RepoPutRecord(r.Context(), client, &comatproto.RepoPutRecord_Input{ 1890 Collection: tangled.RepoPullNSID, ··· 1892 Rkey: pull.Rkey, 1893 SwapRecord: ex.Cid, 1894 Record: &lexutil.LexiconTypeDecoder{ 1895 + Val: &record, 1896 }, 1897 }) 1898 if err != nil { ··· 1978 } 1979 defer tx.Rollback() 1980 1981 + client, err := s.oauth.AuthorizedClient(r) 1982 + if err != nil { 1983 + log.Println("failed to authorize client") 1984 + s.pages.Notice(w, "resubmit-error", "Failed to create pull request. Try again later.") 1985 + return 1986 + } 1987 + 1988 // pds updates to make 1989 var writes []*comatproto.RepoApplyWrites_Input_Writes_Elem 1990 ··· 2018 return 2019 } 2020 2021 + blob, err := comatproto.RepoUploadBlob(r.Context(), client, strings.NewReader(patch)) 2022 + if err != nil { 2023 + log.Println("failed to upload patch blob", err) 2024 + s.pages.Notice(w, "resubmit-error", "Failed to update pull request on the PDS. Try again later.") 2025 + return 2026 + } 2027 record := p.AsRecord() 2028 + record.Patch = blob.Blob.Ref.String() 2029 writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 2030 RepoApplyWrites_Create: &comatproto.RepoApplyWrites_Create{ 2031 Collection: tangled.RepoPullNSID, ··· 2060 return 2061 } 2062 2063 + blob, err := comatproto.RepoUploadBlob(r.Context(), client, strings.NewReader(patch)) 2064 + if err != nil { 2065 + log.Println("failed to upload patch blob", err) 2066 + s.pages.Notice(w, "resubmit-error", "Failed to update pull request on the PDS. Try again later.") 2067 + return 2068 + } 2069 record := np.AsRecord() 2070 + record.Patch = blob.Blob.Ref.String() 2071 writes = append(writes, &comatproto.RepoApplyWrites_Input_Writes_Elem{ 2072 RepoApplyWrites_Update: &comatproto.RepoApplyWrites_Update{ 2073 Collection: tangled.RepoPullNSID, ··· 2101 if err != nil { 2102 log.Println("failed to resubmit pull", err) 2103 s.pages.Notice(w, "pull-resubmit-error", "Failed to resubmit pull request. Try again later.") 2104 return 2105 } 2106