summaryrefslogtreecommitdiffstats
path: root/services/convert/quota.go
diff options
context:
space:
mode:
authorDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
committerDaniel Baumann <daniel@debian.org>2024-10-18 20:33:49 +0200
commitdd136858f1ea40ad3c94191d647487fa4f31926c (patch)
tree58fec94a7b2a12510c9664b21793f1ed560c6518 /services/convert/quota.go
parentInitial commit. (diff)
downloadforgejo-debian.tar.xz
forgejo-debian.zip
Adding upstream version 9.0.0.upstream/9.0.0upstreamdebian
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to 'services/convert/quota.go')
-rw-r--r--services/convert/quota.go185
1 files changed, 185 insertions, 0 deletions
diff --git a/services/convert/quota.go b/services/convert/quota.go
new file mode 100644
index 0000000..791cd8e
--- /dev/null
+++ b/services/convert/quota.go
@@ -0,0 +1,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
+}