summaryrefslogtreecommitdiffstats
path: root/routers/api/packages/nuget/api_v2.go
blob: a726065ad0877a12ab796d8d056c8e5b82c099e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package nuget

import (
	"encoding/xml"
	"strings"
	"time"

	packages_model "code.gitea.io/gitea/models/packages"
	nuget_module "code.gitea.io/gitea/modules/packages/nuget"
)

type AtomTitle struct {
	Type string `xml:"type,attr"`
	Text string `xml:",chardata"`
}

type ServiceCollection struct {
	Href  string    `xml:"href,attr"`
	Title AtomTitle `xml:"atom:title"`
}

type ServiceWorkspace struct {
	Title      AtomTitle         `xml:"atom:title"`
	Collection ServiceCollection `xml:"collection"`
}

type ServiceIndexResponseV2 struct {
	XMLName   xml.Name         `xml:"service"`
	Base      string           `xml:"base,attr"`
	Xmlns     string           `xml:"xmlns,attr"`
	XmlnsAtom string           `xml:"xmlns:atom,attr"`
	Workspace ServiceWorkspace `xml:"workspace"`
}

type EdmxPropertyRef struct {
	Name string `xml:"Name,attr"`
}

type EdmxProperty struct {
	Name     string `xml:"Name,attr"`
	Type     string `xml:"Type,attr"`
	Nullable bool   `xml:"Nullable,attr"`
}

type EdmxEntityType struct {
	Name       string            `xml:"Name,attr"`
	HasStream  bool              `xml:"m:HasStream,attr"`
	Keys       []EdmxPropertyRef `xml:"Key>PropertyRef"`
	Properties []EdmxProperty    `xml:"Property"`
}

type EdmxFunctionParameter struct {
	Name string `xml:"Name,attr"`
	Type string `xml:"Type,attr"`
}

type EdmxFunctionImport struct {
	Name       string                  `xml:"Name,attr"`
	ReturnType string                  `xml:"ReturnType,attr"`
	EntitySet  string                  `xml:"EntitySet,attr"`
	Parameter  []EdmxFunctionParameter `xml:"Parameter"`
}

type EdmxEntitySet struct {
	Name       string `xml:"Name,attr"`
	EntityType string `xml:"EntityType,attr"`
}

type EdmxEntityContainer struct {
	Name                     string               `xml:"Name,attr"`
	IsDefaultEntityContainer bool                 `xml:"m:IsDefaultEntityContainer,attr"`
	EntitySet                EdmxEntitySet        `xml:"EntitySet"`
	FunctionImports          []EdmxFunctionImport `xml:"FunctionImport"`
}

type EdmxSchema struct {
	Xmlns           string               `xml:"xmlns,attr"`
	Namespace       string               `xml:"Namespace,attr"`
	EntityType      *EdmxEntityType      `xml:"EntityType,omitempty"`
	EntityContainer *EdmxEntityContainer `xml:"EntityContainer,omitempty"`
}

type EdmxDataServices struct {
	XmlnsM                string       `xml:"xmlns:m,attr"`
	DataServiceVersion    string       `xml:"m:DataServiceVersion,attr"`
	MaxDataServiceVersion string       `xml:"m:MaxDataServiceVersion,attr"`
	Schema                []EdmxSchema `xml:"Schema"`
}

type EdmxMetadata struct {
	XMLName      xml.Name         `xml:"edmx:Edmx"`
	XmlnsEdmx    string           `xml:"xmlns:edmx,attr"`
	Version      string           `xml:"Version,attr"`
	DataServices EdmxDataServices `xml:"edmx:DataServices"`
}

var Metadata = &EdmxMetadata{
	XmlnsEdmx: "http://schemas.microsoft.com/ado/2007/06/edmx",
	Version:   "1.0",
	DataServices: EdmxDataServices{
		XmlnsM:                "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
		DataServiceVersion:    "2.0",
		MaxDataServiceVersion: "2.0",
		Schema: []EdmxSchema{
			{
				Xmlns:     "http://schemas.microsoft.com/ado/2006/04/edm",
				Namespace: "NuGetGallery.OData",
				EntityType: &EdmxEntityType{
					Name:      "V2FeedPackage",
					HasStream: true,
					Keys: []EdmxPropertyRef{
						{Name: "Id"},
						{Name: "Version"},
					},
					Properties: []EdmxProperty{
						{
							Name: "Id",
							Type: "Edm.String",
						},
						{
							Name: "Version",
							Type: "Edm.String",
						},
						{
							Name:     "NormalizedVersion",
							Type:     "Edm.String",
							Nullable: true,
						},
						{
							Name:     "Authors",
							Type:     "Edm.String",
							Nullable: true,
						},
						{
							Name: "Created",
							Type: "Edm.DateTime",
						},
						{
							Name: "Dependencies",
							Type: "Edm.String",
						},
						{
							Name: "Description",
							Type: "Edm.String",
						},
						{
							Name: "DownloadCount",
							Type: "Edm.Int64",
						},
						{
							Name: "LastUpdated",
							Type: "Edm.DateTime",
						},
						{
							Name: "Published",
							Type: "Edm.DateTime",
						},
						{
							Name: "PackageSize",
							Type: "Edm.Int64",
						},
						{
							Name:     "ProjectUrl",
							Type:     "Edm.String",
							Nullable: true,
						},
						{
							Name:     "ReleaseNotes",
							Type:     "Edm.String",
							Nullable: true,
						},
						{
							Name:     "RequireLicenseAcceptance",
							Type:     "Edm.Boolean",
							Nullable: false,
						},
						{
							Name:     "Title",
							Type:     "Edm.String",
							Nullable: true,
						},
						{
							Name:     "VersionDownloadCount",
							Type:     "Edm.Int64",
							Nullable: false,
						},
					},
				},
			},
			{
				Xmlns:     "http://schemas.microsoft.com/ado/2006/04/edm",
				Namespace: "NuGetGallery",
				EntityContainer: &EdmxEntityContainer{
					Name:                     "V2FeedContext",
					IsDefaultEntityContainer: true,
					EntitySet: EdmxEntitySet{
						Name:       "Packages",
						EntityType: "NuGetGallery.OData.V2FeedPackage",
					},
					FunctionImports: []EdmxFunctionImport{
						{
							Name:       "Search",
							ReturnType: "Collection(NuGetGallery.OData.V2FeedPackage)",
							EntitySet:  "Packages",
							Parameter: []EdmxFunctionParameter{
								{
									Name: "searchTerm",
									Type: "Edm.String",
								},
							},
						},
						{
							Name:       "FindPackagesById",
							ReturnType: "Collection(NuGetGallery.OData.V2FeedPackage)",
							EntitySet:  "Packages",
							Parameter: []EdmxFunctionParameter{
								{
									Name: "id",
									Type: "Edm.String",
								},
							},
						},
					},
				},
			},
		},
	},
}

type FeedEntryCategory struct {
	Term   string `xml:"term,attr"`
	Scheme string `xml:"scheme,attr"`
}

type FeedEntryLink struct {
	Rel  string `xml:"rel,attr"`
	Href string `xml:"href,attr"`
}

type TypedValue[T any] struct {
	Type  string `xml:"type,attr,omitempty"`
	Value T      `xml:",chardata"`
}

type FeedEntryProperties struct {
	Version                  string                `xml:"d:Version"`
	NormalizedVersion        string                `xml:"d:NormalizedVersion"`
	Authors                  string                `xml:"d:Authors"`
	Dependencies             string                `xml:"d:Dependencies"`
	Description              string                `xml:"d:Description"`
	VersionDownloadCount     TypedValue[int64]     `xml:"d:VersionDownloadCount"`
	DownloadCount            TypedValue[int64]     `xml:"d:DownloadCount"`
	PackageSize              TypedValue[int64]     `xml:"d:PackageSize"`
	Created                  TypedValue[time.Time] `xml:"d:Created"`
	LastUpdated              TypedValue[time.Time] `xml:"d:LastUpdated"`
	Published                TypedValue[time.Time] `xml:"d:Published"`
	ProjectURL               string                `xml:"d:ProjectUrl,omitempty"`
	ReleaseNotes             string                `xml:"d:ReleaseNotes,omitempty"`
	RequireLicenseAcceptance TypedValue[bool]      `xml:"d:RequireLicenseAcceptance"`
	Title                    string                `xml:"d:Title"`
}

type FeedEntry struct {
	XMLName    xml.Name             `xml:"entry"`
	Xmlns      string               `xml:"xmlns,attr,omitempty"`
	XmlnsD     string               `xml:"xmlns:d,attr,omitempty"`
	XmlnsM     string               `xml:"xmlns:m,attr,omitempty"`
	Base       string               `xml:"xml:base,attr,omitempty"`
	ID         string               `xml:"id"`
	Category   FeedEntryCategory    `xml:"category"`
	Links      []FeedEntryLink      `xml:"link"`
	Title      TypedValue[string]   `xml:"title"`
	Updated    time.Time            `xml:"updated"`
	Author     string               `xml:"author>name"`
	Summary    string               `xml:"summary"`
	Properties *FeedEntryProperties `xml:"m:properties"`
	Content    string               `xml:",innerxml"`
}

type FeedResponse struct {
	XMLName xml.Name           `xml:"feed"`
	Xmlns   string             `xml:"xmlns,attr,omitempty"`
	XmlnsD  string             `xml:"xmlns:d,attr,omitempty"`
	XmlnsM  string             `xml:"xmlns:m,attr,omitempty"`
	Base    string             `xml:"xml:base,attr,omitempty"`
	ID      string             `xml:"id"`
	Title   TypedValue[string] `xml:"title"`
	Updated time.Time          `xml:"updated"`
	Links   []FeedEntryLink    `xml:"link"`
	Entries []*FeedEntry       `xml:"entry"`
	Count   int64              `xml:"m:count"`
}

func createFeedResponse(l *linkBuilder, totalEntries int64, pds []*packages_model.PackageDescriptor) *FeedResponse {
	entries := make([]*FeedEntry, 0, len(pds))
	for _, pd := range pds {
		entries = append(entries, createEntry(l, pd, false))
	}

	links := []FeedEntryLink{
		{Rel: "self", Href: l.Base},
	}
	if l.Next != nil {
		links = append(links, FeedEntryLink{
			Rel:  "next",
			Href: l.GetNextURL(),
		})
	}

	return &FeedResponse{
		Xmlns:   "http://www.w3.org/2005/Atom",
		Base:    l.Base,
		XmlnsD:  "http://schemas.microsoft.com/ado/2007/08/dataservices",
		XmlnsM:  "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata",
		ID:      "http://schemas.datacontract.org/2004/07/",
		Updated: time.Now(),
		Links:   links,
		Count:   totalEntries,
		Entries: entries,
	}
}

func createEntryResponse(l *linkBuilder, pd *packages_model.PackageDescriptor) *FeedEntry {
	return createEntry(l, pd, true)
}

func createEntry(l *linkBuilder, pd *packages_model.PackageDescriptor, withNamespace bool) *FeedEntry {
	metadata := pd.Metadata.(*nuget_module.Metadata)

	id := l.GetPackageMetadataURL(pd.Package.Name, pd.Version.Version)

	// Workaround to force a self-closing tag to satisfy XmlReader.IsEmptyElement used by the NuGet client.
	// https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreader.isemptyelement
	content := `<content type="application/zip" src="` + l.GetPackageDownloadURL(pd.Package.Name, pd.Version.Version) + `"/>`

	createdValue := TypedValue[time.Time]{
		Type:  "Edm.DateTime",
		Value: pd.Version.CreatedUnix.AsLocalTime(),
	}

	entry := &FeedEntry{
		ID:       id,
		Category: FeedEntryCategory{Term: "NuGetGallery.OData.V2FeedPackage", Scheme: "http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"},
		Links: []FeedEntryLink{
			{Rel: "self", Href: id},
			{Rel: "edit", Href: id},
		},
		Title:   TypedValue[string]{Type: "text", Value: pd.Package.Name},
		Updated: pd.Version.CreatedUnix.AsLocalTime(),
		Author:  metadata.Authors,
		Content: content,
		Properties: &FeedEntryProperties{
			Version:                  pd.Version.Version,
			NormalizedVersion:        pd.Version.Version,
			Authors:                  metadata.Authors,
			Dependencies:             buildDependencyString(metadata),
			Description:              metadata.Description,
			VersionDownloadCount:     TypedValue[int64]{Type: "Edm.Int64", Value: pd.Version.DownloadCount},
			DownloadCount:            TypedValue[int64]{Type: "Edm.Int64", Value: pd.Version.DownloadCount},
			PackageSize:              TypedValue[int64]{Type: "Edm.Int64", Value: pd.CalculateBlobSize()},
			Created:                  createdValue,
			LastUpdated:              createdValue,
			Published:                createdValue,
			ProjectURL:               metadata.ProjectURL,
			ReleaseNotes:             metadata.ReleaseNotes,
			RequireLicenseAcceptance: TypedValue[bool]{Type: "Edm.Boolean", Value: metadata.RequireLicenseAcceptance},
			Title:                    pd.Package.Name,
		},
	}

	if withNamespace {
		entry.Xmlns = "http://www.w3.org/2005/Atom"
		entry.Base = l.Base
		entry.XmlnsD = "http://schemas.microsoft.com/ado/2007/08/dataservices"
		entry.XmlnsM = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
	}

	return entry
}

func buildDependencyString(metadata *nuget_module.Metadata) string {
	var b strings.Builder
	first := true
	for group, deps := range metadata.Dependencies {
		for _, dep := range deps {
			if !first {
				b.WriteByte('|')
			}
			first = false

			b.WriteString(dep.ID)
			b.WriteByte(':')
			b.WriteString(dep.Version)
			b.WriteByte(':')
			b.WriteString(group)
		}
	}
	return b.String()
}