You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_v2.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package nuget
  4. import (
  5. "encoding/xml"
  6. "strings"
  7. "time"
  8. packages_model "code.gitea.io/gitea/models/packages"
  9. nuget_module "code.gitea.io/gitea/modules/packages/nuget"
  10. )
  11. type AtomTitle struct {
  12. Type string `xml:"type,attr"`
  13. Text string `xml:",chardata"`
  14. }
  15. type ServiceCollection struct {
  16. Href string `xml:"href,attr"`
  17. Title AtomTitle `xml:"atom:title"`
  18. }
  19. type ServiceWorkspace struct {
  20. Title AtomTitle `xml:"atom:title"`
  21. Collection ServiceCollection `xml:"collection"`
  22. }
  23. type ServiceIndexResponseV2 struct {
  24. XMLName xml.Name `xml:"service"`
  25. Base string `xml:"base,attr"`
  26. Xmlns string `xml:"xmlns,attr"`
  27. XmlnsAtom string `xml:"xmlns:atom,attr"`
  28. Workspace ServiceWorkspace `xml:"workspace"`
  29. }
  30. type EdmxPropertyRef struct {
  31. Name string `xml:"Name,attr"`
  32. }
  33. type EdmxProperty struct {
  34. Name string `xml:"Name,attr"`
  35. Type string `xml:"Type,attr"`
  36. Nullable bool `xml:"Nullable,attr"`
  37. }
  38. type EdmxEntityType struct {
  39. Name string `xml:"Name,attr"`
  40. HasStream bool `xml:"m:HasStream,attr"`
  41. Keys []EdmxPropertyRef `xml:"Key>PropertyRef"`
  42. Properties []EdmxProperty `xml:"Property"`
  43. }
  44. type EdmxFunctionParameter struct {
  45. Name string `xml:"Name,attr"`
  46. Type string `xml:"Type,attr"`
  47. }
  48. type EdmxFunctionImport struct {
  49. Name string `xml:"Name,attr"`
  50. ReturnType string `xml:"ReturnType,attr"`
  51. EntitySet string `xml:"EntitySet,attr"`
  52. Parameter []EdmxFunctionParameter `xml:"Parameter"`
  53. }
  54. type EdmxEntitySet struct {
  55. Name string `xml:"Name,attr"`
  56. EntityType string `xml:"EntityType,attr"`
  57. }
  58. type EdmxEntityContainer struct {
  59. Name string `xml:"Name,attr"`
  60. IsDefaultEntityContainer bool `xml:"m:IsDefaultEntityContainer,attr"`
  61. EntitySet EdmxEntitySet `xml:"EntitySet"`
  62. FunctionImports []EdmxFunctionImport `xml:"FunctionImport"`
  63. }
  64. type EdmxSchema struct {
  65. Xmlns string `xml:"xmlns,attr"`
  66. Namespace string `xml:"Namespace,attr"`
  67. EntityType *EdmxEntityType `xml:"EntityType,omitempty"`
  68. EntityContainer *EdmxEntityContainer `xml:"EntityContainer,omitempty"`
  69. }
  70. type EdmxDataServices struct {
  71. XmlnsM string `xml:"xmlns:m,attr"`
  72. DataServiceVersion string `xml:"m:DataServiceVersion,attr"`
  73. MaxDataServiceVersion string `xml:"m:MaxDataServiceVersion,attr"`
  74. Schema []EdmxSchema `xml:"Schema"`
  75. }
  76. type EdmxMetadata struct {
  77. XMLName xml.Name `xml:"edmx:Edmx"`
  78. XmlnsEdmx string `xml:"xmlns:edmx,attr"`
  79. Version string `xml:"Version,attr"`
  80. DataServices EdmxDataServices `xml:"edmx:DataServices"`
  81. }
  82. var Metadata = &EdmxMetadata{
  83. XmlnsEdmx: "http://schemas.microsoft.com/ado/2007/06/edmx",
  84. Version: "1.0",
  85. DataServices: EdmxDataServices{
  86. XmlnsM: "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
  87. DataServiceVersion: "2.0",
  88. MaxDataServiceVersion: "2.0",
  89. Schema: []EdmxSchema{
  90. {
  91. Xmlns: "http://schemas.microsoft.com/ado/2006/04/edm",
  92. Namespace: "NuGetGallery.OData",
  93. EntityType: &EdmxEntityType{
  94. Name: "V2FeedPackage",
  95. HasStream: true,
  96. Keys: []EdmxPropertyRef{
  97. {Name: "Id"},
  98. {Name: "Version"},
  99. },
  100. Properties: []EdmxProperty{
  101. {
  102. Name: "Id",
  103. Type: "Edm.String",
  104. },
  105. {
  106. Name: "Version",
  107. Type: "Edm.String",
  108. },
  109. {
  110. Name: "NormalizedVersion",
  111. Type: "Edm.String",
  112. Nullable: true,
  113. },
  114. {
  115. Name: "Authors",
  116. Type: "Edm.String",
  117. Nullable: true,
  118. },
  119. {
  120. Name: "Created",
  121. Type: "Edm.DateTime",
  122. },
  123. {
  124. Name: "Dependencies",
  125. Type: "Edm.String",
  126. },
  127. {
  128. Name: "Description",
  129. Type: "Edm.String",
  130. },
  131. {
  132. Name: "DownloadCount",
  133. Type: "Edm.Int64",
  134. },
  135. {
  136. Name: "LastUpdated",
  137. Type: "Edm.DateTime",
  138. },
  139. {
  140. Name: "Published",
  141. Type: "Edm.DateTime",
  142. },
  143. {
  144. Name: "PackageSize",
  145. Type: "Edm.Int64",
  146. },
  147. {
  148. Name: "ProjectUrl",
  149. Type: "Edm.String",
  150. Nullable: true,
  151. },
  152. {
  153. Name: "ReleaseNotes",
  154. Type: "Edm.String",
  155. Nullable: true,
  156. },
  157. {
  158. Name: "RequireLicenseAcceptance",
  159. Type: "Edm.Boolean",
  160. Nullable: false,
  161. },
  162. {
  163. Name: "Title",
  164. Type: "Edm.String",
  165. Nullable: true,
  166. },
  167. {
  168. Name: "VersionDownloadCount",
  169. Type: "Edm.Int64",
  170. Nullable: false,
  171. },
  172. },
  173. },
  174. },
  175. {
  176. Xmlns: "http://schemas.microsoft.com/ado/2006/04/edm",
  177. Namespace: "NuGetGallery",
  178. EntityContainer: &EdmxEntityContainer{
  179. Name: "V2FeedContext",
  180. IsDefaultEntityContainer: true,
  181. EntitySet: EdmxEntitySet{
  182. Name: "Packages",
  183. EntityType: "NuGetGallery.OData.V2FeedPackage",
  184. },
  185. FunctionImports: []EdmxFunctionImport{
  186. {
  187. Name: "Search",
  188. ReturnType: "Collection(NuGetGallery.OData.V2FeedPackage)",
  189. EntitySet: "Packages",
  190. Parameter: []EdmxFunctionParameter{
  191. {
  192. Name: "searchTerm",
  193. Type: "Edm.String",
  194. },
  195. },
  196. },
  197. {
  198. Name: "FindPackagesById",
  199. ReturnType: "Collection(NuGetGallery.OData.V2FeedPackage)",
  200. EntitySet: "Packages",
  201. Parameter: []EdmxFunctionParameter{
  202. {
  203. Name: "id",
  204. Type: "Edm.String",
  205. },
  206. },
  207. },
  208. },
  209. },
  210. },
  211. },
  212. },
  213. }
  214. type FeedEntryCategory struct {
  215. Term string `xml:"term,attr"`
  216. Scheme string `xml:"scheme,attr"`
  217. }
  218. type FeedEntryLink struct {
  219. Rel string `xml:"rel,attr"`
  220. Href string `xml:"href,attr"`
  221. }
  222. type TypedValue[T any] struct {
  223. Type string `xml:"type,attr,omitempty"`
  224. Value T `xml:",chardata"`
  225. }
  226. type FeedEntryProperties struct {
  227. Version string `xml:"d:Version"`
  228. NormalizedVersion string `xml:"d:NormalizedVersion"`
  229. Authors string `xml:"d:Authors"`
  230. Dependencies string `xml:"d:Dependencies"`
  231. Description string `xml:"d:Description"`
  232. VersionDownloadCount TypedValue[int64] `xml:"d:VersionDownloadCount"`
  233. DownloadCount TypedValue[int64] `xml:"d:DownloadCount"`
  234. PackageSize TypedValue[int64] `xml:"d:PackageSize"`
  235. Created TypedValue[time.Time] `xml:"d:Created"`
  236. LastUpdated TypedValue[time.Time] `xml:"d:LastUpdated"`
  237. Published TypedValue[time.Time] `xml:"d:Published"`
  238. ProjectURL string `xml:"d:ProjectUrl,omitempty"`
  239. ReleaseNotes string `xml:"d:ReleaseNotes,omitempty"`
  240. RequireLicenseAcceptance TypedValue[bool] `xml:"d:RequireLicenseAcceptance"`
  241. Title string `xml:"d:Title"`
  242. }
  243. type FeedEntry struct {
  244. XMLName xml.Name `xml:"entry"`
  245. Xmlns string `xml:"xmlns,attr,omitempty"`
  246. XmlnsD string `xml:"xmlns:d,attr,omitempty"`
  247. XmlnsM string `xml:"xmlns:m,attr,omitempty"`
  248. Base string `xml:"xml:base,attr,omitempty"`
  249. ID string `xml:"id"`
  250. Category FeedEntryCategory `xml:"category"`
  251. Links []FeedEntryLink `xml:"link"`
  252. Title TypedValue[string] `xml:"title"`
  253. Updated time.Time `xml:"updated"`
  254. Author string `xml:"author>name"`
  255. Summary string `xml:"summary"`
  256. Properties *FeedEntryProperties `xml:"m:properties"`
  257. Content string `xml:",innerxml"`
  258. }
  259. type FeedResponse struct {
  260. XMLName xml.Name `xml:"feed"`
  261. Xmlns string `xml:"xmlns,attr,omitempty"`
  262. XmlnsD string `xml:"xmlns:d,attr,omitempty"`
  263. XmlnsM string `xml:"xmlns:m,attr,omitempty"`
  264. Base string `xml:"xml:base,attr,omitempty"`
  265. ID string `xml:"id"`
  266. Title TypedValue[string] `xml:"title"`
  267. Updated time.Time `xml:"updated"`
  268. Links []FeedEntryLink `xml:"link"`
  269. Entries []*FeedEntry `xml:"entry"`
  270. Count int64 `xml:"m:count"`
  271. }
  272. func createFeedResponse(l *linkBuilder, totalEntries int64, pds []*packages_model.PackageDescriptor) *FeedResponse {
  273. entries := make([]*FeedEntry, 0, len(pds))
  274. for _, pd := range pds {
  275. entries = append(entries, createEntry(l, pd, false))
  276. }
  277. links := []FeedEntryLink{
  278. {Rel: "self", Href: l.Base},
  279. }
  280. if l.Next != nil {
  281. links = append(links, FeedEntryLink{
  282. Rel: "next",
  283. Href: l.GetNextURL(),
  284. })
  285. }
  286. return &FeedResponse{
  287. Xmlns: "http://www.w3.org/2005/Atom",
  288. Base: l.Base,
  289. XmlnsD: "http://schemas.microsoft.com/ado/2007/08/dataservices",
  290. XmlnsM: "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
  291. ID: "http://schemas.datacontract.org/2004/07/",
  292. Updated: time.Now(),
  293. Links: links,
  294. Count: totalEntries,
  295. Entries: entries,
  296. }
  297. }
  298. func createEntryResponse(l *linkBuilder, pd *packages_model.PackageDescriptor) *FeedEntry {
  299. return createEntry(l, pd, true)
  300. }
  301. func createEntry(l *linkBuilder, pd *packages_model.PackageDescriptor, withNamespace bool) *FeedEntry {
  302. metadata := pd.Metadata.(*nuget_module.Metadata)
  303. id := l.GetPackageMetadataURL(pd.Package.Name, pd.Version.Version)
  304. // Workaround to force a self-closing tag to satisfy XmlReader.IsEmptyElement used by the NuGet client.
  305. // https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreader.isemptyelement
  306. content := `<content type="application/zip" src="` + l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version) + `"/>`
  307. createdValue := TypedValue[time.Time]{
  308. Type: "Edm.DateTime",
  309. Value: pd.Version.CreatedUnix.AsLocalTime(),
  310. }
  311. entry := &FeedEntry{
  312. ID: id,
  313. Category: FeedEntryCategory{Term: "NuGetGallery.OData.V2FeedPackage", Scheme: "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"},
  314. Links: []FeedEntryLink{
  315. {Rel: "self", Href: id},
  316. {Rel: "edit", Href: id},
  317. },
  318. Title: TypedValue[string]{Type: "text", Value: pd.Package.Name},
  319. Updated: pd.Version.CreatedUnix.AsLocalTime(),
  320. Author: metadata.Authors,
  321. Content: content,
  322. Properties: &FeedEntryProperties{
  323. Version: pd.Version.Version,
  324. NormalizedVersion: pd.Version.Version,
  325. Authors: metadata.Authors,
  326. Dependencies: buildDependencyString(metadata),
  327. Description: metadata.Description,
  328. VersionDownloadCount: TypedValue[int64]{Type: "Edm.Int64", Value: pd.Version.DownloadCount},
  329. DownloadCount: TypedValue[int64]{Type: "Edm.Int64", Value: pd.Version.DownloadCount},
  330. PackageSize: TypedValue[int64]{Type: "Edm.Int64", Value: pd.CalculateBlobSize()},
  331. Created: createdValue,
  332. LastUpdated: createdValue,
  333. Published: createdValue,
  334. ProjectURL: metadata.ProjectURL,
  335. ReleaseNotes: metadata.ReleaseNotes,
  336. RequireLicenseAcceptance: TypedValue[bool]{Type: "Edm.Boolean", Value: metadata.RequireLicenseAcceptance},
  337. Title: pd.Package.Name,
  338. },
  339. }
  340. if withNamespace {
  341. entry.Xmlns = "http://www.w3.org/2005/Atom"
  342. entry.Base = l.Base
  343. entry.XmlnsD = "http://schemas.microsoft.com/ado/2007/08/dataservices"
  344. entry.XmlnsM = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
  345. }
  346. return entry
  347. }
  348. func buildDependencyString(metadata *nuget_module.Metadata) string {
  349. var b strings.Builder
  350. first := true
  351. for group, deps := range metadata.Dependencies {
  352. for _, dep := range deps {
  353. if !first {
  354. b.WriteByte('|')
  355. }
  356. first = false
  357. b.WriteString(dep.ID)
  358. b.WriteByte(':')
  359. b.WriteString(dep.Version)
  360. b.WriteByte(':')
  361. b.WriteString(group)
  362. }
  363. }
  364. return b.String()
  365. }