Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

api_v3.go 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package nuget
  4. import (
  5. "sort"
  6. "time"
  7. packages_model "code.gitea.io/gitea/models/packages"
  8. nuget_module "code.gitea.io/gitea/modules/packages/nuget"
  9. )
  10. // https://docs.microsoft.com/en-us/nuget/api/service-index#resources
  11. type ServiceIndexResponseV3 struct {
  12. Version string `json:"version"`
  13. Resources []ServiceResource `json:"resources"`
  14. }
  15. // https://docs.microsoft.com/en-us/nuget/api/service-index#resource
  16. type ServiceResource struct {
  17. ID string `json:"@id"`
  18. Type string `json:"@type"`
  19. }
  20. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#response
  21. type RegistrationIndexResponse struct {
  22. RegistrationIndexURL string `json:"@id"`
  23. Type []string `json:"@type"`
  24. Count int `json:"count"`
  25. Pages []*RegistrationIndexPage `json:"items"`
  26. }
  27. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#registration-page-object
  28. type RegistrationIndexPage struct {
  29. RegistrationPageURL string `json:"@id"`
  30. Lower string `json:"lower"`
  31. Upper string `json:"upper"`
  32. Count int `json:"count"`
  33. Items []*RegistrationIndexPageItem `json:"items"`
  34. }
  35. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#registration-leaf-object-in-a-page
  36. type RegistrationIndexPageItem struct {
  37. RegistrationLeafURL string `json:"@id"`
  38. PackageContentURL string `json:"packageContent"`
  39. CatalogEntry *CatalogEntry `json:"catalogEntry"`
  40. }
  41. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#catalog-entry
  42. type CatalogEntry struct {
  43. CatalogLeafURL string `json:"@id"`
  44. PackageContentURL string `json:"packageContent"`
  45. ID string `json:"id"`
  46. Version string `json:"version"`
  47. Description string `json:"description"`
  48. ReleaseNotes string `json:"releaseNotes"`
  49. Authors string `json:"authors"`
  50. RequireLicenseAcceptance bool `json:"requireLicenseAcceptance"`
  51. ProjectURL string `json:"projectURL"`
  52. DependencyGroups []*PackageDependencyGroup `json:"dependencyGroups"`
  53. }
  54. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#package-dependency-group
  55. type PackageDependencyGroup struct {
  56. TargetFramework string `json:"targetFramework"`
  57. Dependencies []*PackageDependency `json:"dependencies"`
  58. }
  59. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#package-dependency
  60. type PackageDependency struct {
  61. ID string `json:"id"`
  62. Range string `json:"range"`
  63. }
  64. func createRegistrationIndexResponse(l *linkBuilder, pds []*packages_model.PackageDescriptor) *RegistrationIndexResponse {
  65. sort.Slice(pds, func(i, j int) bool {
  66. return pds[i].SemVer.LessThan(pds[j].SemVer)
  67. })
  68. items := make([]*RegistrationIndexPageItem, 0, len(pds))
  69. for _, p := range pds {
  70. items = append(items, createRegistrationIndexPageItem(l, p))
  71. }
  72. return &RegistrationIndexResponse{
  73. RegistrationIndexURL: l.GetRegistrationIndexURL(pds[0].Package.Name),
  74. Type: []string{"catalog:CatalogRoot", "PackageRegistration", "catalog:Permalink"},
  75. Count: 1,
  76. Pages: []*RegistrationIndexPage{
  77. {
  78. RegistrationPageURL: l.GetRegistrationIndexURL(pds[0].Package.Name),
  79. Count: len(pds),
  80. Lower: pds[0].Version.Version,
  81. Upper: pds[len(pds)-1].Version.Version,
  82. Items: items,
  83. },
  84. },
  85. }
  86. }
  87. func createRegistrationIndexPageItem(l *linkBuilder, pd *packages_model.PackageDescriptor) *RegistrationIndexPageItem {
  88. metadata := pd.Metadata.(*nuget_module.Metadata)
  89. return &RegistrationIndexPageItem{
  90. RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
  91. PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
  92. CatalogEntry: &CatalogEntry{
  93. CatalogLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
  94. PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
  95. ID: pd.Package.Name,
  96. Version: pd.Version.Version,
  97. Description: metadata.Description,
  98. ReleaseNotes: metadata.ReleaseNotes,
  99. Authors: metadata.Authors,
  100. ProjectURL: metadata.ProjectURL,
  101. DependencyGroups: createDependencyGroups(pd),
  102. },
  103. }
  104. }
  105. func createDependencyGroups(pd *packages_model.PackageDescriptor) []*PackageDependencyGroup {
  106. metadata := pd.Metadata.(*nuget_module.Metadata)
  107. dependencyGroups := make([]*PackageDependencyGroup, 0, len(metadata.Dependencies))
  108. for k, v := range metadata.Dependencies {
  109. dependencies := make([]*PackageDependency, 0, len(v))
  110. for _, dep := range v {
  111. dependencies = append(dependencies, &PackageDependency{
  112. ID: dep.ID,
  113. Range: dep.Version,
  114. })
  115. }
  116. dependencyGroups = append(dependencyGroups, &PackageDependencyGroup{
  117. TargetFramework: k,
  118. Dependencies: dependencies,
  119. })
  120. }
  121. return dependencyGroups
  122. }
  123. // https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource#registration-leaf
  124. type RegistrationLeafResponse struct {
  125. RegistrationLeafURL string `json:"@id"`
  126. Type []string `json:"@type"`
  127. Listed bool `json:"listed"`
  128. PackageContentURL string `json:"packageContent"`
  129. Published time.Time `json:"published"`
  130. RegistrationIndexURL string `json:"registration"`
  131. }
  132. func createRegistrationLeafResponse(l *linkBuilder, pd *packages_model.PackageDescriptor) *RegistrationLeafResponse {
  133. return &RegistrationLeafResponse{
  134. Type: []string{"Package", "http://schema.nuget.org/catalog#Permalink"},
  135. Listed: true,
  136. Published: pd.Version.CreatedUnix.AsLocalTime(),
  137. RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
  138. PackageContentURL: l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version),
  139. RegistrationIndexURL: l.GetRegistrationIndexURL(pd.Package.Name),
  140. }
  141. }
  142. // https://docs.microsoft.com/en-us/nuget/api/package-base-address-resource#response
  143. type PackageVersionsResponse struct {
  144. Versions []string `json:"versions"`
  145. }
  146. func createPackageVersionsResponse(pvs []*packages_model.PackageVersion) *PackageVersionsResponse {
  147. versions := make([]string, 0, len(pvs))
  148. for _, pv := range pvs {
  149. versions = append(versions, pv.Version)
  150. }
  151. return &PackageVersionsResponse{
  152. Versions: versions,
  153. }
  154. }
  155. // https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#response
  156. type SearchResultResponse struct {
  157. TotalHits int64 `json:"totalHits"`
  158. Data []*SearchResult `json:"data"`
  159. }
  160. // https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-result
  161. type SearchResult struct {
  162. ID string `json:"id"`
  163. Version string `json:"version"`
  164. Versions []*SearchResultVersion `json:"versions"`
  165. Description string `json:"description"`
  166. Authors string `json:"authors"`
  167. ProjectURL string `json:"projectURL"`
  168. RegistrationIndexURL string `json:"registration"`
  169. }
  170. // https://docs.microsoft.com/en-us/nuget/api/search-query-service-resource#search-result
  171. type SearchResultVersion struct {
  172. RegistrationLeafURL string `json:"@id"`
  173. Version string `json:"version"`
  174. Downloads int64 `json:"downloads"`
  175. }
  176. func createSearchResultResponse(l *linkBuilder, totalHits int64, pds []*packages_model.PackageDescriptor) *SearchResultResponse {
  177. grouped := make(map[string][]*packages_model.PackageDescriptor)
  178. for _, pd := range pds {
  179. grouped[pd.Package.Name] = append(grouped[pd.Package.Name], pd)
  180. }
  181. data := make([]*SearchResult, 0, len(pds))
  182. for _, group := range grouped {
  183. data = append(data, createSearchResult(l, group))
  184. }
  185. return &SearchResultResponse{
  186. TotalHits: totalHits,
  187. Data: data,
  188. }
  189. }
  190. func createSearchResult(l *linkBuilder, pds []*packages_model.PackageDescriptor) *SearchResult {
  191. latest := pds[0]
  192. versions := make([]*SearchResultVersion, 0, len(pds))
  193. for _, pd := range pds {
  194. if latest.SemVer.LessThan(pd.SemVer) {
  195. latest = pd
  196. }
  197. versions = append(versions, &SearchResultVersion{
  198. RegistrationLeafURL: l.GetRegistrationLeafURL(pd.Package.Name, pd.Version.Version),
  199. Version: pd.Version.Version,
  200. })
  201. }
  202. metadata := latest.Metadata.(*nuget_module.Metadata)
  203. return &SearchResult{
  204. ID: latest.Package.Name,
  205. Version: latest.Version.Version,
  206. Versions: versions,
  207. Description: metadata.Description,
  208. Authors: metadata.Authors,
  209. ProjectURL: metadata.ProjectURL,
  210. RegistrationIndexURL: l.GetRegistrationIndexURL(latest.Package.Name),
  211. }
  212. }