summaryrefslogtreecommitdiffstats
path: root/services/convert/quota.go
blob: 791cd8e038b1bead1d90b61f46d7093f9770ede1 (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
// Copyright 2024 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package convert

import (
	"context"
	"strconv"

	action_model "code.gitea.io/gitea/models/actions"
	issue_model "code.gitea.io/gitea/models/issues"
	package_model "code.gitea.io/gitea/models/packages"
	quota_model "code.gitea.io/gitea/models/quota"
	repo_model "code.gitea.io/gitea/models/repo"
	api "code.gitea.io/gitea/modules/structs"
)

func ToQuotaRuleInfo(rule quota_model.Rule, withName bool) api.QuotaRuleInfo {
	info := api.QuotaRuleInfo{
		Limit:    rule.Limit,
		Subjects: make([]string, len(rule.Subjects)),
	}
	for i := range len(rule.Subjects) {
		info.Subjects[i] = rule.Subjects[i].String()
	}

	if withName {
		info.Name = rule.Name
	}

	return info
}

func toQuotaInfoUsed(used *quota_model.Used) api.QuotaUsed {
	info := api.QuotaUsed{
		Size: api.QuotaUsedSize{
			Repos: api.QuotaUsedSizeRepos{
				Public:  used.Size.Repos.Public,
				Private: used.Size.Repos.Private,
			},
			Git: api.QuotaUsedSizeGit{
				LFS: used.Size.Git.LFS,
			},
			Assets: api.QuotaUsedSizeAssets{
				Attachments: api.QuotaUsedSizeAssetsAttachments{
					Issues:   used.Size.Assets.Attachments.Issues,
					Releases: used.Size.Assets.Attachments.Releases,
				},
				Artifacts: used.Size.Assets.Artifacts,
				Packages: api.QuotaUsedSizeAssetsPackages{
					All: used.Size.Assets.Packages.All,
				},
			},
		},
	}
	return info
}

func ToQuotaInfo(used *quota_model.Used, groups quota_model.GroupList, withNames bool) api.QuotaInfo {
	info := api.QuotaInfo{
		Used:   toQuotaInfoUsed(used),
		Groups: ToQuotaGroupList(groups, withNames),
	}

	return info
}

func ToQuotaGroup(group quota_model.Group, withNames bool) api.QuotaGroup {
	info := api.QuotaGroup{
		Rules: make([]api.QuotaRuleInfo, len(group.Rules)),
	}
	if withNames {
		info.Name = group.Name
	}
	for i := range len(group.Rules) {
		info.Rules[i] = ToQuotaRuleInfo(group.Rules[i], withNames)
	}

	return info
}

func ToQuotaGroupList(groups quota_model.GroupList, withNames bool) api.QuotaGroupList {
	list := make(api.QuotaGroupList, len(groups))

	for i := range len(groups) {
		list[i] = ToQuotaGroup(*groups[i], withNames)
	}

	return list
}

func ToQuotaUsedAttachmentList(ctx context.Context, attachments []*repo_model.Attachment) (*api.QuotaUsedAttachmentList, error) {
	getAttachmentContainer := func(a *repo_model.Attachment) (string, string, error) {
		if a.ReleaseID != 0 {
			release, err := repo_model.GetReleaseByID(ctx, a.ReleaseID)
			if err != nil {
				return "", "", err
			}
			if err = release.LoadAttributes(ctx); err != nil {
				return "", "", err
			}
			return release.APIURL(), release.HTMLURL(), nil
		}
		if a.CommentID != 0 {
			comment, err := issue_model.GetCommentByID(ctx, a.CommentID)
			if err != nil {
				return "", "", err
			}
			return comment.APIURL(ctx), comment.HTMLURL(ctx), nil
		}
		if a.IssueID != 0 {
			issue, err := issue_model.GetIssueByID(ctx, a.IssueID)
			if err != nil {
				return "", "", err
			}
			if err = issue.LoadRepo(ctx); err != nil {
				return "", "", err
			}
			return issue.APIURL(ctx), issue.HTMLURL(), nil
		}
		return "", "", nil
	}

	result := make(api.QuotaUsedAttachmentList, len(attachments))
	for i, a := range attachments {
		capiURL, chtmlURL, err := getAttachmentContainer(a)
		if err != nil {
			return nil, err
		}

		apiURL := capiURL + "/assets/" + strconv.FormatInt(a.ID, 10)
		result[i] = &api.QuotaUsedAttachment{
			Name:   a.Name,
			Size:   a.Size,
			APIURL: apiURL,
		}
		result[i].ContainedIn.APIURL = capiURL
		result[i].ContainedIn.HTMLURL = chtmlURL
	}

	return &result, nil
}

func ToQuotaUsedPackageList(ctx context.Context, packages []*package_model.PackageVersion) (*api.QuotaUsedPackageList, error) {
	result := make(api.QuotaUsedPackageList, len(packages))
	for i, pv := range packages {
		d, err := package_model.GetPackageDescriptor(ctx, pv)
		if err != nil {
			return nil, err
		}

		var size int64
		for _, file := range d.Files {
			size += file.Blob.Size
		}

		result[i] = &api.QuotaUsedPackage{
			Name:    d.Package.Name,
			Type:    d.Package.Type.Name(),
			Version: d.Version.Version,
			Size:    size,
			HTMLURL: d.VersionHTMLURL(),
		}
	}

	return &result, nil
}

func ToQuotaUsedArtifactList(ctx context.Context, artifacts []*action_model.ActionArtifact) (*api.QuotaUsedArtifactList, error) {
	result := make(api.QuotaUsedArtifactList, len(artifacts))
	for i, a := range artifacts {
		run, err := action_model.GetRunByID(ctx, a.RunID)
		if err != nil {
			return nil, err
		}

		result[i] = &api.QuotaUsedArtifact{
			Name:    a.ArtifactName,
			Size:    a.FileCompressedSize,
			HTMLURL: run.HTMLURL(),
		}
	}

	return &result, nil
}